@lvce-editor/auth-worker 2.0.0 → 3.1.0
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/authWorkerMain.js +121 -97
- package/package.json +1 -1
package/dist/authWorkerMain.js
CHANGED
|
@@ -1864,9 +1864,12 @@ async function getResponseJsonBody(response, check = assertApplicationJson) {
|
|
|
1864
1864
|
}
|
|
1865
1865
|
const _expectedIssuer = Symbol();
|
|
1866
1866
|
|
|
1867
|
-
const trailingSlashesRegex = /\/+$/;
|
|
1868
1867
|
const trimTrailingSlashes = value => {
|
|
1869
|
-
|
|
1868
|
+
let end = value.length;
|
|
1869
|
+
while (end > 0 && value[end - 1] === '/') {
|
|
1870
|
+
end--;
|
|
1871
|
+
}
|
|
1872
|
+
return value.slice(0, end);
|
|
1870
1873
|
};
|
|
1871
1874
|
|
|
1872
1875
|
const getBackendAuthUrl = (backendUrl, path) => {
|
|
@@ -1948,14 +1951,18 @@ const getOidcUserName = async (backendUrl, accessToken, fetchFn = fetch) => {
|
|
|
1948
1951
|
if (!payload || typeof payload !== 'object') {
|
|
1949
1952
|
return '';
|
|
1950
1953
|
}
|
|
1951
|
-
const
|
|
1954
|
+
const {
|
|
1955
|
+
displayName
|
|
1956
|
+
} = payload;
|
|
1952
1957
|
return typeof displayName === 'string' ? displayName : '';
|
|
1953
1958
|
};
|
|
1954
1959
|
|
|
1955
1960
|
const databaseName = 'auth-worker';
|
|
1956
1961
|
const objectStoreName = 'auth';
|
|
1957
1962
|
const memoryStorage = new Map();
|
|
1958
|
-
|
|
1963
|
+
const state$1 = {
|
|
1964
|
+
databasePromise: undefined
|
|
1965
|
+
};
|
|
1959
1966
|
|
|
1960
1967
|
// IndexedDB request objects are mutable browser primitives and cannot satisfy the readonly rule structurally.
|
|
1961
1968
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
@@ -1988,8 +1995,8 @@ const getDatabase = async () => {
|
|
|
1988
1995
|
if (typeof indexedDB === 'undefined') {
|
|
1989
1996
|
return undefined;
|
|
1990
1997
|
}
|
|
1991
|
-
if (!databasePromise) {
|
|
1992
|
-
databasePromise = new Promise((resolve, reject) => {
|
|
1998
|
+
if (!state$1.databasePromise) {
|
|
1999
|
+
state$1.databasePromise = new Promise((resolve, reject) => {
|
|
1993
2000
|
const request = indexedDB.open(databaseName, 1);
|
|
1994
2001
|
request.addEventListener('upgradeneeded', () => {
|
|
1995
2002
|
const database = request.result;
|
|
@@ -2005,7 +2012,7 @@ const getDatabase = async () => {
|
|
|
2005
2012
|
});
|
|
2006
2013
|
});
|
|
2007
2014
|
}
|
|
2008
|
-
return databasePromise;
|
|
2015
|
+
return state$1.databasePromise;
|
|
2009
2016
|
};
|
|
2010
2017
|
const getPersistentAuthValue = async key => {
|
|
2011
2018
|
const database = await getDatabase();
|
|
@@ -2084,7 +2091,7 @@ const normalizeRedirectUri = value => {
|
|
|
2084
2091
|
const url = new URL(value);
|
|
2085
2092
|
url.hash = '';
|
|
2086
2093
|
url.search = '';
|
|
2087
|
-
return url.
|
|
2094
|
+
return url.href;
|
|
2088
2095
|
};
|
|
2089
2096
|
const getCallbackHref = async () => {
|
|
2090
2097
|
const storedCallbackUrl = await getOidcCallbackUrl();
|
|
@@ -2107,7 +2114,6 @@ const completeBrowserOidcLogin = async backendUrl => {
|
|
|
2107
2114
|
}
|
|
2108
2115
|
const code = url.searchParams.get('code') || '';
|
|
2109
2116
|
const error = url.searchParams.get('error') || '';
|
|
2110
|
-
const errorDescription = url.searchParams.get('error_description') || '';
|
|
2111
2117
|
if (!code && !error) {
|
|
2112
2118
|
return undefined;
|
|
2113
2119
|
}
|
|
@@ -2120,6 +2126,7 @@ const completeBrowserOidcLogin = async backendUrl => {
|
|
|
2120
2126
|
await clearPendingOidcAuthState();
|
|
2121
2127
|
return getLoggedOutBackendAuthState('Authentication returned to an unexpected redirect URI.');
|
|
2122
2128
|
}
|
|
2129
|
+
const errorDescription = url.searchParams.get('error_description') || '';
|
|
2123
2130
|
if (error) {
|
|
2124
2131
|
await clearPendingOidcAuthState();
|
|
2125
2132
|
return getLoggedOutBackendAuthState(errorDescription || error);
|
|
@@ -2176,24 +2183,24 @@ const getPersistedAuthSession = async () => {
|
|
|
2176
2183
|
authAccessToken: accessToken,
|
|
2177
2184
|
authErrorMessage: '',
|
|
2178
2185
|
userState: 'loggedIn',
|
|
2179
|
-
...(optionalAuthClientId
|
|
2186
|
+
...(optionalAuthClientId && {
|
|
2180
2187
|
authClientId
|
|
2181
|
-
}
|
|
2182
|
-
...(optionalRefreshToken
|
|
2188
|
+
}),
|
|
2189
|
+
...(optionalRefreshToken && {
|
|
2183
2190
|
authRefreshToken: refreshToken
|
|
2184
|
-
}
|
|
2185
|
-
...(optionalUserName
|
|
2191
|
+
}),
|
|
2192
|
+
...(optionalUserName && {
|
|
2186
2193
|
userName
|
|
2187
|
-
}
|
|
2188
|
-
...(optionalSubscriptionPlan
|
|
2194
|
+
}),
|
|
2195
|
+
...(optionalSubscriptionPlan && {
|
|
2189
2196
|
userSubscriptionPlan
|
|
2190
|
-
}
|
|
2191
|
-
...(optionalSubscriptionStatus
|
|
2197
|
+
}),
|
|
2198
|
+
...(optionalSubscriptionStatus && {
|
|
2192
2199
|
userSubscriptionStatus
|
|
2193
|
-
}
|
|
2194
|
-
...(typeof optionalUsedTokens === 'number'
|
|
2200
|
+
}),
|
|
2201
|
+
...(typeof optionalUsedTokens === 'number' && {
|
|
2195
2202
|
userUsedTokens: optionalUsedTokens
|
|
2196
|
-
}
|
|
2203
|
+
})
|
|
2197
2204
|
};
|
|
2198
2205
|
};
|
|
2199
2206
|
const persistAuthSession = async loginResult => {
|
|
@@ -2209,6 +2216,65 @@ const persistLoginResult = async loginResult => {
|
|
|
2209
2216
|
return loginResult;
|
|
2210
2217
|
};
|
|
2211
2218
|
|
|
2219
|
+
const getAuthorizationServer = backendUrl => {
|
|
2220
|
+
return {
|
|
2221
|
+
issuer: getBackendAuthUrl(backendUrl, '/oidc'),
|
|
2222
|
+
jwks_uri: getBackendAuthUrl(backendUrl, '/oidc/jwks'),
|
|
2223
|
+
token_endpoint: getBackendOidcTokenUrl(backendUrl)
|
|
2224
|
+
};
|
|
2225
|
+
};
|
|
2226
|
+
const getClient = clientId => {
|
|
2227
|
+
return {
|
|
2228
|
+
client_id: clientId
|
|
2229
|
+
};
|
|
2230
|
+
};
|
|
2231
|
+
const refreshOidcTokens = async (backendUrl, clientId, refreshToken, requestTokenEndpoint = genericTokenEndpointRequest, processTokenEndpointResponse = processGenericTokenEndpointResponse) => {
|
|
2232
|
+
const authorizationServer = getAuthorizationServer(backendUrl);
|
|
2233
|
+
const client = getClient(clientId);
|
|
2234
|
+
const response = await requestTokenEndpoint(authorizationServer, client, None(), 'refresh_token', new URLSearchParams({
|
|
2235
|
+
refresh_token: refreshToken
|
|
2236
|
+
}));
|
|
2237
|
+
const tokenResponse = await processTokenEndpointResponse(authorizationServer, client, response);
|
|
2238
|
+
return {
|
|
2239
|
+
accessToken: tokenResponse.access_token,
|
|
2240
|
+
refreshToken: typeof tokenResponse.refresh_token === 'string' ? tokenResponse.refresh_token : refreshToken
|
|
2241
|
+
};
|
|
2242
|
+
};
|
|
2243
|
+
|
|
2244
|
+
const clearStoredOidcAuth = async () => {
|
|
2245
|
+
await clearPersistedAuthSession();
|
|
2246
|
+
};
|
|
2247
|
+
const toLoginResult = (accessToken, refreshToken, clientId, userName) => {
|
|
2248
|
+
return {
|
|
2249
|
+
authAccessToken: accessToken,
|
|
2250
|
+
authClientId: clientId,
|
|
2251
|
+
authErrorMessage: '',
|
|
2252
|
+
authRefreshToken: refreshToken,
|
|
2253
|
+
userName,
|
|
2254
|
+
userState: accessToken ? 'loggedIn' : 'loggedOut'
|
|
2255
|
+
};
|
|
2256
|
+
};
|
|
2257
|
+
const restoreOidcAuth = async backendUrl => {
|
|
2258
|
+
const [accessToken, refreshToken, clientId] = await Promise.all([getPersistentAuthValue('accessToken'), getPersistentAuthValue('refreshToken'), getStoredOidcClientId()]);
|
|
2259
|
+
if (!refreshToken || !clientId) {
|
|
2260
|
+
return undefined;
|
|
2261
|
+
}
|
|
2262
|
+
try {
|
|
2263
|
+
const refreshedTokens = await refreshOidcTokens(backendUrl, clientId, refreshToken);
|
|
2264
|
+
const userName = await getOidcUserName(backendUrl, refreshedTokens.accessToken);
|
|
2265
|
+
return toLoginResult(refreshedTokens.accessToken, refreshedTokens.refreshToken, clientId, userName);
|
|
2266
|
+
} catch {
|
|
2267
|
+
if (accessToken) {
|
|
2268
|
+
const userName = await getOidcUserName(backendUrl, accessToken);
|
|
2269
|
+
if (userName) {
|
|
2270
|
+
return toLoginResult(accessToken, refreshToken, clientId, userName);
|
|
2271
|
+
}
|
|
2272
|
+
}
|
|
2273
|
+
await clearStoredOidcAuth();
|
|
2274
|
+
return undefined;
|
|
2275
|
+
}
|
|
2276
|
+
};
|
|
2277
|
+
|
|
2212
2278
|
const getBackendUrl = options => {
|
|
2213
2279
|
if (typeof options === 'number') {
|
|
2214
2280
|
return '';
|
|
@@ -2223,6 +2289,10 @@ const initialize = async options => {
|
|
|
2223
2289
|
if (completedBrowserLogin) {
|
|
2224
2290
|
return persistLoginResult(completedBrowserLogin);
|
|
2225
2291
|
}
|
|
2292
|
+
const restoredOidcAuth = await restoreOidcAuth(backendUrl);
|
|
2293
|
+
if (restoredOidcAuth) {
|
|
2294
|
+
return persistLoginResult(restoredOidcAuth);
|
|
2295
|
+
}
|
|
2226
2296
|
}
|
|
2227
2297
|
const persistedAuthSession = await getPersistedAuthSession();
|
|
2228
2298
|
if (persistedAuthSession) {
|
|
@@ -2264,30 +2334,32 @@ const delay = async ms => {
|
|
|
2264
2334
|
await new Promise(resolve => setTimeout(resolve, ms));
|
|
2265
2335
|
};
|
|
2266
2336
|
|
|
2267
|
-
|
|
2268
|
-
|
|
2337
|
+
const state = {
|
|
2338
|
+
nextLoginResponse: undefined,
|
|
2339
|
+
nextRefreshResponse: undefined
|
|
2340
|
+
};
|
|
2269
2341
|
const setNextLoginResponse = response => {
|
|
2270
|
-
nextLoginResponse = response;
|
|
2342
|
+
state.nextLoginResponse = response;
|
|
2271
2343
|
};
|
|
2272
2344
|
const setNextRefreshResponse = response => {
|
|
2273
|
-
nextRefreshResponse = response;
|
|
2345
|
+
state.nextRefreshResponse = response;
|
|
2274
2346
|
};
|
|
2275
2347
|
const clear = () => {
|
|
2276
|
-
nextLoginResponse = undefined;
|
|
2277
|
-
nextRefreshResponse = undefined;
|
|
2348
|
+
state.nextLoginResponse = undefined;
|
|
2349
|
+
state.nextRefreshResponse = undefined;
|
|
2278
2350
|
};
|
|
2279
2351
|
const hasPendingMockLoginResponse = () => {
|
|
2280
|
-
return !!nextLoginResponse;
|
|
2352
|
+
return !!state.nextLoginResponse;
|
|
2281
2353
|
};
|
|
2282
2354
|
const hasPendingMockRefreshResponse = () => {
|
|
2283
|
-
return !!nextRefreshResponse;
|
|
2355
|
+
return !!state.nextRefreshResponse;
|
|
2284
2356
|
};
|
|
2285
2357
|
const consumeNextLoginResponse = async () => {
|
|
2286
|
-
if (!nextLoginResponse) {
|
|
2358
|
+
if (!state.nextLoginResponse) {
|
|
2287
2359
|
return undefined;
|
|
2288
2360
|
}
|
|
2289
|
-
const response = nextLoginResponse;
|
|
2290
|
-
nextLoginResponse = undefined;
|
|
2361
|
+
const response = state.nextLoginResponse;
|
|
2362
|
+
state.nextLoginResponse = undefined;
|
|
2291
2363
|
if (response.delay > 0) {
|
|
2292
2364
|
await delay(response.delay);
|
|
2293
2365
|
}
|
|
@@ -2297,11 +2369,11 @@ const consumeNextLoginResponse = async () => {
|
|
|
2297
2369
|
return response.response;
|
|
2298
2370
|
};
|
|
2299
2371
|
const consumeNextRefreshResponse = async () => {
|
|
2300
|
-
if (!nextRefreshResponse) {
|
|
2372
|
+
if (!state.nextRefreshResponse) {
|
|
2301
2373
|
return undefined;
|
|
2302
2374
|
}
|
|
2303
|
-
const response = nextRefreshResponse;
|
|
2304
|
-
nextRefreshResponse = undefined;
|
|
2375
|
+
const response = state.nextRefreshResponse;
|
|
2376
|
+
state.nextRefreshResponse = undefined;
|
|
2305
2377
|
if (response.delay > 0) {
|
|
2306
2378
|
await new Promise(resolve => setTimeout(resolve, response.delay));
|
|
2307
2379
|
}
|
|
@@ -2353,65 +2425,6 @@ const parseBackendAuthResponse = value => {
|
|
|
2353
2425
|
return toBackendAuthState(value);
|
|
2354
2426
|
};
|
|
2355
2427
|
|
|
2356
|
-
const getAuthorizationServer = backendUrl => {
|
|
2357
|
-
return {
|
|
2358
|
-
issuer: getBackendAuthUrl(backendUrl, '/oidc'),
|
|
2359
|
-
jwks_uri: getBackendAuthUrl(backendUrl, '/oidc/jwks'),
|
|
2360
|
-
token_endpoint: getBackendOidcTokenUrl(backendUrl)
|
|
2361
|
-
};
|
|
2362
|
-
};
|
|
2363
|
-
const getClient = clientId => {
|
|
2364
|
-
return {
|
|
2365
|
-
client_id: clientId
|
|
2366
|
-
};
|
|
2367
|
-
};
|
|
2368
|
-
const refreshOidcTokens = async (backendUrl, clientId, refreshToken, requestTokenEndpoint = genericTokenEndpointRequest, processTokenEndpointResponse = processGenericTokenEndpointResponse) => {
|
|
2369
|
-
const authorizationServer = getAuthorizationServer(backendUrl);
|
|
2370
|
-
const client = getClient(clientId);
|
|
2371
|
-
const response = await requestTokenEndpoint(authorizationServer, client, None(), 'refresh_token', new URLSearchParams({
|
|
2372
|
-
refresh_token: refreshToken
|
|
2373
|
-
}));
|
|
2374
|
-
const tokenResponse = await processTokenEndpointResponse(authorizationServer, client, response);
|
|
2375
|
-
return {
|
|
2376
|
-
accessToken: tokenResponse.access_token,
|
|
2377
|
-
refreshToken: typeof tokenResponse.refresh_token === 'string' ? tokenResponse.refresh_token : refreshToken
|
|
2378
|
-
};
|
|
2379
|
-
};
|
|
2380
|
-
|
|
2381
|
-
const clearStoredOidcAuth = async () => {
|
|
2382
|
-
await clearPersistedAuthSession();
|
|
2383
|
-
};
|
|
2384
|
-
const toLoginResult = (accessToken, refreshToken, clientId, userName) => {
|
|
2385
|
-
return {
|
|
2386
|
-
authAccessToken: accessToken,
|
|
2387
|
-
authClientId: clientId,
|
|
2388
|
-
authErrorMessage: '',
|
|
2389
|
-
authRefreshToken: refreshToken,
|
|
2390
|
-
userName,
|
|
2391
|
-
userState: accessToken ? 'loggedIn' : 'loggedOut'
|
|
2392
|
-
};
|
|
2393
|
-
};
|
|
2394
|
-
const restoreOidcAuth = async backendUrl => {
|
|
2395
|
-
const [accessToken, refreshToken, clientId] = await Promise.all([getPersistentAuthValue('accessToken'), getPersistentAuthValue('refreshToken'), getStoredOidcClientId()]);
|
|
2396
|
-
if (!refreshToken || !clientId) {
|
|
2397
|
-
return undefined;
|
|
2398
|
-
}
|
|
2399
|
-
try {
|
|
2400
|
-
const refreshedTokens = await refreshOidcTokens(backendUrl, clientId, refreshToken);
|
|
2401
|
-
const userName = await getOidcUserName(backendUrl, refreshedTokens.accessToken);
|
|
2402
|
-
return toLoginResult(refreshedTokens.accessToken, refreshedTokens.refreshToken, clientId, userName);
|
|
2403
|
-
} catch {
|
|
2404
|
-
if (accessToken) {
|
|
2405
|
-
const userName = await getOidcUserName(backendUrl, accessToken);
|
|
2406
|
-
if (userName) {
|
|
2407
|
-
return toLoginResult(accessToken, refreshToken, clientId, userName);
|
|
2408
|
-
}
|
|
2409
|
-
}
|
|
2410
|
-
await clearStoredOidcAuth();
|
|
2411
|
-
return undefined;
|
|
2412
|
-
}
|
|
2413
|
-
};
|
|
2414
|
-
|
|
2415
2428
|
const getPayload = async response => {
|
|
2416
2429
|
try {
|
|
2417
2430
|
return await response.json();
|
|
@@ -2743,6 +2756,16 @@ const getElectronRedirectUri = async uid => {
|
|
|
2743
2756
|
return `http://localhost:${localOauthServerPort}/callback`;
|
|
2744
2757
|
};
|
|
2745
2758
|
|
|
2759
|
+
const getGithubPagesBasePath = url => {
|
|
2760
|
+
if (!url.hostname.endsWith('.github.io')) {
|
|
2761
|
+
return '';
|
|
2762
|
+
}
|
|
2763
|
+
const firstSegment = url.pathname.split('/').find(Boolean);
|
|
2764
|
+
if (!firstSegment) {
|
|
2765
|
+
return '';
|
|
2766
|
+
}
|
|
2767
|
+
return `/${firstSegment}`;
|
|
2768
|
+
};
|
|
2746
2769
|
const getWebRedirectUri = async () => {
|
|
2747
2770
|
const href = await getCurrentHref();
|
|
2748
2771
|
if (!href) {
|
|
@@ -2750,7 +2773,7 @@ const getWebRedirectUri = async () => {
|
|
|
2750
2773
|
}
|
|
2751
2774
|
try {
|
|
2752
2775
|
const url = new URL(href);
|
|
2753
|
-
return `${url.origin}/auth/callback`;
|
|
2776
|
+
return `${url.origin}${getGithubPagesBasePath(url)}/auth/callback`;
|
|
2754
2777
|
} catch {
|
|
2755
2778
|
return '';
|
|
2756
2779
|
}
|
|
@@ -2796,7 +2819,7 @@ const getBackendLoginRequest = async (backendUrl, platform = 0, uid = 0, redirec
|
|
|
2796
2819
|
return {
|
|
2797
2820
|
clientId,
|
|
2798
2821
|
codeVerifier,
|
|
2799
|
-
loginUrl: loginUrl.
|
|
2822
|
+
loginUrl: loginUrl.href,
|
|
2800
2823
|
nonce,
|
|
2801
2824
|
redirectUri: effectiveRedirectUri,
|
|
2802
2825
|
state
|
|
@@ -2806,12 +2829,13 @@ const getBackendLoginRequest = async (backendUrl, platform = 0, uid = 0, redirec
|
|
|
2806
2829
|
const getLoggedInState = (state, response) => {
|
|
2807
2830
|
const accessToken = typeof response.accessToken === 'string' ? response.accessToken : '';
|
|
2808
2831
|
const refreshToken = typeof response.refreshToken === 'string' ? response.refreshToken : '';
|
|
2832
|
+
const userNameState = typeof response.userName === 'string' ? response : state;
|
|
2809
2833
|
return {
|
|
2810
2834
|
...state,
|
|
2811
2835
|
authAccessToken: accessToken,
|
|
2812
2836
|
authErrorMessage: '',
|
|
2813
2837
|
authRefreshToken: refreshToken,
|
|
2814
|
-
userName:
|
|
2838
|
+
userName: userNameState.userName,
|
|
2815
2839
|
userState: accessToken ? 'loggedIn' : 'loggedOut',
|
|
2816
2840
|
userSubscriptionPlan: typeof response.subscriptionPlan === 'string' ? response.subscriptionPlan : state.userSubscriptionPlan,
|
|
2817
2841
|
userSubscriptionStatus: typeof response.subscriptionStatus === 'string' ? response.subscriptionStatus : state.userSubscriptionStatus,
|