@multiplatform.one/auth 9.16.1 → 10.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/cjs/accessTokenRefresh.cjs +53 -51
- package/dist/cjs/accessTokenRefresh.native.cjs +79 -0
- package/dist/cjs/accessTokenRefresh.native.js +58 -55
- package/dist/cjs/accessTokenRefresh.native.js.map +1 -1
- package/dist/cjs/index.cjs +392 -436
- package/dist/cjs/index.native.cjs +498 -0
- package/dist/cjs/index.native.js +454 -497
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/esm/accessTokenRefresh.mjs +35 -30
- package/dist/esm/accessTokenRefresh.mjs.map +1 -1
- package/dist/esm/accessTokenRefresh.native.js +40 -35
- package/dist/esm/accessTokenRefresh.native.js.map +1 -1
- package/dist/esm/index.mjs +370 -411
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/index.native.js +432 -473
- package/dist/esm/index.native.js.map +1 -1
- package/dist/jsx/accessTokenRefresh.mjs +35 -30
- package/dist/jsx/accessTokenRefresh.mjs.map +1 -1
- package/dist/jsx/accessTokenRefresh.native.cjs +79 -0
- package/dist/jsx/accessTokenRefresh.native.js +58 -55
- package/dist/jsx/accessTokenRefresh.native.js.map +1 -1
- package/dist/jsx/index.js +370 -411
- package/dist/jsx/index.js.map +1 -1
- package/dist/jsx/index.mjs +370 -411
- package/dist/jsx/index.mjs.map +1 -1
- package/dist/jsx/index.native.cjs +498 -0
- package/dist/jsx/index.native.js +454 -497
- package/dist/jsx/index.native.js.map +1 -1
- package/package.json +4 -4
package/dist/jsx/index.js
CHANGED
|
@@ -4,439 +4,398 @@ import { APIError, createAuthEndpoint, createAuthMiddleware, getAccessToken, get
|
|
|
4
4
|
import { deleteSessionCookie, getAccountCookie } from "better-auth/cookies";
|
|
5
5
|
import { genericOAuth, keycloak } from "better-auth/plugins";
|
|
6
6
|
import { expo } from "@better-auth/expo";
|
|
7
|
-
import { FAILED_TO_GET_ACCESS_TOKEN, TOKEN_REFRESH_UNAVAILABLE } from "./accessTokenRefresh.mjs";
|
|
8
|
-
|
|
7
|
+
import { ACCESS_TOKEN_RETRY_DELAYS_MS, FAILED_TO_GET_ACCESS_TOKEN, FAILED_TO_GET_ACCESS_TOKEN as FAILED_TO_GET_ACCESS_TOKEN2, TOKEN_REFRESH_UNAVAILABLE, TOKEN_REFRESH_UNAVAILABLE as TOKEN_REFRESH_UNAVAILABLE2, classifyAccessTokenResponse, retryWhileTransient, watchDocumentVisible } from "./accessTokenRefresh.mjs";
|
|
8
|
+
|
|
9
9
|
const requestOriginStorage = new AsyncLocalStorage();
|
|
10
10
|
function deriveRequestOrigin(req) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
11
|
+
const isDevHost = (host) => DEV_LAN_HOST_PATTERNS.some((pattern) => matchesHostPattern(host, pattern));
|
|
12
|
+
const forwardedHost = req.headers.get("x-forwarded-host");
|
|
13
|
+
if (forwardedHost && isDevHost(forwardedHost)) {
|
|
14
|
+
const proto = req.headers.get("x-forwarded-proto") === "https" ? "https" : "http";
|
|
15
|
+
return `${proto}://${forwardedHost}`;
|
|
16
|
+
}
|
|
17
|
+
for (const candidate of [req.headers.get("origin"), req.url]) {
|
|
18
|
+
if (!candidate) continue;
|
|
19
|
+
try {
|
|
20
|
+
const url = new URL(candidate);
|
|
21
|
+
if (isDevHost(url.host)) return url.origin;
|
|
22
|
+
} catch {}
|
|
23
|
+
}
|
|
24
|
+
return void 0;
|
|
25
25
|
}
|
|
26
|
-
const DEV_LAN_HOST_PATTERNS = [
|
|
26
|
+
const DEV_LAN_HOST_PATTERNS = [
|
|
27
|
+
"localhost",
|
|
28
|
+
"localhost:*",
|
|
29
|
+
"127.0.0.1",
|
|
30
|
+
"127.0.0.1:*",
|
|
31
|
+
"10.*",
|
|
32
|
+
"192.168.*",
|
|
33
|
+
"172.*",
|
|
34
|
+
"*.local",
|
|
35
|
+
"*.local:*"
|
|
36
|
+
];
|
|
27
37
|
const OFFLINE_ACCESS_SCOPE = "offline_access";
|
|
28
38
|
function isNativeRequest(headers) {
|
|
29
|
-
|
|
39
|
+
return !!headers?.get("expo-origin");
|
|
30
40
|
}
|
|
31
41
|
function withOfflineAccessScope(authorizationUrl) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
42
|
+
const url = new URL(authorizationUrl);
|
|
43
|
+
const scopes = (url.searchParams.get("scope") ?? "").split(" ").filter(Boolean);
|
|
44
|
+
if (scopes.includes("offline_access")) return authorizationUrl;
|
|
45
|
+
url.searchParams.set("scope", [...scopes, OFFLINE_ACCESS_SCOPE].join(" "));
|
|
46
|
+
return url.toString();
|
|
37
47
|
}
|
|
38
48
|
function isOfflineRefreshToken(token) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
49
|
+
const payload = token?.split(".")[1];
|
|
50
|
+
if (!payload) return false;
|
|
51
|
+
try {
|
|
52
|
+
const json = atob(payload.replace(/-/g, "+").replace(/_/g, "/"));
|
|
53
|
+
return JSON.parse(json).typ === "Offline";
|
|
54
|
+
} catch {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
47
57
|
}
|
|
48
58
|
const accessTokenBodySchema = getAccessToken.options.body;
|
|
49
59
|
function acceptsAccessTokenBody(body) {
|
|
50
|
-
|
|
60
|
+
return accessTokenBodySchema?.safeParse?.(body).success !== false;
|
|
51
61
|
}
|
|
52
62
|
function createKeycloakSsoPlugin(options) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
if (!definitive) {
|
|
238
|
-
return new APIError("INTERNAL_SERVER_ERROR", {
|
|
239
|
-
message: "Keycloak token refresh temporarily unavailable",
|
|
240
|
-
code: TOKEN_REFRESH_UNAVAILABLE
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
ctx.context.logger.warn("Keycloak refresh token is dead (invalid_grant); revoking the stale Better Auth session");
|
|
244
|
-
const sessionToken = await ctx.getSignedCookie(ctx.context.authCookies.sessionToken.name, ctx.context.secret);
|
|
245
|
-
if (sessionToken) {
|
|
246
|
-
await ctx.context.internalAdapter.deleteSession(sessionToken).catch(error => {
|
|
247
|
-
ctx.context.logger.error("Failed to delete stale session", error);
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
deleteSessionCookie(ctx);
|
|
251
|
-
const headers = new Headers();
|
|
252
|
-
const accumulated = ctx.responseHeaders;
|
|
253
|
-
accumulated?.getSetCookie().forEach(cookie => {
|
|
254
|
-
headers.append("set-cookie", cookie);
|
|
255
|
-
});
|
|
256
|
-
return new APIError("BAD_REQUEST", {
|
|
257
|
-
message: body?.message || "Failed to get a valid access token",
|
|
258
|
-
code: FAILED_TO_GET_ACCESS_TOKEN
|
|
259
|
-
}, headers);
|
|
260
|
-
})
|
|
261
|
-
}]
|
|
262
|
-
}
|
|
263
|
-
};
|
|
63
|
+
const { realm, clientId, clientSecret, issuer, baseUrl, publicKeycloakBaseFor, nativeOfflineAccess } = options;
|
|
64
|
+
const tokenEndpoint = `${issuer}/protocol/openid-connect/token`;
|
|
65
|
+
const logoutEndpoint = `${issuer}/protocol/openid-connect/logout`;
|
|
66
|
+
const resolveVisitorOrigin = (request) => requestOriginStorage.getStore() ?? (request ? deriveRequestOrigin(request) : void 0);
|
|
67
|
+
const isRefreshDefinitivelyDead = async (refreshToken) => {
|
|
68
|
+
try {
|
|
69
|
+
const res = await fetch(tokenEndpoint, {
|
|
70
|
+
method: "POST",
|
|
71
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
72
|
+
body: new URLSearchParams({
|
|
73
|
+
grant_type: "refresh_token",
|
|
74
|
+
refresh_token: refreshToken,
|
|
75
|
+
client_id: clientId,
|
|
76
|
+
client_secret: clientSecret
|
|
77
|
+
}),
|
|
78
|
+
signal: AbortSignal.timeout(5e3)
|
|
79
|
+
});
|
|
80
|
+
if (res.ok) return false;
|
|
81
|
+
if (res.status !== 400 && res.status !== 401) return false;
|
|
82
|
+
const body = await res.json().catch(() => null);
|
|
83
|
+
return body?.error === "invalid_grant" || body?.error === "invalid_token";
|
|
84
|
+
} catch {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const findDeviceKeycloakAccount = async (ctx) => {
|
|
89
|
+
const accountCookie = await getAccountCookie(ctx).catch(() => null);
|
|
90
|
+
if (accountCookie?.providerId === "keycloak" && accountCookie.refreshToken) {
|
|
91
|
+
return accountCookie;
|
|
92
|
+
}
|
|
93
|
+
const session = await getSessionFromCtx(ctx).catch(() => null);
|
|
94
|
+
if (!session) return void 0;
|
|
95
|
+
const accounts = await ctx.context.internalAdapter.findAccounts(session.user.id).catch(() => []);
|
|
96
|
+
return accounts.find((account) => account.providerId === "keycloak");
|
|
97
|
+
};
|
|
98
|
+
const endOfflineSession = async (ctx, refreshToken) => {
|
|
99
|
+
if (!refreshToken || !isOfflineRefreshToken(refreshToken)) return;
|
|
100
|
+
try {
|
|
101
|
+
const res = await fetch(logoutEndpoint, {
|
|
102
|
+
method: "POST",
|
|
103
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
104
|
+
body: new URLSearchParams({
|
|
105
|
+
refresh_token: refreshToken,
|
|
106
|
+
client_id: clientId,
|
|
107
|
+
client_secret: clientSecret
|
|
108
|
+
}),
|
|
109
|
+
signal: AbortSignal.timeout(5e3)
|
|
110
|
+
});
|
|
111
|
+
if (!res.ok) {
|
|
112
|
+
ctx.context.logger.warn(`Keycloak offline session logout answered ${res.status}`);
|
|
113
|
+
}
|
|
114
|
+
} catch (error) {
|
|
115
|
+
ctx.context.logger.warn("Keycloak offline session logout failed", error);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const signOutSso = createAuthEndpoint("/sign-out/sso", { method: "GET" }, async (ctx) => {
|
|
119
|
+
const visitorOrigin = resolveVisitorOrigin(ctx.request) ?? baseUrl;
|
|
120
|
+
const searchParams = ctx.request ? new URL(ctx.request.url).searchParams : void 0;
|
|
121
|
+
const rawCallback = searchParams?.get("callbackURL") ?? null;
|
|
122
|
+
const jsonMode = searchParams?.get("mode") === "json";
|
|
123
|
+
const answer = (url, redirect) => {
|
|
124
|
+
if (jsonMode) return ctx.json({
|
|
125
|
+
redirect,
|
|
126
|
+
url
|
|
127
|
+
});
|
|
128
|
+
throw ctx.redirect(url);
|
|
129
|
+
};
|
|
130
|
+
const callbackURL = rawCallback || "/";
|
|
131
|
+
const postLogoutRedirect = callbackURL.startsWith("/") ? `${visitorOrigin}${callbackURL}` : callbackURL;
|
|
132
|
+
let idTokenHint;
|
|
133
|
+
const sessionToken = await ctx.getSignedCookie(ctx.context.authCookies.sessionToken.name, ctx.context.secret);
|
|
134
|
+
const session = await getSessionFromCtx(ctx).catch(() => null);
|
|
135
|
+
if (session) {
|
|
136
|
+
const accounts = await ctx.context.internalAdapter.findAccounts(session.user.id).catch(() => []);
|
|
137
|
+
idTokenHint = accounts.find((account) => account.providerId === "keycloak")?.idToken ?? void 0;
|
|
138
|
+
}
|
|
139
|
+
if (!idTokenHint) {
|
|
140
|
+
const accountCookie = await getAccountCookie(ctx).catch(() => null);
|
|
141
|
+
if (accountCookie?.providerId === "keycloak" && accountCookie.idToken) {
|
|
142
|
+
idTokenHint = accountCookie.idToken;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
await endOfflineSession(ctx, (await findDeviceKeycloakAccount(ctx))?.refreshToken);
|
|
146
|
+
if (sessionToken) {
|
|
147
|
+
await ctx.context.internalAdapter.deleteSession(sessionToken).catch((error) => {
|
|
148
|
+
ctx.context.logger.error("Failed to delete session during SSO sign-out", error);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
deleteSessionCookie(ctx);
|
|
152
|
+
const keycloakBase = publicKeycloakBaseFor(visitorOrigin);
|
|
153
|
+
if (!keycloakBase || !session && !idTokenHint && !sessionToken) {
|
|
154
|
+
return answer(postLogoutRedirect, false);
|
|
155
|
+
}
|
|
156
|
+
const endSession = new URL(`${keycloakBase}/realms/${realm}/protocol/openid-connect/logout`);
|
|
157
|
+
endSession.searchParams.set("post_logout_redirect_uri", postLogoutRedirect);
|
|
158
|
+
endSession.searchParams.set("client_id", clientId);
|
|
159
|
+
if (idTokenHint) endSession.searchParams.set("id_token_hint", idTokenHint);
|
|
160
|
+
return answer(endSession.toString(), true);
|
|
161
|
+
});
|
|
162
|
+
return {
|
|
163
|
+
id: "keycloak-sso",
|
|
164
|
+
endpoints: { signOutSso },
|
|
165
|
+
hooks: {
|
|
166
|
+
before: [{
|
|
167
|
+
matcher: (ctx) => ctx.path === "/sign-out",
|
|
168
|
+
handler: createAuthMiddleware(async (ctx) => {
|
|
169
|
+
await endOfflineSession(ctx, (await findDeviceKeycloakAccount(ctx))?.refreshToken);
|
|
170
|
+
})
|
|
171
|
+
}, {
|
|
172
|
+
matcher: (ctx) => ctx.path === "/get-access-token",
|
|
173
|
+
handler: createAuthMiddleware(async (ctx) => {
|
|
174
|
+
const session = await getSessionFromCtx(ctx).catch(() => null);
|
|
175
|
+
if (!session) return ctx.json(null);
|
|
176
|
+
const body = ctx.body;
|
|
177
|
+
if (body?.useAccountCookie !== true || acceptsAccessTokenBody(body)) return;
|
|
178
|
+
const legacy = {
|
|
179
|
+
...body,
|
|
180
|
+
providerId: "keycloak"
|
|
181
|
+
};
|
|
182
|
+
if (acceptsAccessTokenBody(legacy)) return { context: { body: legacy } };
|
|
183
|
+
})
|
|
184
|
+
}],
|
|
185
|
+
after: [{
|
|
186
|
+
matcher: (ctx) => nativeOfflineAccess && (ctx.path === "/sign-in/social" || ctx.path === "/sign-in/oauth2"),
|
|
187
|
+
handler: createAuthMiddleware(async (ctx) => {
|
|
188
|
+
if (!isNativeRequest(ctx.request?.headers ?? ctx.headers)) return;
|
|
189
|
+
const body = ctx.body;
|
|
190
|
+
if ((body?.provider ?? body?.providerId) !== "keycloak") return;
|
|
191
|
+
const returned = ctx.context.returned;
|
|
192
|
+
if (isAPIError(returned) || typeof returned?.url !== "string") return;
|
|
193
|
+
const url = withOfflineAccessScope(returned.url);
|
|
194
|
+
if (ctx.context.responseHeaders?.has("location")) ctx.setHeader("location", url);
|
|
195
|
+
return ctx.json({
|
|
196
|
+
...returned,
|
|
197
|
+
url
|
|
198
|
+
});
|
|
199
|
+
})
|
|
200
|
+
}, {
|
|
201
|
+
matcher: (ctx) => ctx.path === "/get-access-token",
|
|
202
|
+
handler: createAuthMiddleware(async (ctx) => {
|
|
203
|
+
const returned = ctx.context.returned;
|
|
204
|
+
if (!isAPIError(returned)) return;
|
|
205
|
+
const body = returned.body;
|
|
206
|
+
if (body?.code !== FAILED_TO_GET_ACCESS_TOKEN) return;
|
|
207
|
+
const providerId = ctx.body?.providerId;
|
|
208
|
+
if (providerId && providerId !== "keycloak") return;
|
|
209
|
+
const session = await getSessionFromCtx(ctx).catch(() => null);
|
|
210
|
+
if (!session) return;
|
|
211
|
+
const accounts = await ctx.context.internalAdapter.findAccounts(session.user.id).catch(() => []);
|
|
212
|
+
let refreshToken = accounts.find((account) => account.providerId === "keycloak")?.refreshToken;
|
|
213
|
+
if (!refreshToken) {
|
|
214
|
+
const accountCookie = await getAccountCookie(ctx).catch(() => null);
|
|
215
|
+
if (accountCookie?.providerId === "keycloak") {
|
|
216
|
+
refreshToken = accountCookie.refreshToken;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const definitive = refreshToken ? await isRefreshDefinitivelyDead(refreshToken) : true;
|
|
220
|
+
if (!definitive) {
|
|
221
|
+
return new APIError("INTERNAL_SERVER_ERROR", {
|
|
222
|
+
message: "Keycloak token refresh temporarily unavailable",
|
|
223
|
+
code: TOKEN_REFRESH_UNAVAILABLE
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
ctx.context.logger.warn("Keycloak refresh token is dead (invalid_grant); revoking the stale Better Auth session");
|
|
227
|
+
const sessionToken = await ctx.getSignedCookie(ctx.context.authCookies.sessionToken.name, ctx.context.secret);
|
|
228
|
+
if (sessionToken) {
|
|
229
|
+
await ctx.context.internalAdapter.deleteSession(sessionToken).catch((error) => {
|
|
230
|
+
ctx.context.logger.error("Failed to delete stale session", error);
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
deleteSessionCookie(ctx);
|
|
234
|
+
const headers = new Headers();
|
|
235
|
+
const accumulated = ctx.responseHeaders;
|
|
236
|
+
accumulated?.getSetCookie().forEach((cookie) => {
|
|
237
|
+
headers.append("set-cookie", cookie);
|
|
238
|
+
});
|
|
239
|
+
return new APIError("BAD_REQUEST", {
|
|
240
|
+
message: body?.message || "Failed to get a valid access token",
|
|
241
|
+
code: FAILED_TO_GET_ACCESS_TOKEN
|
|
242
|
+
}, headers);
|
|
243
|
+
})
|
|
244
|
+
}]
|
|
245
|
+
}
|
|
246
|
+
};
|
|
264
247
|
}
|
|
265
248
|
function runtimeEnv() {
|
|
266
|
-
|
|
249
|
+
return typeof process === "undefined" ? {} : process.env;
|
|
267
250
|
}
|
|
268
251
|
function createAuth(options = {}) {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
// derives its callbackURL via Linking.createURL, which yields
|
|
390
|
-
// exp://<host>:<port>/--/ there. Never trusted in production builds.
|
|
391
|
-
// Dev also trusts private-network origins (see DEV_LAN_HOST_PATTERNS)
|
|
392
|
-
// so sign-in from a phone / the machine's LAN IP is not rejected.
|
|
393
|
-
...(isProduction ? [] : ["exp://", ...DEV_LAN_HOST_PATTERNS.map(host => `http://${host}`)])],
|
|
394
|
-
session: {
|
|
395
|
-
cookieCache: {
|
|
396
|
-
enabled: true,
|
|
397
|
-
maxAge: options.sessionMaxAge ?? 7 * 24 * 60 * 60,
|
|
398
|
-
strategy: "jwe",
|
|
399
|
-
refreshCache: true
|
|
400
|
-
}
|
|
401
|
-
},
|
|
402
|
-
account: {
|
|
403
|
-
storeStateStrategy: "cookie",
|
|
404
|
-
storeAccountCookie: true
|
|
405
|
-
},
|
|
406
|
-
plugins
|
|
407
|
-
});
|
|
252
|
+
const env = runtimeEnv();
|
|
253
|
+
const baseUrl = options.baseUrl || env.BASE_URL || `http://localhost:${env.ONE_PORT || "3000"}`;
|
|
254
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
255
|
+
const keycloakBaseUrl = options.keycloak?.baseUrl || env.KEYCLOAK_BACKCHANNEL_URL || `${baseUrl}/auth`;
|
|
256
|
+
const keycloakRealm = options.keycloak?.realm || env.KEYCLOAK_REALM || "master";
|
|
257
|
+
const issuer = `${keycloakBaseUrl}/realms/${keycloakRealm}`;
|
|
258
|
+
const keycloakClientId = options.keycloak?.clientId || env.KEYCLOAK_CLIENT_ID || "";
|
|
259
|
+
const keycloakClientSecret = options.keycloak?.clientSecret || env.KEYCLOAK_CLIENT_SECRET || "";
|
|
260
|
+
const expoPlugin = options.enableExpo !== false ? expo() : void 0;
|
|
261
|
+
const basePath = options.basePath || "/api/auth";
|
|
262
|
+
const keycloakProviderConfig = {
|
|
263
|
+
...keycloak({
|
|
264
|
+
clientId: keycloakClientId,
|
|
265
|
+
clientSecret: keycloakClientSecret,
|
|
266
|
+
issuer
|
|
267
|
+
}),
|
|
268
|
+
tokenUrl: `${issuer}/protocol/openid-connect/token`,
|
|
269
|
+
userInfoUrl: `${issuer}/protocol/openid-connect/userinfo`,
|
|
270
|
+
mapProfileToUser: (profile) => {
|
|
271
|
+
const handle = profile.preferred_username || profile.sub || profile.id || profile.name;
|
|
272
|
+
const email = profile.email || (handle ? `${handle}@wallet` : void 0);
|
|
273
|
+
return {
|
|
274
|
+
...profile,
|
|
275
|
+
email,
|
|
276
|
+
emailVerified: profile.email_verified ?? true,
|
|
277
|
+
name: profile.name || handle
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
const keycloakPublicPath = (() => {
|
|
282
|
+
try {
|
|
283
|
+
const path = new URL(keycloakBaseUrl).pathname.replace(/\/+$/, "");
|
|
284
|
+
return path && path !== "/" ? path : void 0;
|
|
285
|
+
} catch {
|
|
286
|
+
return void 0;
|
|
287
|
+
}
|
|
288
|
+
})();
|
|
289
|
+
const publicKeycloakBaseFor = (requestOrigin) => {
|
|
290
|
+
if (!keycloakPublicPath) return keycloakBaseUrl;
|
|
291
|
+
try {
|
|
292
|
+
const url = new URL(baseUrl);
|
|
293
|
+
if (!isProduction && requestOrigin) {
|
|
294
|
+
url.hostname = new URL(requestOrigin).hostname;
|
|
295
|
+
}
|
|
296
|
+
return `${url.origin}${keycloakPublicPath}`;
|
|
297
|
+
} catch {
|
|
298
|
+
return void 0;
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
if (!isProduction) {
|
|
302
|
+
Object.defineProperty(keycloakProviderConfig, "redirectURI", {
|
|
303
|
+
enumerable: true,
|
|
304
|
+
get() {
|
|
305
|
+
const origin = requestOriginStorage.getStore();
|
|
306
|
+
return origin ? `${origin}${basePath}/oauth2/callback/keycloak` : void 0;
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
const keycloakPublicBase = () => {
|
|
310
|
+
const origin = requestOriginStorage.getStore();
|
|
311
|
+
if (!origin || !keycloakPublicPath) return void 0;
|
|
312
|
+
return publicKeycloakBaseFor(origin);
|
|
313
|
+
};
|
|
314
|
+
if (keycloakPublicPath) {
|
|
315
|
+
Object.defineProperty(keycloakProviderConfig, "authorizationUrl", {
|
|
316
|
+
enumerable: true,
|
|
317
|
+
get() {
|
|
318
|
+
const base = keycloakPublicBase();
|
|
319
|
+
return base ? `${base}/realms/${keycloakRealm}/protocol/openid-connect/auth` : void 0;
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
Object.defineProperty(keycloakProviderConfig, "issuer", {
|
|
323
|
+
enumerable: true,
|
|
324
|
+
get() {
|
|
325
|
+
const base = keycloakPublicBase();
|
|
326
|
+
return base ? `${base}/realms/${keycloakRealm}` : void 0;
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const oauthPlugin = genericOAuth({ config: [keycloakProviderConfig] });
|
|
332
|
+
const keycloakSsoPlugin = createKeycloakSsoPlugin({
|
|
333
|
+
realm: keycloakRealm,
|
|
334
|
+
clientId: keycloakClientId,
|
|
335
|
+
clientSecret: keycloakClientSecret,
|
|
336
|
+
issuer,
|
|
337
|
+
baseUrl,
|
|
338
|
+
publicKeycloakBaseFor,
|
|
339
|
+
nativeOfflineAccess: options.nativeOfflineAccess !== false
|
|
340
|
+
});
|
|
341
|
+
const plugins = [
|
|
342
|
+
...expoPlugin ? [expoPlugin] : [],
|
|
343
|
+
oauthPlugin,
|
|
344
|
+
keycloakSsoPlugin,
|
|
345
|
+
...options.plugins ?? []
|
|
346
|
+
];
|
|
347
|
+
const database = options.database ?? (env.DATABASE_URL || void 0);
|
|
348
|
+
return betterAuth({
|
|
349
|
+
basePath,
|
|
350
|
+
baseURL: baseUrl,
|
|
351
|
+
...database ? { database } : {},
|
|
352
|
+
secret: options.secret || env.SECRET || "",
|
|
353
|
+
trustedOrigins: options.trustedOrigins || [
|
|
354
|
+
baseUrl,
|
|
355
|
+
keycloakBaseUrl,
|
|
356
|
+
...env.ONE_PORT && !baseUrl.includes(`:${env.ONE_PORT}`) ? [`http://localhost:${env.ONE_PORT}`] : [],
|
|
357
|
+
"multiplatform-one://",
|
|
358
|
+
...isProduction ? [] : ["exp://", ...DEV_LAN_HOST_PATTERNS.map((host) => `http://${host}`)]
|
|
359
|
+
],
|
|
360
|
+
session: { cookieCache: {
|
|
361
|
+
enabled: true,
|
|
362
|
+
maxAge: options.sessionMaxAge ?? 7 * 24 * 60 * 60,
|
|
363
|
+
strategy: "jwe",
|
|
364
|
+
refreshCache: true
|
|
365
|
+
} },
|
|
366
|
+
account: {
|
|
367
|
+
storeStateStrategy: "cookie",
|
|
368
|
+
storeAccountCookie: true
|
|
369
|
+
},
|
|
370
|
+
plugins
|
|
371
|
+
});
|
|
408
372
|
}
|
|
409
373
|
function createAuthRouteHandler(auth) {
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
374
|
+
const handler = (req) => {
|
|
375
|
+
const origin = deriveRequestOrigin(req);
|
|
376
|
+
return origin ? requestOriginStorage.run(origin, () => auth.handler(req)) : auth.handler(req);
|
|
377
|
+
};
|
|
378
|
+
return {
|
|
379
|
+
GET: handler,
|
|
380
|
+
POST: handler
|
|
381
|
+
};
|
|
418
382
|
}
|
|
419
383
|
function createSessionHelpers(auth) {
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
return session;
|
|
435
|
-
}
|
|
436
|
-
return {
|
|
437
|
-
verifyAuth,
|
|
438
|
-
getSession
|
|
439
|
-
};
|
|
384
|
+
async function getSession(req) {
|
|
385
|
+
return auth.api.getSession({ headers: req.headers });
|
|
386
|
+
}
|
|
387
|
+
async function verifyAuth(req) {
|
|
388
|
+
const session = await auth.api.getSession({ headers: req.headers });
|
|
389
|
+
if (!session) {
|
|
390
|
+
throw new Response("Unauthorized", { status: 401 });
|
|
391
|
+
}
|
|
392
|
+
return session;
|
|
393
|
+
}
|
|
394
|
+
return {
|
|
395
|
+
verifyAuth,
|
|
396
|
+
getSession
|
|
397
|
+
};
|
|
440
398
|
}
|
|
399
|
+
|
|
441
400
|
export { ACCESS_TOKEN_RETRY_DELAYS_MS, FAILED_TO_GET_ACCESS_TOKEN2 as FAILED_TO_GET_ACCESS_TOKEN, OFFLINE_ACCESS_SCOPE, TOKEN_REFRESH_UNAVAILABLE2 as TOKEN_REFRESH_UNAVAILABLE, classifyAccessTokenResponse, createAuth, createAuthRouteHandler, createSessionHelpers, isOfflineRefreshToken, retryWhileTransient, watchDocumentVisible, withOfflineAccessScope };
|
|
442
401
|
//# sourceMappingURL=index.js.map
|