peta-auth 0.1.3 → 0.2.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/crypto-DR-ETdLZ.d.mts +35 -0
- package/dist/crypto-WcFV83Nz.mjs +84 -0
- package/dist/csrf.d.mts +31 -2
- package/dist/csrf.mjs +34 -2
- package/dist/elysia.d.mts +22 -1
- package/dist/elysia.mjs +16 -7
- package/dist/errors-DxJ-WUJL.mjs +17 -0
- package/dist/hono.d.mts +22 -1
- package/dist/hono.mjs +13 -3
- package/dist/index.d.mts +59 -5
- package/dist/index.mjs +56 -8
- package/dist/jwt.d.mts +23 -2
- package/dist/jwt.mjs +29 -9
- package/dist/nuxt.d.mts +26 -3
- package/dist/nuxt.mjs +18 -6
- package/dist/oauth/github.d.mts +18 -5
- package/dist/oauth/github.mjs +62 -71
- package/dist/oauth/google.d.mts +20 -7
- package/dist/oauth/google.mjs +65 -75
- package/dist/session-0bF8_7Ui.d.mts +53 -0
- package/dist/session-BGCQ1Z1Q.mjs +87 -0
- package/dist/utils-CKT3C1Lq.mjs +174 -0
- package/package.json +11 -7
- package/dist/crypto-Ln_Mj_zp.d.mts +0 -19
- package/dist/oauth/index.d.mts +0 -25
- package/dist/oauth/index.mjs +0 -103
- package/dist/session-DSwf3XPH.mjs +0 -119
- package/dist/session-z20gaFVT.d.mts +0 -23
package/dist/nuxt.d.mts
CHANGED
|
@@ -1,9 +1,32 @@
|
|
|
1
|
-
import { r as SessionOptions, t as IronSession } from "./session-
|
|
1
|
+
import { r as SessionOptions, t as IronSession } from "./session-0bF8_7Ui.mjs";
|
|
2
2
|
import { H3Event } from "h3";
|
|
3
3
|
|
|
4
4
|
//#region src/nuxt.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Create a session from an h3 event (Nuxt / h3).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* // In a Nuxt server handler:
|
|
11
|
+
* const session = await useSession(event, { password: process.env.NUXT_SESSION_PASSWORD })
|
|
12
|
+
* session.userId = 42
|
|
13
|
+
* await session.save()
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
5
16
|
declare function useSession<T extends Record<string, unknown> = Record<string, unknown>>(event: H3Event, options: SessionOptions): Promise<T & IronSession>;
|
|
6
|
-
|
|
7
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Guard that requires session data.
|
|
19
|
+
*
|
|
20
|
+
* Throws a 401 h3 error when the session is empty.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* const session = await useSession(event, options)
|
|
25
|
+
* requireSession(event, session)
|
|
26
|
+
* requireSession(event, session, "role") // require specific key
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
declare function requireSession(event: H3Event, session: IronSession): void;
|
|
30
|
+
declare function requireSession<K extends string>(event: H3Event, session: IronSession, key: K): void;
|
|
8
31
|
//#endregion
|
|
9
32
|
export { requireSession, useSession };
|
package/dist/nuxt.mjs
CHANGED
|
@@ -1,21 +1,33 @@
|
|
|
1
|
-
import { t as
|
|
1
|
+
import { t as PetaAuthError } from "./errors-DxJ-WUJL.mjs";
|
|
2
|
+
import { n as sessionHasData, t as createSessionFromAdapter } from "./session-BGCQ1Z1Q.mjs";
|
|
2
3
|
import { appendHeader, createError, getCookie } from "h3";
|
|
3
4
|
//#region src/nuxt.ts
|
|
5
|
+
/**
|
|
6
|
+
* Create a session from an h3 event (Nuxt / h3).
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* // In a Nuxt server handler:
|
|
11
|
+
* const session = await useSession(event, { password: process.env.NUXT_SESSION_PASSWORD })
|
|
12
|
+
* session.userId = 42
|
|
13
|
+
* await session.save()
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
4
16
|
function useSession(event, options) {
|
|
5
17
|
const password = options.password ?? process.env.NUXT_SESSION_PASSWORD;
|
|
6
|
-
if (!password) throw new
|
|
18
|
+
if (!password) throw new PetaAuthError("MISSING_PASSWORD", "peta-auth/nuxt: NUXT_SESSION_PASSWORD is required");
|
|
7
19
|
return createSessionFromAdapter({
|
|
8
20
|
getCookie: (name) => getCookie(event, name),
|
|
9
21
|
setCookie: (value) => appendHeader(event, "Set-Cookie", value)
|
|
10
22
|
}, {
|
|
11
23
|
password,
|
|
12
|
-
cookieName: options
|
|
13
|
-
|
|
14
|
-
cookieOptions: options
|
|
24
|
+
cookieName: options.cookieName ?? "nuxt-session",
|
|
25
|
+
timeToLive: options.timeToLive,
|
|
26
|
+
cookieOptions: options.cookieOptions
|
|
15
27
|
});
|
|
16
28
|
}
|
|
17
29
|
function requireSession(_event, session, key) {
|
|
18
|
-
if (!(
|
|
30
|
+
if (!sessionHasData(session, key)) throw createError({
|
|
19
31
|
statusCode: 401,
|
|
20
32
|
statusMessage: "unauthorized"
|
|
21
33
|
});
|
package/dist/oauth/github.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
//#region src/oauth/github.d.ts
|
|
2
|
+
/** Configuration for GitHub OAuth. */
|
|
2
3
|
interface OAuthGitHubConfig {
|
|
3
4
|
clientId?: string;
|
|
4
5
|
clientSecret?: string;
|
|
@@ -10,6 +11,11 @@ interface OAuthGitHubConfig {
|
|
|
10
11
|
authorizationParams?: Record<string, string>;
|
|
11
12
|
redirectURL?: string;
|
|
12
13
|
}
|
|
14
|
+
interface GitHubTokens {
|
|
15
|
+
access_token: string;
|
|
16
|
+
scope: string;
|
|
17
|
+
token_type: string;
|
|
18
|
+
}
|
|
13
19
|
interface GitHubUser {
|
|
14
20
|
login: string;
|
|
15
21
|
id: number;
|
|
@@ -19,11 +25,18 @@ interface GitHubUser {
|
|
|
19
25
|
email: string | null;
|
|
20
26
|
email_verified?: boolean;
|
|
21
27
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Define a GitHub OAuth event handler.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const handle = defineOAuthGitHubEventHandler({
|
|
34
|
+
* onSuccess: async ({ user, tokens }) =>
|
|
35
|
+
* new Response(`Welcome ${user.login}!`),
|
|
36
|
+
* })
|
|
37
|
+
* serve(handle)
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
27
40
|
declare function defineOAuthGitHubEventHandler(options: {
|
|
28
41
|
config?: OAuthGitHubConfig;
|
|
29
42
|
onSuccess: (event: {
|
package/dist/oauth/github.mjs
CHANGED
|
@@ -1,92 +1,83 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as defineOAuthHandler } from "../utils-CKT3C1Lq.mjs";
|
|
2
2
|
//#region src/oauth/github.ts
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (!config.clientSecret) missing.push("clientSecret");
|
|
36
|
-
return handleMissingConfiguration("github", missing, onError);
|
|
37
|
-
}
|
|
38
|
-
const redirectURL = config.redirectURL || getOAuthRedirectURL(request);
|
|
39
|
-
const state = handleState(request);
|
|
40
|
-
if (!queryCode) {
|
|
41
|
-
if (config.emailRequired && !config.scope.includes("user:email")) config.scope.push("user:email");
|
|
42
|
-
const authUrl = new URL(config.authorizationURL);
|
|
43
|
-
authUrl.searchParams.set("client_id", config.clientId);
|
|
44
|
-
authUrl.searchParams.set("redirect_uri", redirectURL);
|
|
45
|
-
authUrl.searchParams.set("scope", config.scope.join(" "));
|
|
46
|
-
authUrl.searchParams.set("state", state.state ?? "");
|
|
47
|
-
for (const [k, v] of Object.entries(config.authorizationParams)) authUrl.searchParams.set(k, v);
|
|
48
|
-
return redirect(authUrl.toString(), state.setCookie);
|
|
49
|
-
}
|
|
50
|
-
if (!queryState || queryState !== state.expectedState) return handleInvalidState("github", onError);
|
|
51
|
-
const tokens = await requestAccessToken(config.tokenURL, { body: {
|
|
3
|
+
const githubProvider = {
|
|
4
|
+
name: "github",
|
|
5
|
+
resolveConfig(config) {
|
|
6
|
+
const c = config;
|
|
7
|
+
return {
|
|
8
|
+
authorizationURL: c.authorizationURL ?? "https://github.com/login/oauth/authorize",
|
|
9
|
+
tokenURL: c.tokenURL ?? "https://github.com/login/oauth/access_token",
|
|
10
|
+
apiURL: c.apiURL ?? "https://api.github.com",
|
|
11
|
+
clientId: c.clientId ?? process.env.PETA_OAUTH_GITHUB_CLIENT_ID ?? "",
|
|
12
|
+
clientSecret: c.clientSecret ?? process.env.PETA_OAUTH_GITHUB_CLIENT_SECRET ?? "",
|
|
13
|
+
scope: c.scope ?? [],
|
|
14
|
+
emailRequired: c.emailRequired ?? false,
|
|
15
|
+
authorizationParams: c.authorizationParams ?? {},
|
|
16
|
+
redirectURL: c.redirectURL
|
|
17
|
+
};
|
|
18
|
+
},
|
|
19
|
+
buildAuthUrl(config, redirectURL, state, _pkce) {
|
|
20
|
+
const c = config;
|
|
21
|
+
if (c.emailRequired && !c.scope.includes("user:email")) c.scope.push("user:email");
|
|
22
|
+
const authUrl = new URL(c.authorizationURL);
|
|
23
|
+
authUrl.searchParams.set("client_id", c.clientId);
|
|
24
|
+
authUrl.searchParams.set("redirect_uri", redirectURL);
|
|
25
|
+
authUrl.searchParams.set("scope", c.scope.join(" "));
|
|
26
|
+
authUrl.searchParams.set("state", state.state ?? "");
|
|
27
|
+
for (const [key, value] of Object.entries(c.authorizationParams)) authUrl.searchParams.set(key, value);
|
|
28
|
+
return {
|
|
29
|
+
url: authUrl.toString(),
|
|
30
|
+
cookies: state.setCookie
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
requestTokenBody(config, redirectURL, code, _pkce) {
|
|
34
|
+
return {
|
|
52
35
|
grant_type: "authorization_code",
|
|
53
36
|
client_id: config.clientId,
|
|
54
37
|
client_secret: config.clientSecret,
|
|
55
38
|
redirect_uri: redirectURL,
|
|
56
|
-
code
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
39
|
+
code
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
async fetchUser(config, tokens, _request) {
|
|
43
|
+
const c = config;
|
|
60
44
|
const accessToken = tokens.access_token;
|
|
61
|
-
const userResponse = await fetch(`${
|
|
62
|
-
"User-Agent": `GitHub-OAuth-${
|
|
45
|
+
const userResponse = await fetch(`${c.apiURL}/user`, { headers: {
|
|
46
|
+
"User-Agent": `GitHub-OAuth-${c.clientId}`,
|
|
63
47
|
Authorization: `token ${accessToken}`
|
|
64
48
|
} });
|
|
65
|
-
if (!userResponse.ok) {
|
|
66
|
-
const err = /* @__PURE__ */ new Error(`GitHub user fetch failed: ${userResponse.status}`);
|
|
67
|
-
if (onError) return onError(err);
|
|
68
|
-
throw err;
|
|
69
|
-
}
|
|
49
|
+
if (!userResponse.ok) throw new Error(`GitHub user fetch failed: ${userResponse.status}`);
|
|
70
50
|
const user = await userResponse.json();
|
|
71
|
-
if (!user.email &&
|
|
72
|
-
const emailsResponse = await fetch(`${
|
|
73
|
-
"User-Agent": `GitHub-OAuth-${
|
|
51
|
+
if (!user.email && c.emailRequired) {
|
|
52
|
+
const emailsResponse = await fetch(`${c.apiURL}/user/emails`, { headers: {
|
|
53
|
+
"User-Agent": `GitHub-OAuth-${c.clientId}`,
|
|
74
54
|
Authorization: `token ${accessToken}`
|
|
75
55
|
} });
|
|
76
56
|
if (emailsResponse.ok) {
|
|
77
|
-
const primaryEmail = (await emailsResponse.json()).find((
|
|
57
|
+
const primaryEmail = (await emailsResponse.json()).find((entry) => entry.primary);
|
|
78
58
|
if (primaryEmail) {
|
|
79
59
|
user.email = primaryEmail.email;
|
|
80
60
|
user.email_verified = primaryEmail.verified;
|
|
81
61
|
}
|
|
82
62
|
}
|
|
83
63
|
}
|
|
84
|
-
return
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
64
|
+
return user;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Define a GitHub OAuth event handler.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* const handle = defineOAuthGitHubEventHandler({
|
|
73
|
+
* onSuccess: async ({ user, tokens }) =>
|
|
74
|
+
* new Response(`Welcome ${user.login}!`),
|
|
75
|
+
* })
|
|
76
|
+
* serve(handle)
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
function defineOAuthGitHubEventHandler(options) {
|
|
80
|
+
return defineOAuthHandler(githubProvider, options);
|
|
90
81
|
}
|
|
91
82
|
//#endregion
|
|
92
83
|
export { defineOAuthGitHubEventHandler };
|
package/dist/oauth/google.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
//#region src/oauth/google.d.ts
|
|
2
|
+
/** Configuration for Google OAuth. */
|
|
2
3
|
interface OAuthGoogleConfig {
|
|
3
4
|
clientId?: string;
|
|
4
5
|
clientSecret?: string;
|
|
@@ -9,6 +10,13 @@ interface OAuthGoogleConfig {
|
|
|
9
10
|
authorizationParams?: Record<string, string>;
|
|
10
11
|
redirectURL?: string;
|
|
11
12
|
}
|
|
13
|
+
interface GoogleTokens {
|
|
14
|
+
access_token: string;
|
|
15
|
+
id_token: string;
|
|
16
|
+
scope: string;
|
|
17
|
+
token_type: string;
|
|
18
|
+
expires_in: number;
|
|
19
|
+
}
|
|
12
20
|
interface GoogleUser {
|
|
13
21
|
sub: string;
|
|
14
22
|
name: string;
|
|
@@ -19,13 +27,18 @@ interface GoogleUser {
|
|
|
19
27
|
email_verified: boolean;
|
|
20
28
|
locale: string;
|
|
21
29
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
30
|
+
/**
|
|
31
|
+
* Define a Google OAuth event handler.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const handle = defineOAuthGoogleEventHandler({
|
|
36
|
+
* onSuccess: async ({ user }) =>
|
|
37
|
+
* new Response(`Welcome ${user.name}!`),
|
|
38
|
+
* })
|
|
39
|
+
* serve(handle)
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
29
42
|
declare function defineOAuthGoogleEventHandler(options: {
|
|
30
43
|
config?: OAuthGoogleConfig;
|
|
31
44
|
onSuccess: (event: {
|
package/dist/oauth/google.mjs
CHANGED
|
@@ -1,84 +1,74 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as defineOAuthHandler } from "../utils-CKT3C1Lq.mjs";
|
|
2
2
|
//#region src/oauth/google.ts
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
if (!config.clientId || !config.clientSecret) {
|
|
36
|
-
const missing = [];
|
|
37
|
-
if (!config.clientId) missing.push("clientId");
|
|
38
|
-
if (!config.clientSecret) missing.push("clientSecret");
|
|
39
|
-
return handleMissingConfiguration("google", missing, onError);
|
|
3
|
+
const googleProvider = {
|
|
4
|
+
name: "google",
|
|
5
|
+
resolveConfig(config) {
|
|
6
|
+
const c = config;
|
|
7
|
+
return {
|
|
8
|
+
authorizationURL: c.authorizationURL ?? "https://accounts.google.com/o/oauth2/v2/auth",
|
|
9
|
+
tokenURL: c.tokenURL ?? "https://oauth2.googleapis.com/token",
|
|
10
|
+
userInfoURL: c.userInfoURL ?? "https://www.googleapis.com/oauth2/v3/userinfo",
|
|
11
|
+
clientId: c.clientId ?? process.env.PETA_OAUTH_GOOGLE_CLIENT_ID ?? "",
|
|
12
|
+
clientSecret: c.clientSecret ?? process.env.PETA_OAUTH_GOOGLE_CLIENT_SECRET ?? "",
|
|
13
|
+
scope: c.scope ?? [
|
|
14
|
+
"openid",
|
|
15
|
+
"email",
|
|
16
|
+
"profile"
|
|
17
|
+
],
|
|
18
|
+
authorizationParams: c.authorizationParams ?? {},
|
|
19
|
+
redirectURL: c.redirectURL
|
|
20
|
+
};
|
|
21
|
+
},
|
|
22
|
+
buildAuthUrl(config, redirectURL, state, pkce) {
|
|
23
|
+
const c = config;
|
|
24
|
+
const authUrl = new URL(c.authorizationURL);
|
|
25
|
+
authUrl.searchParams.set("client_id", c.clientId);
|
|
26
|
+
authUrl.searchParams.set("redirect_uri", redirectURL);
|
|
27
|
+
authUrl.searchParams.set("scope", c.scope.join(" "));
|
|
28
|
+
authUrl.searchParams.set("response_type", "code");
|
|
29
|
+
authUrl.searchParams.set("state", state.state ?? "");
|
|
30
|
+
if (pkce.codeChallenge) {
|
|
31
|
+
authUrl.searchParams.set("code_challenge", pkce.codeChallenge);
|
|
32
|
+
authUrl.searchParams.set("code_challenge_method", pkce.codeChallengeMethod ?? "S256");
|
|
40
33
|
}
|
|
41
|
-
const
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
authUrl.searchParams.set("state", state.state ?? "");
|
|
51
|
-
if (pkce.codeChallenge) {
|
|
52
|
-
authUrl.searchParams.set("code_challenge", pkce.codeChallenge);
|
|
53
|
-
authUrl.searchParams.set("code_challenge_method", pkce.codeChallengeMethod ?? "S256");
|
|
54
|
-
}
|
|
55
|
-
for (const [k, v] of Object.entries(config.authorizationParams)) authUrl.searchParams.set(k, v);
|
|
56
|
-
const cookies = [state.setCookie, pkce.setCookie].filter(Boolean).join("; ");
|
|
57
|
-
return redirect(authUrl.toString(), cookies || void 0);
|
|
58
|
-
}
|
|
59
|
-
if (!queryState || queryState !== state.expectedState) return handleInvalidState("google", onError);
|
|
60
|
-
const tokens = await requestAccessToken(config.tokenURL, { body: {
|
|
34
|
+
for (const [key, value] of Object.entries(c.authorizationParams)) authUrl.searchParams.set(key, value);
|
|
35
|
+
const cookies = [state.setCookie, pkce.setCookie].filter(Boolean).join("; ");
|
|
36
|
+
return {
|
|
37
|
+
url: authUrl.toString(),
|
|
38
|
+
cookies: cookies || void 0
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
requestTokenBody(config, redirectURL, code, pkce) {
|
|
42
|
+
return {
|
|
61
43
|
grant_type: "authorization_code",
|
|
62
44
|
client_id: config.clientId,
|
|
63
45
|
client_secret: config.clientSecret,
|
|
64
46
|
redirect_uri: redirectURL,
|
|
65
|
-
code
|
|
66
|
-
code_verifier: pkce.codeVerifier
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
47
|
+
code,
|
|
48
|
+
code_verifier: pkce.codeVerifier ?? ""
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
async fetchUser(config, tokens, _request) {
|
|
52
|
+
const userURL = config.userInfoURL ?? "https://www.googleapis.com/oauth2/v3/userinfo";
|
|
53
|
+
const userResponse = await fetch(userURL, { headers: { Authorization: `Bearer ${tokens.access_token}` } });
|
|
54
|
+
if (!userResponse.ok) throw new Error(`Google user fetch failed: ${userResponse.status}`);
|
|
55
|
+
return userResponse.json();
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Define a Google OAuth event handler.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* const handle = defineOAuthGoogleEventHandler({
|
|
64
|
+
* onSuccess: async ({ user }) =>
|
|
65
|
+
* new Response(`Welcome ${user.name}!`),
|
|
66
|
+
* })
|
|
67
|
+
* serve(handle)
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
function defineOAuthGoogleEventHandler(options) {
|
|
71
|
+
return defineOAuthHandler(googleProvider, options);
|
|
82
72
|
}
|
|
83
73
|
//#endregion
|
|
84
74
|
export { defineOAuthGoogleEventHandler };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { t as Password } from "./crypto-DR-ETdLZ.mjs";
|
|
2
|
+
import { SerializeOptions } from "cookie";
|
|
3
|
+
|
|
4
|
+
//#region src/session.d.ts
|
|
5
|
+
/** Options for creating a cookie session. */
|
|
6
|
+
interface SessionOptions {
|
|
7
|
+
/** Password(s) used to encrypt the session cookie. */
|
|
8
|
+
password: Password;
|
|
9
|
+
/** Name of the cookie. */
|
|
10
|
+
cookieName: string;
|
|
11
|
+
/** Session lifetime in seconds (default 14 days). */
|
|
12
|
+
timeToLive?: number;
|
|
13
|
+
/** Extra cookie serialization options. */
|
|
14
|
+
cookieOptions?: Omit<SerializeOptions, "encode">;
|
|
15
|
+
}
|
|
16
|
+
/** A session instance returned by {@link createSessionFromAdapter}. */
|
|
17
|
+
interface IronSession {
|
|
18
|
+
/** Persist the session to the response cookie. */
|
|
19
|
+
save(): Promise<void>;
|
|
20
|
+
/** Clear the session cookie. */
|
|
21
|
+
destroy(): void;
|
|
22
|
+
/** Update session config at runtime. */
|
|
23
|
+
updateConfig(options: SessionOptions): void;
|
|
24
|
+
/** Arbitrary session data keys. */
|
|
25
|
+
[key: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
/** An adapter between the framework and the session cookie store. */
|
|
28
|
+
interface SessionAdapter {
|
|
29
|
+
/** Read a cookie by name from the incoming request. */
|
|
30
|
+
getCookie(name: string): string | undefined;
|
|
31
|
+
/** Set a cookie on the outgoing response. */
|
|
32
|
+
setCookie(cookie: string): void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Create a session from a framework adapter.
|
|
36
|
+
*
|
|
37
|
+
* Reads the session cookie (if present), hydrates the data, and
|
|
38
|
+
* returns a session object with {@link IronSession.save},
|
|
39
|
+
* {@link IronSession.destroy}, and {@link IronSession.updateConfig}.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const session = await createSessionFromAdapter(adapter, {
|
|
44
|
+
* password: "my-32-char-password...",
|
|
45
|
+
* cookieName: "my-session",
|
|
46
|
+
* })
|
|
47
|
+
* session.userId = 42
|
|
48
|
+
* await session.save()
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare function createSessionFromAdapter<T extends Record<string, unknown> = Record<string, unknown>>(adapter: SessionAdapter, options: SessionOptions): Promise<T & IronSession>;
|
|
52
|
+
//#endregion
|
|
53
|
+
export { createSessionFromAdapter as i, SessionAdapter as n, SessionOptions as r, IronSession as t };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { n as sealData, r as unsealData, t as normalizePassword } from "./crypto-WcFV83Nz.mjs";
|
|
2
|
+
import { t as PetaAuthError } from "./errors-DxJ-WUJL.mjs";
|
|
3
|
+
import { serialize } from "cookie";
|
|
4
|
+
//#region src/session.ts
|
|
5
|
+
const TIMESTAMP_SKEW_SECONDS = 60;
|
|
6
|
+
const DEFAULTS = {
|
|
7
|
+
timeToLive: 336 * 3600,
|
|
8
|
+
cookieOptions: {
|
|
9
|
+
httpOnly: true,
|
|
10
|
+
sameSite: "lax",
|
|
11
|
+
path: "/"
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
function computeMaxAge(timeToLive) {
|
|
15
|
+
if (timeToLive === 0) return 2147483647;
|
|
16
|
+
return timeToLive - TIMESTAMP_SKEW_SECONDS;
|
|
17
|
+
}
|
|
18
|
+
/** Check whether a session has any user data keys beyond the built-in methods. */
|
|
19
|
+
function sessionHasData(session, key) {
|
|
20
|
+
if (key) return !!session[key];
|
|
21
|
+
return Object.keys(session).some((k) => k !== "save" && k !== "destroy" && k !== "updateConfig");
|
|
22
|
+
}
|
|
23
|
+
/** @internal */
|
|
24
|
+
function resolveConfig(options) {
|
|
25
|
+
const timeToLive = options.timeToLive ?? DEFAULTS.timeToLive;
|
|
26
|
+
const cookieOptions = {
|
|
27
|
+
...DEFAULTS.cookieOptions,
|
|
28
|
+
secure: process.env.NODE_ENV !== "development",
|
|
29
|
+
...options.cookieOptions
|
|
30
|
+
};
|
|
31
|
+
if (!("maxAge" in (options.cookieOptions ?? {}))) cookieOptions.maxAge = computeMaxAge(timeToLive);
|
|
32
|
+
const passwordsMap = normalizePassword(options.password);
|
|
33
|
+
for (const secret of Object.values(passwordsMap)) if (secret.length < 32) throw new PetaAuthError("PASSWORD_TOO_SHORT", "peta-auth: password must be at least 32 characters");
|
|
34
|
+
return {
|
|
35
|
+
timeToLive,
|
|
36
|
+
cookieName: options.cookieName,
|
|
37
|
+
password: options.password,
|
|
38
|
+
cookieOptions
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Create a session from a framework adapter.
|
|
43
|
+
*
|
|
44
|
+
* Reads the session cookie (if present), hydrates the data, and
|
|
45
|
+
* returns a session object with {@link IronSession.save},
|
|
46
|
+
* {@link IronSession.destroy}, and {@link IronSession.updateConfig}.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const session = await createSessionFromAdapter(adapter, {
|
|
51
|
+
* password: "my-32-char-password...",
|
|
52
|
+
* cookieName: "my-session",
|
|
53
|
+
* })
|
|
54
|
+
* session.userId = 42
|
|
55
|
+
* await session.save()
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
async function createSessionFromAdapter(adapter, options) {
|
|
59
|
+
let config = resolveConfig(options);
|
|
60
|
+
const seal = adapter.getCookie(config.cookieName);
|
|
61
|
+
const session = seal ? await unsealData(seal, {
|
|
62
|
+
password: config.password,
|
|
63
|
+
ttl: config.timeToLive
|
|
64
|
+
}) : {};
|
|
65
|
+
session.save = async () => {
|
|
66
|
+
const s = await sealData(session, {
|
|
67
|
+
password: config.password,
|
|
68
|
+
ttl: config.timeToLive
|
|
69
|
+
});
|
|
70
|
+
const cookieValue = serialize(config.cookieName, s, config.cookieOptions);
|
|
71
|
+
if (cookieValue.length > 4096) throw new PetaAuthError("COOKIE_TOO_LARGE", `peta-auth: cookie too large (${cookieValue.length} bytes)`);
|
|
72
|
+
adapter.setCookie(cookieValue);
|
|
73
|
+
};
|
|
74
|
+
session.destroy = () => {
|
|
75
|
+
for (const key of Object.keys(session)) if (key !== "save" && key !== "destroy" && key !== "updateConfig") delete session[key];
|
|
76
|
+
adapter.setCookie(serialize(config.cookieName, "", {
|
|
77
|
+
...config.cookieOptions,
|
|
78
|
+
maxAge: 0
|
|
79
|
+
}));
|
|
80
|
+
};
|
|
81
|
+
session.updateConfig = (updatedOptions) => {
|
|
82
|
+
config = resolveConfig(updatedOptions);
|
|
83
|
+
};
|
|
84
|
+
return session;
|
|
85
|
+
}
|
|
86
|
+
//#endregion
|
|
87
|
+
export { sessionHasData as n, createSessionFromAdapter as t };
|