@lvce-editor/auth-worker 3.4.1 → 3.5.1

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.
@@ -1060,6 +1060,31 @@ const initializeOpenerWorker = async () => {
1060
1060
  set(rpc);
1061
1061
  };
1062
1062
 
1063
+ const millisecondsPerSecond = 1000;
1064
+ const getAccessTokenExpiresAt = (expiresIn, now = Date.now()) => {
1065
+ if (typeof expiresIn !== 'number' || !Number.isFinite(expiresIn) || expiresIn <= 0) {
1066
+ return undefined;
1067
+ }
1068
+ return now + expiresIn * millisecondsPerSecond;
1069
+ };
1070
+ const isAccessTokenValid = (accessToken, expiresAt, now = Date.now()) => {
1071
+ if (!accessToken || !expiresAt) {
1072
+ return false;
1073
+ }
1074
+ const expiresAtNumber = Number(expiresAt);
1075
+ return Number.isFinite(expiresAtNumber) && expiresAtNumber > now;
1076
+ };
1077
+
1078
+ const state$2 = {
1079
+ backendUrl: ''
1080
+ };
1081
+ const getAuthBackendUrl = () => {
1082
+ return state$2.backendUrl;
1083
+ };
1084
+ const setAuthBackendUrl = backendUrl => {
1085
+ state$2.backendUrl = backendUrl;
1086
+ };
1087
+
1063
1088
  const databaseName = 'auth-worker';
1064
1089
  const objectStoreName = 'auth';
1065
1090
  const memoryStorage = new Map();
@@ -1150,19 +1175,44 @@ const clearPersistentAuthValue = async key => {
1150
1175
  await transactionToPromise(transaction);
1151
1176
  };
1152
1177
 
1153
- const getAccessToken = () => {
1154
- return getPersistentAuthValue('accessToken');
1178
+ const callbackUrlKey = 'oidcCallbackUrl';
1179
+ const clientIdKey = 'oidcClientId';
1180
+ const pendingClientIdKey = 'pendingOidcClientId';
1181
+ const pendingCodeVerifierKey = 'pendingOidcCodeVerifier';
1182
+ const pendingRedirectUriKey = 'pendingOidcRedirectUri';
1183
+ const pendingStateKey = 'pendingOidcState';
1184
+ const clearOidcCallbackUrl = async () => {
1185
+ await clearPersistentAuthValue(callbackUrlKey);
1155
1186
  };
1156
-
1157
- const handleMessagePort = async (port, rpcId) => {
1158
- const rpc = await create$4({
1159
- commandMap: commandMap,
1160
- isMessagePortOpen: true,
1161
- messagePort: port
1162
- });
1163
- if (rpcId) {
1164
- set$2(rpcId, rpc);
1187
+ const clearStoredOidcClientId = async () => {
1188
+ await clearPersistentAuthValue(clientIdKey);
1189
+ };
1190
+ const clearPendingOidcAuthState = async () => {
1191
+ await Promise.all([clearPersistentAuthValue(pendingClientIdKey), clearPersistentAuthValue(pendingCodeVerifierKey), clearPersistentAuthValue(pendingRedirectUriKey), clearPersistentAuthValue(pendingStateKey)]);
1192
+ };
1193
+ const getOidcCallbackUrl = async () => {
1194
+ return getPersistentAuthValue(callbackUrlKey);
1195
+ };
1196
+ const getStoredOidcClientId = async () => {
1197
+ return getPersistentAuthValue(clientIdKey);
1198
+ };
1199
+ const loadPendingOidcAuthState = async () => {
1200
+ const [clientId, codeVerifier, redirectUri, state] = await Promise.all([getPersistentAuthValue(pendingClientIdKey), getPersistentAuthValue(pendingCodeVerifierKey), getPersistentAuthValue(pendingRedirectUriKey), getPersistentAuthValue(pendingStateKey)]);
1201
+ if (!clientId || !codeVerifier || !redirectUri || !state) {
1202
+ return undefined;
1165
1203
  }
1204
+ return {
1205
+ clientId,
1206
+ codeVerifier,
1207
+ redirectUri,
1208
+ state
1209
+ };
1210
+ };
1211
+ const saveOidcClientId = async clientId => {
1212
+ await setPersistentAuthValue(clientIdKey, clientId);
1213
+ };
1214
+ const savePendingOidcAuthState = async value => {
1215
+ await Promise.all([setPersistentAuthValue(pendingClientIdKey, value.clientId), setPersistentAuthValue(pendingCodeVerifierKey, value.codeVerifier), setPersistentAuthValue(pendingRedirectUriKey, value.redirectUri), setPersistentAuthValue(pendingStateKey, value.state)]);
1166
1216
  };
1167
1217
 
1168
1218
  let USER_AGENT;
@@ -1986,9 +2036,62 @@ const getClient$1 = clientId => {
1986
2036
  client_id: clientId
1987
2037
  };
1988
2038
  };
1989
- const exchangeAuthorizationCode = async (backendUrl, clientId, code, redirectUri, codeVerifier, requestTokenEndpoint = genericTokenEndpointRequest, processTokenEndpointResponse = processGenericTokenEndpointResponse) => {
2039
+ const refreshOidcTokens = async (backendUrl, clientId, refreshToken, requestTokenEndpoint = genericTokenEndpointRequest, processTokenEndpointResponse = processGenericTokenEndpointResponse) => {
1990
2040
  const authorizationServer = getAuthorizationServer$1(backendUrl);
1991
2041
  const client = getClient$1(clientId);
2042
+ const response = await requestTokenEndpoint(authorizationServer, client, None(), 'refresh_token', new URLSearchParams({
2043
+ refresh_token: refreshToken
2044
+ }));
2045
+ const tokenResponse = await processTokenEndpointResponse(authorizationServer, client, response);
2046
+ return {
2047
+ accessToken: tokenResponse.access_token,
2048
+ expiresIn: typeof tokenResponse.expires_in === 'number' ? tokenResponse.expires_in : undefined,
2049
+ refreshToken: typeof tokenResponse.refresh_token === 'string' ? tokenResponse.refresh_token : refreshToken
2050
+ };
2051
+ };
2052
+
2053
+ const getAccessToken = async (options = {}, refreshTokens = refreshOidcTokens, now = Date.now()) => {
2054
+ const [accessToken, accessTokenExpiresAt] = await Promise.all([getPersistentAuthValue('accessToken'), getPersistentAuthValue('accessTokenExpiresAt')]);
2055
+ if (options.refresh !== 'if-needed' || isAccessTokenValid(accessToken, accessTokenExpiresAt, now)) {
2056
+ return accessToken;
2057
+ }
2058
+ const [clientId, refreshToken] = await Promise.all([getStoredOidcClientId(), getPersistentAuthValue('refreshToken')]);
2059
+ const backendUrl = getAuthBackendUrl();
2060
+ if (!backendUrl || !clientId || !refreshToken) {
2061
+ return accessToken;
2062
+ }
2063
+ const refreshedTokens = await refreshTokens(backendUrl, clientId, refreshToken);
2064
+ const expiresAt = getAccessTokenExpiresAt(refreshedTokens.expiresIn, now);
2065
+ await Promise.all([setPersistentAuthValue('accessToken', refreshedTokens.accessToken), setPersistentAuthValue('accessTokenExpiresAt', typeof expiresAt === 'number' ? String(expiresAt) : ''), setPersistentAuthValue('refreshToken', refreshedTokens.refreshToken)]);
2066
+ return refreshedTokens.accessToken;
2067
+ };
2068
+
2069
+ const handleMessagePort = async (port, rpcId) => {
2070
+ const rpc = await create$4({
2071
+ commandMap: commandMap,
2072
+ isMessagePortOpen: true,
2073
+ messagePort: port
2074
+ });
2075
+ if (rpcId) {
2076
+ set$2(rpcId, rpc);
2077
+ }
2078
+ };
2079
+
2080
+ const getAuthorizationServer = backendUrl => {
2081
+ return {
2082
+ issuer: getBackendAuthUrl(backendUrl, '/oidc'),
2083
+ jwks_uri: getBackendAuthUrl(backendUrl, '/oidc/jwks'),
2084
+ token_endpoint: getBackendOidcTokenUrl(backendUrl)
2085
+ };
2086
+ };
2087
+ const getClient = clientId => {
2088
+ return {
2089
+ client_id: clientId
2090
+ };
2091
+ };
2092
+ const exchangeAuthorizationCode = async (backendUrl, clientId, code, redirectUri, codeVerifier, requestTokenEndpoint = genericTokenEndpointRequest, processTokenEndpointResponse = processGenericTokenEndpointResponse) => {
2093
+ const authorizationServer = getAuthorizationServer(backendUrl);
2094
+ const client = getClient(clientId);
1992
2095
  const response = await requestTokenEndpoint(authorizationServer, client, None(), 'authorization_code', new URLSearchParams({
1993
2096
  code,
1994
2097
  code_verifier: codeVerifier,
@@ -1997,6 +2100,7 @@ const exchangeAuthorizationCode = async (backendUrl, clientId, code, redirectUri
1997
2100
  const tokenResponse = await processTokenEndpointResponse(authorizationServer, client, response);
1998
2101
  return {
1999
2102
  accessToken: tokenResponse.access_token,
2103
+ expiresIn: typeof tokenResponse.expires_in === 'number' ? tokenResponse.expires_in : undefined,
2000
2104
  refreshToken: typeof tokenResponse.refresh_token === 'string' ? tokenResponse.refresh_token : ''
2001
2105
  };
2002
2106
  };
@@ -2051,46 +2155,6 @@ const getOidcUserName = async (backendUrl, accessToken, fetchFn = fetch) => {
2051
2155
  return typeof displayName === 'string' ? displayName : '';
2052
2156
  };
2053
2157
 
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
2158
  const normalizeRedirectUri = value => {
2095
2159
  const url = new URL(value);
2096
2160
  url.hash = '';
@@ -2142,9 +2206,13 @@ const completeBrowserOidcLogin = async backendUrl => {
2142
2206
  }
2143
2207
  const exchanged = await exchangeAuthorizationCode(backendUrl, pendingAuthState.clientId, code, pendingAuthState.redirectUri, pendingAuthState.codeVerifier);
2144
2208
  const userName = await getOidcUserName(backendUrl, exchanged.accessToken);
2209
+ const authAccessTokenExpiresAt = getAccessTokenExpiresAt(exchanged.expiresIn);
2145
2210
  await clearPendingOidcAuthState();
2146
2211
  return {
2147
2212
  authAccessToken: exchanged.accessToken,
2213
+ ...(authAccessTokenExpiresAt && {
2214
+ authAccessTokenExpiresAt
2215
+ }),
2148
2216
  authClientId: pendingAuthState.clientId,
2149
2217
  authErrorMessage: '',
2150
2218
  authRefreshToken: exchanged.refreshToken,
@@ -2154,6 +2222,7 @@ const completeBrowserOidcLogin = async backendUrl => {
2154
2222
  };
2155
2223
 
2156
2224
  const accessTokenKey = 'accessToken';
2225
+ const accessTokenExpiresAtKey = 'accessTokenExpiresAt';
2157
2226
  const refreshTokenKey = 'refreshToken';
2158
2227
  const userNameKey = 'userName';
2159
2228
  const userSubscriptionPlanKey = 'userSubscriptionPlan';
@@ -2170,14 +2239,15 @@ const toOptionalNumber = value => {
2170
2239
  return Number.isFinite(numberValue) ? numberValue : undefined;
2171
2240
  };
2172
2241
  const clearPersistedAuthSession = async () => {
2173
- await Promise.all([clearPersistentAuthValue(accessTokenKey), clearPersistentAuthValue(refreshTokenKey), clearStoredOidcClientId(), clearPersistentAuthValue(userNameKey), clearPersistentAuthValue(userSubscriptionPlanKey), clearPersistentAuthValue(userSubscriptionStatusKey), clearPersistentAuthValue(userUsedTokensKey)]);
2242
+ await Promise.all([clearPersistentAuthValue(accessTokenKey), clearPersistentAuthValue(accessTokenExpiresAtKey), clearPersistentAuthValue(refreshTokenKey), clearStoredOidcClientId(), clearPersistentAuthValue(userNameKey), clearPersistentAuthValue(userSubscriptionPlanKey), clearPersistentAuthValue(userSubscriptionStatusKey), clearPersistentAuthValue(userUsedTokensKey)]);
2174
2243
  };
2175
2244
  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)]);
2245
+ 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
2246
  if (!accessToken && !refreshToken) {
2178
2247
  return undefined;
2179
2248
  }
2180
2249
  const optionalAuthClientId = toOptionalString(authClientId);
2250
+ const optionalAccessTokenExpiresAt = toOptionalNumber(accessTokenExpiresAt);
2181
2251
  const optionalRefreshToken = toOptionalString(refreshToken);
2182
2252
  const optionalUserName = toOptionalString(userName);
2183
2253
  const optionalSubscriptionPlan = toOptionalString(userSubscriptionPlan);
@@ -2185,6 +2255,9 @@ const getPersistedAuthSession = async () => {
2185
2255
  const optionalUsedTokens = toOptionalNumber(userUsedTokens);
2186
2256
  return {
2187
2257
  authAccessToken: accessToken,
2258
+ ...(typeof optionalAccessTokenExpiresAt === 'number' && {
2259
+ authAccessTokenExpiresAt: optionalAccessTokenExpiresAt
2260
+ }),
2188
2261
  authErrorMessage: '',
2189
2262
  userState: 'loggedIn',
2190
2263
  ...(optionalAuthClientId && {
@@ -2208,7 +2281,7 @@ const getPersistedAuthSession = async () => {
2208
2281
  };
2209
2282
  };
2210
2283
  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) : '')]);
2284
+ 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
2285
  };
2213
2286
 
2214
2287
  const persistLoginResult = async loginResult => {
@@ -2220,37 +2293,15 @@ const persistLoginResult = async loginResult => {
2220
2293
  return loginResult;
2221
2294
  };
2222
2295
 
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
2296
  const clearStoredOidcAuth = async () => {
2249
2297
  await clearPersistedAuthSession();
2250
2298
  };
2251
- const toLoginResult = (accessToken, refreshToken, clientId, userName) => {
2299
+ const toLoginResult = (accessToken, refreshToken, clientId, userName, authAccessTokenExpiresAt) => {
2252
2300
  return {
2253
2301
  authAccessToken: accessToken,
2302
+ ...(authAccessTokenExpiresAt && {
2303
+ authAccessTokenExpiresAt
2304
+ }),
2254
2305
  authClientId: clientId,
2255
2306
  authErrorMessage: '',
2256
2307
  authRefreshToken: refreshToken,
@@ -2266,7 +2317,8 @@ const restoreOidcAuth = async backendUrl => {
2266
2317
  try {
2267
2318
  const refreshedTokens = await refreshOidcTokens(backendUrl, clientId, refreshToken);
2268
2319
  const userName = await getOidcUserName(backendUrl, refreshedTokens.accessToken);
2269
- return toLoginResult(refreshedTokens.accessToken, refreshedTokens.refreshToken, clientId, userName);
2320
+ const authAccessTokenExpiresAt = getAccessTokenExpiresAt(refreshedTokens.expiresIn);
2321
+ return toLoginResult(refreshedTokens.accessToken, refreshedTokens.refreshToken, clientId, userName, authAccessTokenExpiresAt);
2270
2322
  } catch {
2271
2323
  if (accessToken) {
2272
2324
  const userName = await getOidcUserName(backendUrl, accessToken);
@@ -2287,6 +2339,7 @@ const getBackendUrl = options => {
2287
2339
  };
2288
2340
  const initialize = async options => {
2289
2341
  const backendUrl = getBackendUrl(options);
2342
+ setAuthBackendUrl(backendUrl);
2290
2343
  try {
2291
2344
  if (backendUrl) {
2292
2345
  const completedBrowserLogin = await completeBrowserOidcLogin(backendUrl);
@@ -2871,8 +2924,12 @@ const waitForElectronBackendLogin = async (backendUrl, uid, redirectUri, codeVer
2871
2924
  const authorizationCode = await getAuthorizationCode(uid);
2872
2925
  if (hasAuthorizationCode(authorizationCode)) {
2873
2926
  const tokenResponse = await exchangeAuthorizationCode(backendUrl, authorizationCode, redirectUri, codeVerifier);
2927
+ const authAccessTokenExpiresAt = getAccessTokenExpiresAt(tokenResponse.expiresIn);
2874
2928
  return {
2875
2929
  authAccessToken: tokenResponse.accessToken,
2930
+ ...(authAccessTokenExpiresAt && {
2931
+ authAccessTokenExpiresAt
2932
+ }),
2876
2933
  authErrorMessage: '',
2877
2934
  authRefreshToken: tokenResponse.refreshToken,
2878
2935
  userState: tokenResponse.accessToken ? 'loggedIn' : 'loggedOut'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/auth-worker",
3
- "version": "3.4.1",
3
+ "version": "3.5.1",
4
4
  "description": "Auth Worker",
5
5
  "repository": {
6
6
  "type": "git",