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';
|
2021-02-23 08:56:42 +00:00
|
|
|
import dataSource from 'consul-ui/decorators/data-source';
|
2020-05-05 16:29:35 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-23 08:56:42 +00:00
|
|
|
@dataSource('/:ns/:dc/oidc/providers')
|
|
|
|
async findAllByDatacenter() {
|
|
|
|
return super.findAllByDatacenter(...arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
@dataSource('/:ns/:dc/oidc/provider/:id')
|
|
|
|
async findBySlug() {
|
|
|
|
return super.findBySlug(...arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
@dataSource('/:ns/:dc/oidc/authorize/:id/:code/:state')
|
|
|
|
authorize(params, configuration = {}) {
|
|
|
|
return this.store.authorize(this.getModelName(), params);
|
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
|
|
|
}
|
|
|
|
}
|