@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.
- package/dist/OidcServiceWorker.js +51 -74
- package/dist/OidcServiceWorker.js.map +1 -1
- package/dist/src/utils/__tests__/normalizeUrl.spec.d.ts +2 -0
- package/dist/src/utils/__tests__/normalizeUrl.spec.d.ts.map +1 -0
- package/dist/src/utils/domains.d.ts +0 -1
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/normalizeUrl.d.ts +1 -0
- package/dist/src/version.d.ts +1 -1
- package/package.json +1 -1
- package/src/OidcServiceWorker.ts +442 -401
- package/src/utils/__tests__/domains.spec.ts +33 -61
- package/src/utils/__tests__/normalizeUrl.spec.ts +28 -0
- package/src/utils/domains.ts +3 -86
- package/src/utils/index.ts +1 -0
- package/src/utils/normalizeUrl.ts +9 -0
- package/src/version.ts +1 -1
package/src/OidcServiceWorker.ts
CHANGED
|
@@ -1,34 +1,35 @@
|
|
|
1
1
|
import { acceptAnyDomainToken, scriptFilename, TOKEN } from './constants';
|
|
2
|
-
import version from './version';
|
|
3
2
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
Database,
|
|
4
|
+
MessageEventData,
|
|
5
|
+
OidcConfig,
|
|
6
|
+
TrustedDomains,
|
|
8
7
|
} from './types';
|
|
9
8
|
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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 (
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
45
|
-
|
|
45
|
+
console.log('[OidcServiceWorker] service worker installed ' + id);
|
|
46
|
+
event.waitUntil(_self.skipWaiting());
|
|
46
47
|
};
|
|
47
48
|
|
|
48
49
|
const handleActivate = (event: ExtendableEvent) => {
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
-
|
|
121
|
+
let requestMode = originalRequest.mode;
|
|
149
122
|
|
|
150
|
-
|
|
151
|
-
|
|
123
|
+
if (
|
|
124
|
+
originalRequest.mode !== 'navigate' &&
|
|
125
|
+
currentDatabaseForRequestAccessToken.convertAllRequestsToCorsExceptNavigate
|
|
126
|
+
) {
|
|
127
|
+
requestMode = 'cors';
|
|
128
|
+
}
|
|
152
129
|
|
|
153
|
-
|
|
154
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
213
|
-
|
|
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
|
-
|
|
238
|
-
|
|
239
|
-
|
|
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
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
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
|
-
|
|
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
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
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
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
337
|
+
if (!trustedDomains[configurationName]) {
|
|
338
|
+
trustedDomains[configurationName] = [];
|
|
339
|
+
}
|
|
340
|
+
}
|
|
321
341
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
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
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
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);
|