@axa-fr/react-oidc 6.6.1 → 6.6.2-alpha0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/package.json +2 -1
  2. package/src/App.css +38 -0
  3. package/src/App.specold.tsx +46 -0
  4. package/src/App.tsx +103 -0
  5. package/src/FetchUser.tsx +53 -0
  6. package/src/Home.tsx +24 -0
  7. package/src/MultiAuth.tsx +129 -0
  8. package/src/Profile.tsx +77 -0
  9. package/src/configurations.ts +70 -0
  10. package/src/index.css +13 -0
  11. package/src/index.tsx +9 -0
  12. package/src/logo.svg +7 -0
  13. package/src/oidc/FetchToken.tsx +61 -0
  14. package/src/oidc/OidcProvider.tsx +206 -0
  15. package/src/oidc/OidcSecure.tsx +37 -0
  16. package/src/oidc/ReactOidc.tsx +139 -0
  17. package/src/oidc/User.ts +38 -0
  18. package/src/oidc/core/default-component/AuthenticateError.component.tsx +13 -0
  19. package/src/oidc/core/default-component/Authenticating.component.tsx +13 -0
  20. package/src/oidc/core/default-component/Callback.component.tsx +46 -0
  21. package/src/oidc/core/default-component/Loading.component.tsx +10 -0
  22. package/src/oidc/core/default-component/ServiceWorkerNotSupported.component.tsx +13 -0
  23. package/src/oidc/core/default-component/SessionLost.component.tsx +14 -0
  24. package/src/oidc/core/default-component/SilentCallback.component.tsx +22 -0
  25. package/src/oidc/core/default-component/SilentLogin.component.tsx +35 -0
  26. package/src/oidc/core/default-component/index.ts +6 -0
  27. package/src/oidc/core/routes/OidcRoutes.spec.tsx +15 -0
  28. package/src/oidc/core/routes/OidcRoutes.tsx +69 -0
  29. package/src/oidc/core/routes/__snapshots__/OidcRoutes.spec.tsx.snap +7 -0
  30. package/src/oidc/core/routes/index.ts +2 -0
  31. package/src/oidc/core/routes/withRouter.spec.tsx +48 -0
  32. package/src/oidc/core/routes/withRouter.tsx +64 -0
  33. package/src/oidc/index.ts +5 -0
  34. package/src/oidc/vanilla/OidcServiceWorker.js +442 -0
  35. package/src/oidc/vanilla/OidcTrustedDomains.js +16 -0
  36. package/src/oidc/vanilla/checkSessionIFrame.ts +82 -0
  37. package/src/oidc/vanilla/index.ts +1 -0
  38. package/src/oidc/vanilla/initSession.ts +67 -0
  39. package/src/oidc/vanilla/initWorker.ts +165 -0
  40. package/src/oidc/vanilla/memoryStorageBackend.ts +33 -0
  41. package/src/oidc/vanilla/noHashQueryStringUtils.ts +33 -0
  42. package/src/oidc/vanilla/oidc.ts +1230 -0
  43. package/src/oidc/vanilla/parseTokens.ts +142 -0
  44. package/src/oidc/vanilla/route-utils.spec.ts +15 -0
  45. package/src/oidc/vanilla/route-utils.ts +76 -0
  46. package/src/oidc/vanilla/timer.ts +165 -0
  47. package/src/override/AuthenticateError.component.tsx +14 -0
  48. package/src/override/Authenticating.component.tsx +14 -0
  49. package/src/override/Callback.component.tsx +13 -0
  50. package/src/override/Loading.component.tsx +13 -0
  51. package/src/override/ServiceWorkerNotSupported.component.tsx +15 -0
  52. package/src/override/SessionLost.component.tsx +21 -0
  53. package/src/override/style.ts +10 -0
  54. package/src/setupTests.js +5 -0
@@ -0,0 +1,15 @@
1
+ import * as React from 'react';
2
+ import OidcRoutes from './OidcRoutes';
3
+ import {render} from "@testing-library/react";
4
+
5
+ describe('Authenticating test suite', () => {
6
+ it('renders correctly', () => {
7
+ const props = {
8
+ children: 'http://url.com',
9
+ callbackComponent: () => <div>tcallback component</div>,
10
+ redirect_uri: 'http://example.com:3000/authentication/callback',
11
+ };
12
+ const { asFragment } = render(<OidcRoutes {...props} />);
13
+ expect(asFragment()).toMatchSnapshot();
14
+ });
15
+ });
@@ -0,0 +1,69 @@
1
+ import React, { ComponentType, FC, PropsWithChildren, useEffect, useState } from 'react';
2
+ import { getPath } from '../../vanilla/route-utils';
3
+ import CallbackComponent from '../default-component/Callback.component';
4
+ import SilentCallbackComponent from "../default-component/SilentCallback.component";
5
+ import { CustomHistory } from "./withRouter";
6
+ import SilentLoginComponent from "../default-component/SilentLogin.component";
7
+
8
+ const defaultProps: Partial<OidcRoutesProps> = {
9
+
10
+ };
11
+
12
+ type OidcRoutesProps = {
13
+ callbackSuccessComponent?: ComponentType;
14
+ callbackErrorComponent?: ComponentType;
15
+ authenticatingComponent?: ComponentType;
16
+ configurationName:string;
17
+ redirect_uri: string;
18
+ silent_redirect_uri?: string;
19
+ silent_login_uri?:string;
20
+ withCustomHistory?: () => CustomHistory;
21
+ };
22
+
23
+ const OidcRoutes: FC<PropsWithChildren<OidcRoutesProps>> = ({
24
+ callbackErrorComponent,
25
+ callbackSuccessComponent,
26
+ redirect_uri,
27
+ silent_redirect_uri,
28
+ silent_login_uri,
29
+ children, configurationName,
30
+ withCustomHistory=null,
31
+ }) => {
32
+ // This exist because in next.js window outside useEffect is null
33
+ let pathname = window ? getPath(window.location.href) : '';
34
+
35
+ const [path, setPath] = useState(pathname);
36
+
37
+ useEffect(() => {
38
+ const setNewPath = () => setPath(getPath(window.location.href));
39
+ setNewPath();
40
+ window.addEventListener('popstate', setNewPath, false);
41
+ return () => window.removeEventListener('popstate', setNewPath, false);
42
+ }, []);
43
+
44
+ const callbackPath = getPath(redirect_uri);
45
+
46
+ if(silent_redirect_uri){
47
+ if(path === getPath(silent_redirect_uri)){
48
+ return <SilentCallbackComponent configurationName={configurationName} />
49
+ }
50
+ }
51
+
52
+ if(silent_login_uri){
53
+ if(path === getPath(silent_login_uri)){
54
+ return <SilentLoginComponent configurationName={configurationName} />
55
+ }
56
+ }
57
+
58
+ switch (path) {
59
+ case callbackPath:
60
+ return <CallbackComponent callBackError={callbackErrorComponent} callBackSuccess={callbackSuccessComponent} configurationName={configurationName} withCustomHistory={withCustomHistory} />;
61
+ default:
62
+ return <>{children}</>;
63
+ }
64
+ };
65
+
66
+ // @ts-ignore
67
+ OidcRoutes.defaultProps = defaultProps;
68
+
69
+ export default React.memo(OidcRoutes);
@@ -0,0 +1,7 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`Authenticating test suite renders correctly 1`] = `
4
+ <DocumentFragment>
5
+ http://url.com
6
+ </DocumentFragment>
7
+ `;
@@ -0,0 +1,2 @@
1
+ export { default as OidcRoutes } from './OidcRoutes';
2
+ export { ReactOidcHistory } from './withRouter';
@@ -0,0 +1,48 @@
1
+ import React from 'react';
2
+ import '@testing-library/jest-dom/extend-expect';
3
+ import { CreateEvent, WindowInternal } from './withRouter';
4
+
5
+ describe('WithRouter test Suite', () => {
6
+ const generateKeyMock = () => '123ABC';
7
+ const paramsMock = { bubbles: false, cancelable: false, detail: 'detail' };
8
+ beforeEach(() => {});
9
+ it('should CreateEvent return correct Event if not on IE', () => {
10
+ const windowMock = {
11
+ CustomEvent: jest.fn().mockImplementation((event, params) => {
12
+ return { event, params };
13
+ }),
14
+ };
15
+ const documentMock = {} as Document;
16
+ const res = CreateEvent((windowMock as unknown) as WindowInternal, documentMock)(
17
+ 'event test',
18
+ paramsMock
19
+ );
20
+ expect(res).toEqual({
21
+ event: 'event test',
22
+ params: { bubbles: false, cancelable: false, detail: 'detail' },
23
+ });
24
+ });
25
+
26
+ it('should createEvent create a polyfill when the default func is undefined', () => {
27
+ const windowMock = {
28
+ Event: {
29
+ prototype: 'protoMock',
30
+ },
31
+ };
32
+ const evtMock = {
33
+ initCustomEvent: jest.fn(),
34
+ };
35
+ const documentMock = {
36
+ createEvent: jest.fn(() => evtMock),
37
+ };
38
+ const typedDocumentMock = (documentMock as unknown) as Document;
39
+ const res = CreateEvent((windowMock as unknown) as WindowInternal, typedDocumentMock)(
40
+ 'event test',
41
+ paramsMock
42
+ );
43
+ expect(res).toEqual({ ...evtMock });
44
+ expect(documentMock.createEvent).toHaveBeenCalledWith('CustomEvent');
45
+ expect(evtMock.initCustomEvent).toHaveBeenCalledWith('event test', false, false, 'detail');
46
+ });
47
+
48
+ });
@@ -0,0 +1,64 @@
1
+ import React from 'react';
2
+
3
+ const generateKey = () =>
4
+ Math.random()
5
+ .toString(36)
6
+ .substr(2, 6);
7
+
8
+ // Exported only for test
9
+ export type WindowInternal = Window & {
10
+ CustomEvent?: new <T>(typeArg: string, eventInitDict?: CustomEventInit<T>) => CustomEvent<T>;
11
+ Event: typeof Event;
12
+ };
13
+
14
+ type IPrototype = {
15
+ prototype: any;
16
+ };
17
+
18
+ type InitCustomEventParams<T = any> = {
19
+ bubbles: boolean;
20
+ cancelable: boolean;
21
+ detail: T;
22
+ };
23
+
24
+ // IE Polyfill for CustomEvent
25
+ export const CreateEvent = (windowInternal: WindowInternal, documentInternal: Document) => (
26
+ event: string,
27
+ params: InitCustomEventParams
28
+ ): CustomEvent => {
29
+ if (typeof windowInternal.CustomEvent === 'function') {
30
+ return new windowInternal.CustomEvent(event, params);
31
+ }
32
+ const paramsToFunction = params || { bubbles: false, cancelable: false, detail: undefined };
33
+ const evt: CustomEvent = documentInternal.createEvent('CustomEvent');
34
+ evt.initCustomEvent(event, paramsToFunction.bubbles, paramsToFunction.cancelable, paramsToFunction.detail);
35
+ (evt as CustomEvent & IPrototype).prototype = windowInternal.Event.prototype;
36
+ return evt;
37
+ };
38
+
39
+ type WindowHistoryState = typeof window.history.state;
40
+
41
+ export interface ReactOidcHistory {
42
+ replaceState: (url?: string | null, stateHistory?: WindowHistoryState) => void;
43
+ }
44
+
45
+ export type CustomHistory = {
46
+ replaceState(url?: string | null, stateHistory?: WindowHistoryState): void;
47
+ }
48
+
49
+ const getHistory = (
50
+ windowInternal: WindowInternal,
51
+ CreateEventInternal: (event: string, params?: InitCustomEventParams) => CustomEvent,
52
+ generateKeyInternal: typeof generateKey
53
+ ): CustomHistory => {
54
+ return {
55
+ replaceState: (url?: string | null, stateHistory?: WindowHistoryState): void => {
56
+ const key = generateKeyInternal();
57
+ const state = stateHistory || windowInternal.history.state;
58
+ windowInternal.history.replaceState({ key, state }, null, url);
59
+ windowInternal.dispatchEvent(CreateEventInternal('popstate'));
60
+ },
61
+ };
62
+ };
63
+
64
+ export const getCustomHistory = () => getHistory(window, CreateEvent(window, document), generateKey);
@@ -0,0 +1,5 @@
1
+ export { withOidcSecure, OidcSecure } from "./OidcSecure";
2
+ export { useOidcUser, OidcUserStatus} from "./User";
3
+ export { useOidc, useOidcAccessToken, useOidcIdToken } from "./ReactOidc";
4
+ export { withOidcFetch, useOidcFetch } from "./FetchToken";
5
+ export { OidcProvider } from "./OidcProvider";
@@ -0,0 +1,442 @@
1
+ this.importScripts('OidcTrustedDomains.js');
2
+
3
+ const id = Math.round(new Date().getTime() / 1000).toString();
4
+
5
+ const acceptAnyDomainToken = "*";
6
+
7
+ const keepAliveJsonFilename = "OidcKeepAliveServiceWorker.json";
8
+ const handleInstall = (event) => {
9
+ console.log('[OidcServiceWorker] service worker installed ' + id);
10
+ event.waitUntil(self.skipWaiting());
11
+ };
12
+
13
+ const handleActivate = (event) => {
14
+ console.log('[OidcServiceWorker] service worker activated ' + id);
15
+ event.waitUntil(self.clients.claim());
16
+ };
17
+
18
+ let currentLoginCallbackConfigurationName = null;
19
+ let database = {
20
+ default: {
21
+ configurationName: "default",
22
+ tokens: null,
23
+ status:null,
24
+ items:[],
25
+ nonce: null,
26
+ oidcServerConfiguration: null
27
+ }
28
+ };
29
+
30
+ const countLetter = (str, find)=> {
31
+ return (str.split(find)).length - 1;
32
+ }
33
+
34
+ const b64DecodeUnicode = (str) =>
35
+ decodeURIComponent(Array.prototype.map.call(atob(str), (c) => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join(''));
36
+ const parseJwt = (token) => JSON.parse(b64DecodeUnicode(token.split('.')[1].replace('-', '+').replace('_', '/')));
37
+ const extractTokenPayload=(token)=> {
38
+ try{
39
+ if (!token) {
40
+ return null;
41
+ }
42
+ if(countLetter(token,'.') === 2) {
43
+ return parseJwt(token);
44
+ } else {
45
+ return null;
46
+ }
47
+ } catch (e) {
48
+ console.warn(e);
49
+ }
50
+ return null;
51
+ }
52
+
53
+ const computeTimeLeft = (refreshTimeBeforeTokensExpirationInSecond, expiresAt)=>{
54
+ const currentTimeUnixSecond = new Date().getTime() /1000;
55
+ return Math.round(((expiresAt - refreshTimeBeforeTokensExpirationInSecond) - currentTimeUnixSecond));
56
+ }
57
+
58
+ const isTokensValid= (tokens) =>{
59
+ if(!tokens){
60
+ return false;
61
+ }
62
+ return computeTimeLeft(0, tokens.expiresAt) > 0;
63
+ }
64
+
65
+ // https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation (excluding rules #1, #4, #5, #7, #8, #12, and #13 which did not apply).
66
+ // https://github.com/openid/AppAuth-JS/issues/65
67
+ const isTokensOidcValid =(tokens, nonce, oidcServerConfiguration) =>{
68
+ if(tokens.idTokenPayload) {
69
+ const idTokenPayload = tokens.idTokenPayload;
70
+ // 2: The Issuer Identifier for the OpenID Provider (which is typically obtained during Discovery) MUST exactly match the value of the iss (issuer) Claim.
71
+ if(oidcServerConfiguration.issuer !== idTokenPayload.iss){
72
+ return false;
73
+ }
74
+ // 3: The Client MUST validate that the aud (audience) Claim contains its client_id value registered at the Issuer identified by the iss (issuer) Claim as an audience. The aud (audience) Claim MAY contain an array with more than one element. The ID Token MUST be rejected if the ID Token does not list the Client as a valid audience, or if it contains additional audiences not trusted by the Client.
75
+
76
+ // 6: If the ID Token is received via direct communication between the Client and the Token Endpoint (which it is in this flow), the TLS server validation MAY be used to validate the issuer in place of checking the token signature. The Client MUST validate the signature of all other ID Tokens according to JWS [JWS] using the algorithm specified in the JWT alg Header Parameter. The Client MUST use the keys provided by the Issuer.
77
+
78
+ // 9: The current time MUST be before the time represented by the exp Claim.
79
+ const currentTimeUnixSecond = new Date().getTime() /1000;
80
+ if(idTokenPayload.exp && idTokenPayload.exp < currentTimeUnixSecond) {
81
+ return false;
82
+ }
83
+ // 10: The iat Claim can be used to reject tokens that were issued too far away from the current time, limiting the amount of time that nonces need to be stored to prevent attacks. The acceptable range is Client specific.
84
+ const timeInSevenDays = 60 * 60 * 24 * 7;
85
+ if(idTokenPayload.iat && (idTokenPayload.iat + timeInSevenDays) < currentTimeUnixSecond) {
86
+ return false;
87
+ }
88
+ // 11: If a nonce value was sent in the Authentication Request, a nonce Claim MUST be present and its value checked to verify that it is the same value as the one that was sent in the Authentication Request. The Client SHOULD check the nonce value for replay attacks. The precise method for detecting replay attacks is Client specific.
89
+ if (idTokenPayload.nonce && idTokenPayload.nonce !== nonce) {
90
+ return false;
91
+ }
92
+ }
93
+ return true;
94
+ }
95
+
96
+ function hideTokens(currentDatabaseElement) {
97
+ const configurationName = currentDatabaseElement.configurationName;
98
+ return (response) => {
99
+ if(response.status !== 200){
100
+ return response;
101
+ }
102
+ return response.json().then(tokens => {
103
+ if(!tokens.issued_at) {
104
+ const currentTimeUnixSecond = new Date().getTime() /1000;
105
+ tokens.issued_at = currentTimeUnixSecond;
106
+ }
107
+
108
+ const accessTokenPayload = extractTokenPayload(tokens.access_token);
109
+ const secureTokens = {
110
+ ...tokens,
111
+ access_token: ACCESS_TOKEN +"_" + configurationName,
112
+ accessTokenPayload : accessTokenPayload
113
+ };
114
+ tokens.accessTokenPayload = accessTokenPayload;
115
+
116
+ let _idTokenPayload = null;
117
+ if(tokens.id_token) {
118
+ _idTokenPayload = extractTokenPayload(tokens.id_token);
119
+ tokens.idTokenPayload = {..._idTokenPayload};
120
+ if(_idTokenPayload.nonce) {
121
+ const keyNonce = NONCE_TOKEN + '_'+ currentDatabaseElement.configurationName;
122
+ _idTokenPayload.nonce = keyNonce;
123
+ }
124
+ secureTokens.idTokenPayload = _idTokenPayload;
125
+ }
126
+ if(tokens.refresh_token){
127
+ secureTokens.refresh_token = REFRESH_TOKEN + "_" + configurationName;
128
+ }
129
+
130
+ const idTokenExpiresAt =(_idTokenPayload && _idTokenPayload.exp) ? _idTokenPayload.exp: Number.MAX_VALUE;
131
+ const accessTokenExpiresAt = (accessTokenPayload && accessTokenPayload.exp)? accessTokenPayload.exp : tokens.issued_at + tokens.expires_in;
132
+ const expiresAt = idTokenExpiresAt < accessTokenExpiresAt ? idTokenExpiresAt : accessTokenExpiresAt;
133
+ secureTokens.expiresAt = expiresAt;
134
+
135
+ tokens.expiresAt = expiresAt;
136
+
137
+ if(!isTokensOidcValid(tokens, currentDatabaseElement.nonce.nonce, currentDatabaseElement.oidcServerConfiguration)){
138
+ throw Error("Tokens are not OpenID valid");
139
+ }
140
+
141
+ // When refresh_token is not rotated we reuse ald refresh_token
142
+ if(currentDatabaseElement.tokens != null && "refresh_token" in currentDatabaseElement.tokens && !("refresh_token" in tokens)){
143
+ const refreshToken = currentDatabaseElement.tokens.refresh_token;
144
+ currentDatabaseElement.tokens = {...tokens, refresh_token : refreshToken};
145
+ } else{
146
+ currentDatabaseElement.tokens = tokens;
147
+ }
148
+
149
+ currentDatabaseElement.status = "LOGGED_IN";
150
+ const body = JSON.stringify(secureTokens);
151
+ return new Response(body, response);
152
+ });
153
+ };
154
+ }
155
+
156
+ const getCurrentDatabasesTokenEndpoint = (database, url) => {
157
+ const databases = [];
158
+ for (const [key, value] of Object.entries(database)) {
159
+ if(value && value.oidcServerConfiguration !=null && url.startsWith(value.oidcServerConfiguration.tokenEndpoint)){
160
+ databases.push(value);
161
+ }
162
+ }
163
+ return databases;
164
+ }
165
+
166
+ const openidWellknownUrlEndWith = "/.well-known/openid-configuration"
167
+ const getCurrentDatabaseDomain = (database, url) => {
168
+ if(url.endsWith(openidWellknownUrlEndWith)){
169
+ return null;
170
+ }
171
+ for (const [key, currentDatabase] of Object.entries(database)) {
172
+ const oidcServerConfiguration = currentDatabase.oidcServerConfiguration;
173
+
174
+ if(!oidcServerConfiguration){
175
+ continue;
176
+ }
177
+
178
+ if(oidcServerConfiguration.tokenEndpoint && url === oidcServerConfiguration.tokenEndpoint){
179
+ continue;
180
+ }
181
+
182
+ const domainsToSendTokens = oidcServerConfiguration.userInfoEndpoint ? [
183
+ oidcServerConfiguration.userInfoEndpoint, ...trustedDomains[key]
184
+ ] : [...trustedDomains[key]];
185
+
186
+ let hasToSendToken = false;
187
+ if (domainsToSendTokens.find((f) => f === acceptAnyDomainToken)) {
188
+ hasToSendToken= true;
189
+ } else {
190
+ for (let i = 0; i < domainsToSendTokens.length; i++) {
191
+ const domain = domainsToSendTokens[i];
192
+ if (url.startsWith(domain)) {
193
+ hasToSendToken = true;
194
+ break;
195
+ }
196
+ }
197
+ }
198
+
199
+ if(hasToSendToken) {
200
+ if(!currentDatabase.tokens) {
201
+ return null;
202
+ }
203
+ return currentDatabase;
204
+ }
205
+ }
206
+
207
+ return null;
208
+ }
209
+
210
+ const serializeHeaders = (headers) => {
211
+ let headersObj = {};
212
+ for (let key of headers.keys()) {
213
+ headersObj[key] = headers.get(key);
214
+ }
215
+ return headersObj;
216
+ };
217
+
218
+ const REFRESH_TOKEN = 'REFRESH_TOKEN_SECURED_BY_OIDC_SERVICE_WORKER';
219
+ const ACCESS_TOKEN = 'ACCESS_TOKEN_SECURED_BY_OIDC_SERVICE_WORKER';
220
+ const NONCE_TOKEN = 'NONCE_SECURED_BY_OIDC_SERVICE_WORKER';
221
+
222
+ const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
223
+
224
+ const keepAliveAsync = async (event) => {
225
+ const originalRequest = event.request;
226
+ const isFromVanilla = originalRequest.headers.has('oidc-vanilla');
227
+ const init = {"status": 200, "statusText": 'oidc-service-worker'};
228
+ const response = new Response('{}', init);
229
+ if(!isFromVanilla) {
230
+ for(let i=0; i<240;i++){
231
+ await sleep(1000 + Math.floor(Math.random() * 1000));
232
+ const cache = await caches.open("oidc_dummy_cache");
233
+ await cache.put(event.request, response.clone());
234
+ }
235
+ }
236
+
237
+ return response;
238
+ }
239
+
240
+ const handleFetch = async (event) => {
241
+ const originalRequest = event.request;
242
+
243
+ if(originalRequest.url.includes(keepAliveJsonFilename) ){
244
+ event.respondWith(keepAliveAsync(event));
245
+ return;
246
+ }
247
+
248
+ const currentDatabaseForRequestAccessToken = getCurrentDatabaseDomain(database, originalRequest.url);
249
+ if(currentDatabaseForRequestAccessToken && currentDatabaseForRequestAccessToken.tokens && currentDatabaseForRequestAccessToken.tokens.access_token) {
250
+ while (currentDatabaseForRequestAccessToken.tokens && !isTokensValid(currentDatabaseForRequestAccessToken.tokens)){
251
+ await sleep(200);
252
+ }
253
+ const newRequest = new Request(originalRequest, {
254
+ headers: {
255
+ ...serializeHeaders(originalRequest.headers),
256
+ authorization: "Bearer " + currentDatabaseForRequestAccessToken.tokens.access_token
257
+ }
258
+ });
259
+ event.waitUntil(event.respondWith(fetch(newRequest)));
260
+ return;
261
+ }
262
+
263
+ if(event.request.method !== "POST"){
264
+ return;
265
+ }
266
+ let currentDatabase = null;
267
+ const currentDatabases = getCurrentDatabasesTokenEndpoint(database, originalRequest.url);
268
+ const numberDatabase = currentDatabases.length;
269
+ if(numberDatabase > 0) {
270
+ const maPromesse = new Promise((resolve, reject) => {
271
+ const clonedRequest = originalRequest.clone();
272
+ const response = clonedRequest.text().then(actualBody => {
273
+ if(actualBody.includes(REFRESH_TOKEN)) {
274
+ let newBody = actualBody;
275
+ for(let i= 0;i<numberDatabase;i++){
276
+ const currentDb = currentDatabases[i];
277
+
278
+ if(currentDb && currentDb.tokens != null) {
279
+ const keyRefreshToken = REFRESH_TOKEN + '_'+ currentDb.configurationName;
280
+ if(actualBody.includes(keyRefreshToken)) {
281
+ newBody = newBody.replace(keyRefreshToken, encodeURIComponent(currentDb.tokens.refresh_token));
282
+ currentDatabase = currentDb;
283
+ break;
284
+ }
285
+ }
286
+ }
287
+
288
+ return fetch(originalRequest, {
289
+ body: newBody,
290
+ method: clonedRequest.method,
291
+ headers: {
292
+ ...serializeHeaders(originalRequest.headers),
293
+ },
294
+ mode: clonedRequest.mode,
295
+ cache: clonedRequest.cache,
296
+ redirect: clonedRequest.redirect,
297
+ referrer: clonedRequest.referrer,
298
+ credentials: clonedRequest.credentials,
299
+ integrity: clonedRequest.integrity
300
+ }).then(hideTokens(currentDatabase));
301
+ } else if(actualBody.includes("code_verifier=") && currentLoginCallbackConfigurationName){
302
+ currentDatabase = database[currentLoginCallbackConfigurationName];
303
+ currentLoginCallbackConfigurationName=null;
304
+ return fetch(originalRequest,{
305
+ body: actualBody,
306
+ method: clonedRequest.method,
307
+ headers: {
308
+ ...serializeHeaders(originalRequest.headers),
309
+ },
310
+ mode: clonedRequest.mode,
311
+ cache: clonedRequest.cache,
312
+ redirect: clonedRequest.redirect,
313
+ referrer: clonedRequest.referrer,
314
+ credentials: clonedRequest.credentials,
315
+ integrity: clonedRequest.integrity
316
+ }).then(hideTokens(currentDatabase));
317
+ }
318
+ });
319
+ response.then(r => {
320
+ if(r !== undefined){
321
+ resolve(r);
322
+ } else{
323
+ console.log("success undefined");
324
+ reject(new Error("Response is undefined inside a success"));
325
+ }
326
+ }).catch(err => {
327
+ if(err !== undefined) {
328
+ reject(err);
329
+ } else{
330
+ console.log("error undefined");
331
+ reject(new Error("Response is undefined inside a error"));
332
+ }
333
+ });
334
+ });
335
+ event.waitUntil(event.respondWith(maPromesse));
336
+ }
337
+ };
338
+
339
+ self.addEventListener('install', handleInstall);
340
+ self.addEventListener('activate', handleActivate);
341
+ self.addEventListener('fetch', handleFetch);
342
+
343
+
344
+ const checkDomain = (domains, endpoint) => {
345
+ if(!endpoint){
346
+ return;
347
+ }
348
+
349
+ const domain = domains.find(domain => endpoint.startsWith(domain));
350
+ if(!domain){
351
+ throw new Error("Domain " + endpoint + " is not trusted, please add domain in TrustedDomains.js");
352
+ }
353
+ }
354
+
355
+ addEventListener('message', event => {
356
+ const port = event.ports[0];
357
+ const data = event.data;
358
+ const configurationName = data.configurationName;
359
+ let currentDatabase = database[configurationName];
360
+
361
+ if(!currentDatabase){
362
+ database[configurationName] = {
363
+ tokens: null,
364
+ items:[],
365
+ oidcServerConfiguration: null,
366
+ status:null,
367
+ configurationName: configurationName,
368
+ };
369
+ currentDatabase = database[configurationName];
370
+ if(!trustedDomains[configurationName]) {
371
+ trustedDomains[configurationName] = [];
372
+ }
373
+ }
374
+ switch (data.type){
375
+ case "loadItems":
376
+ port.postMessage(database[configurationName].items);
377
+ return;
378
+ case "clear":
379
+ currentDatabase.tokens = null;
380
+ currentDatabase.items = null;
381
+ currentDatabase.status = data.data.status;
382
+ port.postMessage({configurationName});
383
+ return;
384
+ case "init":
385
+ const oidcServerConfiguration = data.data.oidcServerConfiguration;
386
+ const domains = trustedDomains[configurationName];
387
+ if (!domains.find(f => f === acceptAnyDomainToken)) {
388
+ checkDomain(domains, oidcServerConfiguration.tokenEndpoint);
389
+ checkDomain(domains, oidcServerConfiguration.userInfoEndpoint);
390
+ checkDomain(domains, oidcServerConfiguration.issuer);
391
+ }
392
+ currentDatabase.oidcServerConfiguration = oidcServerConfiguration;
393
+ const where = data.data.where;
394
+ if(where === "loginCallbackAsync" || where === "tryKeepExistingSessionAsync") {
395
+ currentLoginCallbackConfigurationName = configurationName;
396
+ } else{
397
+ currentLoginCallbackConfigurationName = null;
398
+ }
399
+
400
+ if(!currentDatabase.tokens){
401
+ port.postMessage({
402
+ tokens:null,
403
+ status: currentDatabase.status,
404
+ configurationName});
405
+ } else {
406
+ const tokens = {
407
+ ...currentDatabase.tokens,
408
+ access_token: ACCESS_TOKEN + "_" + configurationName
409
+ };
410
+ if(tokens.refresh_token){
411
+ tokens.refresh_token = REFRESH_TOKEN + "_" + configurationName;
412
+ }
413
+ if(tokens.idTokenPayload && tokens.idTokenPayload.nonce){
414
+ tokens.idTokenPayload.nonce = NONCE_TOKEN + "_" + configurationName;
415
+ }
416
+ port.postMessage({
417
+ tokens,
418
+ status: currentDatabase.status,
419
+ configurationName
420
+ });
421
+ }
422
+ return;
423
+
424
+ case "setSessionState":
425
+ currentDatabase.sessionState = data.data.sessionState;
426
+ port.postMessage({configurationName});
427
+ return;
428
+ case "getSessionState":
429
+ const sessionState = currentDatabase.sessionState;
430
+ port.postMessage({configurationName, sessionState});
431
+ return;
432
+ case "setNonce":
433
+ currentDatabase.nonce = data.data.nonce;
434
+ port.postMessage({configurationName});
435
+ return;
436
+ default:
437
+ currentDatabase.items = { ...data.data };
438
+ port.postMessage({configurationName});
439
+ return;
440
+ }
441
+ });
442
+
@@ -0,0 +1,16 @@
1
+ 
2
+ // Add bellow trusted domains, access tokens will automatically injected to be send to
3
+ // trusted domain can also be a path like https://www.myapi.com/users,
4
+ // then all subroute like https://www.myapi.com/useers/1 will be authorized to send access_token to.
5
+
6
+ // Domains used by OIDC server must be also declared here
7
+ const trustedDomains = {
8
+ default:["https://demo.duendesoftware.com", "https://kdhttps.auth0.com"],
9
+ config_classic: ["https://demo.duendesoftware.com"] ,
10
+ config_without_silent_login: ["https://demo.duendesoftware.com"] ,
11
+ config_without_refresh_token: ["https://demo.duendesoftware.com"],
12
+ config_without_refresh_token_silent_login: ["https://demo.duendesoftware.com"],
13
+ config_google: ["https://oauth2.googleapis.com", "https://openidconnect.googleapis.com"],
14
+ config_with_hash: ["https://demo.duendesoftware.com"]
15
+ };
16
+