@aura-stack/auth 0.1.0-rc.9 → 0.2.0-rc.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.
- package/dist/@types/index.cjs +15 -15
- package/dist/@types/index.d.ts +7 -31
- package/dist/@types/index.js +1 -1
- package/dist/@types/router.d.cjs +1 -1
- package/dist/@types/router.d.d.ts +7 -7
- package/dist/@types/utility.cjs +15 -15
- package/dist/@types/utility.d.ts +4 -8
- package/dist/@types/utility.js +1 -1
- package/dist/actions/callback/access-token.cjs +176 -155
- package/dist/actions/callback/access-token.d.ts +15 -20
- package/dist/actions/callback/access-token.js +10 -4
- package/dist/actions/callback/callback.cjs +394 -490
- package/dist/actions/callback/callback.d.ts +10 -12
- package/dist/actions/callback/callback.js +16 -12
- package/dist/actions/callback/userinfo.cjs +166 -145
- package/dist/actions/callback/userinfo.d.ts +9 -9
- package/dist/actions/callback/userinfo.js +12 -6
- package/dist/actions/csrfToken/csrfToken.cjs +112 -190
- package/dist/actions/csrfToken/csrfToken.d.ts +3 -3
- package/dist/actions/csrfToken/csrfToken.js +12 -8
- package/dist/actions/index.cjs +652 -814
- package/dist/actions/index.d.ts +13 -13
- package/dist/actions/index.js +34 -18
- package/dist/actions/session/session.cjs +86 -176
- package/dist/actions/session/session.d.ts +3 -3
- package/dist/actions/session/session.js +10 -7
- package/dist/actions/signIn/authorization.cjs +254 -263
- package/dist/actions/signIn/authorization.d.ts +12 -18
- package/dist/actions/signIn/authorization.js +16 -6
- package/dist/actions/signIn/signIn.cjs +320 -456
- package/dist/actions/signIn/signIn.d.ts +10 -10
- package/dist/actions/signIn/signIn.js +13 -10
- package/dist/actions/signOut/signOut.cjs +304 -473
- package/dist/actions/signOut/signOut.d.ts +3 -3
- package/dist/actions/signOut/signOut.js +14 -11
- package/dist/assert.cjs +40 -36
- package/dist/assert.d.ts +13 -4
- package/dist/assert.js +12 -2
- package/dist/chunk-3EUWD5BB.js +63 -0
- package/dist/chunk-42XB3YCW.js +19 -17
- package/dist/chunk-6R2YZ4AC.js +22 -0
- package/dist/chunk-A3N4PVAT.js +70 -0
- package/dist/chunk-B737EUJV.js +22 -0
- package/dist/chunk-CXLATHS5.js +143 -0
- package/dist/chunk-E3OXBRYF.js +19 -17
- package/dist/chunk-EIL2FPSS.js +22 -0
- package/dist/chunk-EMKJA2GJ.js +89 -0
- package/dist/chunk-FIPU4MLT.js +18 -16
- package/dist/chunk-FKRDCWBF.js +19 -17
- package/dist/chunk-GA2SMTJO.js +58 -0
- package/dist/chunk-HP34YGGJ.js +22 -0
- package/dist/chunk-HT4YLL7N.js +35 -0
- package/dist/chunk-IKHPGFCW.js +11 -9
- package/dist/chunk-IUYZQTJV.js +30 -0
- package/dist/chunk-IVET23KF.js +58 -0
- package/dist/chunk-JVFTCTTE.js +33 -0
- package/dist/chunk-KRNOMBXQ.js +19 -17
- package/dist/chunk-KSWLO5ZU.js +102 -0
- package/dist/chunk-N2APGLXA.js +71 -0
- package/dist/chunk-N4SX7TZT.js +96 -0
- package/dist/chunk-RRLIF4PQ.js +55 -0
- package/dist/chunk-STHEPPUZ.js +8 -6
- package/dist/chunk-TLE4PXY3.js +39 -0
- package/dist/chunk-W6LG7BFW.js +197 -0
- package/dist/chunk-YRCB5FLE.js +79 -0
- package/dist/chunk-ZNCZVF6U.js +14 -0
- package/dist/cookie.cjs +222 -187
- package/dist/cookie.d.ts +39 -76
- package/dist/cookie.js +27 -34
- package/dist/errors.cjs +85 -0
- package/dist/errors.d.ts +49 -0
- package/dist/errors.js +18 -0
- package/dist/headers.cjs +28 -28
- package/dist/headers.d.ts +2 -2
- package/dist/headers.js +6 -2
- package/dist/index-DkaLJFn8.d.ts +679 -0
- package/dist/index.cjs +1074 -1007
- package/dist/index.d.ts +10 -31
- package/dist/index.js +85 -51
- package/dist/jose.cjs +74 -66
- package/dist/jose.d.ts +11 -8
- package/dist/jose.js +10 -5
- package/dist/oauth/bitbucket.cjs +38 -38
- package/dist/oauth/bitbucket.d.ts +7 -7
- package/dist/oauth/bitbucket.js +6 -2
- package/dist/oauth/discord.cjs +47 -48
- package/dist/oauth/discord.d.ts +7 -7
- package/dist/oauth/discord.js +6 -2
- package/dist/oauth/figma.cjs +39 -39
- package/dist/oauth/figma.d.ts +7 -7
- package/dist/oauth/figma.js +6 -2
- package/dist/oauth/github.cjs +31 -31
- package/dist/oauth/github.d.ts +7 -7
- package/dist/oauth/github.js +6 -2
- package/dist/oauth/gitlab.cjs +39 -39
- package/dist/oauth/gitlab.d.ts +7 -7
- package/dist/oauth/gitlab.js +6 -2
- package/dist/oauth/index.cjs +350 -180
- package/dist/oauth/index.d.ts +7 -26
- package/dist/oauth/index.js +51 -9
- package/dist/oauth/mailchimp.cjs +46 -0
- package/dist/oauth/mailchimp.d.ts +7 -0
- package/dist/oauth/mailchimp.js +6 -0
- package/dist/oauth/pinterest.cjs +46 -0
- package/dist/oauth/pinterest.d.ts +7 -0
- package/dist/oauth/pinterest.js +6 -0
- package/dist/oauth/spotify.cjs +39 -39
- package/dist/oauth/spotify.d.ts +7 -7
- package/dist/oauth/spotify.js +6 -2
- package/dist/oauth/strava.cjs +46 -0
- package/dist/oauth/strava.d.ts +7 -0
- package/dist/oauth/strava.js +6 -0
- package/dist/oauth/x.cjs +39 -39
- package/dist/oauth/x.d.ts +7 -7
- package/dist/oauth/x.js +6 -2
- package/dist/request.cjs +38 -0
- package/dist/request.d.ts +13 -0
- package/dist/request.js +6 -0
- package/dist/schemas.cjs +103 -97
- package/dist/schemas.d.ts +96 -126
- package/dist/schemas.js +20 -18
- package/dist/secure.cjs +106 -101
- package/dist/secure.d.ts +17 -17
- package/dist/secure.js +19 -4
- package/dist/utils.cjs +197 -135
- package/dist/utils.d.ts +20 -26
- package/dist/utils.js +25 -21
- package/package.json +8 -7
- package/dist/chunk-256KIVJL.js +0 -110
- package/dist/chunk-6SM22VVJ.js +0 -15
- package/dist/chunk-CAKJT3KS.js +0 -77
- package/dist/chunk-EBPE35JT.js +0 -29
- package/dist/chunk-FJUDBLCP.js +0 -52
- package/dist/chunk-GZU3RBTB.js +0 -51
- package/dist/chunk-HGJ4TXY4.js +0 -105
- package/dist/chunk-HMRKN75I.js +0 -74
- package/dist/chunk-JAPMIE6S.js +0 -8
- package/dist/chunk-LLR722CL.js +0 -75
- package/dist/chunk-RLT4RFKV.js +0 -36
- package/dist/chunk-SJPDVKUS.js +0 -93
- package/dist/chunk-SMQO5WD7.js +0 -20
- package/dist/chunk-UJJ7R56J.js +0 -42
- package/dist/chunk-UTDLUEEG.js +0 -25
- package/dist/chunk-VFTYH33W.js +0 -44
- package/dist/chunk-XXJKNKGQ.js +0 -27
- package/dist/chunk-ZV4BH47P.js +0 -156
- package/dist/error.cjs +0 -88
- package/dist/error.d.ts +0 -62
- package/dist/error.js +0 -9
- package/dist/index-DpfbvTZ_.d.ts +0 -597
- package/dist/response.cjs +0 -34
- package/dist/response.d.ts +0 -10
- package/dist/response.js +0 -2
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import {
|
|
2
|
+
equals,
|
|
3
|
+
formatZodError,
|
|
4
|
+
getNormalizedOriginPath,
|
|
5
|
+
sanitizeURL,
|
|
6
|
+
toCastCase
|
|
7
|
+
} from "./chunk-CXLATHS5.js";
|
|
8
|
+
import {
|
|
9
|
+
isValidURL
|
|
10
|
+
} from "./chunk-EIL2FPSS.js";
|
|
11
|
+
import {
|
|
12
|
+
AuthInternalError,
|
|
13
|
+
AuthSecurityError,
|
|
14
|
+
isAuthSecurityError
|
|
15
|
+
} from "./chunk-RRLIF4PQ.js";
|
|
16
|
+
import {
|
|
17
|
+
OAuthAuthorization
|
|
18
|
+
} from "./chunk-YRCB5FLE.js";
|
|
19
|
+
|
|
20
|
+
// src/actions/signIn/authorization.ts
|
|
21
|
+
var createAuthorizationURL = (oauthConfig, redirectURI, state, codeChallenge, codeChallengeMethod) => {
|
|
22
|
+
const parsed = OAuthAuthorization.safeParse({ ...oauthConfig, redirectURI, state, codeChallenge, codeChallengeMethod });
|
|
23
|
+
if (!parsed.success) {
|
|
24
|
+
const msg = JSON.stringify(formatZodError(parsed.error), null, 2);
|
|
25
|
+
throw new AuthInternalError("INVALID_OAUTH_CONFIGURATION", msg);
|
|
26
|
+
}
|
|
27
|
+
const { authorizeURL, ...options } = parsed.data;
|
|
28
|
+
const { userInfo, accessToken, clientSecret, ...required } = options;
|
|
29
|
+
const searchParams = new URLSearchParams(toCastCase(required));
|
|
30
|
+
return `${authorizeURL}?${searchParams}`;
|
|
31
|
+
};
|
|
32
|
+
var getOriginURL = (request, trustedProxyHeaders) => {
|
|
33
|
+
const headers = request.headers;
|
|
34
|
+
if (trustedProxyHeaders) {
|
|
35
|
+
const protocol = headers.get("X-Forwarded-Proto") ?? headers.get("Forwarded")?.match(/proto=([^;]+)/i)?.[1] ?? "http";
|
|
36
|
+
const host = headers.get("X-Forwarded-Host") ?? headers.get("Host") ?? headers.get("Forwarded")?.match(/host=([^;]+)/i)?.[1] ?? null;
|
|
37
|
+
return new URL(`${protocol}://${host}${getNormalizedOriginPath(new URL(request.url).pathname)}`);
|
|
38
|
+
} else {
|
|
39
|
+
return new URL(getNormalizedOriginPath(request.url));
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
var createRedirectURI = (request, oauth, basePath, trustedProxyHeaders) => {
|
|
43
|
+
const url = getOriginURL(request, trustedProxyHeaders);
|
|
44
|
+
return `${url.origin}${basePath}/callback/${oauth}`;
|
|
45
|
+
};
|
|
46
|
+
var createRedirectTo = (request, redirectTo, trustedProxyHeaders) => {
|
|
47
|
+
try {
|
|
48
|
+
const headers = request.headers;
|
|
49
|
+
const origin = headers.get("Origin");
|
|
50
|
+
const referer = headers.get("Referer");
|
|
51
|
+
let hostedURL = getOriginURL(request, trustedProxyHeaders);
|
|
52
|
+
if (redirectTo) {
|
|
53
|
+
if (redirectTo.startsWith("/")) {
|
|
54
|
+
return sanitizeURL(redirectTo);
|
|
55
|
+
}
|
|
56
|
+
const redirectToURL = new URL(sanitizeURL(getNormalizedOriginPath(redirectTo)));
|
|
57
|
+
if (!isValidURL(redirectTo) || !equals(redirectToURL.origin, hostedURL.origin)) {
|
|
58
|
+
throw new AuthSecurityError(
|
|
59
|
+
"POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED",
|
|
60
|
+
"The redirectTo parameter does not match the hosted origin."
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
return sanitizeURL(redirectToURL.pathname);
|
|
64
|
+
}
|
|
65
|
+
if (referer) {
|
|
66
|
+
const refererURL = new URL(sanitizeURL(referer));
|
|
67
|
+
if (!isValidURL(referer) || !equals(refererURL.origin, hostedURL.origin)) {
|
|
68
|
+
throw new AuthSecurityError(
|
|
69
|
+
"POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED",
|
|
70
|
+
"The referer of the request does not match the hosted origin."
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
return sanitizeURL(refererURL.pathname);
|
|
74
|
+
}
|
|
75
|
+
if (origin) {
|
|
76
|
+
const originURL = new URL(sanitizeURL(getNormalizedOriginPath(origin)));
|
|
77
|
+
if (!isValidURL(origin) || !equals(originURL.origin, hostedURL.origin)) {
|
|
78
|
+
throw new AuthSecurityError("POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED", "Invalid origin (potential CSRF).");
|
|
79
|
+
}
|
|
80
|
+
return sanitizeURL(originURL.pathname);
|
|
81
|
+
}
|
|
82
|
+
return "/";
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (isAuthSecurityError(error)) {
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
throw new AuthSecurityError("POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED", "Invalid origin (potential CSRF).");
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export {
|
|
92
|
+
createAuthorizationURL,
|
|
93
|
+
getOriginURL,
|
|
94
|
+
createRedirectURI,
|
|
95
|
+
createRedirectTo
|
|
96
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var OAuthProtocolError = class extends Error {
|
|
3
|
+
type = "OAUTH_PROTOCOL_ERROR";
|
|
4
|
+
error;
|
|
5
|
+
errorURI;
|
|
6
|
+
constructor(error, description, errorURI, options) {
|
|
7
|
+
super(description, options);
|
|
8
|
+
this.error = error;
|
|
9
|
+
this.errorURI = errorURI;
|
|
10
|
+
this.name = new.target.name;
|
|
11
|
+
Error.captureStackTrace(this, new.target);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var AuthInternalError = class extends Error {
|
|
15
|
+
type = "AUTH_INTERNAL_ERROR";
|
|
16
|
+
code;
|
|
17
|
+
constructor(code, message, options) {
|
|
18
|
+
super(message, options);
|
|
19
|
+
this.code = code;
|
|
20
|
+
this.name = new.target.name;
|
|
21
|
+
Error.captureStackTrace(this, new.target);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var AuthSecurityError = class extends Error {
|
|
25
|
+
type = "AUTH_SECURITY_ERROR";
|
|
26
|
+
code;
|
|
27
|
+
constructor(code, message, options) {
|
|
28
|
+
super(message, options);
|
|
29
|
+
this.code = code;
|
|
30
|
+
this.name = new.target.name;
|
|
31
|
+
Error.captureStackTrace(this, new.target);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var isNativeError = (error) => {
|
|
35
|
+
return error instanceof Error;
|
|
36
|
+
};
|
|
37
|
+
var isOAuthProtocolError = (error) => {
|
|
38
|
+
return error instanceof OAuthProtocolError;
|
|
39
|
+
};
|
|
40
|
+
var isAuthInternalError = (error) => {
|
|
41
|
+
return error instanceof AuthInternalError;
|
|
42
|
+
};
|
|
43
|
+
var isAuthSecurityError = (error) => {
|
|
44
|
+
return error instanceof AuthSecurityError;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export {
|
|
48
|
+
OAuthProtocolError,
|
|
49
|
+
AuthInternalError,
|
|
50
|
+
AuthSecurityError,
|
|
51
|
+
isNativeError,
|
|
52
|
+
isOAuthProtocolError,
|
|
53
|
+
isAuthInternalError,
|
|
54
|
+
isAuthSecurityError
|
|
55
|
+
};
|
package/dist/chunk-STHEPPUZ.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// src/headers.ts
|
|
2
2
|
var cacheControl = {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
3
|
+
"Cache-Control": "no-store",
|
|
4
|
+
Pragma: "no-cache",
|
|
5
|
+
Expires: "0",
|
|
6
|
+
Vary: "Cookie"
|
|
7
|
+
};
|
|
8
8
|
|
|
9
|
-
export {
|
|
9
|
+
export {
|
|
10
|
+
cacheControl
|
|
11
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createDerivedSalt
|
|
3
|
+
} from "./chunk-N2APGLXA.js";
|
|
4
|
+
import {
|
|
5
|
+
AuthInternalError
|
|
6
|
+
} from "./chunk-RRLIF4PQ.js";
|
|
7
|
+
|
|
8
|
+
// src/jose.ts
|
|
9
|
+
import "dotenv/config";
|
|
10
|
+
import { createJWT, createJWS, createJWE, createDeriveKey } from "@aura-stack/jose";
|
|
11
|
+
var createJoseInstance = (secret) => {
|
|
12
|
+
const env = process.env;
|
|
13
|
+
secret ??= env.AURA_AUTH_SECRET ?? env.AUTH_SECRET;
|
|
14
|
+
if (!secret) {
|
|
15
|
+
throw new AuthInternalError(
|
|
16
|
+
"JOSE_INITIALIZATION_FAILED",
|
|
17
|
+
"AURA_AUTH_SECRET environment variable is not set and no secret was provided."
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
const salt = env.AURA_AUTH_SALT ?? env.AUTH_SALT ?? createDerivedSalt(secret);
|
|
21
|
+
const { derivedKey: derivedSigningKey } = createDeriveKey(secret, salt, "signing");
|
|
22
|
+
const { derivedKey: derivedEncryptionKey } = createDeriveKey(secret, salt, "encryption");
|
|
23
|
+
const { derivedKey: derivedCsrfTokenKey } = createDeriveKey(secret, salt, "csrfToken");
|
|
24
|
+
const { decodeJWT, encodeJWT } = createJWT({ jws: derivedSigningKey, jwe: derivedEncryptionKey });
|
|
25
|
+
const { signJWS, verifyJWS } = createJWS(derivedCsrfTokenKey);
|
|
26
|
+
const { encryptJWE, decryptJWE } = createJWE(derivedEncryptionKey);
|
|
27
|
+
return {
|
|
28
|
+
decodeJWT,
|
|
29
|
+
encodeJWT,
|
|
30
|
+
signJWS,
|
|
31
|
+
verifyJWS,
|
|
32
|
+
encryptJWE,
|
|
33
|
+
decryptJWE
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export {
|
|
38
|
+
createJoseInstance
|
|
39
|
+
};
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AuthInternalError
|
|
3
|
+
} from "./chunk-RRLIF4PQ.js";
|
|
4
|
+
|
|
5
|
+
// src/cookie.ts
|
|
6
|
+
import { parse, parseSetCookie, serialize } from "@aura-stack/router/cookie";
|
|
7
|
+
var COOKIE_NAME = "aura-auth";
|
|
8
|
+
var defaultCookieOptions = {
|
|
9
|
+
httpOnly: true,
|
|
10
|
+
sameSite: "lax",
|
|
11
|
+
path: "/",
|
|
12
|
+
maxAge: 60 * 60 * 24 * 15
|
|
13
|
+
};
|
|
14
|
+
var defaultStandardCookieConfig = {
|
|
15
|
+
secure: false,
|
|
16
|
+
httpOnly: true
|
|
17
|
+
};
|
|
18
|
+
var defaultSecureCookieConfig = {
|
|
19
|
+
secure: true,
|
|
20
|
+
httpOnly: true
|
|
21
|
+
};
|
|
22
|
+
var defaultHostCookieConfig = {
|
|
23
|
+
secure: true,
|
|
24
|
+
httpOnly: true,
|
|
25
|
+
path: "/",
|
|
26
|
+
domain: void 0
|
|
27
|
+
};
|
|
28
|
+
var oauthCookieOptions = {
|
|
29
|
+
httpOnly: true,
|
|
30
|
+
maxAge: 5 * 60,
|
|
31
|
+
sameSite: "lax",
|
|
32
|
+
expires: new Date(Date.now() + 5 * 60 * 1e3)
|
|
33
|
+
};
|
|
34
|
+
var setCookie = (cookieName, value, options) => {
|
|
35
|
+
return serialize(cookieName, value, options);
|
|
36
|
+
};
|
|
37
|
+
var expiredCookieAttributes = {
|
|
38
|
+
...defaultCookieOptions,
|
|
39
|
+
expires: /* @__PURE__ */ new Date(0),
|
|
40
|
+
maxAge: 0
|
|
41
|
+
};
|
|
42
|
+
var getCookie = (request, cookieName) => {
|
|
43
|
+
const cookies = request.headers.get("Cookie");
|
|
44
|
+
if (!cookies) {
|
|
45
|
+
throw new AuthInternalError("COOKIE_NOT_FOUND", "No cookies found. There is no active session");
|
|
46
|
+
}
|
|
47
|
+
const value = parse(cookies)[cookieName];
|
|
48
|
+
if (!value) {
|
|
49
|
+
throw new AuthInternalError("COOKIE_NOT_FOUND", `Cookie "${cookieName}" not found. There is no active session`);
|
|
50
|
+
}
|
|
51
|
+
return value;
|
|
52
|
+
};
|
|
53
|
+
var getSetCookie = (response, cookieName) => {
|
|
54
|
+
const cookies = response.headers.getSetCookie();
|
|
55
|
+
if (!cookies) {
|
|
56
|
+
throw new AuthInternalError("COOKIE_NOT_FOUND", "No cookies found in response.");
|
|
57
|
+
}
|
|
58
|
+
const strCookie = cookies.find((cookie) => cookie.startsWith(`${cookieName}=`));
|
|
59
|
+
if (!strCookie) {
|
|
60
|
+
throw new AuthInternalError("COOKIE_NOT_FOUND", `Cookie "${cookieName}" not found in response.`);
|
|
61
|
+
}
|
|
62
|
+
return parseSetCookie(strCookie).value;
|
|
63
|
+
};
|
|
64
|
+
var createSessionCookie = async (jose, session) => {
|
|
65
|
+
try {
|
|
66
|
+
const encoded = await jose.encodeJWT(session);
|
|
67
|
+
return encoded;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
throw new AuthInternalError("INVALID_JWT_TOKEN", "Failed to create session cookie", { cause: error });
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
var defineSecureCookieOptions = (useSecure, attributes, strategy) => {
|
|
73
|
+
if (!attributes.httpOnly) {
|
|
74
|
+
console.warn(
|
|
75
|
+
"[WARNING]: Cookie is configured without HttpOnly. This allows JavaScript access via document.cookie and increases XSS risk."
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
if (attributes.domain === "*") {
|
|
79
|
+
attributes.domain = void 0;
|
|
80
|
+
console.warn("[WARNING]: Cookie 'Domain' is set to '*', which is insecure. Avoid wildcard domains.");
|
|
81
|
+
}
|
|
82
|
+
if (!useSecure) {
|
|
83
|
+
if (attributes.secure) {
|
|
84
|
+
console.warn(
|
|
85
|
+
"[WARNING]: The 'Secure' attribute will be disabled for this cookie. Serve over HTTPS to enforce Secure cookies."
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
if (attributes.sameSite == "none") {
|
|
89
|
+
attributes.sameSite = "lax";
|
|
90
|
+
console.warn("[WARNING]: SameSite=None requires Secure attribute. Changing SameSite to 'Lax'.");
|
|
91
|
+
}
|
|
92
|
+
if (process.env.NODE_ENV === "production") {
|
|
93
|
+
console.warn("[WARNING]: In production, ensure cookies are served over HTTPS to maintain security.");
|
|
94
|
+
}
|
|
95
|
+
if (strategy === "host") {
|
|
96
|
+
console.warn("[WARNING]: __Host- cookies require a secure context. Falling back to standard cookie settings.");
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
...defaultCookieOptions,
|
|
100
|
+
...attributes,
|
|
101
|
+
...defaultStandardCookieConfig
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
return strategy === "host" ? {
|
|
105
|
+
...defaultCookieOptions,
|
|
106
|
+
...attributes,
|
|
107
|
+
...defaultHostCookieConfig
|
|
108
|
+
} : { ...defaultCookieOptions, ...attributes, ...defaultSecureCookieConfig };
|
|
109
|
+
};
|
|
110
|
+
var createCookieStore = (useSecure, prefix, overrides) => {
|
|
111
|
+
prefix ??= COOKIE_NAME;
|
|
112
|
+
const securePrefix = useSecure ? "__Secure-" : "";
|
|
113
|
+
const hostPrefix = useSecure ? "__Host-" : "";
|
|
114
|
+
return {
|
|
115
|
+
sessionToken: {
|
|
116
|
+
name: `${securePrefix}${prefix}.${overrides?.sessionToken?.name ?? "session_token"}`,
|
|
117
|
+
attributes: defineSecureCookieOptions(
|
|
118
|
+
useSecure,
|
|
119
|
+
{
|
|
120
|
+
...defaultCookieOptions,
|
|
121
|
+
...overrides?.sessionToken?.attributes
|
|
122
|
+
},
|
|
123
|
+
overrides?.sessionToken?.attributes?.strategy ?? "secure"
|
|
124
|
+
)
|
|
125
|
+
},
|
|
126
|
+
state: {
|
|
127
|
+
name: `${securePrefix}${prefix}.${overrides?.state?.name ?? "state"}`,
|
|
128
|
+
attributes: defineSecureCookieOptions(
|
|
129
|
+
useSecure,
|
|
130
|
+
{
|
|
131
|
+
...oauthCookieOptions,
|
|
132
|
+
...overrides?.state?.attributes
|
|
133
|
+
},
|
|
134
|
+
overrides?.state?.attributes?.strategy ?? "secure"
|
|
135
|
+
)
|
|
136
|
+
},
|
|
137
|
+
csrfToken: {
|
|
138
|
+
name: `${hostPrefix}${prefix}.${overrides?.csrfToken?.name ?? "csrf_token"}`,
|
|
139
|
+
attributes: defineSecureCookieOptions(
|
|
140
|
+
useSecure,
|
|
141
|
+
{
|
|
142
|
+
...overrides?.csrfToken?.attributes,
|
|
143
|
+
...defaultHostCookieConfig
|
|
144
|
+
},
|
|
145
|
+
overrides?.csrfToken?.attributes?.strategy ?? "host"
|
|
146
|
+
)
|
|
147
|
+
},
|
|
148
|
+
redirectTo: {
|
|
149
|
+
name: `${securePrefix}${prefix}.${overrides?.redirectTo?.name ?? "redirect_to"}`,
|
|
150
|
+
attributes: defineSecureCookieOptions(
|
|
151
|
+
useSecure,
|
|
152
|
+
{
|
|
153
|
+
...oauthCookieOptions,
|
|
154
|
+
...overrides?.redirectTo?.attributes
|
|
155
|
+
},
|
|
156
|
+
overrides?.redirectTo?.attributes?.strategy ?? "secure"
|
|
157
|
+
)
|
|
158
|
+
},
|
|
159
|
+
redirectURI: {
|
|
160
|
+
name: `${securePrefix}${prefix}.${overrides?.redirectURI?.name ?? "redirect_uri"}`,
|
|
161
|
+
attributes: defineSecureCookieOptions(
|
|
162
|
+
useSecure,
|
|
163
|
+
{
|
|
164
|
+
...oauthCookieOptions,
|
|
165
|
+
...overrides?.redirectURI?.attributes
|
|
166
|
+
},
|
|
167
|
+
overrides?.redirectURI?.attributes?.strategy ?? "secure"
|
|
168
|
+
)
|
|
169
|
+
},
|
|
170
|
+
codeVerifier: {
|
|
171
|
+
name: `${securePrefix}${prefix}.${overrides?.codeVerifier?.name ?? "code_verifier"}`,
|
|
172
|
+
attributes: defineSecureCookieOptions(
|
|
173
|
+
useSecure,
|
|
174
|
+
{
|
|
175
|
+
...oauthCookieOptions,
|
|
176
|
+
...overrides?.codeVerifier?.attributes
|
|
177
|
+
},
|
|
178
|
+
overrides?.codeVerifier?.attributes?.strategy ?? "secure"
|
|
179
|
+
)
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
export {
|
|
185
|
+
COOKIE_NAME,
|
|
186
|
+
defaultCookieOptions,
|
|
187
|
+
defaultStandardCookieConfig,
|
|
188
|
+
defaultSecureCookieConfig,
|
|
189
|
+
defaultHostCookieConfig,
|
|
190
|
+
setCookie,
|
|
191
|
+
expiredCookieAttributes,
|
|
192
|
+
getCookie,
|
|
193
|
+
getSetCookie,
|
|
194
|
+
createSessionCookie,
|
|
195
|
+
defineSecureCookieOptions,
|
|
196
|
+
createCookieStore
|
|
197
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// src/schemas.ts
|
|
2
|
+
import { object, string, enum as options, number, z, null as nullable } from "zod";
|
|
3
|
+
var OAuthProviderConfigSchema = object({
|
|
4
|
+
authorizeURL: string().url(),
|
|
5
|
+
accessToken: string().url(),
|
|
6
|
+
scope: string().optional(),
|
|
7
|
+
userInfo: string().url(),
|
|
8
|
+
responseType: options(["code", "token", "id_token"]),
|
|
9
|
+
clientId: string(),
|
|
10
|
+
clientSecret: string()
|
|
11
|
+
});
|
|
12
|
+
var OAuthAuthorization = OAuthProviderConfigSchema.extend({
|
|
13
|
+
redirectURI: string(),
|
|
14
|
+
state: string(),
|
|
15
|
+
codeChallenge: string(),
|
|
16
|
+
codeChallengeMethod: options(["plain", "S256"])
|
|
17
|
+
});
|
|
18
|
+
var OAuthAuthorizationResponse = object({
|
|
19
|
+
state: string({ message: "Missing state parameter in the OAuth authorization response." }),
|
|
20
|
+
code: string({ message: "Missing code parameter in the OAuth authorization response." })
|
|
21
|
+
});
|
|
22
|
+
var OAuthAuthorizationErrorResponse = object({
|
|
23
|
+
error: options([
|
|
24
|
+
"invalid_request",
|
|
25
|
+
"unauthorized_client",
|
|
26
|
+
"access_denied",
|
|
27
|
+
"unsupported_response_type",
|
|
28
|
+
"invalid_scope",
|
|
29
|
+
"server_error",
|
|
30
|
+
"temporarily_unavailable"
|
|
31
|
+
]),
|
|
32
|
+
error_description: string().optional(),
|
|
33
|
+
error_uri: string().optional(),
|
|
34
|
+
state: string()
|
|
35
|
+
});
|
|
36
|
+
var OAuthAccessToken = OAuthProviderConfigSchema.extend({
|
|
37
|
+
redirectURI: string(),
|
|
38
|
+
code: string(),
|
|
39
|
+
codeVerifier: string().min(43).max(128)
|
|
40
|
+
});
|
|
41
|
+
var OAuthAccessTokenResponse = object({
|
|
42
|
+
access_token: string(),
|
|
43
|
+
token_type: string().optional(),
|
|
44
|
+
expires_in: number().optional(),
|
|
45
|
+
refresh_token: string().optional(),
|
|
46
|
+
scope: string().optional().or(nullable())
|
|
47
|
+
});
|
|
48
|
+
var OAuthAccessTokenErrorResponse = object({
|
|
49
|
+
error: options([
|
|
50
|
+
"invalid_request",
|
|
51
|
+
"invalid_client",
|
|
52
|
+
"invalid_grant",
|
|
53
|
+
"unauthorized_client",
|
|
54
|
+
"unsupported_grant_type",
|
|
55
|
+
"invalid_scope"
|
|
56
|
+
]),
|
|
57
|
+
error_description: string().optional(),
|
|
58
|
+
error_uri: string().optional()
|
|
59
|
+
});
|
|
60
|
+
var OAuthErrorResponse = object({
|
|
61
|
+
error: string(),
|
|
62
|
+
error_description: string().optional()
|
|
63
|
+
});
|
|
64
|
+
var OAuthEnvSchema = object({
|
|
65
|
+
clientId: z.string().min(1, "OAuth Client ID is required in the environment variables."),
|
|
66
|
+
clientSecret: z.string().min(1, "OAuth Client Secret is required in the environment variables.")
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
export {
|
|
70
|
+
OAuthProviderConfigSchema,
|
|
71
|
+
OAuthAuthorization,
|
|
72
|
+
OAuthAuthorizationResponse,
|
|
73
|
+
OAuthAuthorizationErrorResponse,
|
|
74
|
+
OAuthAccessToken,
|
|
75
|
+
OAuthAccessTokenResponse,
|
|
76
|
+
OAuthAccessTokenErrorResponse,
|
|
77
|
+
OAuthErrorResponse,
|
|
78
|
+
OAuthEnvSchema
|
|
79
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/request.ts
|
|
2
|
+
var fetchAsync = async (url, options = {}, timeout = 5e3) => {
|
|
3
|
+
const controller = new AbortController();
|
|
4
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
5
|
+
const response = await fetch(url, {
|
|
6
|
+
...options,
|
|
7
|
+
signal: controller.signal
|
|
8
|
+
}).finally(() => clearTimeout(timeoutId));
|
|
9
|
+
return response;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
fetchAsync
|
|
14
|
+
};
|