-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathoidc.js
68 lines (58 loc) · 1.93 KB
/
oidc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { service } from '@ember/service';
import { isEmpty } from '@ember/utils';
import BaseAuthenticator from 'ember-simple-auth/authenticators/base';
import ENV from 'pix-admin/config/environment';
import { decodeToken } from 'pix-admin/helpers/jwt';
export default class OidcAuthenticator extends BaseAuthenticator {
@service session;
@service oidcIdentityProviders;
async authenticate({ code, state, authenticationKey, email, identityProviderSlug }) {
const identityProvider = this.oidcIdentityProviders.list.find((provider) => provider.id === identityProviderSlug);
let url = `${ENV.APP.API_HOST}/api/admin/oidc/user/reconcile`;
let body = {
identity_provider: identityProvider.code,
authentication_key: authenticationKey,
email,
};
const isReconciliation = authenticationKey === undefined;
if (isReconciliation) {
url = `${ENV.APP.API_HOST}/api/oidc/token`;
body = {
identity_provider: identityProvider.code,
code,
state: state,
};
if (this.session.isAuthenticated) {
this.session.set('skipRedirectAfterSessionInvalidation', true);
await this.session.invalidate();
}
}
const response = await fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ data: { attributes: body } }),
});
const data = await response.json();
if (!response.ok) {
return Promise.reject(data);
}
const decodedAccessToken = decodeToken(data.access_token);
return {
access_token: data.access_token,
user_id: decodedAccessToken.user_id,
source: identityProvider.source,
identityProviderCode: identityProvider.code,
};
}
restore(data) {
return new Promise((resolve, reject) => {
if (!isEmpty(data['access_token'])) {
resolve(data);
}
reject();
});
}
}