@lvce-editor/auth-worker 1.18.0 → 3.0.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 +144 -53
- package/package.json +1 -1
package/dist/authWorkerMain.js
CHANGED
|
@@ -1071,48 +1071,6 @@ const handleMessagePort = async (port, rpcId) => {
|
|
|
1071
1071
|
}
|
|
1072
1072
|
};
|
|
1073
1073
|
|
|
1074
|
-
const initialize = async platform => {
|
|
1075
|
-
// TODO
|
|
1076
|
-
};
|
|
1077
|
-
|
|
1078
|
-
const trailingSlashesRegex = /\/+$/;
|
|
1079
|
-
const trimTrailingSlashes = value => {
|
|
1080
|
-
return value.replace(trailingSlashesRegex, '');
|
|
1081
|
-
};
|
|
1082
|
-
|
|
1083
|
-
const getBackendAuthUrl = (backendUrl, path) => {
|
|
1084
|
-
return `${trimTrailingSlashes(backendUrl)}${path}`;
|
|
1085
|
-
};
|
|
1086
|
-
|
|
1087
|
-
const getLoggedOutBackendAuthState = (authErrorMessage = '') => {
|
|
1088
|
-
return {
|
|
1089
|
-
authAccessToken: '',
|
|
1090
|
-
authErrorMessage,
|
|
1091
|
-
userState: 'loggedOut'
|
|
1092
|
-
};
|
|
1093
|
-
};
|
|
1094
|
-
|
|
1095
|
-
const getBackendLogoutUrl = backendUrl => {
|
|
1096
|
-
return getBackendAuthUrl(backendUrl, '/auth/logout');
|
|
1097
|
-
};
|
|
1098
|
-
|
|
1099
|
-
const logoutFromBackend = async backendUrl => {
|
|
1100
|
-
if (!backendUrl) {
|
|
1101
|
-
return;
|
|
1102
|
-
}
|
|
1103
|
-
try {
|
|
1104
|
-
await fetch(getBackendLogoutUrl(backendUrl), {
|
|
1105
|
-
credentials: 'include',
|
|
1106
|
-
headers: {
|
|
1107
|
-
Accept: 'application/json'
|
|
1108
|
-
},
|
|
1109
|
-
method: 'POST'
|
|
1110
|
-
});
|
|
1111
|
-
} catch {
|
|
1112
|
-
// Ignore logout failures and still clear local auth state.
|
|
1113
|
-
}
|
|
1114
|
-
};
|
|
1115
|
-
|
|
1116
1074
|
let USER_AGENT;
|
|
1117
1075
|
if (typeof navigator === 'undefined' || !navigator.userAgent?.startsWith?.('Mozilla/5.0 ')) {
|
|
1118
1076
|
const NAME = 'oauth4webapi';
|
|
@@ -1906,6 +1864,15 @@ async function getResponseJsonBody(response, check = assertApplicationJson) {
|
|
|
1906
1864
|
}
|
|
1907
1865
|
const _expectedIssuer = Symbol();
|
|
1908
1866
|
|
|
1867
|
+
const trailingSlashesRegex = /\/+$/;
|
|
1868
|
+
const trimTrailingSlashes = value => {
|
|
1869
|
+
return value.replace(trailingSlashesRegex, '');
|
|
1870
|
+
};
|
|
1871
|
+
|
|
1872
|
+
const getBackendAuthUrl = (backendUrl, path) => {
|
|
1873
|
+
return `${trimTrailingSlashes(backendUrl)}${path}`;
|
|
1874
|
+
};
|
|
1875
|
+
|
|
1909
1876
|
const getBackendOidcTokenUrl = backendUrl => {
|
|
1910
1877
|
return getBackendAuthUrl(backendUrl, '/oidc/token');
|
|
1911
1878
|
};
|
|
@@ -1949,6 +1916,14 @@ const getCurrentHref = async () => {
|
|
|
1949
1916
|
return globalThis.location.href;
|
|
1950
1917
|
};
|
|
1951
1918
|
|
|
1919
|
+
const getLoggedOutBackendAuthState = (authErrorMessage = '') => {
|
|
1920
|
+
return {
|
|
1921
|
+
authAccessToken: '',
|
|
1922
|
+
authErrorMessage,
|
|
1923
|
+
userState: 'loggedOut'
|
|
1924
|
+
};
|
|
1925
|
+
};
|
|
1926
|
+
|
|
1952
1927
|
const getPayload$1 = async response => {
|
|
1953
1928
|
try {
|
|
1954
1929
|
return await response.json();
|
|
@@ -2167,6 +2142,120 @@ const completeBrowserOidcLogin = async backendUrl => {
|
|
|
2167
2142
|
};
|
|
2168
2143
|
};
|
|
2169
2144
|
|
|
2145
|
+
const accessTokenKey = 'accessToken';
|
|
2146
|
+
const refreshTokenKey = 'refreshToken';
|
|
2147
|
+
const userNameKey = 'userName';
|
|
2148
|
+
const userSubscriptionPlanKey = 'userSubscriptionPlan';
|
|
2149
|
+
const userSubscriptionStatusKey = 'userSubscriptionStatus';
|
|
2150
|
+
const userUsedTokensKey = 'userUsedTokens';
|
|
2151
|
+
const toOptionalString = value => {
|
|
2152
|
+
return value || undefined;
|
|
2153
|
+
};
|
|
2154
|
+
const toOptionalNumber = value => {
|
|
2155
|
+
if (!value) {
|
|
2156
|
+
return undefined;
|
|
2157
|
+
}
|
|
2158
|
+
const numberValue = Number(value);
|
|
2159
|
+
return Number.isFinite(numberValue) ? numberValue : undefined;
|
|
2160
|
+
};
|
|
2161
|
+
const clearPersistedAuthSession = async () => {
|
|
2162
|
+
await Promise.all([clearPersistentAuthValue(accessTokenKey), clearPersistentAuthValue(refreshTokenKey), clearStoredOidcClientId(), clearPersistentAuthValue(userNameKey), clearPersistentAuthValue(userSubscriptionPlanKey), clearPersistentAuthValue(userSubscriptionStatusKey), clearPersistentAuthValue(userUsedTokensKey)]);
|
|
2163
|
+
};
|
|
2164
|
+
const getPersistedAuthSession = async () => {
|
|
2165
|
+
const [accessToken, refreshToken, authClientId, userName, userSubscriptionPlan, userSubscriptionStatus, userUsedTokens] = await Promise.all([getPersistentAuthValue(accessTokenKey), getPersistentAuthValue(refreshTokenKey), getStoredOidcClientId(), getPersistentAuthValue(userNameKey), getPersistentAuthValue(userSubscriptionPlanKey), getPersistentAuthValue(userSubscriptionStatusKey), getPersistentAuthValue(userUsedTokensKey)]);
|
|
2166
|
+
if (!accessToken && !refreshToken) {
|
|
2167
|
+
return undefined;
|
|
2168
|
+
}
|
|
2169
|
+
const optionalAuthClientId = toOptionalString(authClientId);
|
|
2170
|
+
const optionalRefreshToken = toOptionalString(refreshToken);
|
|
2171
|
+
const optionalUserName = toOptionalString(userName);
|
|
2172
|
+
const optionalSubscriptionPlan = toOptionalString(userSubscriptionPlan);
|
|
2173
|
+
const optionalSubscriptionStatus = toOptionalString(userSubscriptionStatus);
|
|
2174
|
+
const optionalUsedTokens = toOptionalNumber(userUsedTokens);
|
|
2175
|
+
return {
|
|
2176
|
+
authAccessToken: accessToken,
|
|
2177
|
+
authErrorMessage: '',
|
|
2178
|
+
userState: 'loggedIn',
|
|
2179
|
+
...(optionalAuthClientId ? {
|
|
2180
|
+
authClientId
|
|
2181
|
+
} : {}),
|
|
2182
|
+
...(optionalRefreshToken ? {
|
|
2183
|
+
authRefreshToken: refreshToken
|
|
2184
|
+
} : {}),
|
|
2185
|
+
...(optionalUserName ? {
|
|
2186
|
+
userName
|
|
2187
|
+
} : {}),
|
|
2188
|
+
...(optionalSubscriptionPlan ? {
|
|
2189
|
+
userSubscriptionPlan
|
|
2190
|
+
} : {}),
|
|
2191
|
+
...(optionalSubscriptionStatus ? {
|
|
2192
|
+
userSubscriptionStatus
|
|
2193
|
+
} : {}),
|
|
2194
|
+
...(typeof optionalUsedTokens === 'number' ? {
|
|
2195
|
+
userUsedTokens: optionalUsedTokens
|
|
2196
|
+
} : {})
|
|
2197
|
+
};
|
|
2198
|
+
};
|
|
2199
|
+
const persistAuthSession = async loginResult => {
|
|
2200
|
+
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) : '')]);
|
|
2201
|
+
};
|
|
2202
|
+
|
|
2203
|
+
const persistLoginResult = async loginResult => {
|
|
2204
|
+
if (loginResult.userState !== 'loggedIn') {
|
|
2205
|
+
await clearPersistedAuthSession();
|
|
2206
|
+
return loginResult;
|
|
2207
|
+
}
|
|
2208
|
+
await persistAuthSession(loginResult);
|
|
2209
|
+
return loginResult;
|
|
2210
|
+
};
|
|
2211
|
+
|
|
2212
|
+
const getBackendUrl = options => {
|
|
2213
|
+
if (typeof options === 'number') {
|
|
2214
|
+
return '';
|
|
2215
|
+
}
|
|
2216
|
+
return options.backendUrl || '';
|
|
2217
|
+
};
|
|
2218
|
+
const initialize = async options => {
|
|
2219
|
+
const backendUrl = getBackendUrl(options);
|
|
2220
|
+
try {
|
|
2221
|
+
if (backendUrl) {
|
|
2222
|
+
const completedBrowserLogin = await completeBrowserOidcLogin(backendUrl);
|
|
2223
|
+
if (completedBrowserLogin) {
|
|
2224
|
+
return persistLoginResult(completedBrowserLogin);
|
|
2225
|
+
}
|
|
2226
|
+
}
|
|
2227
|
+
const persistedAuthSession = await getPersistedAuthSession();
|
|
2228
|
+
if (persistedAuthSession) {
|
|
2229
|
+
return persistedAuthSession;
|
|
2230
|
+
}
|
|
2231
|
+
return getLoggedOutBackendAuthState();
|
|
2232
|
+
} catch (error) {
|
|
2233
|
+
const authErrorMessage = error instanceof Error && error.message ? error.message : 'Backend authentication failed.';
|
|
2234
|
+
return getLoggedOutBackendAuthState(authErrorMessage);
|
|
2235
|
+
}
|
|
2236
|
+
};
|
|
2237
|
+
|
|
2238
|
+
const getBackendLogoutUrl = backendUrl => {
|
|
2239
|
+
return getBackendAuthUrl(backendUrl, '/auth/logout');
|
|
2240
|
+
};
|
|
2241
|
+
|
|
2242
|
+
const logoutFromBackend = async backendUrl => {
|
|
2243
|
+
if (!backendUrl) {
|
|
2244
|
+
return;
|
|
2245
|
+
}
|
|
2246
|
+
try {
|
|
2247
|
+
await fetch(getBackendLogoutUrl(backendUrl), {
|
|
2248
|
+
credentials: 'include',
|
|
2249
|
+
headers: {
|
|
2250
|
+
Accept: 'application/json'
|
|
2251
|
+
},
|
|
2252
|
+
method: 'POST'
|
|
2253
|
+
});
|
|
2254
|
+
} catch {
|
|
2255
|
+
// Ignore logout failures and still clear local auth state.
|
|
2256
|
+
}
|
|
2257
|
+
};
|
|
2258
|
+
|
|
2170
2259
|
const getBackendRefreshUrl = backendUrl => {
|
|
2171
2260
|
return getBackendAuthUrl(backendUrl, '/auth/refresh');
|
|
2172
2261
|
};
|
|
@@ -2264,14 +2353,6 @@ const parseBackendAuthResponse = value => {
|
|
|
2264
2353
|
return toBackendAuthState(value);
|
|
2265
2354
|
};
|
|
2266
2355
|
|
|
2267
|
-
const persistLoginResult = async loginResult => {
|
|
2268
|
-
if (loginResult.userState !== 'loggedIn') {
|
|
2269
|
-
return loginResult;
|
|
2270
|
-
}
|
|
2271
|
-
await Promise.all([setPersistentAuthValue('accessToken', loginResult.authAccessToken ?? ''), setPersistentAuthValue('refreshToken', loginResult.authRefreshToken ?? ''), loginResult.authClientId ? saveOidcClientId(loginResult.authClientId) : Promise.resolve()]);
|
|
2272
|
-
return loginResult;
|
|
2273
|
-
};
|
|
2274
|
-
|
|
2275
2356
|
const getAuthorizationServer = backendUrl => {
|
|
2276
2357
|
return {
|
|
2277
2358
|
issuer: getBackendAuthUrl(backendUrl, '/oidc'),
|
|
@@ -2298,7 +2379,7 @@ const refreshOidcTokens = async (backendUrl, clientId, refreshToken, requestToke
|
|
|
2298
2379
|
};
|
|
2299
2380
|
|
|
2300
2381
|
const clearStoredOidcAuth = async () => {
|
|
2301
|
-
await
|
|
2382
|
+
await clearPersistedAuthSession();
|
|
2302
2383
|
};
|
|
2303
2384
|
const toLoginResult = (accessToken, refreshToken, clientId, userName) => {
|
|
2304
2385
|
return {
|
|
@@ -2662,6 +2743,16 @@ const getElectronRedirectUri = async uid => {
|
|
|
2662
2743
|
return `http://localhost:${localOauthServerPort}/callback`;
|
|
2663
2744
|
};
|
|
2664
2745
|
|
|
2746
|
+
const getGithubPagesBasePath = url => {
|
|
2747
|
+
if (!url.hostname.endsWith('.github.io')) {
|
|
2748
|
+
return '';
|
|
2749
|
+
}
|
|
2750
|
+
const firstSegment = url.pathname.split('/').find(Boolean);
|
|
2751
|
+
if (!firstSegment) {
|
|
2752
|
+
return '';
|
|
2753
|
+
}
|
|
2754
|
+
return `/${firstSegment}`;
|
|
2755
|
+
};
|
|
2665
2756
|
const getWebRedirectUri = async () => {
|
|
2666
2757
|
const href = await getCurrentHref();
|
|
2667
2758
|
if (!href) {
|
|
@@ -2669,7 +2760,7 @@ const getWebRedirectUri = async () => {
|
|
|
2669
2760
|
}
|
|
2670
2761
|
try {
|
|
2671
2762
|
const url = new URL(href);
|
|
2672
|
-
return `${url.origin}/auth/callback`;
|
|
2763
|
+
return `${url.origin}${getGithubPagesBasePath(url)}/auth/callback`;
|
|
2673
2764
|
} catch {
|
|
2674
2765
|
return '';
|
|
2675
2766
|
}
|
|
@@ -2857,7 +2948,7 @@ const logout = async state => {
|
|
|
2857
2948
|
userState: 'loggingOut'
|
|
2858
2949
|
};
|
|
2859
2950
|
await logoutFromBackend(state.backendUrl);
|
|
2860
|
-
await Promise.all([clearOidcCallbackUrl(), clearPendingOidcAuthState(),
|
|
2951
|
+
await Promise.all([clearOidcCallbackUrl(), clearPendingOidcAuthState(), clearPersistedAuthSession()]);
|
|
2861
2952
|
return {
|
|
2862
2953
|
...loggingOutState,
|
|
2863
2954
|
...getLoggedOutBackendAuthState()
|