feat: 🎸 initial flow to open new window to 3rd party auth

This commit is contained in:
Randall Morey committed 2021-01-21 14:26:02 -05:00
1 parent af8947dc62
commit 2f3478d043
4 files changed
+96 -12

No files matched your search

+9 -8
View File
@@ -25,8 +25,11 @@ import { Response } from 'miragejs';
const methodHandlers = {
password: {
authenticate: (scopeAttrs) =>
new Response(200, {}, {
authenticate: (payload, scopeAttrs) => {
if (payload.credentials.login_name === 'error') {
return new Response(400);
} else {
return new Response(200, {}, {
scope: scopeAttrs,
id: 'token123',
token: 'thetokenstring',
@@ -37,7 +40,9 @@ const methodHandlers = {
updated_time: '',
last_used_time: '',
expiration_time: ''
})
});
}
}
},
/**
* OIDC authentication is a two-step process:
@@ -63,15 +68,11 @@ const methodHandlers = {
export function authHandler({ scopes, authMethods }, request) {
const payload = JSON.parse(request.requestBody);
if (payload.credentials.login_name === 'error') {
return new Response(400);
} else {
const [, id, method] = request.params.id_method.match(/(?<id>.[^:]*)(?::)?(?<method>.*)?/);
const authMethod = authMethods.find(id);
const scope = scopes.find(authMethod.scopeId);
const scopeAttrs = this.serialize(scopes.find(scope.id));
return methodHandlers[authMethod.type][method](scopeAttrs);
}
return methodHandlers[authMethod.type][method](payload, scopeAttrs);
}
export function deauthHandler() {
+39 -3
View File
@@ -1,7 +1,6 @@
import BaseAuthenticator from './base';
// import { resolve, reject } from 'rsvp';
// import fetch from 'fetch';
//import { Promise } from 'rsvp';
import { resolve, reject } from 'rsvp';
import fetch from 'fetch';
/**
*
@@ -16,6 +15,43 @@ export default class OIDCAuthenticator extends BaseAuthenticator {
authMethod: { id: authMethodID },
} */) { }
buildStartAuthEndpointURL(/* {
scope: { id: scopeID },
authMethod: { id: authMethodID },
} */) { }
/**
*
*/
async startAuthentication(
creds,
requestCookies = true,
options
) {
const body = JSON.stringify({
token_type: requestCookies ? 'cookie' : null
});
const authEndpointURL = this.buildStartAuthEndpointURL(options);
const response = await fetch(authEndpointURL, { method: 'post', body });
const json = await response.json();
const normalized = this.normalizeData(json);
if (response.status < 400) {
await this.openExternalOIDCFlow(normalized.authorization_request_url);
resolve(normalized);
} else {
reject();
}
}
/**
* Opens the specified URL in a new tab or window. By default this uses
* `window.open`, but may be overriden.
* @param {string} url
*/
openExternalOIDCFlow(url) {
window.open(url);
}
async authenticate(
// creds,
// requestCookies = true,
+30 -1
View File
@@ -1,8 +1,37 @@
import BaseOIDCAuthenticator from 'auth/authenticators/oidc';
//import { inject as service } from '@ember/service';
import { inject as service } from '@ember/service';
/**
*
*/
export default class OIDCAuthenticator extends BaseOIDCAuthenticator {
// =services
@service store;
// =attributes
/**
* Generates an auth method URL with which to authenticate.
* @override
* @param {object} options
* @param {string} options.scope.scope_id
* @param {string} options.authMethod.id
* @return {string}
*/
buildStartAuthEndpointURL({
scope: { id: scopeID },
authMethod: { id: authMethodID },
}) {
const adapter = this.store.adapterFor('application');
const options = {
adapterOptions: {
scopeID,
method: 'authenticate:start'
}
};
return adapter.buildURL('auth-method', authMethodID, options, 'findRecord');
}
}
@@ -1,5 +1,6 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import { getOwner } from '@ember/application';
import { action } from '@ember/object';
import loading from 'ember-loading/decorator';
import { notifyError } from 'core/decorators/notify';
@@ -37,6 +38,8 @@ export default class ScopesScopeAuthenticateMethodRoute extends Route {
const authMethod = this.modelFor('scopes.scope.authenticate.method');
const authenticatorName = `authenticator:${authMethod.type}`;
const requestCookies = false;
switch (authMethod.type) {
case 'password':
await this.session.authenticate(
authenticatorName,
creds,
@@ -44,5 +47,20 @@ export default class ScopesScopeAuthenticateMethodRoute extends Route {
{ scope, authMethod }
);
this.transitionTo('index');
break;
case 'oidc':
await this.startOIDCAuthentication(
authenticatorName,
creds,
requestCookies,
{ scope, authMethod }
);
break;
}
}
async startOIDCAuthentication(authenticatorName, creds, requestCookies, options) {
const oidc = getOwner(this).lookup(authenticatorName);
oidc.startAuthentication(creds, requestCookies, options);
}
}