@lvce-editor/auth-worker 3.18.1 → 3.19.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.
@@ -1253,7 +1253,8 @@ const setAuthBackendUrl = backendUrl => {
1253
1253
  };
1254
1254
 
1255
1255
  const state$3 = {
1256
- platform: Web
1256
+ platform: Web,
1257
+ useNativeAuth: false
1257
1258
  };
1258
1259
  const getAuthPlatform = () => {
1259
1260
  return state$3.platform;
@@ -1262,6 +1263,12 @@ const setAuthPlatform = value => {
1262
1263
  state$3.platform = value;
1263
1264
  };
1264
1265
 
1266
+ // Native shells can use loopback OAuth without Electron's process or secret storage.
1267
+ const setUseNativeAuth = value => {
1268
+ state$3.useNativeAuth = value;
1269
+ };
1270
+ const getUseNativeAuth = () => state$3.useNativeAuth;
1271
+
1265
1272
  let USER_AGENT;
1266
1273
  if (typeof navigator === 'undefined' || !navigator.userAgent?.startsWith?.('Mozilla/5.0 ')) {
1267
1274
  const NAME = 'oauth4webapi';
@@ -3118,7 +3125,16 @@ const getElectronAuthorizationCode = async uid => {
3118
3125
  const waitForElectronBackendLogin = async (backendUrl, uid, redirectUri, codeVerifier, timeoutMs = 30_000, pollIntervalMs = 1000, getAuthorizationCode = getElectronAuthorizationCode, exchangeAuthorizationCode = exchangeElectronAuthorizationCode, getUserName = getOidcUserName) => {
3119
3126
  const deadline = Date.now() + timeoutMs;
3120
3127
  while (Date.now() < deadline) {
3121
- const authorizationCode = await getAuthorizationCode(uid);
3128
+ let timer;
3129
+ const timeout = new Promise(resolve => {
3130
+ timer = setTimeout(resolve, Math.max(0, deadline - Date.now()));
3131
+ });
3132
+ let authorizationCode;
3133
+ try {
3134
+ authorizationCode = await Promise.race([getAuthorizationCode(uid), timeout]);
3135
+ } finally {
3136
+ clearTimeout(timer);
3137
+ }
3122
3138
  if (hasAuthorizationCode(authorizationCode)) {
3123
3139
  const tokenResponse = await exchangeAuthorizationCode(backendUrl, authorizationCode, redirectUri, codeVerifier);
3124
3140
  const authAccessTokenExpiresAt = getAccessTokenExpiresAt(tokenResponse.expiresIn);
@@ -3159,37 +3175,45 @@ const getMockLoginResult = async (signingInState, persistResult) => {
3159
3175
  return persistResult(getLoggedInState(signingInState, response));
3160
3176
  };
3161
3177
  const getInteractiveLoginResult = async (backendUrl, platform, signingInState, persistResult, selectAccount) => {
3162
- const authUseRedirect = getAuthUseRedirect(platform);
3163
- const uid = 0;
3164
- const {
3165
- clientId,
3166
- codeVerifier,
3167
- loginUrl,
3168
- redirectUri,
3169
- state
3170
- } = await getBackendLoginRequest(backendUrl, platform, uid);
3171
- if (platform !== Electron) {
3172
- const returnUrl = await getCurrentHref();
3173
- await savePendingOidcAuthState({
3178
+ const nativeAuth = getUseNativeAuth();
3179
+ const transportPlatform = nativeAuth ? Electron : platform;
3180
+ const authUseRedirect = getAuthUseRedirect(transportPlatform);
3181
+ const uid = nativeAuth ? globalThis.crypto.randomUUID() : 0;
3182
+ try {
3183
+ const {
3174
3184
  clientId,
3175
3185
  codeVerifier,
3186
+ loginUrl,
3176
3187
  redirectUri,
3177
- returnUrl,
3178
3188
  state
3189
+ } = await getBackendLoginRequest(backendUrl, transportPlatform, uid);
3190
+ if (transportPlatform !== Electron) {
3191
+ const returnUrl = await getCurrentHref();
3192
+ await savePendingOidcAuthState({
3193
+ clientId,
3194
+ codeVerifier,
3195
+ redirectUri,
3196
+ returnUrl,
3197
+ state
3198
+ });
3199
+ }
3200
+ await invoke$1('Open.openUrl', selectAccount ? getAccountLoginUrl(backendUrl, loginUrl) : loginUrl, platform, authUseRedirect);
3201
+ if (transportPlatform !== Electron && authUseRedirect) {
3202
+ return signingInState;
3203
+ }
3204
+ const authState = transportPlatform === Electron ? await waitForElectronBackendLogin(backendUrl, uid, redirectUri, codeVerifier) : await waitForBackendLogin(backendUrl);
3205
+ if (transportPlatform !== Electron) {
3206
+ await clearPendingOidcAuthState();
3207
+ }
3208
+ return persistResult({
3209
+ ...authState,
3210
+ authClientId: clientId
3179
3211
  });
3212
+ } finally {
3213
+ if (nativeAuth) {
3214
+ await invoke$3('OAuthServer.dispose', String(uid));
3215
+ }
3180
3216
  }
3181
- await invoke$1('Open.openUrl', selectAccount ? getAccountLoginUrl(backendUrl, loginUrl) : loginUrl, platform, authUseRedirect);
3182
- if (platform !== Electron && authUseRedirect) {
3183
- return signingInState;
3184
- }
3185
- const authState = platform === Electron ? await waitForElectronBackendLogin(backendUrl, uid, redirectUri, codeVerifier) : await waitForBackendLogin(backendUrl);
3186
- if (platform !== Electron) {
3187
- await clearPendingOidcAuthState();
3188
- }
3189
- return persistResult({
3190
- ...authState,
3191
- authClientId: clientId
3192
- });
3193
3217
  };
3194
3218
  const handleClickLogin = async (options, persistResult = persistLoginResult) => {
3195
3219
  const {
@@ -3212,7 +3236,7 @@ const handleClickLogin = async (options, persistResult = persistLoginResult) =>
3212
3236
  if (mockLoginResult) {
3213
3237
  return mockLoginResult;
3214
3238
  }
3215
- return getInteractiveLoginResult(backendUrl, platform, signingInState, persistResult, options.selectAccount === true);
3239
+ return await getInteractiveLoginResult(backendUrl, platform, signingInState, persistResult, options.selectAccount === true);
3216
3240
  } catch (error) {
3217
3241
  await clearPendingOidcAuthState();
3218
3242
  const errorMessage = error instanceof Error && error.message ? error.message : 'Backend authentication failed.';
@@ -3524,8 +3548,11 @@ const initializeMainProcess = async () => {
3524
3548
  const configure = async options => {
3525
3549
  setAuthBackendUrl(options.backendUrl);
3526
3550
  setAuthPlatform(options.platform);
3551
+ setUseNativeAuth(options.useNativeAuth === true);
3527
3552
  if (options.platform === Electron) {
3528
3553
  await Promise.all([initializeAuthProcess(), initializeMainProcess()]);
3554
+ } else if (options.useNativeAuth) {
3555
+ await initializeAuthProcess();
3529
3556
  }
3530
3557
  };
3531
3558
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/auth-worker",
3
- "version": "3.18.1",
3
+ "version": "3.19.0",
4
4
  "description": "Auth Worker",
5
5
  "repository": {
6
6
  "type": "git",