2020-05-05 16:29:35 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
2020-11-09 09:25:35 +00:00
|
|
|
import RepositoryService from 'consul-ui/services/repository';
|
2020-05-05 16:29:35 +00:00
|
|
|
import { getOwner } from '@ember/application';
|
|
|
|
import { set } from '@ember/object';
|
|
|
|
|
|
|
|
const modelName = 'oidc-provider';
|
|
|
|
const OAUTH_PROVIDER_NAME = 'oidc-with-url';
|
2020-11-09 09:25:35 +00:00
|
|
|
export default class OidcProviderService extends RepositoryService {
|
|
|
|
@service('torii')
|
|
|
|
manager;
|
|
|
|
|
|
|
|
init() {
|
|
|
|
super.init(...arguments);
|
2020-05-05 16:29:35 +00:00
|
|
|
this.provider = getOwner(this).lookup(`torii-provider:${OAUTH_PROVIDER_NAME}`);
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getModelName() {
|
2020-05-05 16:29:35 +00:00
|
|
|
return modelName;
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
authorize(id, code, state, dc, nspace, configuration = {}) {
|
2020-05-05 16:29:35 +00:00
|
|
|
const query = {
|
|
|
|
id: id,
|
|
|
|
code: code,
|
|
|
|
state: state,
|
|
|
|
dc: dc,
|
|
|
|
ns: nspace,
|
|
|
|
};
|
|
|
|
return this.store.authorize(this.getModelName(), query);
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logout(id, code, state, dc, nspace, configuration = {}) {
|
2020-05-05 16:29:35 +00:00
|
|
|
// TODO: Temporarily call this secret, as we alreayd do that with
|
|
|
|
// self in the `store` look to see whether we should just call it id like
|
|
|
|
// the rest
|
|
|
|
const query = {
|
|
|
|
id: id,
|
|
|
|
};
|
|
|
|
return this.store.logout(this.getModelName(), query);
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2020-05-05 16:29:35 +00:00
|
|
|
this.manager.close(OAUTH_PROVIDER_NAME);
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
findCodeByURL(src) {
|
2020-05-05 16:29:35 +00:00
|
|
|
// TODO: Maybe move this to the provider itself
|
|
|
|
set(this.provider, 'baseUrl', src);
|
|
|
|
return this.manager.open(OAUTH_PROVIDER_NAME, {}).catch(e => {
|
|
|
|
let err;
|
|
|
|
switch (true) {
|
|
|
|
case e.message.startsWith('remote was closed'):
|
|
|
|
err = new Error('Remote was closed');
|
|
|
|
err.statusCode = 499;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err = new Error(e.message);
|
|
|
|
err.statusCode = 500;
|
|
|
|
}
|
|
|
|
this.store.adapterFor(this.getModelName()).error(err);
|
|
|
|
});
|
2020-11-09 09:25:35 +00:00
|
|
|
}
|
|
|
|
}
|