@axa-fr/oidc-client-service-worker 7.9.3-alpha.1132 → 7.9.3

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.
@@ -1,34 +1,35 @@
1
1
  import { acceptAnyDomainToken, scriptFilename, TOKEN } from './constants';
2
- import version from './version';
3
2
  import {
4
- Database,
5
- MessageEventData,
6
- OidcConfig,
7
- TrustedDomains,
3
+ Database,
4
+ MessageEventData,
5
+ OidcConfig,
6
+ TrustedDomains,
8
7
  } from './types';
9
8
  import {
10
- checkDomain,
11
- getCurrentDatabaseDomain,
12
- getDomains,
13
- hideTokens,
14
- isTokensValid,
15
- serializeHeaders,
16
- sleep,
9
+ checkDomain,
10
+ getCurrentDatabaseDomain,
11
+ getDomains,
12
+ hideTokens,
13
+ isTokensValid,
14
+ serializeHeaders,
15
+ sleep,
17
16
  } from './utils';
18
17
  import { replaceCodeVerifier } from './utils/codeVerifier';
18
+ import { normalizeUrl } from './utils/normalizeUrl';
19
+ import version from './version';
19
20
 
20
21
  // @ts-ignore
21
- if ((typeof trustedTypes !== 'undefined') && (typeof trustedTypes.createPolicy == 'function')) {
22
- // @ts-ignore
23
- trustedTypes.createPolicy('default', {
24
- createScriptURL: function(url: string) {
25
- if (url == scriptFilename) {
26
- return url;
27
- } else {
28
- throw new Error('Untrusted script URL blocked: ' + url);
29
- }
30
- },
31
- });
22
+ if (typeof trustedTypes !== 'undefined' && typeof trustedTypes.createPolicy == 'function') {
23
+ // @ts-ignore
24
+ trustedTypes.createPolicy('default', {
25
+ createScriptURL: function (url: string) {
26
+ if (url == scriptFilename) {
27
+ return url;
28
+ } else {
29
+ throw new Error('Untrusted script URL blocked: ' + url);
30
+ }
31
+ },
32
+ });
32
33
  }
33
34
 
34
35
  const _self = self as ServiceWorkerGlobalScope & typeof globalThis;
@@ -41,415 +42,455 @@ const id = Math.round(new Date().getTime() / 1000).toString();
41
42
 
42
43
  const keepAliveJsonFilename = 'OidcKeepAliveServiceWorker.json';
43
44
  const handleInstall = (event: ExtendableEvent) => {
44
- console.log('[OidcServiceWorker] service worker installed ' + id);
45
- event.waitUntil(_self.skipWaiting());
45
+ console.log('[OidcServiceWorker] service worker installed ' + id);
46
+ event.waitUntil(_self.skipWaiting());
46
47
  };
47
48
 
48
49
  const handleActivate = (event: ExtendableEvent) => {
49
- console.log('[OidcServiceWorker] service worker activated ' + id);
50
- event.waitUntil(_self.clients.claim());
50
+ console.log('[OidcServiceWorker] service worker activated ' + id);
51
+ event.waitUntil(_self.clients.claim());
51
52
  };
52
53
 
53
54
  let currentLoginCallbackConfigurationName: string | null = null;
54
55
  const database: Database = {};
55
56
 
56
57
  const getCurrentDatabasesTokenEndpoint = (database: Database, url: string) => {
57
- const databases: OidcConfig[] = [];
58
- for (const [, value] of Object.entries<OidcConfig>(database)) {
59
- if (
60
- value.oidcServerConfiguration != null &&
61
- url.startsWith(value.oidcServerConfiguration.tokenEndpoint)
62
- ) {
63
- databases.push(value);
64
- } else if (
65
- value.oidcServerConfiguration != null &&
66
- value.oidcServerConfiguration.revocationEndpoint &&
67
- url.startsWith(value.oidcServerConfiguration.revocationEndpoint)
68
- ) {
69
- databases.push(value);
70
- }
71
- }
72
- return databases;
58
+ const databases: OidcConfig[] = [];
59
+ for (const [, value] of Object.entries<OidcConfig>(database)) {
60
+ if (
61
+ value.oidcServerConfiguration != null &&
62
+ url.startsWith(normalizeUrl(value.oidcServerConfiguration.tokenEndpoint))
63
+ ) {
64
+ databases.push(value);
65
+ } else if (
66
+ value.oidcServerConfiguration != null &&
67
+ value.oidcServerConfiguration.revocationEndpoint &&
68
+ url.startsWith(
69
+ normalizeUrl(value.oidcServerConfiguration.revocationEndpoint),
70
+ )
71
+ ) {
72
+ databases.push(value);
73
+ }
74
+ }
75
+ return databases;
73
76
  };
74
77
 
75
78
  const keepAliveAsync = async (event: FetchEvent) => {
76
- const originalRequest = event.request;
77
- const isFromVanilla = originalRequest.headers.has('oidc-vanilla');
78
- const init = { status: 200, statusText: 'oidc-service-worker' };
79
- const response = new Response('{}', init);
80
- if (!isFromVanilla) {
81
- const originalRequestUrl = new URL(originalRequest.url);
82
- const minSleepSeconds = Number(originalRequestUrl.searchParams.get('minSleepSeconds')) || 240;
83
- for (let i = 0; i < minSleepSeconds; i++) {
84
- await sleep(1000 + Math.floor(Math.random() * 1000));
85
- const cache = await caches.open('oidc_dummy_cache');
86
- await cache.put(event.request, response.clone());
87
- }
88
- }
89
- return response;
79
+ const originalRequest = event.request;
80
+ const isFromVanilla = originalRequest.headers.has('oidc-vanilla');
81
+ const init = { status: 200, statusText: 'oidc-service-worker' };
82
+ const response = new Response('{}', init);
83
+ if (!isFromVanilla) {
84
+ const originalRequestUrl = new URL(originalRequest.url);
85
+ const minSleepSeconds =
86
+ Number(originalRequestUrl.searchParams.get('minSleepSeconds')) || 240;
87
+ for (let i = 0; i < minSleepSeconds; i++) {
88
+ await sleep(1000 + Math.floor(Math.random() * 1000));
89
+ const cache = await caches.open('oidc_dummy_cache');
90
+ await cache.put(event.request, response.clone());
91
+ }
92
+ }
93
+ return response;
90
94
  };
91
95
 
92
96
  const handleFetch = async (event: FetchEvent) => {
93
- const originalRequest = event.request;
94
- const url = originalRequest.url;
95
- if (originalRequest.url.includes(keepAliveJsonFilename)) {
96
- event.respondWith(keepAliveAsync(event));
97
- return;
98
- }
97
+ const originalRequest = event.request;
98
+ const url = normalizeUrl(originalRequest.url);
99
+ if (url.includes(keepAliveJsonFilename)) {
100
+ event.respondWith(keepAliveAsync(event));
101
+ return;
102
+ }
99
103
 
100
- const currentDatabaseForRequestAccessToken = getCurrentDatabaseDomain(
101
- database,
102
- originalRequest.url,
103
- trustedDomains,
104
- );
105
- if (
106
- currentDatabaseForRequestAccessToken &&
107
- currentDatabaseForRequestAccessToken.tokens &&
108
- currentDatabaseForRequestAccessToken.tokens.access_token
109
- ) {
110
- while (
111
- currentDatabaseForRequestAccessToken.tokens &&
112
- !isTokensValid(currentDatabaseForRequestAccessToken.tokens)
113
- ) {
114
- await sleep(200);
115
- }
116
-
117
- let requestMode = originalRequest.mode;
118
-
119
- if(originalRequest.mode !== "navigate" && currentDatabaseForRequestAccessToken.convertAllRequestsToCorsExceptNavigate) {
120
- requestMode = "cors";
121
- }
122
-
123
- let headers: { [p: string]: string };
124
- if(originalRequest.mode == "navigate" && !currentDatabaseForRequestAccessToken.setAccessTokenToNavigateRequests ) {
125
- headers = {
126
- ...serializeHeaders(originalRequest.headers),
127
- }
128
- } else{
129
- headers = {
130
- ...serializeHeaders(originalRequest.headers),
131
- authorization: 'Bearer ' + currentDatabaseForRequestAccessToken.tokens.access_token,
132
- }
133
- }
134
- let init: RequestInit;
135
- if(originalRequest.mode === "navigate"){
136
- init = {
137
- headers: headers,
138
- }
139
- } else{
140
- init = {
141
- headers: headers,
142
- mode: requestMode,
143
- }
144
- }
145
-
146
- const newRequest = new Request(originalRequest, init);
104
+ const currentDatabaseForRequestAccessToken = getCurrentDatabaseDomain(
105
+ database,
106
+ url,
107
+ trustedDomains,
108
+ );
109
+ if (
110
+ currentDatabaseForRequestAccessToken &&
111
+ currentDatabaseForRequestAccessToken.tokens &&
112
+ currentDatabaseForRequestAccessToken.tokens.access_token
113
+ ) {
114
+ while (
115
+ currentDatabaseForRequestAccessToken.tokens &&
116
+ !isTokensValid(currentDatabaseForRequestAccessToken.tokens)
117
+ ) {
118
+ await sleep(200);
119
+ }
147
120
 
148
- event.respondWith(fetch(newRequest));
121
+ let requestMode = originalRequest.mode;
149
122
 
150
- return;
151
- }
123
+ if (
124
+ originalRequest.mode !== 'navigate' &&
125
+ currentDatabaseForRequestAccessToken.convertAllRequestsToCorsExceptNavigate
126
+ ) {
127
+ requestMode = 'cors';
128
+ }
152
129
 
153
- if (event.request.method !== 'POST') {
154
- return;
155
- }
130
+ let headers: { [p: string]: string };
131
+ if (
132
+ originalRequest.mode == 'navigate' &&
133
+ !currentDatabaseForRequestAccessToken.setAccessTokenToNavigateRequests
134
+ ) {
135
+ headers = {
136
+ ...serializeHeaders(originalRequest.headers),
137
+ };
138
+ } else {
139
+ headers = {
140
+ ...serializeHeaders(originalRequest.headers),
141
+ authorization:
142
+ 'Bearer ' + currentDatabaseForRequestAccessToken.tokens.access_token,
143
+ };
144
+ }
145
+ let init: RequestInit;
146
+ if (originalRequest.mode === 'navigate') {
147
+ init = {
148
+ headers: headers,
149
+ };
150
+ } else {
151
+ init = {
152
+ headers: headers,
153
+ mode: requestMode,
154
+ };
155
+ }
156
156
 
157
- let currentDatabase: OidcConfig | null = null;
158
- const currentDatabases = getCurrentDatabasesTokenEndpoint(
159
- database,
160
- originalRequest.url,
161
- );
162
- const numberDatabase = currentDatabases.length;
163
- if (numberDatabase > 0) {
164
- const maPromesse = new Promise<Response>((resolve, reject) => {
165
- const clonedRequest = originalRequest.clone();
166
- const response = clonedRequest.text().then((actualBody) => {
167
- if (
168
- actualBody.includes(TOKEN.REFRESH_TOKEN) ||
169
- actualBody.includes(TOKEN.ACCESS_TOKEN)
170
- ) {
171
- let newBody = actualBody;
172
- for (let i = 0; i < numberDatabase; i++) {
173
- const currentDb = currentDatabases[i];
157
+ const newRequest = new Request(originalRequest, init);
174
158
 
175
- if (currentDb && currentDb.tokens != null) {
176
- const keyRefreshToken =
177
- TOKEN.REFRESH_TOKEN + '_' + currentDb.configurationName;
178
- if (actualBody.includes(keyRefreshToken)) {
179
- newBody = newBody.replace(
180
- keyRefreshToken,
181
- encodeURIComponent(currentDb.tokens.refresh_token as string),
182
- );
183
- currentDatabase = currentDb;
184
- break;
185
- }
186
- const keyAccessToken =
187
- TOKEN.ACCESS_TOKEN + '_' + currentDb.configurationName;
188
- if (actualBody.includes(keyAccessToken)) {
189
- newBody = newBody.replace(
190
- keyAccessToken,
191
- encodeURIComponent(currentDb.tokens.access_token),
192
- );
193
- currentDatabase = currentDb;
194
- break;
195
- }
196
- }
197
- }
198
- const fetchPromise = fetch(originalRequest, {
199
- body: newBody,
200
- method: clonedRequest.method,
201
- headers: {
202
- ...serializeHeaders(originalRequest.headers),
203
- },
204
- mode: clonedRequest.mode,
205
- cache: clonedRequest.cache,
206
- redirect: clonedRequest.redirect,
207
- referrer: clonedRequest.referrer,
208
- credentials: clonedRequest.credentials,
209
- integrity: clonedRequest.integrity,
210
- });
159
+ event.respondWith(fetch(newRequest));
211
160
 
212
- if (
213
- currentDatabase &&
214
- currentDatabase.oidcServerConfiguration != null &&
215
- currentDatabase.oidcServerConfiguration.revocationEndpoint &&
216
- url.startsWith(
217
- currentDatabase.oidcServerConfiguration.revocationEndpoint,
218
- )
219
- ) {
220
- return fetchPromise.then(async (response) => {
221
- const text = await response.text();
222
- return new Response(text, response);
223
- });
224
- }
225
- return fetchPromise.then(hideTokens(currentDatabase as OidcConfig)); // todo type assertion to OidcConfig but could be null, NEEDS REVIEW
226
- } else if (
227
- actualBody.includes('code_verifier=') &&
228
- currentLoginCallbackConfigurationName
229
- ) {
230
- currentDatabase = database[currentLoginCallbackConfigurationName];
231
- currentLoginCallbackConfigurationName = null;
232
- let newBody = actualBody;
233
- if (currentDatabase && currentDatabase.codeVerifier != null) {
234
- newBody = replaceCodeVerifier(newBody, currentDatabase.codeVerifier);
235
- }
161
+ return;
162
+ }
236
163
 
237
- return fetch(originalRequest, {
238
- body: newBody,
239
- method: clonedRequest.method,
240
- headers: {
241
- ...serializeHeaders(originalRequest.headers),
242
- },
243
- mode: clonedRequest.mode,
244
- cache: clonedRequest.cache,
245
- redirect: clonedRequest.redirect,
246
- referrer: clonedRequest.referrer,
247
- credentials: clonedRequest.credentials,
248
- integrity: clonedRequest.integrity,
249
- }).then(hideTokens(currentDatabase));
250
- }
164
+ if (event.request.method !== 'POST') {
165
+ return;
166
+ }
251
167
 
252
- // if showAccessToken=true, the token is already in the body
253
- // of the request, and it does not need to be injected
254
- // and we can simply clone the request
255
- return fetch(originalRequest, {
256
- body: actualBody,
257
- method: clonedRequest.method,
258
- headers: {
259
- ...serializeHeaders(originalRequest.headers),
260
- },
261
- mode: clonedRequest.mode,
262
- cache: clonedRequest.cache,
263
- redirect: clonedRequest.redirect,
264
- referrer: clonedRequest.referrer,
265
- credentials: clonedRequest.credentials,
266
- integrity: clonedRequest.integrity,
267
- });
268
- });
269
- response
270
- .then((r) => {
271
- resolve(r);
272
- })
273
- .catch((err) => {
274
- reject(err);
275
- });
276
- });
168
+ let currentDatabase: OidcConfig | null = null;
169
+ const currentDatabases = getCurrentDatabasesTokenEndpoint(database, url);
170
+ const numberDatabase = currentDatabases.length;
171
+ if (numberDatabase > 0) {
172
+ const maPromesse = new Promise<Response>((resolve, reject) => {
173
+ const clonedRequest = originalRequest.clone();
174
+ const response = clonedRequest.text().then((actualBody) => {
175
+ if (
176
+ actualBody.includes(TOKEN.REFRESH_TOKEN) ||
177
+ actualBody.includes(TOKEN.ACCESS_TOKEN)
178
+ ) {
179
+ let newBody = actualBody;
180
+ for (let i = 0; i < numberDatabase; i++) {
181
+ const currentDb = currentDatabases[i];
277
182
 
278
- event.respondWith(maPromesse);
279
- }
280
- };
183
+ if (currentDb && currentDb.tokens != null) {
184
+ const keyRefreshToken =
185
+ TOKEN.REFRESH_TOKEN + '_' + currentDb.configurationName;
186
+ if (actualBody.includes(keyRefreshToken)) {
187
+ newBody = newBody.replace(
188
+ keyRefreshToken,
189
+ encodeURIComponent(currentDb.tokens.refresh_token as string),
190
+ );
191
+ currentDatabase = currentDb;
192
+ break;
193
+ }
194
+ const keyAccessToken =
195
+ TOKEN.ACCESS_TOKEN + '_' + currentDb.configurationName;
196
+ if (actualBody.includes(keyAccessToken)) {
197
+ newBody = newBody.replace(
198
+ keyAccessToken,
199
+ encodeURIComponent(currentDb.tokens.access_token),
200
+ );
201
+ currentDatabase = currentDb;
202
+ break;
203
+ }
204
+ }
205
+ }
206
+ const fetchPromise = fetch(originalRequest, {
207
+ body: newBody,
208
+ method: clonedRequest.method,
209
+ headers: {
210
+ ...serializeHeaders(originalRequest.headers),
211
+ },
212
+ mode: clonedRequest.mode,
213
+ cache: clonedRequest.cache,
214
+ redirect: clonedRequest.redirect,
215
+ referrer: clonedRequest.referrer,
216
+ credentials: clonedRequest.credentials,
217
+ integrity: clonedRequest.integrity,
218
+ });
219
+
220
+ if (
221
+ currentDatabase &&
222
+ currentDatabase.oidcServerConfiguration != null &&
223
+ currentDatabase.oidcServerConfiguration.revocationEndpoint &&
224
+ url.startsWith(
225
+ normalizeUrl(
226
+ currentDatabase.oidcServerConfiguration.revocationEndpoint,
227
+ ),
228
+ )
229
+ ) {
230
+ return fetchPromise.then(async (response) => {
231
+ const text = await response.text();
232
+ return new Response(text, response);
233
+ });
234
+ }
235
+ return fetchPromise.then(hideTokens(currentDatabase as OidcConfig)); // todo type assertion to OidcConfig but could be null, NEEDS REVIEW
236
+ } else if (
237
+ actualBody.includes('code_verifier=') &&
238
+ currentLoginCallbackConfigurationName
239
+ ) {
240
+ currentDatabase = database[currentLoginCallbackConfigurationName];
241
+ currentLoginCallbackConfigurationName = null;
242
+ let newBody = actualBody;
243
+ if (currentDatabase && currentDatabase.codeVerifier != null) {
244
+ newBody = replaceCodeVerifier(
245
+ newBody,
246
+ currentDatabase.codeVerifier,
247
+ );
248
+ }
281
249
 
250
+ return fetch(originalRequest, {
251
+ body: newBody,
252
+ method: clonedRequest.method,
253
+ headers: {
254
+ ...serializeHeaders(originalRequest.headers),
255
+ },
256
+ mode: clonedRequest.mode,
257
+ cache: clonedRequest.cache,
258
+ redirect: clonedRequest.redirect,
259
+ referrer: clonedRequest.referrer,
260
+ credentials: clonedRequest.credentials,
261
+ integrity: clonedRequest.integrity,
262
+ }).then(hideTokens(currentDatabase));
263
+ }
264
+
265
+ // if showAccessToken=true, the token is already in the body
266
+ // of the request, and it does not need to be injected
267
+ // and we can simply clone the request
268
+ return fetch(originalRequest, {
269
+ body: actualBody,
270
+ method: clonedRequest.method,
271
+ headers: {
272
+ ...serializeHeaders(originalRequest.headers),
273
+ },
274
+ mode: clonedRequest.mode,
275
+ cache: clonedRequest.cache,
276
+ redirect: clonedRequest.redirect,
277
+ referrer: clonedRequest.referrer,
278
+ credentials: clonedRequest.credentials,
279
+ integrity: clonedRequest.integrity,
280
+ });
281
+ });
282
+ response
283
+ .then((r) => {
284
+ resolve(r);
285
+ })
286
+ .catch((err) => {
287
+ reject(err);
288
+ });
289
+ });
290
+
291
+ event.respondWith(maPromesse);
292
+ }
293
+ };
282
294
 
283
295
  const handleMessage = (event: ExtendableMessageEvent) => {
284
- const port = event.ports[0];
285
- const data = event.data as MessageEventData;
286
- if (event.data.type === "claim") {
287
- _self.clients.claim().then(() => port.postMessage({}));
288
- return;
289
- }
290
- const configurationName = data.configurationName;
291
- let currentDatabase = database[configurationName];
292
- if (trustedDomains == null) {
293
- trustedDomains = {};
294
- }
295
- if (!currentDatabase) {
296
- const trustedDomain = trustedDomains[configurationName];
297
- const showAccessToken = Array.isArray(trustedDomain) ? false : trustedDomain.showAccessToken;
298
- const doNotSetAccessTokenToNavigateRequests = Array.isArray(trustedDomain) ? true : trustedDomain.setAccessTokenToNavigateRequests;
299
- const convertAllRequestsToCorsExceptNavigate = Array.isArray(trustedDomain) ? false : trustedDomain.convertAllRequestsToCorsExceptNavigate;
300
- database[configurationName] = {
301
- tokens: null,
302
- state: null,
303
- codeVerifier: null,
304
- oidcServerConfiguration: null,
305
- oidcConfiguration: undefined,
306
- nonce: null,
307
- status: null,
308
- configurationName,
309
- hideAccessToken: !showAccessToken,
310
- setAccessTokenToNavigateRequests: doNotSetAccessTokenToNavigateRequests ?? true,
311
- convertAllRequestsToCorsExceptNavigate: convertAllRequestsToCorsExceptNavigate ?? false,
312
- demonstratingProofOfPossessionNonce: null,
313
- demonstratingProofOfPossessionJwkJson: null,
314
- };
315
- currentDatabase = database[configurationName];
296
+ const port = event.ports[0];
297
+ const data = event.data as MessageEventData;
298
+ if (event.data.type === 'claim') {
299
+ _self.clients.claim().then(() => port.postMessage({}));
300
+ return;
301
+ }
302
+ const configurationName = data.configurationName;
303
+ let currentDatabase = database[configurationName];
304
+ if (trustedDomains == null) {
305
+ trustedDomains = {};
306
+ }
307
+ if (!currentDatabase) {
308
+ const trustedDomain = trustedDomains[configurationName];
309
+ const showAccessToken = Array.isArray(trustedDomain)
310
+ ? false
311
+ : trustedDomain.showAccessToken;
312
+ const doNotSetAccessTokenToNavigateRequests = Array.isArray(trustedDomain)
313
+ ? true
314
+ : trustedDomain.setAccessTokenToNavigateRequests;
315
+ const convertAllRequestsToCorsExceptNavigate = Array.isArray(trustedDomain)
316
+ ? false
317
+ : trustedDomain.convertAllRequestsToCorsExceptNavigate;
318
+ database[configurationName] = {
319
+ tokens: null,
320
+ state: null,
321
+ codeVerifier: null,
322
+ oidcServerConfiguration: null,
323
+ oidcConfiguration: undefined,
324
+ nonce: null,
325
+ status: null,
326
+ configurationName,
327
+ hideAccessToken: !showAccessToken,
328
+ setAccessTokenToNavigateRequests:
329
+ doNotSetAccessTokenToNavigateRequests ?? true,
330
+ convertAllRequestsToCorsExceptNavigate:
331
+ convertAllRequestsToCorsExceptNavigate ?? false,
332
+ demonstratingProofOfPossessionNonce: null,
333
+ demonstratingProofOfPossessionJwkJson: null,
334
+ };
335
+ currentDatabase = database[configurationName];
316
336
 
317
- if (!trustedDomains[configurationName]) {
318
- trustedDomains[configurationName] = [];
319
- }
320
- }
337
+ if (!trustedDomains[configurationName]) {
338
+ trustedDomains[configurationName] = [];
339
+ }
340
+ }
321
341
 
322
- switch (data.type) {
323
- case 'clear':
324
- currentDatabase.tokens = null;
325
- currentDatabase.state = null;
326
- currentDatabase.codeVerifier = null;
327
- currentDatabase.status = data.data.status;
328
- port.postMessage({ configurationName });
329
- return;
330
- case 'init': {
331
- const oidcServerConfiguration = data.data.oidcServerConfiguration;
332
- const trustedDomain = trustedDomains[configurationName];
333
- const domains = getDomains(trustedDomain, 'oidc');
334
- if (!domains.find((f) => f === acceptAnyDomainToken)) {
335
- [
336
- oidcServerConfiguration.tokenEndpoint,
337
- oidcServerConfiguration.revocationEndpoint,
338
- oidcServerConfiguration.userInfoEndpoint,
339
- oidcServerConfiguration.issuer,
340
- ].forEach((url) => {
341
- checkDomain(domains, url);
342
- });
343
- }
344
- currentDatabase.oidcServerConfiguration = oidcServerConfiguration;
345
- currentDatabase.oidcConfiguration = data.data.oidcConfiguration;
346
- const where = data.data.where;
347
- if (
348
- where === 'loginCallbackAsync' ||
349
- where === 'tryKeepExistingSessionAsync'
350
- ) {
351
- currentLoginCallbackConfigurationName = configurationName;
352
- } else {
353
- currentLoginCallbackConfigurationName = null;
354
- }
342
+ switch (data.type) {
343
+ case 'clear':
344
+ currentDatabase.tokens = null;
345
+ currentDatabase.state = null;
346
+ currentDatabase.codeVerifier = null;
347
+ currentDatabase.status = data.data.status;
348
+ port.postMessage({ configurationName });
349
+ return;
350
+ case 'init': {
351
+ const oidcServerConfiguration = data.data.oidcServerConfiguration;
352
+ const trustedDomain = trustedDomains[configurationName];
353
+ const domains = getDomains(trustedDomain, 'oidc');
354
+ if (!domains.some((domain) => domain === acceptAnyDomainToken)) {
355
+ [
356
+ oidcServerConfiguration.tokenEndpoint,
357
+ oidcServerConfiguration.revocationEndpoint,
358
+ oidcServerConfiguration.userInfoEndpoint,
359
+ oidcServerConfiguration.issuer,
360
+ ].forEach((url) => {
361
+ checkDomain(domains, url);
362
+ });
363
+ }
364
+ currentDatabase.oidcServerConfiguration = oidcServerConfiguration;
365
+ currentDatabase.oidcConfiguration = data.data.oidcConfiguration;
366
+ const where = data.data.where;
367
+ if (
368
+ where === 'loginCallbackAsync' ||
369
+ where === 'tryKeepExistingSessionAsync'
370
+ ) {
371
+ currentLoginCallbackConfigurationName = configurationName;
372
+ } else {
373
+ currentLoginCallbackConfigurationName = null;
374
+ }
355
375
 
356
- if (!currentDatabase.tokens) {
357
- port.postMessage({
358
- tokens: null,
359
- status: currentDatabase.status,
360
- configurationName,
361
- version
362
- });
363
- } else {
364
- const tokens = {
365
- ...currentDatabase.tokens,
366
- };
367
- if (currentDatabase.hideAccessToken) {
368
- tokens.access_token = TOKEN.ACCESS_TOKEN + '_' + configurationName;
369
- }
370
- if (tokens.refresh_token) {
371
- tokens.refresh_token = TOKEN.REFRESH_TOKEN + '_' + configurationName;
372
- }
373
- if (
374
- tokens.idTokenPayload &&
375
- tokens.idTokenPayload.nonce &&
376
- currentDatabase.nonce != null
377
- ) {
378
- tokens.idTokenPayload.nonce =
379
- TOKEN.NONCE_TOKEN + '_' + configurationName;
380
- }
381
- port.postMessage({
382
- tokens,
383
- status: currentDatabase.status,
384
- configurationName,
385
- version
386
- });
387
- }
388
- return;
389
- }
390
- case 'setDemonstratingProofOfPossessionNonce':
391
- currentDatabase.demonstratingProofOfPossessionNonce = data.data.demonstratingProofOfPossessionNonce;
392
- port.postMessage({ configurationName });
393
- return;
394
- case 'getDemonstratingProofOfPossessionNonce':
395
- const demonstratingProofOfPossessionNonce = currentDatabase.demonstratingProofOfPossessionNonce;
396
- port.postMessage({ configurationName, demonstratingProofOfPossessionNonce });
397
- return;
398
- case 'setDemonstratingProofOfPossessionJwk':
399
- currentDatabase.demonstratingProofOfPossessionJwkJson = data.data.demonstratingProofOfPossessionJwkJson;
400
- port.postMessage({ configurationName });
401
- return;
402
- case 'getDemonstratingProofOfPossessionJwk':
403
- const demonstratingProofOfPossessionJwkJson = currentDatabase.demonstratingProofOfPossessionJwkJson;
404
- port.postMessage({ configurationName, demonstratingProofOfPossessionJwkJson });
405
- return;
406
- case 'setState':
407
- currentDatabase.state = data.data.state;
408
- port.postMessage({ configurationName });
409
- return;
410
- case 'getState': {
411
- const state = currentDatabase.state;
412
- port.postMessage({ configurationName, state });
413
- return;
414
- }
415
- case 'setCodeVerifier':
416
- currentDatabase.codeVerifier = data.data.codeVerifier;
417
- port.postMessage({ configurationName });
418
- return;
419
- case 'getCodeVerifier': {
420
- port.postMessage({
421
- configurationName,
422
- codeVerifier: currentDatabase.codeVerifier != null ? TOKEN.CODE_VERIFIER + '_' + configurationName : null,
423
- });
424
- return;
425
- }
426
- case 'setSessionState':
427
- currentDatabase.sessionState = data.data.sessionState;
428
- port.postMessage({ configurationName });
429
- return;
430
- case 'getSessionState': {
431
- const sessionState = currentDatabase.sessionState;
432
- port.postMessage({ configurationName, sessionState });
433
- return;
434
- }
435
- case 'setNonce': {
436
- const nonce = data.data.nonce;
437
- if (nonce) {
438
- currentDatabase.nonce = nonce;
439
- }
440
- port.postMessage({ configurationName });
441
- return;
442
- }
443
- case 'getNonce': {
444
- const keyNonce = TOKEN.NONCE_TOKEN + '_' + configurationName;
445
- const nonce = currentDatabase.nonce ? keyNonce : null;
446
- port.postMessage({ configurationName, nonce });
447
- return;
448
- }
449
- default:
450
- currentDatabase.items = { ...data.data };
451
- port.postMessage({ configurationName });
452
- }
376
+ if (!currentDatabase.tokens) {
377
+ port.postMessage({
378
+ tokens: null,
379
+ status: currentDatabase.status,
380
+ configurationName,
381
+ version,
382
+ });
383
+ } else {
384
+ const tokens = {
385
+ ...currentDatabase.tokens,
386
+ };
387
+ if (currentDatabase.hideAccessToken) {
388
+ tokens.access_token = TOKEN.ACCESS_TOKEN + '_' + configurationName;
389
+ }
390
+ if (tokens.refresh_token) {
391
+ tokens.refresh_token = TOKEN.REFRESH_TOKEN + '_' + configurationName;
392
+ }
393
+ if (
394
+ tokens.idTokenPayload &&
395
+ tokens.idTokenPayload.nonce &&
396
+ currentDatabase.nonce != null
397
+ ) {
398
+ tokens.idTokenPayload.nonce =
399
+ TOKEN.NONCE_TOKEN + '_' + configurationName;
400
+ }
401
+ port.postMessage({
402
+ tokens,
403
+ status: currentDatabase.status,
404
+ configurationName,
405
+ version,
406
+ });
407
+ }
408
+ return;
409
+ }
410
+ case 'setDemonstratingProofOfPossessionNonce': {
411
+ currentDatabase.demonstratingProofOfPossessionNonce =
412
+ data.data.demonstratingProofOfPossessionNonce;
413
+ port.postMessage({ configurationName });
414
+ return;
415
+ }
416
+ case 'getDemonstratingProofOfPossessionNonce': {
417
+ const demonstratingProofOfPossessionNonce =
418
+ currentDatabase.demonstratingProofOfPossessionNonce;
419
+ port.postMessage({
420
+ configurationName,
421
+ demonstratingProofOfPossessionNonce,
422
+ });
423
+ return;
424
+ }
425
+ case 'setDemonstratingProofOfPossessionJwk': {
426
+ currentDatabase.demonstratingProofOfPossessionJwkJson =
427
+ data.data.demonstratingProofOfPossessionJwkJson;
428
+ port.postMessage({ configurationName });
429
+ return;
430
+ }
431
+ case 'getDemonstratingProofOfPossessionJwk': {
432
+ const demonstratingProofOfPossessionJwkJson =
433
+ currentDatabase.demonstratingProofOfPossessionJwkJson;
434
+ port.postMessage({
435
+ configurationName,
436
+ demonstratingProofOfPossessionJwkJson,
437
+ });
438
+ return;
439
+ }
440
+ case 'setState': {
441
+ currentDatabase.state = data.data.state;
442
+ port.postMessage({ configurationName });
443
+ return;
444
+ }
445
+ case 'getState': {
446
+ const state = currentDatabase.state;
447
+ port.postMessage({ configurationName, state });
448
+ return;
449
+ }
450
+ case 'setCodeVerifier': {
451
+ currentDatabase.codeVerifier = data.data.codeVerifier;
452
+ port.postMessage({ configurationName });
453
+ return;
454
+ }
455
+ case 'getCodeVerifier': {
456
+ port.postMessage({
457
+ configurationName,
458
+ codeVerifier:
459
+ currentDatabase.codeVerifier != null
460
+ ? TOKEN.CODE_VERIFIER + '_' + configurationName
461
+ : null,
462
+ });
463
+ return;
464
+ }
465
+ case 'setSessionState': {
466
+ currentDatabase.sessionState = data.data.sessionState;
467
+ port.postMessage({ configurationName });
468
+ return;
469
+ }
470
+ case 'getSessionState': {
471
+ const sessionState = currentDatabase.sessionState;
472
+ port.postMessage({ configurationName, sessionState });
473
+ return;
474
+ }
475
+ case 'setNonce': {
476
+ const nonce = data.data.nonce;
477
+ if (nonce) {
478
+ currentDatabase.nonce = nonce;
479
+ }
480
+ port.postMessage({ configurationName });
481
+ return;
482
+ }
483
+ case 'getNonce': {
484
+ const keyNonce = TOKEN.NONCE_TOKEN + '_' + configurationName;
485
+ const nonce = currentDatabase.nonce ? keyNonce : null;
486
+ port.postMessage({ configurationName, nonce });
487
+ return;
488
+ }
489
+ default: {
490
+ currentDatabase.items = { ...data.data };
491
+ port.postMessage({ configurationName });
492
+ }
493
+ }
453
494
  };
454
495
 
455
496
  _self.addEventListener('install', handleInstall);