-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathauth.tsx
More file actions
188 lines (155 loc) · 5.59 KB
/
auth.tsx
File metadata and controls
188 lines (155 loc) · 5.59 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import {
Credential,
OAuth2Client,
clearDPoPKeyPairs,
type AcrValues,
type JsonRecord,
isOAuth2ErrorResponse,
} from '@okta/spa-platform';
import { AuthorizationCodeFlow, SessionLogoutFlow } from '@okta/spa-platform/flows';
const ADMIN_SPA_REFRESH_TOKEN_TAG = 'admin-spa:mordor-token';
// ############# OAuth Configuration ############# //
export const oauthScopes = ['openid', 'profile', 'email', 'offline_access'];
export const customScopes = [
'test.scope.a',
'test.scope.b',
'test.scope.c',
];
export const oauthConfig: any = {
issuer: customScopes?.length ? `${__ISSUER__}/oauth2/default` : __ISSUER__,
clientId: __DPOP_CLIENT_ID__,
scopes: [...oauthScopes, ...customScopes],
dpop: true
};
oauthConfig.baseURL = oauthConfig.issuer;
export const client = new OAuth2Client(oauthConfig);
// ############# OAuth Flow Instances ############# //
export const signInFlow = new AuthorizationCodeFlow(client, {
redirectUri: `${window.location.origin}/login/callback`,
});
export const signIn = async (originalUri: string = window.location.href, meta: JsonRecord = {}) => {
const url = new URL(originalUri);
if (url.origin !== window.location.origin) {
throw new Error('mismatched domains');
}
await signInFlow.start({ originalUri: url.pathname + url.search, ...meta });
return AuthorizationCodeFlow.PerformRedirect(signInFlow);
};
export async function handleAuthorizationCodeFlowResponse () {
try {
const { token, context } = await signInFlow.resume(window.location.href);
const currentCredential = await getMordorToken();
if (currentCredential) {
await currentCredential.remove();
}
await Credential.store(token, [ADMIN_SPA_REFRESH_TOKEN_TAG]);
return context.originalUri;
}
catch (err) {
console.log(err);
throw err;
}
}
export async function handleAcrStepUp (acrValues: AcrValues, maxAge: number = 1) {
// provide `acrValues` to request a token with a higher assurance level
// provide `maxAge` to force a re-prompt
await signInFlow.start({}, { acrValues, maxAge: maxAge });
// request new mordor token with higher assurance level
const result = await AuthorizationCodeFlow.PerformInPopup(signInFlow);
if (!result.completed) {
alert(`Failed to step up: ${result.reason}`);
throw new Error('Test App Error: step up failed');
}
const { token } = result;
// clear all existing tokens from storage (presumably they have been minted at the lower assurance level)
await clearBrokerTokens();
const mainCredential = await getMordorToken();
await mainCredential?.remove();
// store the new morder token
return await Credential.store(token, [ADMIN_SPA_REFRESH_TOKEN_TAG]);
}
export const signOutFlow = new SessionLogoutFlow(client, {
logoutRedirectUri: `${window.location.origin}/logout`
});
async function clearBrokerTokens () {
const refreshId = (await getMordorToken())?.id;
await Promise.all((await Credential.find(() => true)).map(async (credential) => {
if (credential.id !== refreshId) {
// if (credential.token.isExpired) {
// await credential.remove();
// }
// else {
// await credential.revoke();
// }
await credential.remove(); // revoke was causing 429 with too many test tokens
}
}));
}
export const signOut = async () => {
console.log('signOut called');
const mainCredential = await getMordorToken();
await clearBrokerTokens();
await clearDPoPKeyPairs();
if (mainCredential) {
const idToken = mainCredential?.token?.idToken?.rawValue;
if (idToken) {
// if `idToken` exists, execute a Logout flow
console.log('starting logout flow', idToken);
const logoutUrl = await signOutFlow.start(idToken);
await mainCredential.remove(); // will be revoked via logout call, only needs to be removed from storage
await SessionLogoutFlow.PerformPostRedirect(logoutUrl);
}
else {
// otherwise revoke the default credential
await mainCredential.revoke();
}
}
};
// ############# App/Broker Token Methods ############# //
// the all-scoped token to rule them all
export async function getMordorToken (): Promise<Credential | null> {
return (await Credential.find(meta => meta.tags.includes(ADMIN_SPA_REFRESH_TOKEN_TAG)))?.[0] ?? null;
}
export async function initAuth () {
let authRequired = true;
const mordorCredential = await getMordorToken();
if (mordorCredential) {
if (!mordorCredential.token.isExpired) {
// token is valid, persumably just obtained, can skip introspect call
// note: this is technically checking the validity of the access token
// for a small bootstrap optimization to avoid unecessary introspect calls
authRequired = false;
}
else {
// introspect the refreshToken to determine if the token is still valid
const introspection = await mordorCredential.introspect('refresh_token');
if (isOAuth2ErrorResponse(introspection)) {
authRequired = true;
}
else {
authRequired = !introspection.active;
}
}
}
if (authRequired) {
if (mordorCredential) {
await mordorCredential.remove();
}
await signIn();
}
}
// Just for testing
export async function TEST_revokeToken () {
await (await getMordorToken())?.revoke('REFRESH');
console.log('token revoked');
};
// Just for testing
// removes everything other than the main credential
export async function TEST_removeAccessTokens () {
const refreshId = (await getMordorToken())?.id;
await Promise.all((await Credential.find(() => true)).map(async (credential) => {
if (credential.id !== refreshId) {
await credential.remove();
}
}));
};