Files
threshold-ui/ui/desktop/app/routes/scopes/scope/authenticate/method.js
T

82 lines
2.4 KiB
JavaScript
Raw Normal View History

2020-11-12 15:13:28 -05:00
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
2021-04-12 18:10:08 -04:00
import { getOwner } from '@ember/application';
2020-11-12 15:13:28 -05:00
import { action } from '@ember/object';
2022-01-03 09:28:24 -05:00
import { loading } from 'ember-loading';
import { notifyError } from 'core/decorators/notify';
2020-11-12 15:13:28 -05:00
export default class ScopesScopeAuthenticateMethodRoute extends Route {
// =services
@service session;
@service notify;
2021-04-12 18:10:08 -04:00
@service ipc;
2020-11-12 15:13:28 -05:00
@service intl;
@service router;
2020-11-12 15:13:28 -05:00
// =methods
/**
* Returns an auth method by id.
* @param {object} params
* @return {Promise{AuthMethodModel}}
*/
model({ auth_method_id: id }) {
const adapterOptions = { scopeID: this.modelFor('scopes.scope').id };
return this.store.peekRecord('auth-method', id, adapterOptions);
2020-11-12 15:13:28 -05:00
}
2021-04-12 18:10:08 -04:00
/**
*
*/
async startOIDCAuthentication(authenticatorName, options) {
const oidc = getOwner(this).lookup(authenticatorName);
// TODO: delegate this call from the session service so that we don't have
// to look up the authenticator directly
const json = await oidc.startAuthentication(options);
await this.openExternalOIDCFlow(json.attributes.auth_url);
}
/**
* Opens the specified URL in a new tab or window. By default this uses
* `window.open`, but may be overriden.
* @param {string} url
*/
async openExternalOIDCFlow(url) {
await this.ipc.invoke('openExternal', url);
}
2020-11-12 15:13:28 -05:00
// =actions
/**
* Delegates authentication to an authenticator of type matching the current
* auth method. If authentication fails, notifies with an alert.
2020-11-12 15:13:28 -05:00
*/
@action
@loading
@notifyError(() => 'errors.authentication-failed.title', { catch: true })
2020-11-12 15:13:28 -05:00
async authenticate(creds) {
const scope = this.modelFor('scopes.scope');
const authMethod = this.modelFor('scopes.scope.authenticate.method');
const authenticatorName = `authenticator:${authMethod.type}`;
2020-11-12 15:13:28 -05:00
const requestCookies = false;
2021-04-12 18:10:08 -04:00
switch (authMethod.type) {
case 'password':
await this.session.authenticate(
authenticatorName,
creds,
requestCookies,
{ scope, authMethod }
);
this.router.transitionTo('index');
2021-04-12 18:10:08 -04:00
break;
case 'oidc':
await this.startOIDCAuthentication(authenticatorName, {
scope,
authMethod,
});
this.router.transitionTo('scopes.scope.authenticate.method.oidc');
2021-04-12 18:10:08 -04:00
break;
}
2020-11-12 15:13:28 -05:00
}
}