Files
threshold-ui/addons/auth/addon/authenticators/oidc.js
T

99 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-01-15 11:10:33 -05:00
import BaseAuthenticator from './base';
2021-01-22 17:01:12 -05:00
import { inject as service } from '@ember/service';
import { reject } from 'rsvp';
import fetch from 'fetch';
2021-01-15 11:10:33 -05:00
/**
*
*/
export default class OIDCAuthenticator extends BaseAuthenticator {
2021-01-22 17:01:12 -05:00
// =services
2021-01-15 11:10:33 -05:00
2021-01-22 17:01:12 -05:00
@service session;
// =methods
/**
* 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}
*/
2021-01-15 11:10:33 -05:00
buildAuthEndpointURL(/* {
scope: { id: scopeID },
authMethod: { id: authMethodID },
} */) { }
2021-01-22 17:01:12 -05:00
/**
* Generates an auth method URL with which to start authentication.
* @override
* @param {object} options
* @param {string} options.scope.scope_id
* @param {string} options.authMethod.id
* @return {string}
*/
buildStartAuthEndpointURL(/* {
scope: { id: scopeID },
authMethod: { id: authMethodID },
} */) { }
/**
2021-01-22 17:01:12 -05:00
* Kicks-off the OIDC flow: calls the `authenticate:start` action on the
* specified auth method, which returns some meta data along with an
* authorization request URL.
*
2021-01-22 17:01:12 -05:00
* @param {boolean} requestCookies request cookie tokens (default `true`)
* @param {object} options
* @return {Promise}
*/
2021-01-22 17:01:12 -05:00
async startAuthentication(requestCookies = true, options) {
const url = this.buildStartAuthEndpointURL(options);
const body = JSON.stringify({
token_type: requestCookies ? 'cookie' : null
});
2021-01-22 17:01:12 -05:00
const response = await fetch(url, { method: 'post', body });
const json = await response.json();
if (response.status < 400) {
2021-01-22 17:01:12 -05:00
// Store meta about the pending OIDC flow
this.session.set('data.pending', {
oidc: json
});
return json;
} else {
2021-01-22 17:01:12 -05:00
return reject();
}
}
/**
2021-01-22 17:01:12 -05:00
*
*/
2021-01-22 17:01:12 -05:00
async authenticate(json) {
console.info('Authenticated with OIDC provider');
return this.normalizeData(json);
}
2021-01-22 17:01:12 -05:00
/**
*
*/
async attemptOIDCAuthentication(options) {
const url = this.buildAuthEndpointURL(options);
const body = JSON.stringify({
state: this.session.get('data.pending.oidc.state'),
token_request_id: this.session.get('data.pending.oidc.token_request_id')
});
const response = await fetch(url, { method: 'post', body });
const json = await response.json();
if (response.status === 100) {
return false;
} else if (response.status < 400) {
// Authentication with OIDC is indirect...
await this.session.authenticate('authenticator:oidc', json);
return true;
} else {
return reject();
}
2021-01-15 11:10:33 -05:00
}
}