@warlock.js/auth 5.12.0 → 5.14.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/CHANGELOG.md +30 -0
- package/README.md +88 -86
- package/esm/commands/auth-cleanup-command.mjs +1 -1
- package/esm/commands/auth-cleanup-command.mjs.map +1 -1
- package/esm/contracts/index.d.mts +2 -1
- package/esm/contracts/providers.d.mts +101 -0
- package/esm/contracts/providers.d.mts.map +1 -0
- package/esm/contracts/types.d.mts +94 -1
- package/esm/contracts/types.d.mts.map +1 -1
- package/esm/contracts/types.mjs.map +1 -1
- package/esm/errors/auth-provider-sdk-missing.error.d.mts +17 -0
- package/esm/errors/auth-provider-sdk-missing.error.d.mts.map +1 -0
- package/esm/errors/auth-provider-sdk-missing.error.mjs +22 -0
- package/esm/errors/auth-provider-sdk-missing.error.mjs.map +1 -0
- package/esm/errors/email-not-verified.error.d.mts +16 -0
- package/esm/errors/email-not-verified.error.d.mts.map +1 -0
- package/esm/errors/email-not-verified.error.mjs +20 -0
- package/esm/errors/email-not-verified.error.mjs.map +1 -0
- package/esm/errors/index.d.mts +7 -0
- package/esm/errors/index.mjs +9 -0
- package/esm/errors/invalid-one-time-token.error.d.mts +17 -0
- package/esm/errors/invalid-one-time-token.error.d.mts.map +1 -0
- package/esm/errors/invalid-one-time-token.error.mjs +21 -0
- package/esm/errors/invalid-one-time-token.error.mjs.map +1 -0
- package/esm/errors/invalid-passkey.error.d.mts +18 -0
- package/esm/errors/invalid-passkey.error.d.mts.map +1 -0
- package/esm/errors/invalid-passkey.error.mjs +23 -0
- package/esm/errors/invalid-passkey.error.mjs.map +1 -0
- package/esm/errors/invalid-provider-callback.error.d.mts +18 -0
- package/esm/errors/invalid-provider-callback.error.d.mts.map +1 -0
- package/esm/errors/invalid-provider-callback.error.mjs +23 -0
- package/esm/errors/invalid-provider-callback.error.mjs.map +1 -0
- package/esm/errors/notifications-unavailable.error.d.mts +15 -0
- package/esm/errors/notifications-unavailable.error.d.mts.map +1 -0
- package/esm/errors/notifications-unavailable.error.mjs +20 -0
- package/esm/errors/notifications-unavailable.error.mjs.map +1 -0
- package/esm/errors/provider-email-not-verified.error.d.mts +16 -0
- package/esm/errors/provider-email-not-verified.error.d.mts.map +1 -0
- package/esm/errors/provider-email-not-verified.error.mjs +20 -0
- package/esm/errors/provider-email-not-verified.error.mjs.map +1 -0
- package/esm/index.d.mts +27 -3
- package/esm/index.mjs +27 -1
- package/esm/middleware/index.d.mts +4 -1
- package/esm/middleware/index.mjs +3 -0
- package/esm/middleware/one-time-token-throttle.middleware.d.mts +36 -0
- package/esm/middleware/one-time-token-throttle.middleware.d.mts.map +1 -0
- package/esm/middleware/one-time-token-throttle.middleware.mjs +53 -0
- package/esm/middleware/one-time-token-throttle.middleware.mjs.map +1 -0
- package/esm/middleware/otp-throttle.middleware.d.mts +30 -0
- package/esm/middleware/otp-throttle.middleware.d.mts.map +1 -0
- package/esm/middleware/otp-throttle.middleware.mjs +45 -0
- package/esm/middleware/otp-throttle.middleware.mjs.map +1 -0
- package/esm/middleware/require-verified-email.middleware.d.mts +18 -0
- package/esm/middleware/require-verified-email.middleware.d.mts.map +1 -0
- package/esm/middleware/require-verified-email.middleware.mjs +24 -0
- package/esm/middleware/require-verified-email.middleware.mjs.map +1 -0
- package/esm/models/access-token/access-token.model.d.mts +8 -0
- package/esm/models/access-token/access-token.model.d.mts.map +1 -1
- package/esm/models/access-token/access-token.model.mjs +3 -0
- package/esm/models/access-token/access-token.model.mjs.map +1 -1
- package/esm/models/index.d.mts +3 -1
- package/esm/models/index.d.mts.map +1 -1
- package/esm/models/index.mjs +16 -1
- package/esm/models/index.mjs.map +1 -1
- package/esm/models/one-time-token/index.d.mts +1 -0
- package/esm/models/one-time-token/index.mjs +3 -0
- package/esm/models/one-time-token/migration.mjs +31 -0
- package/esm/models/one-time-token/migration.mjs.map +1 -0
- package/esm/models/one-time-token/one-time-token.model.d.mts +173 -0
- package/esm/models/one-time-token/one-time-token.model.d.mts.map +1 -0
- package/esm/models/one-time-token/one-time-token.model.mjs +151 -0
- package/esm/models/one-time-token/one-time-token.model.mjs.map +1 -0
- package/esm/models/passkey-credential/index.d.mts +1 -0
- package/esm/models/passkey-credential/index.mjs +3 -0
- package/esm/models/passkey-credential/migration.mjs +25 -0
- package/esm/models/passkey-credential/migration.mjs.map +1 -0
- package/esm/models/passkey-credential/passkey-credential.model.d.mts +108 -0
- package/esm/models/passkey-credential/passkey-credential.model.d.mts.map +1 -0
- package/esm/models/passkey-credential/passkey-credential.model.mjs +70 -0
- package/esm/models/passkey-credential/passkey-credential.model.mjs.map +1 -0
- package/esm/models/provider-account/index.d.mts +1 -0
- package/esm/models/provider-account/index.mjs +3 -0
- package/esm/models/provider-account/migration.mjs +25 -0
- package/esm/models/provider-account/migration.mjs.map +1 -0
- package/esm/models/provider-account/provider-account.model.d.mts +75 -0
- package/esm/models/provider-account/provider-account.model.d.mts.map +1 -0
- package/esm/models/provider-account/provider-account.model.mjs +55 -0
- package/esm/models/provider-account/provider-account.model.mjs.map +1 -0
- package/esm/models/refresh-token/refresh-token.model.d.mts +7 -0
- package/esm/models/refresh-token/refresh-token.model.d.mts.map +1 -1
- package/esm/models/refresh-token/refresh-token.model.mjs +3 -0
- package/esm/models/refresh-token/refresh-token.model.mjs.map +1 -1
- package/esm/otp/index.d.mts +1 -0
- package/esm/otp/index.mjs +3 -0
- package/esm/otp/otp.d.mts +38 -0
- package/esm/otp/otp.d.mts.map +1 -0
- package/esm/otp/otp.mjs +105 -0
- package/esm/otp/otp.mjs.map +1 -0
- package/esm/passkeys/index.d.mts +3 -0
- package/esm/passkeys/index.mjs +4 -0
- package/esm/passkeys/passkey-authentication.d.mts +33 -0
- package/esm/passkeys/passkey-authentication.d.mts.map +1 -0
- package/esm/passkeys/passkey-authentication.mjs +93 -0
- package/esm/passkeys/passkey-authentication.mjs.map +1 -0
- package/esm/passkeys/passkey-challenges.mjs +53 -0
- package/esm/passkeys/passkey-challenges.mjs.map +1 -0
- package/esm/passkeys/passkey-registration.d.mts +29 -0
- package/esm/passkeys/passkey-registration.d.mts.map +1 -0
- package/esm/passkeys/passkey-registration.mjs +88 -0
- package/esm/passkeys/passkey-registration.mjs.map +1 -0
- package/esm/passkeys/simplewebauthn.d.mts +21 -0
- package/esm/passkeys/simplewebauthn.d.mts.map +1 -0
- package/esm/passkeys/simplewebauthn.mjs +13 -0
- package/esm/passkeys/simplewebauthn.mjs.map +1 -0
- package/esm/providers/google-provider.d.mts +40 -0
- package/esm/providers/google-provider.d.mts.map +1 -0
- package/esm/providers/google-provider.mjs +105 -0
- package/esm/providers/google-provider.mjs.map +1 -0
- package/esm/providers/index.d.mts +2 -0
- package/esm/providers/index.mjs +4 -0
- package/esm/providers/provider-login.d.mts +43 -0
- package/esm/providers/provider-login.d.mts.map +1 -0
- package/esm/providers/provider-login.mjs +86 -0
- package/esm/providers/provider-login.mjs.map +1 -0
- package/esm/providers/provider-state-cookie.mjs +63 -0
- package/esm/providers/provider-state-cookie.mjs.map +1 -0
- package/esm/providers/resolve-provider-user.mjs +45 -0
- package/esm/providers/resolve-provider-user.mjs.map +1 -0
- package/esm/services/auth-config.mjs +72 -0
- package/esm/services/auth-config.mjs.map +1 -1
- package/esm/services/auth.service.d.mts +18 -1
- package/esm/services/auth.service.d.mts.map +1 -1
- package/esm/services/auth.service.mjs +28 -1
- package/esm/services/auth.service.mjs.map +1 -1
- package/esm/services/email-verification.d.mts +35 -0
- package/esm/services/email-verification.d.mts.map +1 -0
- package/esm/services/email-verification.mjs +55 -0
- package/esm/services/email-verification.mjs.map +1 -0
- package/esm/services/index.d.mts +4 -1
- package/esm/services/index.mjs +3 -0
- package/esm/services/one-time-tokens.d.mts +32 -0
- package/esm/services/one-time-tokens.d.mts.map +1 -0
- package/esm/services/one-time-tokens.mjs +56 -0
- package/esm/services/one-time-tokens.mjs.map +1 -0
- package/esm/services/optional-peer.mjs +37 -0
- package/esm/services/optional-peer.mjs.map +1 -0
- package/esm/services/password-reset.d.mts +34 -0
- package/esm/services/password-reset.d.mts.map +1 -0
- package/esm/services/password-reset.mjs +81 -0
- package/esm/services/password-reset.mjs.map +1 -0
- package/esm/services/token-notifications.mjs +87 -0
- package/esm/services/token-notifications.mjs.map +1 -0
- package/esm/utils/auth-error-codes.d.mts +39 -1
- package/esm/utils/auth-error-codes.d.mts.map +1 -1
- package/esm/utils/auth-error-codes.mjs +33 -0
- package/esm/utils/auth-error-codes.mjs.map +1 -1
- package/esm/utils/index.mjs +1 -0
- package/esm/utils/safe-equal.d.mts +6 -0
- package/esm/utils/safe-equal.d.mts.map +1 -0
- package/esm/utils/safe-equal.mjs +13 -0
- package/esm/utils/safe-equal.mjs.map +1 -0
- package/llms-full.txt +313 -10
- package/llms.txt +2 -0
- package/package.json +21 -7
- package/skills/login-with-providers/SKILL.md +154 -0
- package/skills/manage-tokens/SKILL.md +7 -1
- package/skills/register-user/SKILL.md +6 -8
- package/skills/run-auth-commands/SKILL.md +1 -1
- package/skills/verify-email-and-reset-password/SKILL.md +137 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { InvalidProviderCallbackError } from "../errors/invalid-provider-callback.error.mjs";
|
|
2
|
+
import { loadOptionalPeer } from "../services/optional-peer.mjs";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
|
|
5
|
+
//#region ../auth/src/providers/google-provider.ts
|
|
6
|
+
const AUTHORIZATION_ENDPOINT = "https://accounts.google.com/o/oauth2/v2/auth";
|
|
7
|
+
const TOKEN_ENDPOINT = "https://oauth2.googleapis.com/token";
|
|
8
|
+
const JWKS_URI = "https://www.googleapis.com/oauth2/v3/certs";
|
|
9
|
+
const ISSUERS = ["https://accounts.google.com", "accounts.google.com"];
|
|
10
|
+
const DEFAULT_SCOPES = [
|
|
11
|
+
"openid",
|
|
12
|
+
"email",
|
|
13
|
+
"profile"
|
|
14
|
+
];
|
|
15
|
+
/** The `warlock add` feature that installs `jose`. */
|
|
16
|
+
const GOOGLE_AUTH_FEATURE = "auth-google";
|
|
17
|
+
/** Google's key set, created once per process; `jose` caches the fetched JWKS inside it. */
|
|
18
|
+
let googleKeySet;
|
|
19
|
+
/** PKCE S256 code challenge for a verifier. */
|
|
20
|
+
function pkceChallenge(codeVerifier) {
|
|
21
|
+
return createHash("sha256").update(codeVerifier).digest("base64url");
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Google sign-in: OpenID Connect authorization code flow with PKCE (S256),
|
|
25
|
+
* `state` and `nonce`. The code is exchanged with plain `fetch`; the id_token
|
|
26
|
+
* is verified with `jose` against Google's JWKS (signature, `iss`, `aud` = the
|
|
27
|
+
* client id, `exp`), then its `nonce` is compared. No Google SDK.
|
|
28
|
+
*/
|
|
29
|
+
var GoogleProvider = class {
|
|
30
|
+
constructor(settings) {
|
|
31
|
+
this.settings = settings;
|
|
32
|
+
this.name = "google";
|
|
33
|
+
}
|
|
34
|
+
authorizationUrl(state) {
|
|
35
|
+
const url = new URL(AUTHORIZATION_ENDPOINT);
|
|
36
|
+
url.searchParams.set("client_id", this.settings.clientId);
|
|
37
|
+
url.searchParams.set("redirect_uri", this.settings.redirectUri);
|
|
38
|
+
url.searchParams.set("response_type", "code");
|
|
39
|
+
url.searchParams.set("scope", (this.settings.scopes ?? DEFAULT_SCOPES).join(" "));
|
|
40
|
+
url.searchParams.set("state", state.state);
|
|
41
|
+
url.searchParams.set("nonce", state.nonce);
|
|
42
|
+
url.searchParams.set("code_challenge", pkceChallenge(state.codeVerifier));
|
|
43
|
+
url.searchParams.set("code_challenge_method", "S256");
|
|
44
|
+
return url.toString();
|
|
45
|
+
}
|
|
46
|
+
async handleCallback({ query, expected }) {
|
|
47
|
+
const jose = await loadOptionalPeer("jose", GOOGLE_AUTH_FEATURE);
|
|
48
|
+
const code = query.code;
|
|
49
|
+
if (typeof code !== "string" || code.length === 0) throw new InvalidProviderCallbackError("missing-code");
|
|
50
|
+
const idToken = await this.exchangeCode(code, expected.codeVerifier);
|
|
51
|
+
const claims = await this.verifyIdToken(jose, idToken);
|
|
52
|
+
if (typeof claims.nonce !== "string" || claims.nonce !== expected.nonce) throw new InvalidProviderCallbackError("nonce-mismatch");
|
|
53
|
+
if (typeof claims.sub !== "string" || claims.sub.length === 0) throw new InvalidProviderCallbackError("missing-subject");
|
|
54
|
+
return {
|
|
55
|
+
provider: this.name,
|
|
56
|
+
providerUserId: claims.sub,
|
|
57
|
+
email: typeof claims.email === "string" ? claims.email : void 0,
|
|
58
|
+
emailVerified: claims.email_verified === true,
|
|
59
|
+
name: typeof claims.name === "string" ? claims.name : void 0,
|
|
60
|
+
avatar: typeof claims.picture === "string" ? claims.picture : void 0,
|
|
61
|
+
raw: claims
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/** Redeem the authorization code (with the PKCE verifier) for an id_token. */
|
|
65
|
+
async exchangeCode(code, codeVerifier) {
|
|
66
|
+
let body;
|
|
67
|
+
try {
|
|
68
|
+
const response = await fetch(TOKEN_ENDPOINT, {
|
|
69
|
+
method: "POST",
|
|
70
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
71
|
+
body: new URLSearchParams({
|
|
72
|
+
code,
|
|
73
|
+
client_id: this.settings.clientId,
|
|
74
|
+
client_secret: this.settings.clientSecret,
|
|
75
|
+
redirect_uri: this.settings.redirectUri,
|
|
76
|
+
grant_type: "authorization_code",
|
|
77
|
+
code_verifier: codeVerifier
|
|
78
|
+
})
|
|
79
|
+
});
|
|
80
|
+
if (!response.ok) throw new Error(`Google token endpoint answered ${response.status}`);
|
|
81
|
+
body = await response.json();
|
|
82
|
+
} catch (error) {
|
|
83
|
+
throw new InvalidProviderCallbackError("code-exchange-failed", error);
|
|
84
|
+
}
|
|
85
|
+
if (typeof body.id_token !== "string") throw new InvalidProviderCallbackError("missing-id-token");
|
|
86
|
+
return body.id_token;
|
|
87
|
+
}
|
|
88
|
+
/** Verify signature, issuer, audience and expiry; resolve the claims. */
|
|
89
|
+
async verifyIdToken(jose, idToken) {
|
|
90
|
+
googleKeySet ??= jose.createRemoteJWKSet(new URL(JWKS_URI));
|
|
91
|
+
try {
|
|
92
|
+
const { payload } = await jose.jwtVerify(idToken, googleKeySet, {
|
|
93
|
+
issuer: ISSUERS,
|
|
94
|
+
audience: this.settings.clientId
|
|
95
|
+
});
|
|
96
|
+
return payload;
|
|
97
|
+
} catch (error) {
|
|
98
|
+
throw new InvalidProviderCallbackError("invalid-id-token", error);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
//#endregion
|
|
104
|
+
export { GOOGLE_AUTH_FEATURE, GoogleProvider, pkceChallenge };
|
|
105
|
+
//# sourceMappingURL=google-provider.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google-provider.mjs","names":[],"sources":["../../../../../../../auth/src/providers/google-provider.ts"],"sourcesContent":["import { createHash } from \"node:crypto\";\nimport type {\n AuthProvider,\n GoogleProviderConfig,\n ProviderAuthorizationState,\n ProviderCallbackParams,\n ProviderProfile,\n} from \"../contracts/providers\";\nimport { InvalidProviderCallbackError } from \"../errors/invalid-provider-callback.error\";\nimport { loadOptionalPeer } from \"../services/optional-peer\";\n\nconst AUTHORIZATION_ENDPOINT = \"https://accounts.google.com/o/oauth2/v2/auth\";\nconst TOKEN_ENDPOINT = \"https://oauth2.googleapis.com/token\";\nconst JWKS_URI = \"https://www.googleapis.com/oauth2/v3/certs\";\nconst ISSUERS = [\"https://accounts.google.com\", \"accounts.google.com\"];\nconst DEFAULT_SCOPES = [\"openid\", \"email\", \"profile\"];\n\n/** The `warlock add` feature that installs `jose`. */\nexport const GOOGLE_AUTH_FEATURE = \"auth-google\";\n\n/** The slice of `jose` the Google provider uses — declared locally because `jose` is an optional peer. */\nexport type JoseModule = {\n createRemoteJWKSet: (url: URL) => unknown;\n jwtVerify: (\n token: string,\n key: unknown,\n options: { issuer: string[]; audience: string },\n ) => Promise<{ payload: Record<string, unknown> }>;\n};\n\n/** Google's key set, created once per process; `jose` caches the fetched JWKS inside it. */\nlet googleKeySet: unknown;\n\n/** PKCE S256 code challenge for a verifier. */\nexport function pkceChallenge(codeVerifier: string): string {\n return createHash(\"sha256\").update(codeVerifier).digest(\"base64url\");\n}\n\n/**\n * Google sign-in: OpenID Connect authorization code flow with PKCE (S256),\n * `state` and `nonce`. The code is exchanged with plain `fetch`; the id_token\n * is verified with `jose` against Google's JWKS (signature, `iss`, `aud` = the\n * client id, `exp`), then its `nonce` is compared. No Google SDK.\n */\nexport class GoogleProvider implements AuthProvider {\n public readonly name = \"google\";\n\n public constructor(private readonly settings: GoogleProviderConfig) {}\n\n public authorizationUrl(state: ProviderAuthorizationState): string {\n const url = new URL(AUTHORIZATION_ENDPOINT);\n\n url.searchParams.set(\"client_id\", this.settings.clientId);\n url.searchParams.set(\"redirect_uri\", this.settings.redirectUri);\n url.searchParams.set(\"response_type\", \"code\");\n url.searchParams.set(\"scope\", (this.settings.scopes ?? DEFAULT_SCOPES).join(\" \"));\n url.searchParams.set(\"state\", state.state);\n url.searchParams.set(\"nonce\", state.nonce);\n url.searchParams.set(\"code_challenge\", pkceChallenge(state.codeVerifier));\n url.searchParams.set(\"code_challenge_method\", \"S256\");\n\n return url.toString();\n }\n\n public async handleCallback({\n query,\n expected,\n }: ProviderCallbackParams): Promise<ProviderProfile> {\n // Load the peer first: a missing SDK fails loudly before any network call.\n const jose = await loadOptionalPeer<JoseModule>(\"jose\", GOOGLE_AUTH_FEATURE);\n\n const code = query.code;\n\n if (typeof code !== \"string\" || code.length === 0) {\n throw new InvalidProviderCallbackError(\"missing-code\");\n }\n\n const idToken = await this.exchangeCode(code, expected.codeVerifier);\n const claims = await this.verifyIdToken(jose, idToken);\n\n if (typeof claims.nonce !== \"string\" || claims.nonce !== expected.nonce) {\n throw new InvalidProviderCallbackError(\"nonce-mismatch\");\n }\n\n if (typeof claims.sub !== \"string\" || claims.sub.length === 0) {\n throw new InvalidProviderCallbackError(\"missing-subject\");\n }\n\n return {\n provider: this.name,\n providerUserId: claims.sub,\n email: typeof claims.email === \"string\" ? claims.email : undefined,\n // Google sends a boolean; anything else counts as NOT verified.\n emailVerified: claims.email_verified === true,\n name: typeof claims.name === \"string\" ? claims.name : undefined,\n avatar: typeof claims.picture === \"string\" ? claims.picture : undefined,\n raw: claims,\n };\n }\n\n /** Redeem the authorization code (with the PKCE verifier) for an id_token. */\n private async exchangeCode(code: string, codeVerifier: string): Promise<string> {\n let body: { id_token?: unknown };\n\n try {\n const response = await fetch(TOKEN_ENDPOINT, {\n method: \"POST\",\n headers: { \"content-type\": \"application/x-www-form-urlencoded\" },\n body: new URLSearchParams({\n code,\n client_id: this.settings.clientId,\n client_secret: this.settings.clientSecret,\n redirect_uri: this.settings.redirectUri,\n grant_type: \"authorization_code\",\n code_verifier: codeVerifier,\n }),\n });\n\n if (!response.ok) {\n throw new Error(`Google token endpoint answered ${response.status}`);\n }\n\n body = (await response.json()) as { id_token?: unknown };\n } catch (error) {\n throw new InvalidProviderCallbackError(\"code-exchange-failed\", error);\n }\n\n if (typeof body.id_token !== \"string\") {\n throw new InvalidProviderCallbackError(\"missing-id-token\");\n }\n\n return body.id_token;\n }\n\n /** Verify signature, issuer, audience and expiry; resolve the claims. */\n private async verifyIdToken(jose: JoseModule, idToken: string): Promise<Record<string, unknown>> {\n googleKeySet ??= jose.createRemoteJWKSet(new URL(JWKS_URI));\n\n try {\n const { payload } = await jose.jwtVerify(idToken, googleKeySet, {\n issuer: ISSUERS,\n audience: this.settings.clientId,\n });\n\n return payload;\n } catch (error) {\n throw new InvalidProviderCallbackError(\"invalid-id-token\", error);\n }\n }\n}\n"],"mappings":";;;;;AAWA,MAAM,yBAAyB;AAC/B,MAAM,iBAAiB;AACvB,MAAM,WAAW;AACjB,MAAM,UAAU,CAAC,+BAA+B,qBAAqB;AACrE,MAAM,iBAAiB;CAAC;CAAU;CAAS;AAAS;;AAGpD,MAAa,sBAAsB;;AAanC,IAAI;;AAGJ,SAAgB,cAAc,cAA8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QAAQ;GAEzD,eAAe,OAAO,mBAAmB;GACzC,MAAM,OAAO,OAAO,SAAS,WAAW,OAAO,OAAO;GACtD,QAAQ,OAAO,OAAO,YAAY,WAAW,OAAO,UAAU;GAC9D,KAAK;EACP;CACF;;CAGA,MAAc,aAAa,MAAc,cAAuC;EAC9E,IAAI;EAEJ,IAAI;GACF,MAAM,WAAW,MAAM,MAAM,gBAAgB;IAC3C,QAAQ;IACR,SAAS,EAAE,gBAAgB,oCAAoC;IAC/D,MAAM,IAAI,gBAAgB;KACxB;KACA,WAAW,KAAK,SAAS;KACzB,eAAe,KAAK,SAAS;KAC7B,cAAc,KAAK,SAAS;KAC5B,YAAY;KACZ,eAAe;IACjB,CAAC;GACH,CAAC;GAED,IAAI,CAAC,SAAS,IACZ,MAAM,IAAI,MAAM,kCAAkC,SAAS,QAAQ;GAGrE,OAAQ,MAAM,SAAS,KAAK;EAC9B,SAAS,OAAO;GACd,MAAM,IAAI,6BAA6B,wBAAwB,KAAK;EACtE;EAEA,IAAI,OAAO,KAAK,aAAa,UAC3B,MAAM,IAAI,6BAA6B,kBAAkB;EAG3D,OAAO,KAAK;CACd;;CAGA,MAAc,cAAc,MAAkB,SAAmD;EAC/F,iBAAiB,KAAK,mBAAmB,IAAI,IAAI,QAAQ,CAAC;EAE1D,IAAI;GACF,MAAM,EAAE,YAAY,MAAM,KAAK,UAAU,SAAS,cAAc;IAC9D,QAAQ;IACR,UAAU,KAAK,SAAS;GAC1B,CAAC;GAED,OAAO;EACT,SAAS,OAAO;GACd,MAAM,IAAI,6BAA6B,oBAAoB,KAAK;EAClE;CACF;AACF"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Auth } from "../models/auth.model.mjs";
|
|
2
|
+
import { AuthProvider } from "../contracts/providers.mjs";
|
|
3
|
+
import { DeviceInfo, LoginResult } from "../contracts/types.mjs";
|
|
4
|
+
import { Request, Response } from "@warlock.js/core";
|
|
5
|
+
import { ChildModel } from "@warlock.js/cascade";
|
|
6
|
+
|
|
7
|
+
//#region ../auth/src/providers/provider-login.d.ts
|
|
8
|
+
/**
|
|
9
|
+
* Resolve a provider by name: `google` from `auth.providers.google`, anything
|
|
10
|
+
* else from `auth.providers.custom`. Throws naming the config key when absent.
|
|
11
|
+
*/
|
|
12
|
+
declare function resolveAuthProvider(name: string): AuthProvider;
|
|
13
|
+
/**
|
|
14
|
+
* Start a redirect login: generate `state`, `nonce` and a PKCE verifier, store
|
|
15
|
+
* them in a signed 10-minute cookie, and resolve the provider URL to redirect
|
|
16
|
+
* the browser to.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // GET /auth/google
|
|
20
|
+
* export const googleRedirect: RequestHandler = async (_request, response) =>
|
|
21
|
+
* response.redirect(await startProviderLogin(response, "google"));
|
|
22
|
+
*/
|
|
23
|
+
declare function startProviderLogin(response: Response, providerName: string): Promise<string>;
|
|
24
|
+
/**
|
|
25
|
+
* Finish a redirect login on the provider's callback: consume the state
|
|
26
|
+
* cookie, reject a missing/forged/expired cookie, a cookie from another
|
|
27
|
+
* provider, a provider-reported error or a `state` mismatch; let the provider
|
|
28
|
+
* verify the callback; find-or-link the user; then log them in through
|
|
29
|
+
* `authService.completeLogin` — the same outcome as password login.
|
|
30
|
+
*
|
|
31
|
+
* @throws InvalidProviderCallbackError (400, EC009) for any callback rejection.
|
|
32
|
+
* @throws ProviderEmailNotVerifiedError (403, EC010) for an unlinked, unverified email.
|
|
33
|
+
* @throws AuthProviderSdkMissingError when the provider's SDK is not installed.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* // GET /auth/google/callback
|
|
37
|
+
* const { user, tokens } = await completeProviderLogin(User, "google", request, response);
|
|
38
|
+
* authService.setAuthCookie(response, tokens.accessToken);
|
|
39
|
+
*/
|
|
40
|
+
declare function completeProviderLogin<T extends Auth>(Model: ChildModel<T>, providerName: string, request: Request, response: Response, deviceInfo?: DeviceInfo): Promise<LoginResult<T>>;
|
|
41
|
+
//#endregion
|
|
42
|
+
export { completeProviderLogin, resolveAuthProvider, startProviderLogin };
|
|
43
|
+
//# sourceMappingURL=provider-login.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-login.d.mts","names":[],"sources":["../../../../../../../auth/src/providers/provider-login.ts"],"mappings":";;;;;;;;;AAiBA;;iBAAgB,mBAAA,CAAoB,IAAA,WAAe,YAAY;;AAAA;AA4B/D;;;;;;;;iBAAsB,kBAAA,CACpB,QAAA,EAAU,QAAA,EACV,YAAA,WACC,OAAO;AAAA;AAgCV;;;;;;;;;;;;;;;AAhCU,iBAgCY,qBAAA,WAAgC,IAAA,EACpD,KAAA,EAAO,UAAA,CAAW,CAAA,GAClB,YAAA,UACA,OAAA,EAAS,OAAA,EACT,QAAA,EAAU,QAAA,EACV,UAAA,GAAa,UAAA,GACZ,OAAA,CAAQ,WAAA,CAAY,CAAA"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { authConfig } from "../services/auth-config.mjs";
|
|
2
|
+
import { authService } from "../services/auth.service.mjs";
|
|
3
|
+
import { InvalidProviderCallbackError } from "../errors/invalid-provider-callback.error.mjs";
|
|
4
|
+
import { safeEqual } from "../utils/safe-equal.mjs";
|
|
5
|
+
import { GoogleProvider } from "./google-provider.mjs";
|
|
6
|
+
import { randomUrlSafe, takeProviderState, writeProviderState } from "./provider-state-cookie.mjs";
|
|
7
|
+
import { resolveProviderUser } from "./resolve-provider-user.mjs";
|
|
8
|
+
|
|
9
|
+
//#region ../auth/src/providers/provider-login.ts
|
|
10
|
+
/**
|
|
11
|
+
* Resolve a provider by name: `google` from `auth.providers.google`, anything
|
|
12
|
+
* else from `auth.providers.custom`. Throws naming the config key when absent.
|
|
13
|
+
*/
|
|
14
|
+
function resolveAuthProvider(name) {
|
|
15
|
+
if (name === "google") {
|
|
16
|
+
const google = authConfig.providers.google();
|
|
17
|
+
if (google) return new GoogleProvider(google);
|
|
18
|
+
} else {
|
|
19
|
+
const custom = authConfig.providers.custom(name);
|
|
20
|
+
if (custom) return custom;
|
|
21
|
+
}
|
|
22
|
+
throw new Error(`@warlock.js/auth: no login provider "${name}" — configure \`auth.providers.${name === "google" ? "google" : `custom.${name}`}\`.`);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Start a redirect login: generate `state`, `nonce` and a PKCE verifier, store
|
|
26
|
+
* them in a signed 10-minute cookie, and resolve the provider URL to redirect
|
|
27
|
+
* the browser to.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* // GET /auth/google
|
|
31
|
+
* export const googleRedirect: RequestHandler = async (_request, response) =>
|
|
32
|
+
* response.redirect(await startProviderLogin(response, "google"));
|
|
33
|
+
*/
|
|
34
|
+
async function startProviderLogin(response, providerName) {
|
|
35
|
+
const provider = resolveAuthProvider(providerName);
|
|
36
|
+
const state = {
|
|
37
|
+
state: randomUrlSafe(),
|
|
38
|
+
nonce: randomUrlSafe(),
|
|
39
|
+
codeVerifier: randomUrlSafe()
|
|
40
|
+
};
|
|
41
|
+
const url = await provider.authorizationUrl(state);
|
|
42
|
+
writeProviderState(response, provider.name, state);
|
|
43
|
+
return url;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Finish a redirect login on the provider's callback: consume the state
|
|
47
|
+
* cookie, reject a missing/forged/expired cookie, a cookie from another
|
|
48
|
+
* provider, a provider-reported error or a `state` mismatch; let the provider
|
|
49
|
+
* verify the callback; find-or-link the user; then log them in through
|
|
50
|
+
* `authService.completeLogin` — the same outcome as password login.
|
|
51
|
+
*
|
|
52
|
+
* @throws InvalidProviderCallbackError (400, EC009) for any callback rejection.
|
|
53
|
+
* @throws ProviderEmailNotVerifiedError (403, EC010) for an unlinked, unverified email.
|
|
54
|
+
* @throws AuthProviderSdkMissingError when the provider's SDK is not installed.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* // GET /auth/google/callback
|
|
58
|
+
* const { user, tokens } = await completeProviderLogin(User, "google", request, response);
|
|
59
|
+
* authService.setAuthCookie(response, tokens.accessToken);
|
|
60
|
+
*/
|
|
61
|
+
async function completeProviderLogin(Model, providerName, request, response, deviceInfo) {
|
|
62
|
+
const provider = resolveAuthProvider(providerName);
|
|
63
|
+
const expected = takeProviderState(request, response);
|
|
64
|
+
if (!expected) throw new InvalidProviderCallbackError("missing-or-invalid-state-cookie");
|
|
65
|
+
if (expected.provider !== provider.name) throw new InvalidProviderCallbackError("state-cookie-for-another-provider");
|
|
66
|
+
const query = {
|
|
67
|
+
code: request.input("code"),
|
|
68
|
+
state: request.input("state"),
|
|
69
|
+
error: request.input("error")
|
|
70
|
+
};
|
|
71
|
+
if (query.error) throw new InvalidProviderCallbackError(`provider-error:${String(query.error)}`);
|
|
72
|
+
if (typeof query.state !== "string" || !safeEqual(query.state, expected.state)) throw new InvalidProviderCallbackError("state-mismatch");
|
|
73
|
+
const user = await resolveProviderUser(Model, await provider.handleCallback({
|
|
74
|
+
query,
|
|
75
|
+
expected: {
|
|
76
|
+
state: expected.state,
|
|
77
|
+
nonce: expected.nonce,
|
|
78
|
+
codeVerifier: expected.codeVerifier
|
|
79
|
+
}
|
|
80
|
+
}));
|
|
81
|
+
return authService.completeLogin(user, deviceInfo);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
//#endregion
|
|
85
|
+
export { completeProviderLogin, resolveAuthProvider, startProviderLogin };
|
|
86
|
+
//# sourceMappingURL=provider-login.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-login.mjs","names":[],"sources":["../../../../../../../auth/src/providers/provider-login.ts"],"sourcesContent":["import type { ChildModel } from \"@warlock.js/cascade\";\nimport type { Request, Response } from \"@warlock.js/core\";\nimport type { AuthProvider } from \"../contracts/providers\";\nimport type { DeviceInfo, LoginResult } from \"../contracts/types\";\nimport { InvalidProviderCallbackError } from \"../errors/invalid-provider-callback.error\";\nimport type { Auth } from \"../models/auth.model\";\nimport { authConfig } from \"../services/auth-config\";\nimport { authService } from \"../services/auth.service\";\nimport { GoogleProvider } from \"./google-provider\";\nimport { safeEqual } from \"../utils/safe-equal\";\nimport { randomUrlSafe, takeProviderState, writeProviderState } from \"./provider-state-cookie\";\nimport { resolveProviderUser } from \"./resolve-provider-user\";\n\n/**\n * Resolve a provider by name: `google` from `auth.providers.google`, anything\n * else from `auth.providers.custom`. Throws naming the config key when absent.\n */\nexport function resolveAuthProvider(name: string): AuthProvider {\n if (name === \"google\") {\n const google = authConfig.providers.google();\n\n if (google) return new GoogleProvider(google);\n } else {\n const custom = authConfig.providers.custom(name);\n\n if (custom) return custom;\n }\n\n throw new Error(\n `@warlock.js/auth: no login provider \"${name}\" — configure \\`auth.providers.${\n name === \"google\" ? \"google\" : `custom.${name}`\n }\\`.`,\n );\n}\n\n/**\n * Start a redirect login: generate `state`, `nonce` and a PKCE verifier, store\n * them in a signed 10-minute cookie, and resolve the provider URL to redirect\n * the browser to.\n *\n * @example\n * // GET /auth/google\n * export const googleRedirect: RequestHandler = async (_request, response) =>\n * response.redirect(await startProviderLogin(response, \"google\"));\n */\nexport async function startProviderLogin(\n response: Response,\n providerName: string,\n): Promise<string> {\n const provider = resolveAuthProvider(providerName);\n\n const state = {\n state: randomUrlSafe(),\n nonce: randomUrlSafe(),\n codeVerifier: randomUrlSafe(),\n };\n\n const url = await provider.authorizationUrl(state);\n\n writeProviderState(response, provider.name, state);\n\n return url;\n}\n\n/**\n * Finish a redirect login on the provider's callback: consume the state\n * cookie, reject a missing/forged/expired cookie, a cookie from another\n * provider, a provider-reported error or a `state` mismatch; let the provider\n * verify the callback; find-or-link the user; then log them in through\n * `authService.completeLogin` — the same outcome as password login.\n *\n * @throws InvalidProviderCallbackError (400, EC009) for any callback rejection.\n * @throws ProviderEmailNotVerifiedError (403, EC010) for an unlinked, unverified email.\n * @throws AuthProviderSdkMissingError when the provider's SDK is not installed.\n *\n * @example\n * // GET /auth/google/callback\n * const { user, tokens } = await completeProviderLogin(User, \"google\", request, response);\n * authService.setAuthCookie(response, tokens.accessToken);\n */\nexport async function completeProviderLogin<T extends Auth>(\n Model: ChildModel<T>,\n providerName: string,\n request: Request,\n response: Response,\n deviceInfo?: DeviceInfo,\n): Promise<LoginResult<T>> {\n const provider = resolveAuthProvider(providerName);\n const expected = takeProviderState(request, response);\n\n if (!expected) {\n throw new InvalidProviderCallbackError(\"missing-or-invalid-state-cookie\");\n }\n\n if (expected.provider !== provider.name) {\n throw new InvalidProviderCallbackError(\"state-cookie-for-another-provider\");\n }\n\n const query: Record<string, unknown> = {\n code: request.input(\"code\"),\n state: request.input(\"state\"),\n error: request.input(\"error\"),\n };\n\n if (query.error) {\n throw new InvalidProviderCallbackError(`provider-error:${String(query.error)}`);\n }\n\n if (typeof query.state !== \"string\" || !safeEqual(query.state, expected.state)) {\n throw new InvalidProviderCallbackError(\"state-mismatch\");\n }\n\n const profile = await provider.handleCallback({\n query,\n expected: { state: expected.state, nonce: expected.nonce, codeVerifier: expected.codeVerifier },\n });\n\n const user = await resolveProviderUser(Model, profile);\n\n return authService.completeLogin(user, deviceInfo);\n}\n"],"mappings":";;;;;;;;;;;;;AAiBA,SAAgB,oBAAoB,MAA4B;CAC9D,IAAI,SAAS,UAAU;EACrB,MAAM,SAAS,WAAW,UAAU,OAAO;EAE3C,IAAI,QAAQ,OAAO,IAAI,eAAe,MAAM;CAC9C,OAAO;EACL,MAAM,SAAS,WAAW,UAAU,OAAO,IAAI;EAE/C,IAAI,QAAQ,OAAO;CACrB;CAEA,MAAM,IAAI,MACR,wCAAwC,KAAK,iCAC3C,SAAS,WAAW,WAAW,UAAU,OAC1C,IACH;AACF;;;;;;;;;;;AAYA,eAAsB,mBACpB,UACA,cACiB;CACjB,MAAM,WAAW,oBAAoB,YAAY;CAEjD,MAAM,QAAQ;EACZ,OAAO,cAAc;EACrB,OAAO,cAAc;EACrB,cAAc,cAAc;CAC9B;CAEA,MAAM,MAAM,MAAM,SAAS,iBAAiB,KAAK;CAEjD,mBAAmB,UAAU,SAAS,MAAM,KAAK;CAEjD,OAAO;AACT;;;;;;;;;;;;;;;;;AAkBA,eAAsB,sBACpB,OACA,cACA,SACA,UACA,YACyB;CACzB,MAAM,WAAW,oBAAoB,YAAY;CACjD,MAAM,WAAW,kBAAkB,SAAS,QAAQ;CAEpD,IAAI,CAAC,UACH,MAAM,IAAI,6BAA6B,iCAAiC;CAG1E,IAAI,SAAS,aAAa,SAAS,MACjC,MAAM,IAAI,6BAA6B,mCAAmC;CAG5E,MAAM,QAAiC;EACrC,MAAM,QAAQ,MAAM,MAAM;EAC1B,OAAO,QAAQ,MAAM,OAAO;EAC5B,OAAO,QAAQ,MAAM,OAAO;CAC9B;CAEA,IAAI,MAAM,OACR,MAAM,IAAI,6BAA6B,kBAAkB,OAAO,MAAM,KAAK,GAAG;CAGhF,IAAI,OAAO,MAAM,UAAU,YAAY,CAAC,UAAU,MAAM,OAAO,SAAS,KAAK,GAC3E,MAAM,IAAI,6BAA6B,gBAAgB;CAQzD,MAAM,OAAO,MAAM,oBAAoB,OAAO,MALxB,SAAS,eAAe;EAC5C;EACA,UAAU;GAAE,OAAO,SAAS;GAAO,OAAO,SAAS;GAAO,cAAc,SAAS;EAAa;CAChG,CAAC,CAEoD;CAErD,OAAO,YAAY,cAAc,MAAM,UAAU;AACnD"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { authConfig } from "../services/auth-config.mjs";
|
|
2
|
+
import { safeEqual } from "../utils/safe-equal.mjs";
|
|
3
|
+
import { createHmac, randomBytes } from "node:crypto";
|
|
4
|
+
|
|
5
|
+
//#region ../auth/src/providers/provider-state-cookie.ts
|
|
6
|
+
/** Cookie carrying a redirect login's state from the start to the callback. */
|
|
7
|
+
const PROVIDER_STATE_COOKIE = "auth_provider_state";
|
|
8
|
+
/** How long a started provider login stays completable. */
|
|
9
|
+
const PROVIDER_STATE_TTL_SECONDS = 600;
|
|
10
|
+
/** 32 random bytes, base64url — used for `state`, `nonce` and the PKCE verifier. */
|
|
11
|
+
function randomUrlSafe() {
|
|
12
|
+
return randomBytes(32).toString("base64url");
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* HMAC over the payload with a key DERIVED from the access-token secret, so
|
|
16
|
+
* the state cookie never shares a MAC key with the JWTs themselves.
|
|
17
|
+
*/
|
|
18
|
+
function sign(payload) {
|
|
19
|
+
return createHmac("sha256", createHmac("sha256", authConfig.accessToken.secret()).update("@warlock.js/auth:provider-state").digest()).update(payload).digest("base64url");
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Write the signed, 10-minute state cookie for a provider login. Attribute
|
|
23
|
+
* flags (`HttpOnly`, `SameSite=Lax`, `Secure` outside dev) come from the
|
|
24
|
+
* `response.cookie()` secure defaults; `Lax` still rides the provider's
|
|
25
|
+
* top-level GET redirect back to the app.
|
|
26
|
+
*/
|
|
27
|
+
function writeProviderState(response, provider, state) {
|
|
28
|
+
const stored = {
|
|
29
|
+
...state,
|
|
30
|
+
provider,
|
|
31
|
+
expiresAt: Date.now() + PROVIDER_STATE_TTL_SECONDS * 1e3
|
|
32
|
+
};
|
|
33
|
+
const payload = Buffer.from(JSON.stringify(stored)).toString("base64url");
|
|
34
|
+
response.cookie(PROVIDER_STATE_COOKIE, `${payload}.${sign(payload)}`, {
|
|
35
|
+
raw: true,
|
|
36
|
+
path: "/",
|
|
37
|
+
maxAge: PROVIDER_STATE_TTL_SECONDS
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Read AND clear the state cookie, so a started login completes at most once.
|
|
42
|
+
* Resolves `undefined` for an absent, forged (bad MAC), malformed or expired
|
|
43
|
+
* cookie.
|
|
44
|
+
*/
|
|
45
|
+
function takeProviderState(request, response) {
|
|
46
|
+
const raw = request.cookie(PROVIDER_STATE_COOKIE);
|
|
47
|
+
response.clearCookie(PROVIDER_STATE_COOKIE, { path: "/" });
|
|
48
|
+
if (typeof raw !== "string") return void 0;
|
|
49
|
+
const [payload, signature, ...rest] = raw.split(".");
|
|
50
|
+
if (!payload || !signature || rest.length > 0 || !safeEqual(signature, sign(payload))) return;
|
|
51
|
+
let stored;
|
|
52
|
+
try {
|
|
53
|
+
stored = JSON.parse(Buffer.from(payload, "base64url").toString("utf8"));
|
|
54
|
+
} catch {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (typeof stored?.state !== "string" || typeof stored.nonce !== "string" || typeof stored.codeVerifier !== "string" || typeof stored.provider !== "string" || typeof stored.expiresAt !== "number" || stored.expiresAt <= Date.now()) return;
|
|
58
|
+
return stored;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
export { randomUrlSafe, takeProviderState, writeProviderState };
|
|
63
|
+
//# sourceMappingURL=provider-state-cookie.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider-state-cookie.mjs","names":[],"sources":["../../../../../../../auth/src/providers/provider-state-cookie.ts"],"sourcesContent":["import type { Request, Response } from \"@warlock.js/core\";\nimport { createHmac, randomBytes } from \"node:crypto\";\nimport type { ProviderAuthorizationState } from \"../contracts/providers\";\nimport { authConfig } from \"../services/auth-config\";\nimport { safeEqual } from \"../utils/safe-equal\";\n\n/** Cookie carrying a redirect login's state from the start to the callback. */\nexport const PROVIDER_STATE_COOKIE = \"auth_provider_state\";\n\n/** How long a started provider login stays completable. */\nconst PROVIDER_STATE_TTL_SECONDS = 600;\n\n/** What the signed cookie holds. */\nexport type StoredProviderState = ProviderAuthorizationState & {\n provider: string;\n expiresAt: number;\n};\n\n/** 32 random bytes, base64url — used for `state`, `nonce` and the PKCE verifier. */\nexport function randomUrlSafe(): string {\n return randomBytes(32).toString(\"base64url\");\n}\n\n/**\n * HMAC over the payload with a key DERIVED from the access-token secret, so\n * the state cookie never shares a MAC key with the JWTs themselves.\n */\nfunction sign(payload: string): string {\n const key = createHmac(\"sha256\", authConfig.accessToken.secret())\n .update(\"@warlock.js/auth:provider-state\")\n .digest();\n\n return createHmac(\"sha256\", key).update(payload).digest(\"base64url\");\n}\n\n/**\n * Write the signed, 10-minute state cookie for a provider login. Attribute\n * flags (`HttpOnly`, `SameSite=Lax`, `Secure` outside dev) come from the\n * `response.cookie()` secure defaults; `Lax` still rides the provider's\n * top-level GET redirect back to the app.\n */\nexport function writeProviderState(\n response: Response,\n provider: string,\n state: ProviderAuthorizationState,\n): void {\n const stored: StoredProviderState = {\n ...state,\n provider,\n expiresAt: Date.now() + PROVIDER_STATE_TTL_SECONDS * 1000,\n };\n const payload = Buffer.from(JSON.stringify(stored)).toString(\"base64url\");\n\n response.cookie(PROVIDER_STATE_COOKIE, `${payload}.${sign(payload)}`, {\n raw: true,\n path: \"/\",\n maxAge: PROVIDER_STATE_TTL_SECONDS,\n });\n}\n\n/**\n * Read AND clear the state cookie, so a started login completes at most once.\n * Resolves `undefined` for an absent, forged (bad MAC), malformed or expired\n * cookie.\n */\nexport function takeProviderState(\n request: Request,\n response: Response,\n): StoredProviderState | undefined {\n const raw: unknown = request.cookie(PROVIDER_STATE_COOKIE);\n\n response.clearCookie(PROVIDER_STATE_COOKIE, { path: \"/\" });\n\n if (typeof raw !== \"string\") return undefined;\n\n const [payload, signature, ...rest] = raw.split(\".\");\n\n if (!payload || !signature || rest.length > 0 || !safeEqual(signature, sign(payload))) {\n return undefined;\n }\n\n let stored: Partial<StoredProviderState> | undefined;\n\n try {\n stored = JSON.parse(Buffer.from(payload, \"base64url\").toString(\"utf8\"));\n } catch {\n return undefined;\n }\n\n if (\n typeof stored?.state !== \"string\" ||\n typeof stored.nonce !== \"string\" ||\n typeof stored.codeVerifier !== \"string\" ||\n typeof stored.provider !== \"string\" ||\n typeof stored.expiresAt !== \"number\" ||\n stored.expiresAt <= Date.now()\n ) {\n return undefined;\n }\n\n return stored as StoredProviderState;\n}\n"],"mappings":";;;;;;AAOA,MAAa,wBAAwB;;AAGrC,MAAM,6BAA6B;;AASnC,SAAgB,gBAAwB;CACtC,OAAO,YAAY,EAAE,CAAC,CAAC,SAAS,WAA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
+
import { InvalidProviderCallbackError } from "../errors/invalid-provider-callback.error.mjs";
|
|
3
|
+
import { ProviderEmailNotVerifiedError } from "../errors/provider-email-not-verified.error.mjs";
|
|
4
|
+
import { ProviderAccount } from "../models/provider-account/provider-account.model.mjs";
|
|
5
|
+
import "../models/provider-account/index.mjs";
|
|
6
|
+
|
|
7
|
+
//#region ../auth/src/providers/resolve-provider-user.ts
|
|
8
|
+
/** Default creation for a first provider login: email, name, and the verified stamp. */
|
|
9
|
+
async function createUserFor(Model, profile) {
|
|
10
|
+
const createUser = authConfig.providers.createUser();
|
|
11
|
+
if (createUser) return await createUser(profile, Model);
|
|
12
|
+
return await Model.create({
|
|
13
|
+
[authConfig.providers.emailField()]: profile.email,
|
|
14
|
+
...profile.name ? { name: profile.name } : {},
|
|
15
|
+
[authConfig.verification.field()]: /* @__PURE__ */ new Date()
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Find or create the app user for a provider profile, and link them.
|
|
20
|
+
*
|
|
21
|
+
* 1. An existing `provider_accounts` link wins — the provider's stable id, not
|
|
22
|
+
* the email, identifies the account from then on.
|
|
23
|
+
* 2. Otherwise the provider email MUST be verified. An unverified (or absent)
|
|
24
|
+
* email throws {@link ProviderEmailNotVerifiedError} and nothing is linked
|
|
25
|
+
* or created: linking on it would let anyone who can add an unverified
|
|
26
|
+
* address at the provider take over the matching account.
|
|
27
|
+
* 3. Verified: the user whose `auth.providers.emailField` matches, else a new
|
|
28
|
+
* user (`auth.providers.createUser`, or the default) — then the link is stored.
|
|
29
|
+
*/
|
|
30
|
+
async function resolveProviderUser(Model, profile) {
|
|
31
|
+
const link = await ProviderAccount.findLink(profile.provider, profile.providerUserId);
|
|
32
|
+
if (link) {
|
|
33
|
+
const linked = await Model.find(link.userId);
|
|
34
|
+
if (!linked || linked.userType !== link.userType) throw new InvalidProviderCallbackError("link-points-to-missing-user");
|
|
35
|
+
return linked;
|
|
36
|
+
}
|
|
37
|
+
if (!profile.emailVerified || !profile.email) throw new ProviderEmailNotVerifiedError();
|
|
38
|
+
const user = await Model.first({ [authConfig.providers.emailField()]: profile.email }) ?? await createUserFor(Model, profile);
|
|
39
|
+
await ProviderAccount.link(profile.provider, profile.providerUserId, user, profile.email);
|
|
40
|
+
return user;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
export { resolveProviderUser };
|
|
45
|
+
//# sourceMappingURL=resolve-provider-user.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-provider-user.mjs","names":[],"sources":["../../../../../../../auth/src/providers/resolve-provider-user.ts"],"sourcesContent":["import type { ChildModel } from \"@warlock.js/cascade\";\nimport type { ProviderProfile } from \"../contracts/providers\";\nimport { InvalidProviderCallbackError } from \"../errors/invalid-provider-callback.error\";\nimport { ProviderEmailNotVerifiedError } from \"../errors/provider-email-not-verified.error\";\nimport type { Auth } from \"../models/auth.model\";\nimport { ProviderAccount } from \"../models/provider-account\";\nimport { authConfig } from \"../services/auth-config\";\n\n/** Default creation for a first provider login: email, name, and the verified stamp. */\nasync function createUserFor<T extends Auth>(\n Model: ChildModel<T>,\n profile: ProviderProfile,\n): Promise<T> {\n const createUser = authConfig.providers.createUser();\n\n if (createUser) {\n return (await createUser(profile, Model as unknown as ChildModel<Auth>)) as T;\n }\n\n return (await Model.create({\n [authConfig.providers.emailField()]: profile.email,\n ...(profile.name ? { name: profile.name } : {}),\n // The provider verified this address; record it the way verifyEmail does.\n [authConfig.verification.field()]: new Date(),\n } as never)) as T;\n}\n\n/**\n * Find or create the app user for a provider profile, and link them.\n *\n * 1. An existing `provider_accounts` link wins — the provider's stable id, not\n * the email, identifies the account from then on.\n * 2. Otherwise the provider email MUST be verified. An unverified (or absent)\n * email throws {@link ProviderEmailNotVerifiedError} and nothing is linked\n * or created: linking on it would let anyone who can add an unverified\n * address at the provider take over the matching account.\n * 3. Verified: the user whose `auth.providers.emailField` matches, else a new\n * user (`auth.providers.createUser`, or the default) — then the link is stored.\n */\nexport async function resolveProviderUser<T extends Auth>(\n Model: ChildModel<T>,\n profile: ProviderProfile,\n): Promise<T> {\n const link = await ProviderAccount.findLink(profile.provider, profile.providerUserId);\n\n if (link) {\n const linked = (await Model.find(link.userId)) as T | null;\n\n if (!linked || linked.userType !== link.userType) {\n throw new InvalidProviderCallbackError(\"link-points-to-missing-user\");\n }\n\n return linked;\n }\n\n if (!profile.emailVerified || !profile.email) {\n throw new ProviderEmailNotVerifiedError();\n }\n\n const existing = (await Model.first({\n [authConfig.providers.emailField()]: profile.email,\n })) as T | null;\n\n const user = existing ?? (await createUserFor(Model, profile));\n\n await ProviderAccount.link(profile.provider, profile.providerUserId, user, profile.email);\n\n return user;\n}\n"],"mappings":";;;;;;;;AASA,eAAe,cACb,OACA,SACY;CACZ,MAAM,aAAa,WAAW,UAAU,WAAW;CAEnD,IAAI,YACF,OAAQ,MAAM,WAAW,SAAS,KAAoC;CAGxE,OAAQ,MAAM,MAAM,OAAO;GACxB,WAAW,UAAU,WAAW,IAAI,QAAQ;EAC7C,GAAI,QAAQ,OAAO,EAAE,MAAM,QAAQ,KAAK,IAAI,CAAC;GAE5C,WAAW,aAAa,MAAM,oBAAI,IAAI,KAAK;CAC9C,CAAU;AACZ;;;;;;;;;;;;;AAcA,eAAsB,oBACpB,OACA,SACY;CACZ,MAAM,OAAO,MAAM,gBAAgB,SAAS,QAAQ,UAAU,QAAQ,cAAc;CAEpF,IAAI,MAAM;EACR,MAAM,SAAU,MAAM,MAAM,KAAK,KAAK,MAAM;EAE5C,IAAI,CAAC,UAAU,OAAO,aAAa,KAAK,UACtC,MAAM,IAAI,6BAA6B,6BAA6B;EAGtE,OAAO;CACT;CAEA,IAAI,CAAC,QAAQ,iBAAiB,CAAC,QAAQ,OACrC,MAAM,IAAI,8BAA8B;CAO1C,MAAM,OAAO,MAJW,MAAM,MAAM,GACjC,WAAW,UAAU,WAAW,IAAI,QAAQ,MAC/C,CAAC,KAEyB,MAAM,cAAc,OAAO,OAAO;CAE5D,MAAM,gBAAgB,KAAK,QAAQ,UAAU,QAAQ,gBAAgB,MAAM,QAAQ,KAAK;CAExF,OAAO;AACT"}
|
|
@@ -42,6 +42,14 @@ function parseDuration(key, raw) {
|
|
|
42
42
|
if (typeof parsed !== "number" || !Number.isFinite(parsed) || parsed <= 0) throw new Error(`auth.${key}: ${JSON.stringify(raw)} is not a valid ms duration — use a positive duration string such as "1h", "7d", or NO_EXPIRATION.`);
|
|
43
43
|
return parsed;
|
|
44
44
|
}
|
|
45
|
+
/** Email-verification token lifetime used when nothing is configured. */
|
|
46
|
+
const DEFAULT_VERIFICATION_EXPIRES_IN = "24h";
|
|
47
|
+
/** Password-reset token lifetime used when nothing is configured. */
|
|
48
|
+
const DEFAULT_PASSWORD_RESET_EXPIRES_IN = "60m";
|
|
49
|
+
/** Passkey challenge lifetime used when nothing is configured. */
|
|
50
|
+
const DEFAULT_PASSKEY_CHALLENGE_EXPIRES_IN = "5m";
|
|
51
|
+
/** OTP lifetime used when nothing is configured. */
|
|
52
|
+
const DEFAULT_OTP_EXPIRES_IN = "5m";
|
|
45
53
|
/** Access-token lifetime used when nothing is configured. */
|
|
46
54
|
const DEFAULT_ACCESS_TOKEN_EXPIRES_IN = "1h";
|
|
47
55
|
/**
|
|
@@ -122,6 +130,70 @@ const authConfig = {
|
|
|
122
130
|
* beyond the request's own origin. @default []
|
|
123
131
|
*/
|
|
124
132
|
allowedOrigins: () => config.key("auth.csrf.allowedOrigins", []) },
|
|
133
|
+
verification: {
|
|
134
|
+
/** Lifetime in ms of an email-verification token; throws naming the key when unusable. @default "24h" */
|
|
135
|
+
expiresInMs: () => parseDuration("verification.expiresIn", config.key("auth.verification.expiresIn", DEFAULT_VERIFICATION_EXPIRES_IN)),
|
|
136
|
+
/** User attribute stamped with the verification date. @default "emailVerifiedAt" */
|
|
137
|
+
field: () => config.key("auth.verification.field", "emailVerifiedAt"),
|
|
138
|
+
/** App notification replacing the default verification email. */
|
|
139
|
+
notification: () => config.key("auth.verification.notification"),
|
|
140
|
+
/** Link builder whose result is handed to the notification as `url`. */
|
|
141
|
+
url: () => config.key("auth.verification.url")
|
|
142
|
+
},
|
|
143
|
+
passwordReset: {
|
|
144
|
+
/** Lifetime in ms of a password-reset token; throws naming the key when unusable. @default "60m" */
|
|
145
|
+
expiresInMs: () => parseDuration("passwordReset.expiresIn", config.key("auth.passwordReset.expiresIn", DEFAULT_PASSWORD_RESET_EXPIRES_IN)),
|
|
146
|
+
/** User attribute `requestPasswordReset` looks the account up by. @default "email" */
|
|
147
|
+
identifierField: () => config.key("auth.passwordReset.identifierField", "email"),
|
|
148
|
+
/** App notification replacing the default reset email. */
|
|
149
|
+
notification: () => config.key("auth.passwordReset.notification"),
|
|
150
|
+
/** Link builder whose result is handed to the notification as `url`. */
|
|
151
|
+
url: () => config.key("auth.passwordReset.url"),
|
|
152
|
+
/** App-owned password writer replacing the default. */
|
|
153
|
+
setPassword: () => config.key("auth.passwordReset.setPassword")
|
|
154
|
+
},
|
|
155
|
+
providers: {
|
|
156
|
+
/** `auth.providers.google`, when configured. */
|
|
157
|
+
google: () => config.key("auth.providers.google"),
|
|
158
|
+
/** An app-registered provider under `auth.providers.custom.<name>`. */
|
|
159
|
+
custom: (name) => config.key("auth.providers.custom", {})[name],
|
|
160
|
+
/** User attribute a verified provider email is matched against. @default "email" */
|
|
161
|
+
emailField: () => config.key("auth.providers.emailField", "email"),
|
|
162
|
+
/** App-owned user creation for a first provider login. */
|
|
163
|
+
createUser: () => config.key("auth.providers.createUser")
|
|
164
|
+
},
|
|
165
|
+
passkeys: {
|
|
166
|
+
/** `auth.passkeys`; throws naming the keys when `rpID`, `rpName` or `origin` is missing. */
|
|
167
|
+
settings: () => {
|
|
168
|
+
const settings = config.key("auth.passkeys");
|
|
169
|
+
if (!settings?.rpID || !settings.rpName || !settings.origin) throw new Error("@warlock.js/auth: passkeys need `auth.passkeys.rpID`, `auth.passkeys.rpName` and `auth.passkeys.origin`.");
|
|
170
|
+
return settings;
|
|
171
|
+
},
|
|
172
|
+
/** Allowed ceremony origins, as a list. */
|
|
173
|
+
origins: () => {
|
|
174
|
+
const { origin } = authConfig.passkeys.settings();
|
|
175
|
+
return Array.isArray(origin) ? origin : [origin];
|
|
176
|
+
},
|
|
177
|
+
/** Challenge lifetime in ms; throws naming the key when unusable. @default "5m" */
|
|
178
|
+
challengeExpiresInMs: () => parseDuration("passkeys.challengeExpiresIn", config.key("auth.passkeys.challengeExpiresIn", DEFAULT_PASSKEY_CHALLENGE_EXPIRES_IN))
|
|
179
|
+
},
|
|
180
|
+
otp: {
|
|
181
|
+
/** Notifications channel the code is sent through. @default "sms" */
|
|
182
|
+
channel: () => config.key("auth.otp.channel", "sms"),
|
|
183
|
+
/** User attribute holding the phone number. @default "phone" */
|
|
184
|
+
phoneField: () => config.key("auth.otp.phoneField", "phone"),
|
|
185
|
+
/** Code lifetime in ms; throws naming the key when unusable. @default "5m" */
|
|
186
|
+
expiresInMs: () => parseDuration("otp.expiresIn", config.key("auth.otp.expiresIn", DEFAULT_OTP_EXPIRES_IN)),
|
|
187
|
+
/** Verify attempts before the code is invalidated. @default 5 */
|
|
188
|
+
maxAttempts: () => config.key("auth.otp.maxAttempts", 5),
|
|
189
|
+
/** The message text for a code. */
|
|
190
|
+
message: (code) => {
|
|
191
|
+
const build = config.key("auth.otp.message");
|
|
192
|
+
return build ? build(code) : `Your verification code is ${code}`;
|
|
193
|
+
},
|
|
194
|
+
/** App-owned delivery replacing the notifications channel send. */
|
|
195
|
+
send: () => config.key("auth.otp.send")
|
|
196
|
+
},
|
|
125
197
|
refreshToken: {
|
|
126
198
|
/** Separate refresh secret (legacy: `auth.jwt.refresh.secret`); empty ⇒ fall back to the access secret. */
|
|
127
199
|
secret: () => resolve("refreshToken.secret", "jwt.refresh.secret"),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-config.mjs","names":[],"sources":["../../../../../../../auth/src/services/auth-config.ts"],"sourcesContent":["import { config } from \"@warlock.js/core\";\r\nimport { log } from \"@warlock.js/logger\";\r\nimport { type Algorithm } from \"fast-jwt\";\r\nimport ms from \"ms\";\r\nimport type { CanAuthenticate, LogoutWithoutTokenBehavior } from \"../contracts/types\";\r\nimport type { Auth } from \"../models/auth.model\";\r\n\r\nconst warnedLegacyKeys = new Set<string>();\r\n\r\nconst permitAuthentication: CanAuthenticate = (): boolean => true;\r\n\r\n/**\r\n * `ms` accepts anything at runtime and answers `undefined` for a string it\r\n * cannot parse (`\"30dayz\"`), a *formatted string* for a number (`2592000` ⇒\r\n * `\"43m\"`), and throws for `\"\"`. Its published types claim the far narrower\r\n * `(value: ms.StringValue) => number`, which is exactly what let an\r\n * `as ms.StringValue` cast smuggle arbitrary config text past the compiler and\r\n * hand `undefined` to the JWT signer. Declare the honest runtime signature once,\r\n * here, so every caller is forced to deal with the `undefined`.\r\n */\r\nconst parseMs = ms as unknown as (value: unknown) => number | string | undefined;\r\n\r\n/**\r\n * Turn a configured lifetime into a positive number of milliseconds, or throw\r\n * naming the key.\r\n *\r\n * A token lifetime is a security boundary, so an unusable value is a hard error\r\n * rather than a silent fallback: substituting a default would trade one\r\n * unintended lifetime for another, just as quietly — and quiet is the whole\r\n * problem. Every non-positive, non-finite or non-numeric parse is rejected:\r\n *\r\n * - `\"30dayz\"` / `\"thirty days\"` ⇒ `undefined` ⇒ a JWT signed with **no `exp`\r\n * claim** and an `Invalid Date` written to the token row.\r\n * - `\"0d\"` ⇒ parses cleanly to `0`, which `fast-jwt` treats as falsy and also\r\n * emits with **no `exp` claim** — it survives any guard that only rejects\r\n * `undefined`. `\"-1h\"` signs an already-expired token.\r\n * - `2592000` (a bare number) ⇒ `ms` *formats* it as `\"43m\"`, a string, which\r\n * then turns `Date.now() + expiresIn` into an `Invalid Date`.\r\n */\r\nfunction parseDuration(key: string, raw: unknown): number {\r\n let parsed: number | string | undefined;\r\n\r\n try {\r\n parsed = parseMs(raw);\r\n } catch {\r\n // ms throws (rather than returning undefined) for \"\" and non-strings\r\n parsed = undefined;\r\n }\r\n\r\n if (typeof parsed !== \"number\" || !Number.isFinite(parsed) || parsed <= 0) {\r\n throw new Error(\r\n `auth.${key}: ${JSON.stringify(raw)} is not a valid ms duration — ` +\r\n `use a positive duration string such as \"1h\", \"7d\", or NO_EXPIRATION.`,\r\n );\r\n }\r\n\r\n return parsed;\r\n}\r\n\r\n/** Access-token lifetime used when nothing is configured. */\r\nconst DEFAULT_ACCESS_TOKEN_EXPIRES_IN = \"1h\";\r\n\r\n/**\r\n * Resolve an auth setting, preferring the new `auth.accessToken.*` /\r\n * `auth.refreshToken.*` key and falling back to the deprecated `auth.jwt.*`\r\n * shape — warning once per legacy key. Returns `fallback` when neither is set.\r\n *\r\n * This is the backward-compatible shim that lets existing `auth.jwt.*` configs\r\n * keep working after the config split.\r\n */\r\nfunction resolve<T>(newKey: string, legacyKey: string, fallback?: T): T {\r\n const fromNew = config.key(`auth.${newKey}`);\r\n\r\n if (fromNew !== undefined && fromNew !== null) {\r\n return fromNew as T;\r\n }\r\n\r\n const fromLegacy = config.key(`auth.${legacyKey}`);\r\n\r\n if (fromLegacy !== undefined && fromLegacy !== null) {\r\n if (!warnedLegacyKeys.has(legacyKey)) {\r\n warnedLegacyKeys.add(legacyKey);\r\n log.warn(\"auth\", \"config-deprecation\", `auth.${legacyKey} is deprecated — use auth.${newKey}`);\r\n }\r\n\r\n return fromLegacy as T;\r\n }\r\n\r\n return fallback as T;\r\n}\r\n\r\n/**\r\n * Typed, backward-compatible access to auth configuration. The service and the\r\n * jwt signer read configuration exclusively through here, so the new split\r\n * config and the legacy `auth.jwt.*` shape both resolve the same way.\r\n */\r\nexport const authConfig = {\r\n canAuthenticate: async (user: Auth): Promise<boolean> => {\r\n const canAuthenticate = config.key<CanAuthenticate>(\r\n \"auth.canAuthenticate\",\r\n permitAuthentication,\r\n );\r\n\r\n return canAuthenticate(user);\r\n },\r\n accessToken: {\r\n /** Signing secret (legacy: `auth.jwt.secret`). Throws if neither is set. */\r\n secret: (): string => {\r\n const secret = resolve<string | undefined>(\"accessToken.secret\", \"jwt.secret\");\r\n\r\n if (!secret) {\r\n throw new Error(\"auth: no JWT secret configured — set `auth.accessToken.secret`.\");\r\n }\r\n\r\n return secret;\r\n },\r\n /** Signing algorithm (legacy: `auth.jwt.algorithm`). */\r\n algorithm: (): Algorithm => resolve(\"accessToken.algorithm\", \"jwt.algorithm\", \"HS256\"),\r\n /** Lifetime as an `ms`-string (legacy: `auth.jwt.expiresIn`). */\r\n expiresIn: (): string | undefined => resolve(\"accessToken.expiresIn\", \"jwt.expiresIn\"),\r\n /**\r\n * Lifetime in milliseconds — the only form a token issuer may use. Defaults\r\n * to 1 hour when unset; throws naming the key when the configured value is\r\n * not a positive `ms` duration. See {@link parseDuration}.\r\n */\r\n expiresInMs: (): number =>\r\n parseDuration(\r\n \"accessToken.expiresIn\",\r\n authConfig.accessToken.expiresIn() ?? DEFAULT_ACCESS_TOKEN_EXPIRES_IN,\r\n ),\r\n },\r\n pageAuth: {\r\n /**\r\n * Login path a guarded PAGE route redirects a logged-out browser to.\r\n *\r\n * Unset (the default) ⇒ page-route auth failures keep the JSON 401 API\r\n * contract, so existing apps see NO behavior change. Set it (e.g. \"/login\")\r\n * to opt a browser-facing app into login-redirect UX: a logged-out human\r\n * navigating to a guarded page lands on the login screen instead of reading\r\n * a raw JSON error blob. Finding b9ab9804.\r\n */\r\n loginPath: (): string | undefined => config.key(\"auth.pageAuth.loginPath\"),\r\n /**\r\n * Query parameter the redirect carries the originally-requested path in, so\r\n * the login flow can send the user back where they were headed. Defaults to\r\n * `\"returnUrl\"`.\r\n */\r\n returnUrlParam: (): string => config.key(\"auth.pageAuth.returnUrlParam\", \"returnUrl\"),\r\n },\r\n cookie: {\r\n /** Cookie name `setAuthCookie`/`clearAuthCookie` use. @default \"access_token\" */\r\n name: (): string => config.key(\"auth.cookie.name\", \"access_token\"),\r\n /** Cookie `Path` attribute `setAuthCookie`/`clearAuthCookie` use. @default \"/\" */\r\n path: (): string => config.key(\"auth.cookie.path\", \"/\"),\r\n },\r\n csrf: {\r\n /**\r\n * Extra origins allowed on a cookie-authenticated unsafe-method request,\r\n * beyond the request's own origin. @default []\r\n */\r\n allowedOrigins: (): string[] => config.key(\"auth.csrf.allowedOrigins\", []),\r\n },\r\n refreshToken: {\r\n /** Separate refresh secret (legacy: `auth.jwt.refresh.secret`); empty ⇒ fall back to the access secret. */\r\n secret: (): string | undefined => resolve(\"refreshToken.secret\", \"jwt.refresh.secret\"),\r\n /** Whether refresh tokens are enabled (legacy: `auth.jwt.refresh.enabled`). */\r\n enabled: (): boolean => resolve(\"refreshToken.enabled\", \"jwt.refresh.enabled\", true),\r\n /** Lifetime as an `ms`-string (legacy: `auth.jwt.refresh.expiresIn`). */\r\n expiresIn: (): string => resolve(\"refreshToken.expiresIn\", \"jwt.refresh.expiresIn\", \"7d\"),\r\n /**\r\n * Lifetime in milliseconds — the only form a token issuer may use. Throws\r\n * naming the key when the configured value is not a positive `ms` duration.\r\n */\r\n expiresInMs: (): number =>\r\n parseDuration(\"refreshToken.expiresIn\", authConfig.refreshToken.expiresIn()),\r\n /** Rotate-on-use (legacy: `auth.jwt.refresh.rotation`). */\r\n rotation: (): boolean => resolve(\"refreshToken.rotation\", \"jwt.refresh.rotation\", true),\r\n /** Max active tokens per user (legacy: `auth.jwt.refresh.maxPerUser`). */\r\n maxPerUser: (): number => resolve(\"refreshToken.maxPerUser\", \"jwt.refresh.maxPerUser\", 5),\r\n /** Logout-without-token behavior (legacy: `auth.jwt.refresh.logoutWithoutToken`). */\r\n logoutWithoutToken: (): LogoutWithoutTokenBehavior =>\r\n resolve(\"refreshToken.logoutWithoutToken\", \"jwt.refresh.logoutWithoutToken\", \"revoke-all\"),\r\n },\r\n};\r\n"],"mappings":";;;;;AAOA,MAAM,mCAAmB,IAAI,IAAY;AAEzC,MAAM,6BAAuD;;;;;;;;;;AAW7D,MAAM,UAAU;;;;;;;;;;;;;;;;;;AAmBhB,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AAoB,cAAc;;EAEjE,YAAoB,OAAO,IAAI,oBAAoB,GAAG;CACxD;CACA,MAAM;;;;;AAKJ,sBAAgC,OAAO,IAAI,4BAA4B,CAAC,CAAC,EAC3E;CACA,cAAc;;EAEZ,cAAkC,QAAQ,uBAAuB,oBAAoB;;EAErF,eAAwB,QAAQ,wBAAwB,uBAAuB,IAAI;;EAEnF,iBAAyB,QAAQ,0BAA0B,yBAAyB,IAAI;;;;;EAKxF,mBACE,cAAc,0BAA0B,WAAW,aAAa,UAAU,CAAC;;EAE7E,gBAAyB,QAAQ,yBAAyB,wBAAwB,IAAI;;EAEtF,kBAA0B,QAAQ,2BAA2B,0BAA0B,CAAC;;EAExF,0BACE,QAAQ,mCAAmC,kCAAkC,YAAY;CAC7F;AACF"}
|
|
1
|
+
{"version":3,"file":"auth-config.mjs","names":[],"sources":["../../../../../../../auth/src/services/auth-config.ts"],"sourcesContent":["import { config } from \"@warlock.js/core\";\r\nimport { log } from \"@warlock.js/logger\";\r\nimport { type Algorithm } from \"fast-jwt\";\r\nimport ms from \"ms\";\r\nimport type {\r\n AuthNotification,\r\n CanAuthenticate,\r\n LogoutWithoutTokenBehavior,\r\n OneTimeTokenUrlBuilder,\r\n PasswordSetter,\r\n} from \"../contracts/types\";\r\nimport type {\r\n AuthProvider,\r\n GoogleProviderConfig,\r\n OtpSender,\r\n PasskeysConfig,\r\n ProviderUserCreator,\r\n} from \"../contracts/providers\";\r\nimport type { Auth } from \"../models/auth.model\";\r\n\r\nconst warnedLegacyKeys = new Set<string>();\r\n\r\nconst permitAuthentication: CanAuthenticate = (): boolean => true;\r\n\r\n/**\r\n * `ms` accepts anything at runtime and answers `undefined` for a string it\r\n * cannot parse (`\"30dayz\"`), a *formatted string* for a number (`2592000` ⇒\r\n * `\"43m\"`), and throws for `\"\"`. Its published types claim the far narrower\r\n * `(value: ms.StringValue) => number`, which is exactly what let an\r\n * `as ms.StringValue` cast smuggle arbitrary config text past the compiler and\r\n * hand `undefined` to the JWT signer. Declare the honest runtime signature once,\r\n * here, so every caller is forced to deal with the `undefined`.\r\n */\r\nconst parseMs = ms as unknown as (value: unknown) => number | string | undefined;\r\n\r\n/**\r\n * Turn a configured lifetime into a positive number of milliseconds, or throw\r\n * naming the key.\r\n *\r\n * A token lifetime is a security boundary, so an unusable value is a hard error\r\n * rather than a silent fallback: substituting a default would trade one\r\n * unintended lifetime for another, just as quietly — and quiet is the whole\r\n * problem. Every non-positive, non-finite or non-numeric parse is rejected:\r\n *\r\n * - `\"30dayz\"` / `\"thirty days\"` ⇒ `undefined` ⇒ a JWT signed with **no `exp`\r\n * claim** and an `Invalid Date` written to the token row.\r\n * - `\"0d\"` ⇒ parses cleanly to `0`, which `fast-jwt` treats as falsy and also\r\n * emits with **no `exp` claim** — it survives any guard that only rejects\r\n * `undefined`. `\"-1h\"` signs an already-expired token.\r\n * - `2592000` (a bare number) ⇒ `ms` *formats* it as `\"43m\"`, a string, which\r\n * then turns `Date.now() + expiresIn` into an `Invalid Date`.\r\n */\r\nfunction parseDuration(key: string, raw: unknown): number {\r\n let parsed: number | string | undefined;\r\n\r\n try {\r\n parsed = parseMs(raw);\r\n } catch {\r\n // ms throws (rather than returning undefined) for \"\" and non-strings\r\n parsed = undefined;\r\n }\r\n\r\n if (typeof parsed !== \"number\" || !Number.isFinite(parsed) || parsed <= 0) {\r\n throw new Error(\r\n `auth.${key}: ${JSON.stringify(raw)} is not a valid ms duration — ` +\r\n `use a positive duration string such as \"1h\", \"7d\", or NO_EXPIRATION.`,\r\n );\r\n }\r\n\r\n return parsed;\r\n}\r\n\r\n/** Email-verification token lifetime used when nothing is configured. */\r\nconst DEFAULT_VERIFICATION_EXPIRES_IN = \"24h\";\r\n\r\n/** Password-reset token lifetime used when nothing is configured. */\r\nconst DEFAULT_PASSWORD_RESET_EXPIRES_IN = \"60m\";\r\n\r\n/** Passkey challenge lifetime used when nothing is configured. */\r\nconst DEFAULT_PASSKEY_CHALLENGE_EXPIRES_IN = \"5m\";\r\n\r\n/** OTP lifetime used when nothing is configured. */\r\nconst DEFAULT_OTP_EXPIRES_IN = \"5m\";\r\n\r\n/** Access-token lifetime used when nothing is configured. */\r\nconst DEFAULT_ACCESS_TOKEN_EXPIRES_IN = \"1h\";\r\n\r\n/**\r\n * Resolve an auth setting, preferring the new `auth.accessToken.*` /\r\n * `auth.refreshToken.*` key and falling back to the deprecated `auth.jwt.*`\r\n * shape — warning once per legacy key. Returns `fallback` when neither is set.\r\n *\r\n * This is the backward-compatible shim that lets existing `auth.jwt.*` configs\r\n * keep working after the config split.\r\n */\r\nfunction resolve<T>(newKey: string, legacyKey: string, fallback?: T): T {\r\n const fromNew = config.key(`auth.${newKey}`);\r\n\r\n if (fromNew !== undefined && fromNew !== null) {\r\n return fromNew as T;\r\n }\r\n\r\n const fromLegacy = config.key(`auth.${legacyKey}`);\r\n\r\n if (fromLegacy !== undefined && fromLegacy !== null) {\r\n if (!warnedLegacyKeys.has(legacyKey)) {\r\n warnedLegacyKeys.add(legacyKey);\r\n log.warn(\r\n \"auth\",\r\n \"config-deprecation\",\r\n `auth.${legacyKey} is deprecated — use auth.${newKey}`,\r\n );\r\n }\r\n\r\n return fromLegacy as T;\r\n }\r\n\r\n return fallback as T;\r\n}\r\n\r\n/**\r\n * Typed, backward-compatible access to auth configuration. The service and the\r\n * jwt signer read configuration exclusively through here, so the new split\r\n * config and the legacy `auth.jwt.*` shape both resolve the same way.\r\n */\r\nexport const authConfig = {\r\n canAuthenticate: async (user: Auth): Promise<boolean> => {\r\n const canAuthenticate = config.key<CanAuthenticate>(\r\n \"auth.canAuthenticate\",\r\n permitAuthentication,\r\n );\r\n\r\n return canAuthenticate(user);\r\n },\r\n accessToken: {\r\n /** Signing secret (legacy: `auth.jwt.secret`). Throws if neither is set. */\r\n secret: (): string => {\r\n const secret = resolve<string | undefined>(\"accessToken.secret\", \"jwt.secret\");\r\n\r\n if (!secret) {\r\n throw new Error(\"auth: no JWT secret configured — set `auth.accessToken.secret`.\");\r\n }\r\n\r\n return secret;\r\n },\r\n /** Signing algorithm (legacy: `auth.jwt.algorithm`). */\r\n algorithm: (): Algorithm => resolve(\"accessToken.algorithm\", \"jwt.algorithm\", \"HS256\"),\r\n /** Lifetime as an `ms`-string (legacy: `auth.jwt.expiresIn`). */\r\n expiresIn: (): string | undefined => resolve(\"accessToken.expiresIn\", \"jwt.expiresIn\"),\r\n /**\r\n * Lifetime in milliseconds — the only form a token issuer may use. Defaults\r\n * to 1 hour when unset; throws naming the key when the configured value is\r\n * not a positive `ms` duration. See {@link parseDuration}.\r\n */\r\n expiresInMs: (): number =>\r\n parseDuration(\r\n \"accessToken.expiresIn\",\r\n authConfig.accessToken.expiresIn() ?? DEFAULT_ACCESS_TOKEN_EXPIRES_IN,\r\n ),\r\n },\r\n pageAuth: {\r\n /**\r\n * Login path a guarded PAGE route redirects a logged-out browser to.\r\n *\r\n * Unset (the default) ⇒ page-route auth failures keep the JSON 401 API\r\n * contract, so existing apps see NO behavior change. Set it (e.g. \"/login\")\r\n * to opt a browser-facing app into login-redirect UX: a logged-out human\r\n * navigating to a guarded page lands on the login screen instead of reading\r\n * a raw JSON error blob. Finding b9ab9804.\r\n */\r\n loginPath: (): string | undefined => config.key(\"auth.pageAuth.loginPath\"),\r\n /**\r\n * Query parameter the redirect carries the originally-requested path in, so\r\n * the login flow can send the user back where they were headed. Defaults to\r\n * `\"returnUrl\"`.\r\n */\r\n returnUrlParam: (): string => config.key(\"auth.pageAuth.returnUrlParam\", \"returnUrl\"),\r\n },\r\n cookie: {\r\n /** Cookie name `setAuthCookie`/`clearAuthCookie` use. @default \"access_token\" */\r\n name: (): string => config.key(\"auth.cookie.name\", \"access_token\"),\r\n /** Cookie `Path` attribute `setAuthCookie`/`clearAuthCookie` use. @default \"/\" */\r\n path: (): string => config.key(\"auth.cookie.path\", \"/\"),\r\n },\r\n csrf: {\r\n /**\r\n * Extra origins allowed on a cookie-authenticated unsafe-method request,\r\n * beyond the request's own origin. @default []\r\n */\r\n allowedOrigins: (): string[] => config.key(\"auth.csrf.allowedOrigins\", []),\r\n },\r\n verification: {\r\n /** Lifetime in ms of an email-verification token; throws naming the key when unusable. @default \"24h\" */\r\n expiresInMs: (): number =>\r\n parseDuration(\r\n \"verification.expiresIn\",\r\n config.key(\"auth.verification.expiresIn\", DEFAULT_VERIFICATION_EXPIRES_IN),\r\n ),\r\n /** User attribute stamped with the verification date. @default \"emailVerifiedAt\" */\r\n field: (): string => config.key(\"auth.verification.field\", \"emailVerifiedAt\"),\r\n /** App notification replacing the default verification email. */\r\n notification: (): AuthNotification | undefined => config.key(\"auth.verification.notification\"),\r\n /** Link builder whose result is handed to the notification as `url`. */\r\n url: (): OneTimeTokenUrlBuilder | undefined => config.key(\"auth.verification.url\"),\r\n },\r\n passwordReset: {\r\n /** Lifetime in ms of a password-reset token; throws naming the key when unusable. @default \"60m\" */\r\n expiresInMs: (): number =>\r\n parseDuration(\r\n \"passwordReset.expiresIn\",\r\n config.key(\"auth.passwordReset.expiresIn\", DEFAULT_PASSWORD_RESET_EXPIRES_IN),\r\n ),\r\n /** User attribute `requestPasswordReset` looks the account up by. @default \"email\" */\r\n identifierField: (): string => config.key(\"auth.passwordReset.identifierField\", \"email\"),\r\n /** App notification replacing the default reset email. */\r\n notification: (): AuthNotification | undefined => config.key(\"auth.passwordReset.notification\"),\r\n /** Link builder whose result is handed to the notification as `url`. */\r\n url: (): OneTimeTokenUrlBuilder | undefined => config.key(\"auth.passwordReset.url\"),\r\n /** App-owned password writer replacing the default. */\r\n setPassword: (): PasswordSetter | undefined => config.key(\"auth.passwordReset.setPassword\"),\r\n },\r\n providers: {\r\n /** `auth.providers.google`, when configured. */\r\n google: (): GoogleProviderConfig | undefined => config.key(\"auth.providers.google\"),\r\n /** An app-registered provider under `auth.providers.custom.<name>`. */\r\n custom: (name: string): AuthProvider | undefined =>\r\n config.key<Record<string, AuthProvider>>(\"auth.providers.custom\", {})[name],\r\n /** User attribute a verified provider email is matched against. @default \"email\" */\r\n emailField: (): string => config.key(\"auth.providers.emailField\", \"email\"),\r\n /** App-owned user creation for a first provider login. */\r\n createUser: (): ProviderUserCreator | undefined => config.key(\"auth.providers.createUser\"),\r\n },\r\n passkeys: {\r\n /** `auth.passkeys`; throws naming the keys when `rpID`, `rpName` or `origin` is missing. */\r\n settings: (): PasskeysConfig => {\r\n const settings = config.key<PasskeysConfig | undefined>(\"auth.passkeys\");\r\n\r\n if (!settings?.rpID || !settings.rpName || !settings.origin) {\r\n throw new Error(\r\n \"@warlock.js/auth: passkeys need `auth.passkeys.rpID`, `auth.passkeys.rpName` and `auth.passkeys.origin`.\",\r\n );\r\n }\r\n\r\n return settings;\r\n },\r\n /** Allowed ceremony origins, as a list. */\r\n origins: (): string[] => {\r\n const { origin } = authConfig.passkeys.settings();\r\n\r\n return Array.isArray(origin) ? origin : [origin];\r\n },\r\n /** Challenge lifetime in ms; throws naming the key when unusable. @default \"5m\" */\r\n challengeExpiresInMs: (): number =>\r\n parseDuration(\r\n \"passkeys.challengeExpiresIn\",\r\n config.key(\"auth.passkeys.challengeExpiresIn\", DEFAULT_PASSKEY_CHALLENGE_EXPIRES_IN),\r\n ),\r\n },\r\n otp: {\r\n /** Notifications channel the code is sent through. @default \"sms\" */\r\n channel: (): string => config.key(\"auth.otp.channel\", \"sms\"),\r\n /** User attribute holding the phone number. @default \"phone\" */\r\n phoneField: (): string => config.key(\"auth.otp.phoneField\", \"phone\"),\r\n /** Code lifetime in ms; throws naming the key when unusable. @default \"5m\" */\r\n expiresInMs: (): number =>\r\n parseDuration(\"otp.expiresIn\", config.key(\"auth.otp.expiresIn\", DEFAULT_OTP_EXPIRES_IN)),\r\n /** Verify attempts before the code is invalidated. @default 5 */\r\n maxAttempts: (): number => config.key(\"auth.otp.maxAttempts\", 5),\r\n /** The message text for a code. */\r\n message: (code: string): string => {\r\n const build = config.key<((code: string) => string) | undefined>(\"auth.otp.message\");\r\n\r\n return build ? build(code) : `Your verification code is ${code}`;\r\n },\r\n /** App-owned delivery replacing the notifications channel send. */\r\n send: (): OtpSender | undefined => config.key(\"auth.otp.send\"),\r\n },\r\n refreshToken: {\r\n /** Separate refresh secret (legacy: `auth.jwt.refresh.secret`); empty ⇒ fall back to the access secret. */\r\n secret: (): string | undefined => resolve(\"refreshToken.secret\", \"jwt.refresh.secret\"),\r\n /** Whether refresh tokens are enabled (legacy: `auth.jwt.refresh.enabled`). */\r\n enabled: (): boolean => resolve(\"refreshToken.enabled\", \"jwt.refresh.enabled\", true),\r\n /** Lifetime as an `ms`-string (legacy: `auth.jwt.refresh.expiresIn`). */\r\n expiresIn: (): string => resolve(\"refreshToken.expiresIn\", \"jwt.refresh.expiresIn\", \"7d\"),\r\n /**\r\n * Lifetime in milliseconds — the only form a token issuer may use. Throws\r\n * naming the key when the configured value is not a positive `ms` duration.\r\n */\r\n expiresInMs: (): number =>\r\n parseDuration(\"refreshToken.expiresIn\", authConfig.refreshToken.expiresIn()),\r\n /** Rotate-on-use (legacy: `auth.jwt.refresh.rotation`). */\r\n rotation: (): boolean => resolve(\"refreshToken.rotation\", \"jwt.refresh.rotation\", true),\r\n /** Max active tokens per user (legacy: `auth.jwt.refresh.maxPerUser`). */\r\n maxPerUser: (): number => resolve(\"refreshToken.maxPerUser\", \"jwt.refresh.maxPerUser\", 5),\r\n /** Logout-without-token behavior (legacy: `auth.jwt.refresh.logoutWithoutToken`). */\r\n logoutWithoutToken: (): LogoutWithoutTokenBehavior =>\r\n resolve(\"refreshToken.logoutWithoutToken\", \"jwt.refresh.logoutWithoutToken\", \"revoke-all\"),\r\n },\r\n};\r\n"],"mappings":";;;;;AAoBA,MAAM,mCAAmB,IAAI,IAAY;AAEzC,MAAM,6BAAuD;;;;;;;;;;AAW7D,MAAM,UAAU;;;;;;;;;;;;;;;;;;AAmBhB,SAAS,cAAc,KAAa,KAAsB;CACxD,IAAI;CAEJ,IAAI;EACF,SAAS,QAAQ,GAAG;CACtB,QAAQ;EAEN,SAAS;CACX;CAEA,IAAI,OAAO,WAAW,YAAY,CAA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IAAI,wBAAwB,CAAC;;EAE/D,UAAU,SAAyB;GACjC,MAAM,QAAQ,OAAO,IAA4C,kBAAkB;GAEnF,OAAO,QAAQ,MAAM,IAAI,IAAI,6BAA6B;EAC5D;;EAEA,YAAmC,OAAO,IAAI,eAAe;CAC/D;CACA,cAAc;;EAEZ,cAAkC,QAAQ,uBAAuB,oBAAoB;;EAErF,eAAwB,QAAQ,wBAAwB,uBAAuB,IAAI;;EAEnF,iBAAyB,QAAQ,0BAA0B,yBAAyB,IAAI;;;;;EAKxF,mBACE,cAAc,0BAA0B,WAAW,aAAa,UAAU,CAAC;;EAE7E,gBAAyB,QAAQ,yBAAyB,wBAAwB,IAAI;;EAEtF,kBAA0B,QAAQ,2BAA2B,0BAA0B,CAAC;;EAExF,0BACE,QAAQ,mCAAmC,kCAAkC,YAAY;CAC7F;AACF"}
|
|
@@ -18,6 +18,10 @@ declare class AuthService {
|
|
|
18
18
|
* Resolve the active refresh-token model (default or registered override).
|
|
19
19
|
*/
|
|
20
20
|
private get refreshTokenModel();
|
|
21
|
+
/**
|
|
22
|
+
* Resolve the active one-time-token model (default or registered override).
|
|
23
|
+
*/
|
|
24
|
+
private get oneTimeTokenModel();
|
|
21
25
|
/**
|
|
22
26
|
* Build the default access-token JWT payload from a user.
|
|
23
27
|
*/
|
|
@@ -67,6 +71,18 @@ declare class AuthService {
|
|
|
67
71
|
* the user + token pair on success, `null` on failure.
|
|
68
72
|
*/
|
|
69
73
|
login<T extends Auth>(Model: ChildModel<T>, credentials: AuthCredentials, deviceInfo?: DeviceInfo): Promise<LoginResult<T> | null>;
|
|
74
|
+
/**
|
|
75
|
+
* Log in a user some OTHER method already authenticated — a provider
|
|
76
|
+
* callback, a passkey assertion, a one-time code. Applies the
|
|
77
|
+
* `auth.canAuthenticate` policy (403 on refusal), then produces exactly
|
|
78
|
+
* what {@link login} produces: the same tokens, rows and events. Pair with
|
|
79
|
+
* {@link setAuthCookie} for a cookie session, as with password login.
|
|
80
|
+
*
|
|
81
|
+
* Never call this for a user you have not authenticated.
|
|
82
|
+
*/
|
|
83
|
+
completeLogin<T extends Auth>(user: T, deviceInfo?: DeviceInfo): Promise<LoginResult<T>>;
|
|
84
|
+
/** The one token-issuing tail every login method shares. */
|
|
85
|
+
private finalizeLogin;
|
|
70
86
|
/**
|
|
71
87
|
* Log a user out.
|
|
72
88
|
*
|
|
@@ -101,7 +117,8 @@ declare class AuthService {
|
|
|
101
117
|
*/
|
|
102
118
|
revokeTokenFamily(familyId: string): Promise<void>;
|
|
103
119
|
/**
|
|
104
|
-
* Delete expired tokens (refresh + access)
|
|
120
|
+
* Delete expired tokens (refresh + access) and spent one-time tokens
|
|
121
|
+
* (expired or consumed verification/reset/OTP/passkey rows). Emits `token.expired` per refresh
|
|
105
122
|
* token and `cleanup.completed` with the refresh count. Drives the
|
|
106
123
|
* `auth.cleanup` CLI command.
|
|
107
124
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.service.d.mts","names":[],"sources":["../../../../../../../auth/src/services/auth.service.ts"],"mappings":";;;;;;;;
|
|
1
|
+
{"version":3,"file":"auth.service.d.mts","names":[],"sources":["../../../../../../../auth/src/services/auth.service.ts"],"mappings":";;;;;;;;cA0BM,WAAA;;;;AALiD;;;cAYzC,gBAAA;EAqBgC;;;EAAA,YAdhC,iBAAA;EAwDA;;;EAAA,YAjDA,iBAAA;EA0FG;;;EAnFR,uBAAA,CAAwB,IAAA,EAAM,IAAA,GAAO,MAAA;EAsHU;EA7GzC,eAAA,CAAgB,IAAA,EAAM,IAAA,GAAO,OAAA;EAAA,QAI5B,qBAAA;EAAA,QAMA,gBAAA;EAiHH;;;;;EA5FE,mBAAA,CACX,IAAA,EAAM,IAAA,EACN,OAAA,GAAU,MAAA,oBACT,OAAA,CAAQ,iBAAA;EAAA,QAMG,iBAAA;EA4KN;;;;EA5IK,kBAAA,CACX,IAAA,EAAM,IAAA,EACN,UAAA,GAAa,UAAA,GACZ,OAAA,CAAQ,YAA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