@lvce-editor/auth-worker 3.5.0 → 3.6.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 +225 -127
- package/package.json +1 -1
package/dist/authWorkerMain.js
CHANGED
|
@@ -478,7 +478,10 @@ const constructError = (message, type, name) => {
|
|
|
478
478
|
if (ErrorConstructor === Error) {
|
|
479
479
|
const error = new Error(message);
|
|
480
480
|
if (name && name !== 'VError') {
|
|
481
|
-
error
|
|
481
|
+
Object.defineProperty(error, 'name', {
|
|
482
|
+
configurable: true,
|
|
483
|
+
value: name
|
|
484
|
+
});
|
|
482
485
|
}
|
|
483
486
|
return error;
|
|
484
487
|
}
|
|
@@ -495,8 +498,10 @@ const getCurrentStack = () => {
|
|
|
495
498
|
const currentStack = joinLines(splitLines(new Error().stack || '').slice(stackLinesToSkip));
|
|
496
499
|
return currentStack;
|
|
497
500
|
};
|
|
498
|
-
const getNewLineIndex = (string, startIndex
|
|
499
|
-
|
|
501
|
+
const getNewLineIndex = (string, startIndex) => {
|
|
502
|
+
{
|
|
503
|
+
return string.indexOf(NewLine);
|
|
504
|
+
}
|
|
500
505
|
};
|
|
501
506
|
const getParentStack = error => {
|
|
502
507
|
let parentStack = error.stack || error.data || error.message || '';
|
|
@@ -507,55 +512,91 @@ const getParentStack = error => {
|
|
|
507
512
|
};
|
|
508
513
|
const MethodNotFound = -32601;
|
|
509
514
|
const Custom = -32001;
|
|
515
|
+
const setStack = (error, stack) => {
|
|
516
|
+
const descriptor = Object.getOwnPropertyDescriptor(error, 'stack');
|
|
517
|
+
if (descriptor) {
|
|
518
|
+
if (!descriptor.configurable && !descriptor.writable) {
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
if (!descriptor.configurable && descriptor.writable) {
|
|
522
|
+
error.stack = stack;
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
Object.defineProperty(error, 'stack', {
|
|
527
|
+
configurable: true,
|
|
528
|
+
value: stack,
|
|
529
|
+
writable: true
|
|
530
|
+
});
|
|
531
|
+
};
|
|
532
|
+
const restoreExistingError = (error, currentStack) => {
|
|
533
|
+
if (typeof error.stack === 'string') {
|
|
534
|
+
setStack(error, `${error.stack}${NewLine}${currentStack}`);
|
|
535
|
+
}
|
|
536
|
+
return error;
|
|
537
|
+
};
|
|
538
|
+
const restoreMethodNotFoundError = (error, currentStack) => {
|
|
539
|
+
const restoredError = new JsonRpcError(error.message);
|
|
540
|
+
const parentStack = getParentStack(error);
|
|
541
|
+
setStack(restoredError, `${parentStack}${NewLine}${currentStack}`);
|
|
542
|
+
return restoredError;
|
|
543
|
+
};
|
|
544
|
+
const restoreStackFromData = (restoredError, error, currentStack) => {
|
|
545
|
+
if (error.data.stack && error.data.type && error.message) {
|
|
546
|
+
setStack(restoredError, `${error.data.type}: ${error.message}${NewLine}${error.data.stack}${NewLine}${currentStack}`);
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
if (error.data.stack) {
|
|
550
|
+
setStack(restoredError, error.data.stack);
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
const applyDataProperties = (restoredError, error) => {
|
|
554
|
+
restoreStackFromData(restoredError, error, getCurrentStack());
|
|
555
|
+
if (error.data.codeFrame) {
|
|
556
|
+
// @ts-ignore
|
|
557
|
+
restoredError.codeFrame = error.data.codeFrame;
|
|
558
|
+
}
|
|
559
|
+
if (error.data.code) {
|
|
560
|
+
// @ts-ignore
|
|
561
|
+
restoredError.code = error.data.code;
|
|
562
|
+
}
|
|
563
|
+
if (error.data.type) {
|
|
564
|
+
// @ts-ignore
|
|
565
|
+
restoredError.name = error.data.type;
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
const applyDirectProperties = (restoredError, error) => {
|
|
569
|
+
if (error.stack) {
|
|
570
|
+
const lowerStack = restoredError.stack || '';
|
|
571
|
+
const indexNewLine = getNewLineIndex(lowerStack);
|
|
572
|
+
const parentStack = getParentStack(error);
|
|
573
|
+
// @ts-ignore
|
|
574
|
+
setStack(restoredError, `${parentStack}${lowerStack.slice(indexNewLine)}`);
|
|
575
|
+
}
|
|
576
|
+
if (error.codeFrame) {
|
|
577
|
+
// @ts-ignore
|
|
578
|
+
restoredError.codeFrame = error.codeFrame;
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
const restoreMessageError = (error, _currentStack) => {
|
|
582
|
+
const restoredError = constructError(error.message, error.type, error.name);
|
|
583
|
+
if (error.data) {
|
|
584
|
+
applyDataProperties(restoredError, error);
|
|
585
|
+
} else {
|
|
586
|
+
applyDirectProperties(restoredError, error);
|
|
587
|
+
}
|
|
588
|
+
return restoredError;
|
|
589
|
+
};
|
|
510
590
|
const restoreJsonRpcError = error => {
|
|
511
591
|
const currentStack = getCurrentStack();
|
|
512
592
|
if (error && error instanceof Error) {
|
|
513
|
-
|
|
514
|
-
error.stack = error.stack + NewLine + currentStack;
|
|
515
|
-
}
|
|
516
|
-
return error;
|
|
593
|
+
return restoreExistingError(error, currentStack);
|
|
517
594
|
}
|
|
518
595
|
if (error && error.code && error.code === MethodNotFound) {
|
|
519
|
-
|
|
520
|
-
const parentStack = getParentStack(error);
|
|
521
|
-
restoredError.stack = parentStack + NewLine + currentStack;
|
|
522
|
-
return restoredError;
|
|
596
|
+
return restoreMethodNotFoundError(error, currentStack);
|
|
523
597
|
}
|
|
524
598
|
if (error && error.message) {
|
|
525
|
-
|
|
526
|
-
if (error.data) {
|
|
527
|
-
if (error.data.stack && error.data.type && error.message) {
|
|
528
|
-
restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
|
|
529
|
-
} else if (error.data.stack) {
|
|
530
|
-
restoredError.stack = error.data.stack;
|
|
531
|
-
}
|
|
532
|
-
if (error.data.codeFrame) {
|
|
533
|
-
// @ts-ignore
|
|
534
|
-
restoredError.codeFrame = error.data.codeFrame;
|
|
535
|
-
}
|
|
536
|
-
if (error.data.code) {
|
|
537
|
-
// @ts-ignore
|
|
538
|
-
restoredError.code = error.data.code;
|
|
539
|
-
}
|
|
540
|
-
if (error.data.type) {
|
|
541
|
-
// @ts-ignore
|
|
542
|
-
restoredError.name = error.data.type;
|
|
543
|
-
}
|
|
544
|
-
} else {
|
|
545
|
-
if (error.stack) {
|
|
546
|
-
const lowerStack = restoredError.stack || '';
|
|
547
|
-
// @ts-ignore
|
|
548
|
-
const indexNewLine = getNewLineIndex(lowerStack);
|
|
549
|
-
const parentStack = getParentStack(error);
|
|
550
|
-
// @ts-ignore
|
|
551
|
-
restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
|
|
552
|
-
}
|
|
553
|
-
if (error.codeFrame) {
|
|
554
|
-
// @ts-ignore
|
|
555
|
-
restoredError.codeFrame = error.codeFrame;
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
return restoredError;
|
|
599
|
+
return restoreMessageError(error);
|
|
559
600
|
}
|
|
560
601
|
if (typeof error === 'string') {
|
|
561
602
|
return new Error(`JsonRpc Error: ${error}`);
|
|
@@ -1060,6 +1101,31 @@ const initializeOpenerWorker = async () => {
|
|
|
1060
1101
|
set(rpc);
|
|
1061
1102
|
};
|
|
1062
1103
|
|
|
1104
|
+
const millisecondsPerSecond = 1000;
|
|
1105
|
+
const getAccessTokenExpiresAt = (expiresIn, now = Date.now()) => {
|
|
1106
|
+
if (typeof expiresIn !== 'number' || !Number.isFinite(expiresIn) || expiresIn <= 0) {
|
|
1107
|
+
return undefined;
|
|
1108
|
+
}
|
|
1109
|
+
return now + expiresIn * millisecondsPerSecond;
|
|
1110
|
+
};
|
|
1111
|
+
const isAccessTokenValid = (accessToken, expiresAt, now = Date.now()) => {
|
|
1112
|
+
if (!accessToken || !expiresAt) {
|
|
1113
|
+
return false;
|
|
1114
|
+
}
|
|
1115
|
+
const expiresAtNumber = Number(expiresAt);
|
|
1116
|
+
return Number.isFinite(expiresAtNumber) && expiresAtNumber > now;
|
|
1117
|
+
};
|
|
1118
|
+
|
|
1119
|
+
const state$2 = {
|
|
1120
|
+
backendUrl: ''
|
|
1121
|
+
};
|
|
1122
|
+
const getAuthBackendUrl = () => {
|
|
1123
|
+
return state$2.backendUrl;
|
|
1124
|
+
};
|
|
1125
|
+
const setAuthBackendUrl = backendUrl => {
|
|
1126
|
+
state$2.backendUrl = backendUrl;
|
|
1127
|
+
};
|
|
1128
|
+
|
|
1063
1129
|
const databaseName = 'auth-worker';
|
|
1064
1130
|
const objectStoreName = 'auth';
|
|
1065
1131
|
const memoryStorage = new Map();
|
|
@@ -1150,19 +1216,44 @@ const clearPersistentAuthValue = async key => {
|
|
|
1150
1216
|
await transactionToPromise(transaction);
|
|
1151
1217
|
};
|
|
1152
1218
|
|
|
1153
|
-
const
|
|
1154
|
-
|
|
1219
|
+
const callbackUrlKey = 'oidcCallbackUrl';
|
|
1220
|
+
const clientIdKey = 'oidcClientId';
|
|
1221
|
+
const pendingClientIdKey = 'pendingOidcClientId';
|
|
1222
|
+
const pendingCodeVerifierKey = 'pendingOidcCodeVerifier';
|
|
1223
|
+
const pendingRedirectUriKey = 'pendingOidcRedirectUri';
|
|
1224
|
+
const pendingStateKey = 'pendingOidcState';
|
|
1225
|
+
const clearOidcCallbackUrl = async () => {
|
|
1226
|
+
await clearPersistentAuthValue(callbackUrlKey);
|
|
1155
1227
|
};
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1228
|
+
const clearStoredOidcClientId = async () => {
|
|
1229
|
+
await clearPersistentAuthValue(clientIdKey);
|
|
1230
|
+
};
|
|
1231
|
+
const clearPendingOidcAuthState = async () => {
|
|
1232
|
+
await Promise.all([clearPersistentAuthValue(pendingClientIdKey), clearPersistentAuthValue(pendingCodeVerifierKey), clearPersistentAuthValue(pendingRedirectUriKey), clearPersistentAuthValue(pendingStateKey)]);
|
|
1233
|
+
};
|
|
1234
|
+
const getOidcCallbackUrl = async () => {
|
|
1235
|
+
return getPersistentAuthValue(callbackUrlKey);
|
|
1236
|
+
};
|
|
1237
|
+
const getStoredOidcClientId = async () => {
|
|
1238
|
+
return getPersistentAuthValue(clientIdKey);
|
|
1239
|
+
};
|
|
1240
|
+
const loadPendingOidcAuthState = async () => {
|
|
1241
|
+
const [clientId, codeVerifier, redirectUri, state] = await Promise.all([getPersistentAuthValue(pendingClientIdKey), getPersistentAuthValue(pendingCodeVerifierKey), getPersistentAuthValue(pendingRedirectUriKey), getPersistentAuthValue(pendingStateKey)]);
|
|
1242
|
+
if (!clientId || !codeVerifier || !redirectUri || !state) {
|
|
1243
|
+
return undefined;
|
|
1165
1244
|
}
|
|
1245
|
+
return {
|
|
1246
|
+
clientId,
|
|
1247
|
+
codeVerifier,
|
|
1248
|
+
redirectUri,
|
|
1249
|
+
state
|
|
1250
|
+
};
|
|
1251
|
+
};
|
|
1252
|
+
const saveOidcClientId = async clientId => {
|
|
1253
|
+
await setPersistentAuthValue(clientIdKey, clientId);
|
|
1254
|
+
};
|
|
1255
|
+
const savePendingOidcAuthState = async value => {
|
|
1256
|
+
await Promise.all([setPersistentAuthValue(pendingClientIdKey, value.clientId), setPersistentAuthValue(pendingCodeVerifierKey, value.codeVerifier), setPersistentAuthValue(pendingRedirectUriKey, value.redirectUri), setPersistentAuthValue(pendingStateKey, value.state)]);
|
|
1166
1257
|
};
|
|
1167
1258
|
|
|
1168
1259
|
let USER_AGENT;
|
|
@@ -1986,9 +2077,62 @@ const getClient$1 = clientId => {
|
|
|
1986
2077
|
client_id: clientId
|
|
1987
2078
|
};
|
|
1988
2079
|
};
|
|
1989
|
-
const
|
|
2080
|
+
const refreshOidcTokens = async (backendUrl, clientId, refreshToken, requestTokenEndpoint = genericTokenEndpointRequest, processTokenEndpointResponse = processGenericTokenEndpointResponse) => {
|
|
1990
2081
|
const authorizationServer = getAuthorizationServer$1(backendUrl);
|
|
1991
2082
|
const client = getClient$1(clientId);
|
|
2083
|
+
const response = await requestTokenEndpoint(authorizationServer, client, None(), 'refresh_token', new URLSearchParams({
|
|
2084
|
+
refresh_token: refreshToken
|
|
2085
|
+
}));
|
|
2086
|
+
const tokenResponse = await processTokenEndpointResponse(authorizationServer, client, response);
|
|
2087
|
+
return {
|
|
2088
|
+
accessToken: tokenResponse.access_token,
|
|
2089
|
+
expiresIn: typeof tokenResponse.expires_in === 'number' ? tokenResponse.expires_in : undefined,
|
|
2090
|
+
refreshToken: typeof tokenResponse.refresh_token === 'string' ? tokenResponse.refresh_token : refreshToken
|
|
2091
|
+
};
|
|
2092
|
+
};
|
|
2093
|
+
|
|
2094
|
+
const getAccessToken = async (options = {}, refreshTokens = refreshOidcTokens, now = Date.now()) => {
|
|
2095
|
+
const [accessToken, accessTokenExpiresAt] = await Promise.all([getPersistentAuthValue('accessToken'), getPersistentAuthValue('accessTokenExpiresAt')]);
|
|
2096
|
+
if (options.refresh !== 'if-needed' || isAccessTokenValid(accessToken, accessTokenExpiresAt, now)) {
|
|
2097
|
+
return accessToken;
|
|
2098
|
+
}
|
|
2099
|
+
const [clientId, refreshToken] = await Promise.all([getStoredOidcClientId(), getPersistentAuthValue('refreshToken')]);
|
|
2100
|
+
const backendUrl = getAuthBackendUrl();
|
|
2101
|
+
if (!backendUrl || !clientId || !refreshToken) {
|
|
2102
|
+
return accessToken;
|
|
2103
|
+
}
|
|
2104
|
+
const refreshedTokens = await refreshTokens(backendUrl, clientId, refreshToken);
|
|
2105
|
+
const expiresAt = getAccessTokenExpiresAt(refreshedTokens.expiresIn, now);
|
|
2106
|
+
await Promise.all([setPersistentAuthValue('accessToken', refreshedTokens.accessToken), setPersistentAuthValue('accessTokenExpiresAt', typeof expiresAt === 'number' ? String(expiresAt) : ''), setPersistentAuthValue('refreshToken', refreshedTokens.refreshToken)]);
|
|
2107
|
+
return refreshedTokens.accessToken;
|
|
2108
|
+
};
|
|
2109
|
+
|
|
2110
|
+
const handleMessagePort = async (port, rpcId) => {
|
|
2111
|
+
const rpc = await create$4({
|
|
2112
|
+
commandMap: commandMap,
|
|
2113
|
+
isMessagePortOpen: true,
|
|
2114
|
+
messagePort: port
|
|
2115
|
+
});
|
|
2116
|
+
if (rpcId) {
|
|
2117
|
+
set$2(rpcId, rpc);
|
|
2118
|
+
}
|
|
2119
|
+
};
|
|
2120
|
+
|
|
2121
|
+
const getAuthorizationServer = backendUrl => {
|
|
2122
|
+
return {
|
|
2123
|
+
issuer: getBackendAuthUrl(backendUrl, '/oidc'),
|
|
2124
|
+
jwks_uri: getBackendAuthUrl(backendUrl, '/oidc/jwks'),
|
|
2125
|
+
token_endpoint: getBackendOidcTokenUrl(backendUrl)
|
|
2126
|
+
};
|
|
2127
|
+
};
|
|
2128
|
+
const getClient = clientId => {
|
|
2129
|
+
return {
|
|
2130
|
+
client_id: clientId
|
|
2131
|
+
};
|
|
2132
|
+
};
|
|
2133
|
+
const exchangeAuthorizationCode = async (backendUrl, clientId, code, redirectUri, codeVerifier, requestTokenEndpoint = genericTokenEndpointRequest, processTokenEndpointResponse = processGenericTokenEndpointResponse) => {
|
|
2134
|
+
const authorizationServer = getAuthorizationServer(backendUrl);
|
|
2135
|
+
const client = getClient(clientId);
|
|
1992
2136
|
const response = await requestTokenEndpoint(authorizationServer, client, None(), 'authorization_code', new URLSearchParams({
|
|
1993
2137
|
code,
|
|
1994
2138
|
code_verifier: codeVerifier,
|
|
@@ -1997,6 +2141,7 @@ const exchangeAuthorizationCode = async (backendUrl, clientId, code, redirectUri
|
|
|
1997
2141
|
const tokenResponse = await processTokenEndpointResponse(authorizationServer, client, response);
|
|
1998
2142
|
return {
|
|
1999
2143
|
accessToken: tokenResponse.access_token,
|
|
2144
|
+
expiresIn: typeof tokenResponse.expires_in === 'number' ? tokenResponse.expires_in : undefined,
|
|
2000
2145
|
refreshToken: typeof tokenResponse.refresh_token === 'string' ? tokenResponse.refresh_token : ''
|
|
2001
2146
|
};
|
|
2002
2147
|
};
|
|
@@ -2051,46 +2196,6 @@ const getOidcUserName = async (backendUrl, accessToken, fetchFn = fetch) => {
|
|
|
2051
2196
|
return typeof displayName === 'string' ? displayName : '';
|
|
2052
2197
|
};
|
|
2053
2198
|
|
|
2054
|
-
const callbackUrlKey = 'oidcCallbackUrl';
|
|
2055
|
-
const clientIdKey = 'oidcClientId';
|
|
2056
|
-
const pendingClientIdKey = 'pendingOidcClientId';
|
|
2057
|
-
const pendingCodeVerifierKey = 'pendingOidcCodeVerifier';
|
|
2058
|
-
const pendingRedirectUriKey = 'pendingOidcRedirectUri';
|
|
2059
|
-
const pendingStateKey = 'pendingOidcState';
|
|
2060
|
-
const clearOidcCallbackUrl = async () => {
|
|
2061
|
-
await clearPersistentAuthValue(callbackUrlKey);
|
|
2062
|
-
};
|
|
2063
|
-
const clearStoredOidcClientId = async () => {
|
|
2064
|
-
await clearPersistentAuthValue(clientIdKey);
|
|
2065
|
-
};
|
|
2066
|
-
const clearPendingOidcAuthState = async () => {
|
|
2067
|
-
await Promise.all([clearPersistentAuthValue(pendingClientIdKey), clearPersistentAuthValue(pendingCodeVerifierKey), clearPersistentAuthValue(pendingRedirectUriKey), clearPersistentAuthValue(pendingStateKey)]);
|
|
2068
|
-
};
|
|
2069
|
-
const getOidcCallbackUrl = async () => {
|
|
2070
|
-
return getPersistentAuthValue(callbackUrlKey);
|
|
2071
|
-
};
|
|
2072
|
-
const getStoredOidcClientId = async () => {
|
|
2073
|
-
return getPersistentAuthValue(clientIdKey);
|
|
2074
|
-
};
|
|
2075
|
-
const loadPendingOidcAuthState = async () => {
|
|
2076
|
-
const [clientId, codeVerifier, redirectUri, state] = await Promise.all([getPersistentAuthValue(pendingClientIdKey), getPersistentAuthValue(pendingCodeVerifierKey), getPersistentAuthValue(pendingRedirectUriKey), getPersistentAuthValue(pendingStateKey)]);
|
|
2077
|
-
if (!clientId || !codeVerifier || !redirectUri || !state) {
|
|
2078
|
-
return undefined;
|
|
2079
|
-
}
|
|
2080
|
-
return {
|
|
2081
|
-
clientId,
|
|
2082
|
-
codeVerifier,
|
|
2083
|
-
redirectUri,
|
|
2084
|
-
state
|
|
2085
|
-
};
|
|
2086
|
-
};
|
|
2087
|
-
const saveOidcClientId = async clientId => {
|
|
2088
|
-
await setPersistentAuthValue(clientIdKey, clientId);
|
|
2089
|
-
};
|
|
2090
|
-
const savePendingOidcAuthState = async value => {
|
|
2091
|
-
await Promise.all([setPersistentAuthValue(pendingClientIdKey, value.clientId), setPersistentAuthValue(pendingCodeVerifierKey, value.codeVerifier), setPersistentAuthValue(pendingRedirectUriKey, value.redirectUri), setPersistentAuthValue(pendingStateKey, value.state)]);
|
|
2092
|
-
};
|
|
2093
|
-
|
|
2094
2199
|
const normalizeRedirectUri = value => {
|
|
2095
2200
|
const url = new URL(value);
|
|
2096
2201
|
url.hash = '';
|
|
@@ -2142,9 +2247,13 @@ const completeBrowserOidcLogin = async backendUrl => {
|
|
|
2142
2247
|
}
|
|
2143
2248
|
const exchanged = await exchangeAuthorizationCode(backendUrl, pendingAuthState.clientId, code, pendingAuthState.redirectUri, pendingAuthState.codeVerifier);
|
|
2144
2249
|
const userName = await getOidcUserName(backendUrl, exchanged.accessToken);
|
|
2250
|
+
const authAccessTokenExpiresAt = getAccessTokenExpiresAt(exchanged.expiresIn);
|
|
2145
2251
|
await clearPendingOidcAuthState();
|
|
2146
2252
|
return {
|
|
2147
2253
|
authAccessToken: exchanged.accessToken,
|
|
2254
|
+
...(authAccessTokenExpiresAt && {
|
|
2255
|
+
authAccessTokenExpiresAt
|
|
2256
|
+
}),
|
|
2148
2257
|
authClientId: pendingAuthState.clientId,
|
|
2149
2258
|
authErrorMessage: '',
|
|
2150
2259
|
authRefreshToken: exchanged.refreshToken,
|
|
@@ -2154,6 +2263,7 @@ const completeBrowserOidcLogin = async backendUrl => {
|
|
|
2154
2263
|
};
|
|
2155
2264
|
|
|
2156
2265
|
const accessTokenKey = 'accessToken';
|
|
2266
|
+
const accessTokenExpiresAtKey = 'accessTokenExpiresAt';
|
|
2157
2267
|
const refreshTokenKey = 'refreshToken';
|
|
2158
2268
|
const userNameKey = 'userName';
|
|
2159
2269
|
const userSubscriptionPlanKey = 'userSubscriptionPlan';
|
|
@@ -2170,14 +2280,15 @@ const toOptionalNumber = value => {
|
|
|
2170
2280
|
return Number.isFinite(numberValue) ? numberValue : undefined;
|
|
2171
2281
|
};
|
|
2172
2282
|
const clearPersistedAuthSession = async () => {
|
|
2173
|
-
await Promise.all([clearPersistentAuthValue(accessTokenKey), clearPersistentAuthValue(refreshTokenKey), clearStoredOidcClientId(), clearPersistentAuthValue(userNameKey), clearPersistentAuthValue(userSubscriptionPlanKey), clearPersistentAuthValue(userSubscriptionStatusKey), clearPersistentAuthValue(userUsedTokensKey)]);
|
|
2283
|
+
await Promise.all([clearPersistentAuthValue(accessTokenKey), clearPersistentAuthValue(accessTokenExpiresAtKey), clearPersistentAuthValue(refreshTokenKey), clearStoredOidcClientId(), clearPersistentAuthValue(userNameKey), clearPersistentAuthValue(userSubscriptionPlanKey), clearPersistentAuthValue(userSubscriptionStatusKey), clearPersistentAuthValue(userUsedTokensKey)]);
|
|
2174
2284
|
};
|
|
2175
2285
|
const getPersistedAuthSession = async () => {
|
|
2176
|
-
const [accessToken, refreshToken, authClientId, userName, userSubscriptionPlan, userSubscriptionStatus, userUsedTokens] = await Promise.all([getPersistentAuthValue(accessTokenKey), getPersistentAuthValue(refreshTokenKey), getStoredOidcClientId(), getPersistentAuthValue(userNameKey), getPersistentAuthValue(userSubscriptionPlanKey), getPersistentAuthValue(userSubscriptionStatusKey), getPersistentAuthValue(userUsedTokensKey)]);
|
|
2286
|
+
const [accessToken, accessTokenExpiresAt, refreshToken, authClientId, userName, userSubscriptionPlan, userSubscriptionStatus, userUsedTokens] = await Promise.all([getPersistentAuthValue(accessTokenKey), getPersistentAuthValue(accessTokenExpiresAtKey), getPersistentAuthValue(refreshTokenKey), getStoredOidcClientId(), getPersistentAuthValue(userNameKey), getPersistentAuthValue(userSubscriptionPlanKey), getPersistentAuthValue(userSubscriptionStatusKey), getPersistentAuthValue(userUsedTokensKey)]);
|
|
2177
2287
|
if (!accessToken && !refreshToken) {
|
|
2178
2288
|
return undefined;
|
|
2179
2289
|
}
|
|
2180
2290
|
const optionalAuthClientId = toOptionalString(authClientId);
|
|
2291
|
+
const optionalAccessTokenExpiresAt = toOptionalNumber(accessTokenExpiresAt);
|
|
2181
2292
|
const optionalRefreshToken = toOptionalString(refreshToken);
|
|
2182
2293
|
const optionalUserName = toOptionalString(userName);
|
|
2183
2294
|
const optionalSubscriptionPlan = toOptionalString(userSubscriptionPlan);
|
|
@@ -2185,6 +2296,9 @@ const getPersistedAuthSession = async () => {
|
|
|
2185
2296
|
const optionalUsedTokens = toOptionalNumber(userUsedTokens);
|
|
2186
2297
|
return {
|
|
2187
2298
|
authAccessToken: accessToken,
|
|
2299
|
+
...(typeof optionalAccessTokenExpiresAt === 'number' && {
|
|
2300
|
+
authAccessTokenExpiresAt: optionalAccessTokenExpiresAt
|
|
2301
|
+
}),
|
|
2188
2302
|
authErrorMessage: '',
|
|
2189
2303
|
userState: 'loggedIn',
|
|
2190
2304
|
...(optionalAuthClientId && {
|
|
@@ -2208,7 +2322,7 @@ const getPersistedAuthSession = async () => {
|
|
|
2208
2322
|
};
|
|
2209
2323
|
};
|
|
2210
2324
|
const persistAuthSession = async loginResult => {
|
|
2211
|
-
await Promise.all([setPersistentAuthValue(accessTokenKey, loginResult.authAccessToken ?? ''), setPersistentAuthValue(refreshTokenKey, loginResult.authRefreshToken ?? ''), loginResult.authClientId ? saveOidcClientId(loginResult.authClientId) : clearStoredOidcClientId(), setPersistentAuthValue(userNameKey, loginResult.userName ?? ''), setPersistentAuthValue(userSubscriptionPlanKey, loginResult.userSubscriptionPlan ?? ''), setPersistentAuthValue(userSubscriptionStatusKey, loginResult.userSubscriptionStatus ?? ''), setPersistentAuthValue(userUsedTokensKey, typeof loginResult.userUsedTokens === 'number' ? String(loginResult.userUsedTokens) : '')]);
|
|
2325
|
+
await Promise.all([setPersistentAuthValue(accessTokenKey, loginResult.authAccessToken ?? ''), setPersistentAuthValue(accessTokenExpiresAtKey, typeof loginResult.authAccessTokenExpiresAt === 'number' ? String(loginResult.authAccessTokenExpiresAt) : ''), setPersistentAuthValue(refreshTokenKey, loginResult.authRefreshToken ?? ''), loginResult.authClientId ? saveOidcClientId(loginResult.authClientId) : clearStoredOidcClientId(), setPersistentAuthValue(userNameKey, loginResult.userName ?? ''), setPersistentAuthValue(userSubscriptionPlanKey, loginResult.userSubscriptionPlan ?? ''), setPersistentAuthValue(userSubscriptionStatusKey, loginResult.userSubscriptionStatus ?? ''), setPersistentAuthValue(userUsedTokensKey, typeof loginResult.userUsedTokens === 'number' ? String(loginResult.userUsedTokens) : '')]);
|
|
2212
2326
|
};
|
|
2213
2327
|
|
|
2214
2328
|
const persistLoginResult = async loginResult => {
|
|
@@ -2220,37 +2334,15 @@ const persistLoginResult = async loginResult => {
|
|
|
2220
2334
|
return loginResult;
|
|
2221
2335
|
};
|
|
2222
2336
|
|
|
2223
|
-
const getAuthorizationServer = backendUrl => {
|
|
2224
|
-
return {
|
|
2225
|
-
issuer: getBackendAuthUrl(backendUrl, '/oidc'),
|
|
2226
|
-
jwks_uri: getBackendAuthUrl(backendUrl, '/oidc/jwks'),
|
|
2227
|
-
token_endpoint: getBackendOidcTokenUrl(backendUrl)
|
|
2228
|
-
};
|
|
2229
|
-
};
|
|
2230
|
-
const getClient = clientId => {
|
|
2231
|
-
return {
|
|
2232
|
-
client_id: clientId
|
|
2233
|
-
};
|
|
2234
|
-
};
|
|
2235
|
-
const refreshOidcTokens = async (backendUrl, clientId, refreshToken, requestTokenEndpoint = genericTokenEndpointRequest, processTokenEndpointResponse = processGenericTokenEndpointResponse) => {
|
|
2236
|
-
const authorizationServer = getAuthorizationServer(backendUrl);
|
|
2237
|
-
const client = getClient(clientId);
|
|
2238
|
-
const response = await requestTokenEndpoint(authorizationServer, client, None(), 'refresh_token', new URLSearchParams({
|
|
2239
|
-
refresh_token: refreshToken
|
|
2240
|
-
}));
|
|
2241
|
-
const tokenResponse = await processTokenEndpointResponse(authorizationServer, client, response);
|
|
2242
|
-
return {
|
|
2243
|
-
accessToken: tokenResponse.access_token,
|
|
2244
|
-
refreshToken: typeof tokenResponse.refresh_token === 'string' ? tokenResponse.refresh_token : refreshToken
|
|
2245
|
-
};
|
|
2246
|
-
};
|
|
2247
|
-
|
|
2248
2337
|
const clearStoredOidcAuth = async () => {
|
|
2249
2338
|
await clearPersistedAuthSession();
|
|
2250
2339
|
};
|
|
2251
|
-
const toLoginResult = (accessToken, refreshToken, clientId, userName) => {
|
|
2340
|
+
const toLoginResult = (accessToken, refreshToken, clientId, userName, authAccessTokenExpiresAt) => {
|
|
2252
2341
|
return {
|
|
2253
2342
|
authAccessToken: accessToken,
|
|
2343
|
+
...(authAccessTokenExpiresAt && {
|
|
2344
|
+
authAccessTokenExpiresAt
|
|
2345
|
+
}),
|
|
2254
2346
|
authClientId: clientId,
|
|
2255
2347
|
authErrorMessage: '',
|
|
2256
2348
|
authRefreshToken: refreshToken,
|
|
@@ -2266,7 +2358,8 @@ const restoreOidcAuth = async backendUrl => {
|
|
|
2266
2358
|
try {
|
|
2267
2359
|
const refreshedTokens = await refreshOidcTokens(backendUrl, clientId, refreshToken);
|
|
2268
2360
|
const userName = await getOidcUserName(backendUrl, refreshedTokens.accessToken);
|
|
2269
|
-
|
|
2361
|
+
const authAccessTokenExpiresAt = getAccessTokenExpiresAt(refreshedTokens.expiresIn);
|
|
2362
|
+
return toLoginResult(refreshedTokens.accessToken, refreshedTokens.refreshToken, clientId, userName, authAccessTokenExpiresAt);
|
|
2270
2363
|
} catch {
|
|
2271
2364
|
if (accessToken) {
|
|
2272
2365
|
const userName = await getOidcUserName(backendUrl, accessToken);
|
|
@@ -2287,6 +2380,7 @@ const getBackendUrl = options => {
|
|
|
2287
2380
|
};
|
|
2288
2381
|
const initialize = async options => {
|
|
2289
2382
|
const backendUrl = getBackendUrl(options);
|
|
2383
|
+
setAuthBackendUrl(backendUrl);
|
|
2290
2384
|
try {
|
|
2291
2385
|
if (backendUrl) {
|
|
2292
2386
|
const completedBrowserLogin = await completeBrowserOidcLogin(backendUrl);
|
|
@@ -2871,8 +2965,12 @@ const waitForElectronBackendLogin = async (backendUrl, uid, redirectUri, codeVer
|
|
|
2871
2965
|
const authorizationCode = await getAuthorizationCode(uid);
|
|
2872
2966
|
if (hasAuthorizationCode(authorizationCode)) {
|
|
2873
2967
|
const tokenResponse = await exchangeAuthorizationCode(backendUrl, authorizationCode, redirectUri, codeVerifier);
|
|
2968
|
+
const authAccessTokenExpiresAt = getAccessTokenExpiresAt(tokenResponse.expiresIn);
|
|
2874
2969
|
return {
|
|
2875
2970
|
authAccessToken: tokenResponse.accessToken,
|
|
2971
|
+
...(authAccessTokenExpiresAt && {
|
|
2972
|
+
authAccessTokenExpiresAt
|
|
2973
|
+
}),
|
|
2876
2974
|
authErrorMessage: '',
|
|
2877
2975
|
authRefreshToken: tokenResponse.refreshToken,
|
|
2878
2976
|
userState: tokenResponse.accessToken ? 'loggedIn' : 'loggedOut'
|