@warlock.js/auth 5.18.0 → 5.19.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/CHANGELOG.md +17 -0
- package/esm/contracts/index.d.mts +1 -1
- package/esm/contracts/types.d.mts +23 -2
- package/esm/contracts/types.d.mts.map +1 -1
- package/esm/contracts/types.mjs.map +1 -1
- package/esm/index.d.mts +3 -2
- package/esm/index.mjs +2 -1
- package/esm/middleware/auth.middleware.d.mts +9 -6
- package/esm/middleware/auth.middleware.d.mts.map +1 -1
- package/esm/middleware/auth.middleware.mjs +101 -80
- package/esm/middleware/auth.middleware.mjs.map +1 -1
- package/esm/models/access-token/access-token.model.d.mts +31 -7
- package/esm/models/access-token/access-token.model.d.mts.map +1 -1
- package/esm/models/access-token/access-token.model.mjs +21 -1
- package/esm/models/access-token/access-token.model.mjs.map +1 -1
- package/esm/models/access-token/family-association.migration.mjs +18 -0
- package/esm/models/access-token/family-association.migration.mjs.map +1 -0
- package/esm/models/auth-token-family/auth-token-family.model.d.mts +90 -0
- package/esm/models/auth-token-family/auth-token-family.model.d.mts.map +1 -0
- package/esm/models/auth-token-family/auth-token-family.model.mjs +80 -0
- package/esm/models/auth-token-family/auth-token-family.model.mjs.map +1 -0
- package/esm/models/auth-token-family/index.d.mts +1 -0
- package/esm/models/auth-token-family/index.mjs +3 -0
- package/esm/models/auth-token-family/migration.mjs +25 -0
- package/esm/models/auth-token-family/migration.mjs.map +1 -0
- package/esm/models/index.d.mts +1 -0
- package/esm/models/index.d.mts.map +1 -1
- package/esm/models/index.mjs +8 -0
- package/esm/models/index.mjs.map +1 -1
- package/esm/models/refresh-token/refresh-token.model.d.mts +43 -0
- package/esm/models/refresh-token/refresh-token.model.d.mts.map +1 -1
- package/esm/models/refresh-token/refresh-token.model.mjs +17 -0
- package/esm/models/refresh-token/refresh-token.model.mjs.map +1 -1
- package/esm/models/refresh-token/successor-association.migration.mjs +22 -0
- package/esm/models/refresh-token/successor-association.migration.mjs.map +1 -0
- package/esm/services/auth.service.d.mts +22 -1
- package/esm/services/auth.service.d.mts.map +1 -1
- package/esm/services/auth.service.mjs +216 -32
- package/esm/services/auth.service.mjs.map +1 -1
- package/esm/services/token-family-operation.mjs +74 -0
- package/esm/services/token-family-operation.mjs.map +1 -0
- package/llms-full.txt +63 -58
- package/llms.txt +9 -9
- package/package.json +8 -8
- package/skills/auth-basics/SKILL.md +3 -2
- package/skills/customize-token-storage/SKILL.md +1 -1
- package/skills/customize-user-type/SKILL.md +1 -1
- package/skills/handle-login-and-logout/SKILL.md +1 -1
- package/skills/login-with-providers/SKILL.md +1 -1
- package/skills/manage-tokens/SKILL.md +19 -6
- package/skills/overview/SKILL.md +3 -3
- package/skills/protect-routes/SKILL.md +30 -39
- package/skills/register-user/SKILL.md +1 -1
- package/skills/run-auth-commands/SKILL.md +1 -1
- package/skills/throttle-login-attempts/SKILL.md +1 -1
- package/skills/verify-email-and-reset-password/SKILL.md +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.service.mjs","names":[],"sources":["../../../../../../../auth/src/services/auth.service.ts"],"sourcesContent":["import { Random } from \"@mongez/reinforcements\";\nimport type { ChildModel } from \"@warlock.js/cascade\";\nimport {\n config,\n ForbiddenError,\n hashPassword,\n type Response,\n verifyPassword,\n} from \"@warlock.js/core\";\nimport type {\n AccessTokenOutput,\n AuthCredentials,\n ClearAuthCookieOptions,\n DeviceInfo,\n LoginResult,\n SetAuthCookieOptions,\n TokenPair,\n} from \"../contracts/types\";\nimport { AccessToken } from \"../models/access-token\";\nimport type { Auth } from \"../models/auth.model\";\nimport { OneTimeToken } from \"../models/one-time-token\";\nimport { RefreshToken } from \"../models/refresh-token\";\nimport { authConfig } from \"./auth-config\";\nimport { authEvents } from \"./auth-events\";\nimport { isInvalidCredentialError, jwt } from \"./jwt\";\n\nclass AuthService {\n /**\n * Resolve the active access-token model — the package default, or a subclass\n * an app registered under `config.auth.accessToken.model` (e.g. to add a\n * tenant column). The service never references the concrete class directly so\n * an override is a pure config change.\n */\n private get accessTokenModel(): typeof AccessToken {\n return config.key(\"auth.accessToken.model\", AccessToken);\n }\n\n /**\n * Resolve the active refresh-token model (default or registered override).\n */\n private get refreshTokenModel(): typeof RefreshToken {\n return config.key(\"auth.refreshToken.model\", RefreshToken);\n }\n\n /**\n * Resolve the active one-time-token model (default or registered override).\n */\n private get oneTimeTokenModel(): typeof OneTimeToken {\n return config.key(\"auth.oneTimeToken.model\", OneTimeToken);\n }\n\n /**\n * Build the default access-token JWT payload from a user.\n */\n public buildAccessTokenPayload(user: Auth): Record<string, unknown> {\n return {\n id: user.id,\n userType: user.userType,\n created_at: Date.now(),\n };\n }\n\n /** One policy seam keeps account-state checks identical across all auth paths. */\n public async canAuthenticate(user: Auth): Promise<boolean> {\n return authConfig.canAuthenticate(user);\n }\n\n private async assertCanAuthenticate(user: Auth): Promise<void> {\n if (!(await this.canAuthenticate(user))) {\n throw new ForbiddenError(\"This user cannot authenticate.\");\n }\n }\n\n private async issueAccessToken(\n user: Auth,\n payload?: Record<string, unknown>,\n ): Promise<AccessTokenOutput> {\n const data = payload || this.buildAccessTokenPayload(user);\n // Validate before signing so an unusable lifetime cannot mint an immortal token.\n const expiresIn = authConfig.accessToken.expiresInMs();\n\n const token = await jwt.generate(data, { expiresIn });\n const expiresAt = new Date(Date.now() + expiresIn);\n\n await this.accessTokenModel.issue(user, token, expiresAt);\n\n return { token, expiresAt: expiresAt.toISOString() };\n }\n\n /**\n * Sign + persist an access token for the user and return the token with its\n * expiry. The expiry is computed locally from `expiresIn` rather than by\n * re-verifying the token we just signed.\n */\n public async generateAccessToken(\n user: Auth,\n payload?: Record<string, unknown>,\n ): Promise<AccessTokenOutput> {\n await this.assertCanAuthenticate(user);\n\n return this.issueAccessToken(user, payload);\n }\n\n private async issueRefreshToken(\n user: Auth,\n deviceInfo?: DeviceInfo,\n ): Promise<RefreshToken | undefined> {\n if (!authConfig.refreshToken.enabled()) return;\n\n // Validate the lifetime first — before the per-user cap is enforced, before\n // anything is signed — so a bad value can never revoke a user's oldest\n // session on its way to failing.\n const expiresIn = authConfig.refreshToken.expiresInMs();\n\n const familyId = deviceInfo?.familyId || Random.string(32);\n\n const payload = {\n userId: user.id,\n userType: user.userType,\n familyId,\n };\n\n const expiresAt = new Date(Date.now() + expiresIn).toISOString();\n\n await this.refreshTokenModel.enforceMax(user, authConfig.refreshToken.maxPerUser());\n\n const token = await jwt.generateRefreshToken(payload, { expiresIn });\n\n return this.refreshTokenModel.issue(user, token, { familyId, expiresAt, deviceInfo });\n }\n\n /**\n * Sign + persist a refresh token for the user (enforcing the per-user cap\n * first). Resolves to `undefined` when refresh tokens are disabled in config.\n */\n public async createRefreshToken(\n user: Auth,\n deviceInfo?: DeviceInfo,\n ): Promise<RefreshToken | undefined> {\n if (!authConfig.refreshToken.enabled()) return;\n\n await this.assertCanAuthenticate(user);\n\n return this.issueRefreshToken(user, deviceInfo);\n }\n\n private async issueTokenPair(user: Auth, deviceInfo?: DeviceInfo): Promise<TokenPair> {\n const accessToken = await this.issueAccessToken(user, deviceInfo?.payload);\n const refreshToken = await this.issueRefreshToken(user, deviceInfo);\n\n const tokenPair: TokenPair = {\n accessToken,\n refreshToken: refreshToken\n ? {\n token: refreshToken.get(\"token\"),\n expiresAt: refreshToken.get(\"expires_at\"),\n }\n : undefined,\n };\n\n authEvents.emit(\"token.created\", user, tokenPair);\n\n if (refreshToken) {\n authEvents.emit(\"session.created\", user, refreshToken, deviceInfo);\n }\n\n return tokenPair;\n }\n\n /**\n * Issue both an access and a refresh token, emitting the creation events.\n */\n public async createTokenPair(user: Auth, deviceInfo?: DeviceInfo): Promise<TokenPair> {\n await this.assertCanAuthenticate(user);\n\n return this.issueTokenPair(user, deviceInfo);\n }\n\n /**\n * Exchange a refresh token for a new token pair, with rotation + replay\n * detection. A concurrent reuse of the same token loses the atomic revoke and\n * is treated as a breach — the whole family is revoked and the request fails.\n */\n public async refreshTokens(\n refreshTokenString: string,\n deviceInfo?: DeviceInfo,\n ): Promise<TokenPair | null> {\n let decoded: {\n userId: number;\n userType: string;\n familyId: string;\n } | null;\n\n try {\n decoded = await jwt.verifyRefreshToken<{\n userId: number;\n userType: string;\n familyId: string;\n }>(refreshTokenString);\n } catch (error) {\n if (isInvalidCredentialError(error)) return null;\n\n throw error;\n }\n\n if (!decoded) return null;\n\n const refreshToken = await this.refreshTokenModel.findByToken(refreshTokenString);\n\n if (!refreshToken?.isValid) {\n // An already-invalid token may be a replay of a rotated credential.\n if (refreshToken) {\n await this.revokeTokenFamily(refreshToken.familyId);\n }\n\n return null;\n }\n\n const UserModel = config.key(`auth.userType.${decoded.userType}`);\n\n if (!UserModel) {\n throw new Error(`User type ${decoded.userType} is unknown type.`);\n }\n\n const user = (await UserModel.find(decoded.userId)) as Auth | null;\n\n if (!user) return null;\n\n if (!(await this.canAuthenticate(user))) return null;\n\n const rotationEnabled = authConfig.refreshToken.rotation();\n\n if (rotationEnabled) {\n const won = await refreshToken.revokeIfActive();\n\n if (!won) {\n // A concurrent request already rotated this token.\n await this.revokeTokenFamily(refreshToken.familyId);\n\n return null;\n }\n } else {\n await refreshToken.markAsUsed();\n }\n\n const newTokenPair = await this.issueTokenPair(user, {\n ...deviceInfo,\n familyId: refreshToken.familyId,\n });\n\n authEvents.emit(\"token.refreshed\", user, newTokenPair, refreshToken);\n\n return newTokenPair;\n }\n\n /**\n * Verify a plaintext password against a stored hash.\n */\n public async verifyPassword(plainPassword: string, hashedPassword: string): Promise<boolean> {\n return verifyPassword(plainPassword, hashedPassword);\n }\n\n /**\n * Hash a plaintext password.\n */\n public async hashPassword(password: string): Promise<string> {\n return hashPassword(password);\n }\n\n /**\n * Resolve a user by credentials, verifying the password. Returns `null` on a\n * missing user or a wrong password, emitting `login.failed` either way.\n */\n public async attemptLogin<T extends Auth>(\n Model: ChildModel<T>,\n data: AuthCredentials,\n ): Promise<T | null> {\n const { password, ...otherData } = data;\n\n authEvents.emit(\"login.attempt\", otherData);\n\n const user = (await Model.first(otherData)) as T | null;\n\n if (!user) {\n authEvents.emit(\"login.failed\", otherData, \"User not found\");\n\n return null;\n }\n\n if (!(await this.verifyPassword(password, user.string(\"password\")!))) {\n authEvents.emit(\"login.failed\", otherData, \"Invalid password\");\n\n return null;\n }\n\n if (!(await this.canAuthenticate(user))) {\n authEvents.emit(\"login.failed\", otherData, \"Authentication not allowed\");\n\n return null;\n }\n\n return user;\n }\n\n /**\n * Full login flow: validate credentials, issue tokens, emit events. Returns\n * the user + token pair on success, `null` on failure.\n */\n public async login<T extends Auth>(\n Model: ChildModel<T>,\n credentials: AuthCredentials,\n deviceInfo?: DeviceInfo,\n ): Promise<LoginResult<T> | null> {\n const user = await this.attemptLogin(Model, credentials);\n\n if (!user) {\n return null;\n }\n\n return this.finalizeLogin(user, deviceInfo);\n }\n\n /**\n * Log in a user some OTHER method already authenticated — a provider\n * callback, a passkey assertion, a one-time code. Applies the\n * `auth.canAuthenticate` policy (403 on refusal), then produces exactly\n * what {@link login} produces: the same tokens, rows and events. Pair with\n * {@link setAuthCookie} for a cookie session, as with password login.\n *\n * Never call this for a user you have not authenticated.\n */\n public async completeLogin<T extends Auth>(\n user: T,\n deviceInfo?: DeviceInfo,\n ): Promise<LoginResult<T>> {\n await this.assertCanAuthenticate(user);\n\n return this.finalizeLogin(user, deviceInfo);\n }\n\n /** The one token-issuing tail every login method shares. */\n private async finalizeLogin<T extends Auth>(\n user: T,\n deviceInfo?: DeviceInfo,\n ): Promise<LoginResult<T>> {\n if (!authConfig.refreshToken.enabled()) {\n const accessToken = await this.issueAccessToken(user, deviceInfo?.payload);\n\n return { user, tokens: { accessToken } };\n }\n\n const tokens = await this.issueTokenPair(user, deviceInfo);\n\n authEvents.emit(\"login.success\", user, tokens, deviceInfo);\n\n return { user, tokens };\n }\n\n /**\n * Log a user out.\n *\n * @param accessToken - access token string to revoke (optional)\n * @param refreshToken - refresh token string to revoke (optional)\n *\n * When no refresh token is supplied, `config.auth.refreshToken.logoutWithoutToken`\n * decides the behavior: `\"revoke-all\"` (default, fail-safe) revokes every\n * refresh token; `\"error\"` requires the caller to pass one.\n */\n public async logout(user: Auth, accessToken?: string, refreshToken?: string): Promise<void> {\n if (accessToken) {\n await this.removeAccessToken(user, accessToken);\n }\n\n if (refreshToken) {\n const token = await this.refreshTokenModel.findForUser(user, refreshToken);\n\n if (token) {\n await token.revoke();\n authEvents.emit(\"session.destroyed\", user, token);\n }\n } else {\n const behavior = authConfig.refreshToken.logoutWithoutToken();\n\n if (behavior === \"error\") {\n throw new Error(\"Refresh token required for logout\");\n }\n\n await this.revokeAllTokens(user);\n authEvents.emit(\"logout.failsafe\", user);\n }\n\n authEvents.emit(\"logout\", user);\n }\n\n /**\n * Remove a specific access token belonging to the user.\n */\n public async removeAccessToken(user: Auth, token: string): Promise<void> {\n await this.accessTokenModel.deleteForUser(user, token);\n }\n\n /**\n * Remove every access token belonging to the user.\n */\n public async removeAllAccessTokens(user: Auth): Promise<void> {\n await this.accessTokenModel.deleteAllForUser(user);\n }\n\n /**\n * Remove a specific refresh token belonging to the user.\n */\n public async removeRefreshToken(user: Auth, token: string): Promise<void> {\n await this.refreshTokenModel.deleteForUser(user, token);\n }\n\n /**\n * Revoke every active refresh token for the user and delete their access\n * tokens — \"log out of all devices\". Revocation is a single bulk update; an\n * event fires per revoked token.\n */\n public async revokeAllTokens(user: Auth): Promise<void> {\n const revokedTokens = await this.refreshTokenModel.revokeAllFor(user);\n\n for (const token of revokedTokens) {\n authEvents.emit(\"token.revoked\", user, token);\n }\n\n await this.removeAllAccessTokens(user);\n\n authEvents.emit(\"logout.all\", user);\n }\n\n /**\n * Revoke an entire token family — rotation breach containment.\n */\n public async revokeTokenFamily(familyId: string): Promise<void> {\n const revokedTokens = await this.refreshTokenModel.revokeFamily(familyId);\n\n authEvents.emit(\"token.familyRevoked\", familyId, revokedTokens);\n }\n\n /**\n * Delete expired tokens (refresh + access) and spent one-time tokens\n * (expired or consumed verification/reset/OTP/passkey rows). Emits `token.expired` per refresh\n * token and `cleanup.completed` with the refresh count. Drives the\n * `auth.cleanup` CLI command.\n */\n public async cleanupExpiredTokens(): Promise<number> {\n const expiredTokens = await this.refreshTokenModel.purgeExpired();\n\n for (const token of expiredTokens) {\n authEvents.emit(\"token.expired\", token);\n }\n\n await this.accessTokenModel.purgeExpired();\n await this.oneTimeTokenModel.purgeSpent();\n\n authEvents.emit(\"cleanup.completed\", expiredTokens.length);\n\n return expiredTokens.length;\n }\n\n /**\n * Find every persisted token — access and refresh — that can never retire\n * itself: an unusable `expires_at`, or a token carrying no `exp` claim.\n *\n * This is the remediation read for the pre-4.12.0 `expiresIn` defect (#25).\n * Nothing else surfaces these rows: `auth.cleanup` selects `expires_at < now`,\n * which an `Invalid Date` never satisfies, and a row whose date column is\n * perfectly fine can still hold a token with no deadline in it. Read-only —\n * pair with {@link purgeNeverExpiringTokens} to act on the answer.\n */\n public async findNeverExpiringTokens(): Promise<{\n accessTokens: AccessToken[];\n refreshTokens: RefreshToken[];\n }> {\n return {\n accessTokens: await this.accessTokenModel.findNeverExpiring(),\n refreshTokens: await this.refreshTokenModel.findNeverExpiring(),\n };\n }\n\n /**\n * Revoke every never-expiring token by deleting its row, emitting\n * `token.revoked` per refresh token. Drives `warlock auth.purge-never-expiring`.\n *\n * Access tokens first: deleting one is immediate revocation, while a refresh\n * token left in place for a moment can only mint an access token through a\n * verifier that now rejects it for the missing `exp`.\n */\n public async purgeNeverExpiringTokens(): Promise<{\n accessTokens: number;\n refreshTokens: number;\n }> {\n const accessTokens = await this.accessTokenModel.purgeNeverExpiring();\n const refreshTokens = await this.refreshTokenModel.purgeNeverExpiring();\n\n // `token.expired` (the event `auth.cleanup` already emits for a removed\n // refresh token) rather than `token.revoked`, which carries a loaded `Auth`\n // this batch path has no reason to fetch a user row for.\n for (const token of refreshTokens) {\n authEvents.emit(\"token.expired\", token);\n }\n\n return { accessTokens: accessTokens.length, refreshTokens: refreshTokens.length };\n }\n\n /**\n * Active, unexpired sessions for the user, newest first.\n */\n public async getActiveSessions(user: Auth): Promise<RefreshToken[]> {\n return this.refreshTokenModel.activeFor(user);\n }\n\n /**\n * `Max-Age` (seconds) `setAuthCookie` should apply, derived from an\n * `AccessTokenOutput`'s `expiresAt` — or `undefined` for a bare token\n * string (a session cookie) or an unparseable `expiresAt`.\n */\n private cookieMaxAgeFromToken(token: string | AccessTokenOutput): number | undefined {\n if (typeof token === \"string\") return undefined;\n\n const expiresAt = new Date(token.expiresAt).getTime();\n\n if (Number.isNaN(expiresAt)) return undefined;\n\n return Math.max(0, Math.round((expiresAt - Date.now()) / 1000));\n }\n\n /**\n * Write the auth cookie on `response` — the write side of the `cookie:<name>`\n * token source `authMiddleware([], \"cookie:<name>\")` already reads. An explicit\n * app-controller call, never a side effect of {@link login}, so an existing\n * bearer-only app never starts emitting `Set-Cookie` just by upgrading.\n *\n * Attribute flags (`HttpOnly`, `SameSite=Lax`, `Secure` outside dev) come\n * from `response.cookie()`'s own `secureCookieDefaults()` floor — this\n * method never overrides them, only `name`/`path`/`maxAge`. `raw: true`\n * writes the token unquoted, so the same string a `cookie:<name>` source\n * reads back is exactly what was issued, not a JSON-wrapped copy.\n *\n * @param token - the token string, or an `AccessTokenOutput` (e.g. from\n * {@link login}) whose `expiresAt` sets `Max-Age` automatically when\n * `options.maxAge` is not given. A bare string with no `options.maxAge`\n * produces a session cookie.\n *\n * @example\n * const { user, tokens } = await authService.login(User, credentials);\n * authService.setAuthCookie(response, tokens.accessToken);\n */\n public setAuthCookie(\n response: Response,\n token: string | AccessTokenOutput,\n options: SetAuthCookieOptions = {},\n ): void {\n const name = options.name ?? authConfig.cookie.name();\n const path = options.path ?? authConfig.cookie.path();\n const tokenValue = typeof token === \"string\" ? token : token.token;\n const maxAge = options.maxAge ?? this.cookieMaxAgeFromToken(token);\n\n response.cookie(name, tokenValue, {\n raw: true,\n path,\n ...(maxAge !== undefined ? { maxAge } : {}),\n });\n }\n\n /**\n * Clear the auth cookie {@link setAuthCookie} wrote. An explicit\n * app-controller call — pair it with {@link logout} after the token row is\n * revoked. `path` must match what the cookie was set with, or the browser\n * silently ignores the clear (see `response.clearCookie`'s own doc comment).\n *\n * @example\n * await authService.logout(user, accessToken, refreshToken);\n * authService.clearAuthCookie(response);\n */\n public clearAuthCookie(response: Response, options: ClearAuthCookieOptions = {}): void {\n const name = options.name ?? authConfig.cookie.name();\n const path = options.path ?? authConfig.cookie.path();\n\n response.clearCookie(name, { path });\n }\n}\n\nexport const authService = new AuthService();\n"],"mappings":";;;;;;;;;;;;;AA0BA,IAAM,cAAN,MAAkB;;;;;;;CAOhB,IAAY,mBAAuC;EACjD,OAAO,OAAO,IAAI,0BAA0B,WAAW;CACzD;;;;CAKA,IAAY,oBAAyC;EACnD,OAAO,OAAO,IAAI,2BAA2B,YAAY;CAC3D;;;;CAKA,IAAY,oBAAyC;EACnD,OAAO,OAAO,IAAI,2BAA2B,YAAY;CAC3D;;;;CAKA,AAAO,wBAAwB,MAAqC;EAClE,OAAO;GACL,IAAI,KAAK;GACT,UAAU,KAAK;GACf,YAAY,KAAK,IAAI;EACvB;CACF;;CAGA,MAAa,gBAAgB,MAA8B;EACzD,OAAO,WAAW,gBAAgB,IAAI;CACxC;CAEA,MAAc,sBAAsB,MAA2B;EAC7D,IAAI,CAAE,MAAM,KAAK,gBAAgB,IAAI,GACnC,MAAM,IAAI,eAAe,gCAAgC;CAE7D;CAEA,MAAc,iBACZ,MACA,SAC4B;EAC5B,MAAM,OAAO,WAAW,KAAK,wBAAwB,IAAI;EAEzD,MAAM,YAAY,WAAW,YAAY,YAAY;EAErD,MAAM,QAAQ,MAAM,IAAI,SAAS,MAAM,EAAE,UAAU,CAAC;EACpD,MAAM,YAAY,IAAI,KAAK,KAAK,IAAI,IAAI,SAAS;EAEjD,MAAM,KAAK,iBAAiB,MAAM,MAAM,OAAO,SAAS;EAExD,OAAO;GAAE;GAAO,WAAW,UAAU,YAAY;EAAE;CACrD;;;;;;CAOA,MAAa,oBACX,MACA,S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KtE,KAAK,MAAM,SAAS,eAClB,WAAW,KAAK,iBAAiB,KAAK;EAGxC,OAAO;GAAE,cAAc,aAAa;GAAQ,eAAe,cAAc;EAAO;CAClF;;;;CAKA,MAAa,kBAAkB,MAAqC;EAClE,OAAO,KAAK,kBAAkB,UAAU,IAAI;CAC9C;;;;;;CAOA,AAAQ,sBAAsB,OAAuD;EACnF,IAAI,OAAO,UAAU,UAAU,OAAO;EAEtC,MAAM,YAAY,IAAI,KAAK,MAAM,SAAS,CAAC,CAAC,QAAQ;EAEpD,IAAI,OAAO,MAAM,SAAS,GAAG,OAAO;EAEpC,OAAO,KAAK,IAAI,GAAG,KAAK,OAAO,YAAY,KAAK,IAAI,KAAK,GAAI,CAAC;CAChE;;;;;;;;;;;;;;;;;;;;;;CAuBA,AAAO,cACL,UACA,OACA,UAAgC,CAAC,GAC3B;EACN,MAAM,OAAO,QAAQ,QAAQ,WAAW,OAAO,KAAK;EACpD,MAAM,OAAO,QAAQ,QAAQ,WAAW,OAAO,KAAK;EACpD,MAAM,aAAa,OAAO,UAAU,WAAW,QAAQ,MAAM;EAC7D,MAAM,SAAS,QAAQ,UAAU,KAAK,sBAAsB,KAAK;EAEjE,SAAS,OAAO,MAAM,YAAY;GAChC,KAAK;GACL;GACA,GAAI,WAAW,SAAY,EAAE,OAAO,IAAI,CAAC;EAC3C,CAAC;CACH;;;;;;;;;;;CAYA,AAAO,gBAAgB,UAAoB,UAAkC,CAAC,GAAS;EACrF,MAAM,OAAO,QAAQ,QAAQ,WAAW,OAAO,KAAK;EACpD,MAAM,OAAO,QAAQ,QAAQ,WAAW,OAAO,KAAK;EAEpD,SAAS,YAAY,MAAM,EAAE,KAAK,CAAC;CACrC;AACF;AAEA,MAAa,cAAc,IAAI,YAAY"}
|
|
1
|
+
{"version":3,"file":"auth.service.mjs","names":[],"sources":["../../../../../../../auth/src/services/auth.service.ts"],"sourcesContent":["import { Random } from \"@mongez/reinforcements\";\r\nimport { randomUUID } from \"node:crypto\";\r\nimport type { ChildModel } from \"@warlock.js/cascade\";\r\nimport {\r\n config,\r\n ForbiddenError,\r\n hashPassword,\r\n type Response,\r\n verifyPassword,\r\n} from \"@warlock.js/core\";\r\nimport type {\r\n AccessTokenOutput,\r\n AuthCredentials,\r\n ClearAuthCookieOptions,\r\n DeviceInfo,\r\n LoginResult,\r\n SetAuthCookieOptions,\r\n TokenPair,\r\n} from \"../contracts/types\";\r\nimport { AccessToken } from \"../models/access-token\";\r\nimport type { Auth } from \"../models/auth.model\";\r\nimport { AuthTokenFamily } from \"../models/auth-token-family\";\r\nimport { OneTimeToken } from \"../models/one-time-token\";\r\nimport { RefreshToken } from \"../models/refresh-token\";\r\nimport { authConfig } from \"./auth-config\";\r\nimport { authEvents } from \"./auth-events\";\r\nimport { isInvalidCredentialError, jwt } from \"./jwt\";\r\nimport {\r\n afterTokenFamilyOperation,\r\n runTokenFamilyOperation,\r\n TokenFamilyUnavailableError,\r\n} from \"./token-family-operation\";\r\n\r\nclass AuthService {\r\n /**\r\n * Resolve the active access-token model — the package default, or a subclass\r\n * an app registered under `config.auth.accessToken.model` (e.g. to add a\r\n * tenant column). The service never references the concrete class directly so\r\n * an override is a pure config change.\r\n */\r\n private get accessTokenModel(): typeof AccessToken {\r\n return config.key(\"auth.accessToken.model\", AccessToken);\r\n }\r\n\r\n /**\r\n * Resolve the active refresh-token model (default or registered override).\r\n */\r\n private get refreshTokenModel(): typeof RefreshToken {\r\n return config.key(\"auth.refreshToken.model\", RefreshToken);\r\n }\r\n\r\n /**\r\n * Resolve the active one-time-token model (default or registered override).\r\n */\r\n private get oneTimeTokenModel(): typeof OneTimeToken {\r\n return config.key(\"auth.oneTimeToken.model\", OneTimeToken);\r\n }\r\n\r\n /**\r\n * Build the default access-token JWT payload from a user.\r\n */\r\n public buildAccessTokenPayload(user: Auth): Record<string, unknown> {\r\n return {\r\n id: user.id,\r\n userType: user.userType,\r\n created_at: Date.now(),\r\n };\r\n }\r\n\r\n /** One policy seam keeps account-state checks identical across all auth paths. */\r\n public async canAuthenticate(user: Auth): Promise<boolean> {\r\n return authConfig.canAuthenticate(user);\r\n }\r\n\r\n private async assertCanAuthenticate(user: Auth): Promise<void> {\r\n if (!(await this.canAuthenticate(user))) {\r\n throw new ForbiddenError(\"This user cannot authenticate.\");\r\n }\r\n }\r\n\r\n private async issueAccessToken(\r\n user: Auth,\r\n payload?: Record<string, unknown>,\r\n familyId?: string,\r\n ): Promise<AccessTokenOutput> {\r\n const data = { ...(payload || this.buildAccessTokenPayload(user)), jti: randomUUID() };\r\n // Validate before signing so an unusable lifetime cannot mint an immortal token.\r\n const expiresIn = authConfig.accessToken.expiresInMs();\r\n\r\n const token = await jwt.generate(data, { expiresIn });\r\n const expiresAt = new Date(Date.now() + expiresIn);\r\n\r\n if (familyId) {\r\n await this.accessTokenModel.issue(user, token, expiresAt, familyId);\r\n } else {\r\n await this.accessTokenModel.issue(user, token, expiresAt);\r\n }\r\n\r\n return { token, expiresAt: expiresAt.toISOString() };\r\n }\r\n\r\n /**\r\n * Sign + persist an access token for the user and return the token with its\r\n * expiry. The expiry is computed locally from `expiresIn` rather than by\r\n * re-verifying the token we just signed.\r\n */\r\n public async generateAccessToken(\r\n user: Auth,\r\n payload?: Record<string, unknown>,\r\n ): Promise<AccessTokenOutput> {\r\n await this.assertCanAuthenticate(user);\r\n\r\n return this.issueAccessToken(user, payload);\r\n }\r\n\r\n private async issueRefreshToken(\n user: Auth,\r\n deviceInfo?: DeviceInfo,\r\n ): Promise<RefreshToken | undefined> {\r\n if (!authConfig.refreshToken.enabled()) return;\r\n\r\n // Validate the lifetime first — before the per-user cap is enforced, before\r\n // anything is signed — so a bad value can never revoke a user's oldest\r\n // session on its way to failing.\r\n const expiresIn = authConfig.refreshToken.expiresInMs();\r\n\r\n const familyId = deviceInfo?.familyId || Random.string(32);\r\n\r\n const family = await AuthTokenFamily.ensure(user, familyId);\r\n\r\n if (family.isRevoked) {\r\n throw new Error(\"Cannot issue tokens for a revoked token family\");\r\n }\r\n\r\n const payload = {\r\n userId: user.id,\r\n userType: user.userType,\r\n familyId,\r\n jti: randomUUID(),\r\n };\r\n\r\n const expiresAt = new Date(Date.now() + expiresIn).toISOString();\r\n\r\n await this.refreshTokenModel.enforceMax(user, authConfig.refreshToken.maxPerUser());\r\n\r\n const token = await jwt.generateRefreshToken(payload, { expiresIn });\r\n\r\n return this.refreshTokenModel.issue(user, token, { familyId, expiresAt, deviceInfo });\n }\n\n /**\n * Keep issuance into an explicitly reused family in the same durable\n * transition boundary as rotation and revocation. A new, caller-unspecified\n * family has no concurrent session to protect and retains the lightweight\n * issuance path.\n */\n private async issueInSuppliedFamily<T>(\n user: Auth,\n familyId: string,\n issue: () => Promise<T>,\n ): Promise<T> {\n // Preserve the established error for a family that was already revoked\n // before issuance started. The coordinator covers a revoke that races\n // after this observation.\n const family = await AuthTokenFamily.ensure(user, familyId);\n\n if (family.isRevoked) {\n throw new Error(\"Cannot issue tokens for a revoked token family\");\n }\n\n try {\n return await runTokenFamilyOperation(familyId, async () => issue());\n } catch (error) {\n // Do not expose a timing-dependent error shape to callers: a family\n // revoked between ensure() and the transaction is the same unusable\n // family condition as one found above.\n if (error instanceof TokenFamilyUnavailableError) {\n throw new Error(\"Cannot issue tokens for a revoked token family\");\n }\n\n throw error;\n }\n }\n\r\n /**\r\n * Sign + persist a refresh token for the user (enforcing the per-user cap\r\n * first). Resolves to `undefined` when refresh tokens are disabled in config.\r\n */\r\n public async createRefreshToken(\r\n user: Auth,\r\n deviceInfo?: DeviceInfo,\r\n ): Promise<RefreshToken | undefined> {\r\n if (!authConfig.refreshToken.enabled()) return;\n\n await this.assertCanAuthenticate(user);\n\n if (deviceInfo?.familyId) {\n return this.issueInSuppliedFamily(user, deviceInfo.familyId, () =>\n this.issueRefreshToken(user, deviceInfo),\n );\n }\n\n return this.issueRefreshToken(user, deviceInfo);\n }\r\n\r\n private async issueTokenPair(user: Auth, deviceInfo?: DeviceInfo): Promise<TokenPair> {\r\n const refreshEnabled = authConfig.refreshToken.enabled();\r\n const familyId = deviceInfo?.familyId || Random.string(32);\r\n\r\n if (refreshEnabled) {\r\n const family = await AuthTokenFamily.ensure(user, familyId);\r\n\r\n if (family.isRevoked) {\r\n throw new Error(\"Cannot issue tokens for a revoked token family\");\r\n }\r\n }\r\n\r\n const accessToken = await this.issueAccessToken(\r\n user,\r\n deviceInfo?.payload,\r\n refreshEnabled ? familyId : undefined,\r\n );\r\n const refreshToken = await this.issueRefreshToken(\r\n user,\r\n refreshEnabled ? { ...deviceInfo, familyId } : deviceInfo,\r\n );\r\n\r\n const tokenPair: TokenPair = {\r\n accessToken,\r\n refreshToken: refreshToken\r\n ? {\r\n token: refreshToken.get(\"token\"),\r\n expiresAt: refreshToken.get(\"expires_at\"),\r\n }\r\n : undefined,\r\n };\r\n\r\n afterTokenFamilyOperation(() => authEvents.emit(\"token.created\", user, tokenPair));\r\n\r\n if (refreshToken) {\r\n afterTokenFamilyOperation(() =>\r\n authEvents.emit(\"session.created\", user, refreshToken, deviceInfo),\r\n );\r\n }\r\n\r\n return tokenPair;\r\n }\r\n\r\n /**\r\n * Issue both an access and a refresh token, emitting the creation events.\r\n */\r\n public async createTokenPair(user: Auth, deviceInfo?: DeviceInfo): Promise<TokenPair> {\n await this.assertCanAuthenticate(user);\n\n if (authConfig.refreshToken.enabled() && deviceInfo?.familyId) {\n return this.issueInSuppliedFamily(user, deviceInfo.familyId, () =>\n this.issueTokenPair(user, deviceInfo),\n );\n }\n\n return this.issueTokenPair(user, deviceInfo);\n }\n\r\n /**\r\n * Exchange a refresh token for a new token pair, with rotation + replay\r\n * detection. A concurrent reuse of the same token loses the atomic revoke and\r\n * is treated as a breach — the whole family is revoked and the request fails.\r\n */\r\n public async refreshTokens(\r\n refreshTokenString: string,\r\n deviceInfo?: DeviceInfo,\r\n ): Promise<TokenPair | null> {\r\n let decoded: {\r\n userId: number;\r\n userType: string;\r\n familyId: string;\r\n } | null;\r\n\r\n try {\r\n decoded = await jwt.verifyRefreshToken<{\r\n userId: number;\r\n userType: string;\r\n familyId: string;\r\n }>(refreshTokenString);\r\n } catch (error) {\r\n if (isInvalidCredentialError(error)) return null;\r\n\r\n throw error;\r\n }\r\n\r\n if (!decoded) return null;\r\n\r\n const presentedToken = await this.refreshTokenModel.findByToken(refreshTokenString);\r\n\r\n if (!presentedToken || !this.refreshIdentityMatches(presentedToken, decoded)) return null;\r\n\r\n await AuthTokenFamily.ensure(\r\n { id: decoded.userId, userType: decoded.userType } as Auth,\r\n presentedToken.familyId,\r\n );\r\n return runTokenFamilyOperation(presentedToken.familyId, async () => {\r\n // Each serializable attempt must read the current row in its own snapshot.\r\n const refreshToken = await this.refreshTokenModel.findByToken(refreshTokenString);\r\n if (!refreshToken || !this.refreshIdentityMatches(refreshToken, decoded)) return null;\r\n if (!refreshToken.isValid) {\r\n // An already-invalid token may be a replay of a rotated credential.\r\n if (refreshToken) {\r\n await this.revokeTokenFamily(refreshToken.familyId);\r\n }\r\n\r\n return null;\r\n }\r\n\r\n const UserModel = config.key(`auth.userType.${decoded.userType}`);\r\n\r\n if (!UserModel) {\r\n throw new Error(`User type ${decoded.userType} is unknown type.`);\r\n }\r\n\r\n const user = (await UserModel.find(decoded.userId)) as Auth | null;\r\n\r\n if (!user) return null;\r\n\r\n if (!(await this.canAuthenticate(user))) return null;\r\n\r\n const rotationEnabled = authConfig.refreshToken.rotation();\r\n\r\n if (rotationEnabled) {\r\n const won = await refreshToken.revokeIfActive();\r\n\r\n if (!won) {\r\n // A concurrent request already rotated this token.\r\n await this.revokeTokenFamily(refreshToken.familyId);\r\n\r\n return null;\r\n }\r\n } else {\r\n await refreshToken.markAsUsed();\r\n }\r\n\r\n const newTokenPair = await this.issueTokenPair(user, {\r\n ...deviceInfo,\r\n familyId: refreshToken.familyId,\r\n });\r\n\r\n afterTokenFamilyOperation(() =>\r\n authEvents.emit(\"token.refreshed\", user, newTokenPair, refreshToken),\r\n );\r\n\r\n return newTokenPair;\r\n }).catch((error) => {\r\n if (error instanceof TokenFamilyUnavailableError) return null;\r\n throw error;\r\n });\r\n }\r\n\r\n /**\r\n * Opt-in browser-session renewal. Unlike {@link refreshTokens}, this permits\r\n * a short duplicate presentation to receive the exact committed successor\r\n * pair. It is deliberately not used by the legacy/public refresh API.\r\n */\r\n public async renewAutomaticSession(\r\n refreshTokenString: string,\r\n expectedUserType: string,\r\n options: { overlapMs?: number; deviceInfo?: DeviceInfo } = {},\r\n ): Promise<TokenPair | null> {\r\n let decoded: { userId: number; userType: string; familyId: string } | null;\r\n try {\r\n decoded = await jwt.verifyRefreshToken<{\r\n userId: number;\r\n userType: string;\r\n familyId: string;\r\n }>(refreshTokenString);\r\n } catch (error) {\r\n if (isInvalidCredentialError(error)) return null;\r\n throw error;\r\n }\r\n\r\n if (!decoded || decoded.userType !== expectedUserType) return null;\r\n const presentedToken = await this.refreshTokenModel.findByToken(refreshTokenString);\r\n if (!presentedToken || !this.refreshIdentityMatches(presentedToken, decoded)) return null;\r\n\r\n await AuthTokenFamily.ensure(\r\n { id: decoded.userId, userType: decoded.userType } as Auth,\r\n presentedToken.familyId,\r\n );\r\n return runTokenFamilyOperation(presentedToken.familyId, async () => {\r\n // A retry can follow another process's committed rotation. Never reuse\r\n // a model captured before the transaction or before a failed attempt.\r\n const oldToken = await this.refreshTokenModel.findByToken(refreshTokenString);\r\n if (!oldToken || !this.refreshIdentityMatches(oldToken, decoded)) return null;\r\n if (!oldToken.isValid) {\r\n const successor = await this.readAutomaticSuccessor(oldToken, decoded, options.overlapMs);\r\n if (successor) return successor;\r\n if (\r\n this.isAutomaticOverlapOpen(oldToken, options.overlapMs) &&\r\n this.hasSuccessor(oldToken)\r\n ) {\r\n return null;\r\n }\r\n await this.revokeTokenFamily(oldToken.familyId);\r\n return null;\r\n }\r\n\r\n const UserModel = config.key(`auth.userType.${decoded.userType}`);\r\n if (!UserModel) throw new Error(`User type ${decoded.userType} is unknown type.`);\r\n const user = (await UserModel.find(decoded.userId)) as Auth | null;\r\n if (!user || !(await this.canAuthenticate(user))) return null;\r\n\r\n if (!(await oldToken.revokeIfActive())) {\r\n // A newer successor is never substituted here. A stale automatic\r\n // response must fail quietly rather than revoke a legitimate family.\r\n const currentToken = await this.refreshTokenModel.findByToken(refreshTokenString);\r\n if (!currentToken) return null;\r\n const successor = await this.readAutomaticSuccessor(\r\n currentToken,\r\n decoded,\r\n options.overlapMs,\r\n );\r\n if (successor) return successor;\r\n if (\r\n this.isAutomaticOverlapOpen(currentToken, options.overlapMs) &&\r\n this.hasSuccessor(currentToken)\r\n )\r\n return null;\r\n await this.revokeTokenFamily(oldToken.familyId);\r\n return null;\r\n }\r\n\r\n const pair = await this.issueTokenPair(user, {\r\n ...options.deviceInfo,\r\n familyId: oldToken.familyId,\r\n });\r\n const successorAccess = await this.accessTokenModel.findByToken(pair.accessToken.token);\r\n const successorRefresh = pair.refreshToken\r\n ? await this.refreshTokenModel.findByToken(pair.refreshToken.token)\r\n : null;\r\n if (!successorAccess || !successorRefresh) {\r\n throw new Error(\"Automatic renewal did not persist a complete successor pair\");\r\n }\r\n\r\n // `revokeIfActive()` updates the database, not this hydrated instance.\r\n // Saving that stale instance would write its old `revoked_at: null` back\r\n // and reopen the predecessor. Update only the successor columns instead.\r\n const linked = await this.refreshTokenModel.atomic(\r\n { id: oldToken.id },\r\n {\r\n $set: {\r\n successor_access_token_id: successorAccess.id,\r\n successor_refresh_token_id: successorRefresh.id,\r\n },\r\n },\r\n );\r\n if (linked !== 1) throw new Error(\"Automatic renewal could not link its successor pair\");\r\n afterTokenFamilyOperation(() => authEvents.emit(\"token.refreshed\", user, pair, oldToken));\r\n return pair;\r\n }).catch((error) => {\r\n if (error instanceof TokenFamilyUnavailableError) return null;\r\n throw error;\r\n });\r\n }\r\n\r\n private automaticOverlapMs(overlapMs: number | undefined): number {\r\n return typeof overlapMs === \"number\" && Number.isFinite(overlapMs)\r\n ? Math.max(0, Math.min(10_000, overlapMs))\r\n : 5000;\r\n }\r\n\r\n private refreshIdentityMatches(\r\n token: RefreshToken,\r\n decoded: { userId: string | number; userType: string; familyId: string },\r\n ): boolean {\r\n return (\r\n token.familyId === decoded.familyId &&\r\n token.get(\"user_id\") === decoded.userId &&\r\n token.get(\"user_type\") === decoded.userType\r\n );\r\n }\r\n\r\n private hasSuccessor(oldToken: RefreshToken): boolean {\r\n return Boolean(\r\n oldToken.get(\"successor_access_token_id\") && oldToken.get(\"successor_refresh_token_id\"),\r\n );\r\n }\r\n\r\n private isAutomaticOverlapOpen(oldToken: RefreshToken, overlapMs: number | undefined): boolean {\r\n const revokedAt = oldToken.get<Date | undefined>(\"revoked_at\");\r\n const window = this.automaticOverlapMs(overlapMs);\r\n const elapsed = revokedAt ? Date.now() - new Date(revokedAt).getTime() : NaN;\r\n return window > 0 && elapsed >= 0 && elapsed <= window;\r\n }\r\n\r\n private async readAutomaticSuccessor(\r\n oldToken: RefreshToken,\r\n decoded: { userId: number; userType: string; familyId: string },\r\n overlapMs: number | undefined,\r\n ): Promise<TokenPair | null> {\r\n if (!this.isAutomaticOverlapOpen(oldToken, overlapMs)) return null;\r\n const accessId = oldToken.get<string | undefined>(\"successor_access_token_id\");\r\n const refreshId = oldToken.get<string | undefined>(\"successor_refresh_token_id\");\r\n if (!accessId || !refreshId) return null;\r\n const [access, refresh] = await Promise.all([\r\n this.accessTokenModel.find(accessId),\r\n this.refreshTokenModel.find(refreshId),\r\n ]);\r\n if (!access || access.isExpired || !refresh?.isValid) return null;\r\n if (\r\n access.get(\"family_id\") !== decoded.familyId ||\r\n refresh.get(\"family_id\") !== decoded.familyId ||\r\n access.get(\"user_id\") !== decoded.userId ||\r\n refresh.get(\"user_id\") !== decoded.userId ||\r\n access.get(\"user_type\") !== decoded.userType ||\r\n refresh.get(\"user_type\") !== decoded.userType\r\n )\r\n return null;\r\n const accessExpiry = new Date(access.get(\"expires_at\")).getTime();\r\n if (!Number.isFinite(accessExpiry) || accessExpiry <= Date.now()) return null;\r\n return {\r\n accessToken: { token: access.get(\"token\"), expiresAt: access.get(\"expires_at\") },\r\n refreshToken: { token: refresh.get(\"token\"), expiresAt: refresh.get(\"expires_at\") },\r\n };\r\n }\r\n\r\n /**\r\n * Verify a plaintext password against a stored hash.\r\n */\r\n public async verifyPassword(plainPassword: string, hashedPassword: string): Promise<boolean> {\r\n return verifyPassword(plainPassword, hashedPassword);\r\n }\r\n\r\n /**\r\n * Hash a plaintext password.\r\n */\r\n public async hashPassword(password: string): Promise<string> {\r\n return hashPassword(password);\r\n }\r\n\r\n /**\r\n * Resolve a user by credentials, verifying the password. Returns `null` on a\r\n * missing user or a wrong password, emitting `login.failed` either way.\r\n */\r\n public async attemptLogin<T extends Auth>(\r\n Model: ChildModel<T>,\r\n data: AuthCredentials,\r\n ): Promise<T | null> {\r\n const { password, ...otherData } = data;\r\n\r\n authEvents.emit(\"login.attempt\", otherData);\r\n\r\n const user = (await Model.first(otherData)) as T | null;\r\n\r\n if (!user) {\r\n authEvents.emit(\"login.failed\", otherData, \"User not found\");\r\n\r\n return null;\r\n }\r\n\r\n if (!(await this.verifyPassword(password, user.string(\"password\")!))) {\r\n authEvents.emit(\"login.failed\", otherData, \"Invalid password\");\r\n\r\n return null;\r\n }\r\n\r\n if (!(await this.canAuthenticate(user))) {\r\n authEvents.emit(\"login.failed\", otherData, \"Authentication not allowed\");\r\n\r\n return null;\r\n }\r\n\r\n return user;\r\n }\r\n\r\n /**\r\n * Full login flow: validate credentials, issue tokens, emit events. Returns\r\n * the user + token pair on success, `null` on failure.\r\n */\r\n public async login<T extends Auth>(\r\n Model: ChildModel<T>,\r\n credentials: AuthCredentials,\r\n deviceInfo?: DeviceInfo,\r\n ): Promise<LoginResult<T> | null> {\r\n const user = await this.attemptLogin(Model, credentials);\r\n\r\n if (!user) {\r\n return null;\r\n }\r\n\r\n return this.finalizeLogin(user, deviceInfo);\r\n }\r\n\r\n /**\r\n * Log in a user some OTHER method already authenticated — a provider\r\n * callback, a passkey assertion, a one-time code. Applies the\r\n * `auth.canAuthenticate` policy (403 on refusal), then produces exactly\r\n * what {@link login} produces: the same tokens, rows and events. Pair with\r\n * {@link setAuthCookie} for a cookie session, as with password login.\r\n *\r\n * Never call this for a user you have not authenticated.\r\n */\r\n public async completeLogin<T extends Auth>(\r\n user: T,\r\n deviceInfo?: DeviceInfo,\r\n ): Promise<LoginResult<T>> {\r\n await this.assertCanAuthenticate(user);\r\n\r\n return this.finalizeLogin(user, deviceInfo);\r\n }\r\n\r\n /** The one token-issuing tail every login method shares. */\r\n private async finalizeLogin<T extends Auth>(\r\n user: T,\r\n deviceInfo?: DeviceInfo,\r\n ): Promise<LoginResult<T>> {\r\n if (!authConfig.refreshToken.enabled()) {\r\n const accessToken = await this.issueAccessToken(user, deviceInfo?.payload);\r\n\r\n return { user, tokens: { accessToken } };\r\n }\r\n\r\n const tokens = await this.issueTokenPair(user, deviceInfo);\r\n\r\n authEvents.emit(\"login.success\", user, tokens, deviceInfo);\r\n\r\n return { user, tokens };\r\n }\r\n\r\n /**\r\n * Log a user out.\r\n *\r\n * @param accessToken - access token string to revoke (optional)\r\n * @param refreshToken - refresh token string to revoke (optional)\r\n *\r\n * When no refresh token is supplied, `config.auth.refreshToken.logoutWithoutToken`\r\n * decides the behavior: `\"revoke-all\"` (default, fail-safe) revokes every\r\n * refresh token; `\"error\"` requires the caller to pass one.\r\n */\r\n public async logout(user: Auth, accessToken?: string, refreshToken?: string): Promise<void> {\r\n if (accessToken) {\r\n await this.removeAccessToken(user, accessToken);\r\n }\r\n\r\n if (refreshToken) {\r\n const token = await this.refreshTokenModel.findForUser(user, refreshToken);\r\n\r\n if (token) {\r\n await this.revokeTokenFamily(token.familyId);\r\n authEvents.emit(\"session.destroyed\", user, token);\r\n }\r\n } else {\r\n const behavior = authConfig.refreshToken.logoutWithoutToken();\r\n\r\n if (behavior === \"error\") {\r\n throw new Error(\"Refresh token required for logout\");\r\n }\r\n\r\n await this.revokeAllTokens(user);\r\n authEvents.emit(\"logout.failsafe\", user);\r\n }\r\n\r\n authEvents.emit(\"logout\", user);\r\n }\r\n\r\n /**\r\n * Remove a specific access token belonging to the user.\r\n */\r\n public async removeAccessToken(user: Auth, token: string): Promise<void> {\r\n await this.accessTokenModel.deleteForUser(user, token);\r\n }\r\n\r\n /**\r\n * Remove every access token belonging to the user.\r\n */\r\n public async removeAllAccessTokens(user: Auth): Promise<void> {\r\n await this.accessTokenModel.deleteAllForUser(user);\r\n }\r\n\r\n /**\r\n * Remove a specific refresh token belonging to the user.\r\n */\r\n public async removeRefreshToken(user: Auth, token: string): Promise<void> {\r\n await this.refreshTokenModel.deleteForUser(user, token);\r\n }\r\n\r\n /**\r\n * Revoke every active refresh token for the user and delete their access\r\n * tokens — \"log out of all devices\". Each durable family is first marked through its revision coordinator; an\r\n * event fires per revoked token.\r\n */\r\n public async revokeAllTokens(user: Auth): Promise<void> {\r\n // Upgrade existing installations lazily before the family scan. Include\r\n // revoked rows: a rotation may have revoked its predecessor while its\r\n // durable family still needs logout's marker.\r\n const existingRows = await this.refreshTokenModel.familiesForUser(user);\r\n\r\n for (const token of existingRows) {\r\n await AuthTokenFamily.ensure(user, token.familyId);\r\n }\r\n\r\n const families = await AuthTokenFamily.activeFor(user);\r\n\r\n for (const family of families) {\r\n await this.revokeTokenFamily(family.get<string>(\"family_id\"));\r\n }\r\n\r\n // A deployment can still have rows created before the additive migration\r\n // ran; retain the old bulk cleanup as a final conservative backstop.\r\n const legacyTokens = await this.refreshTokenModel.revokeAllFor(user);\r\n\r\n for (const token of legacyTokens) {\r\n authEvents.emit(\"token.revoked\", user, token);\r\n }\r\n await this.removeAllAccessTokens(user);\r\n\r\n authEvents.emit(\"logout.all\", user);\r\n }\r\n\r\n /**\r\n * Revoke an entire token family — rotation breach containment.\r\n */\r\n public async revokeTokenFamily(familyId: string): Promise<void> {\r\n let family = await AuthTokenFamily.findByFamilyId(familyId);\r\n\r\n if (!family) {\r\n const legacyToken = await this.refreshTokenModel.findInFamily(familyId);\r\n\r\n if (!legacyToken) return;\r\n\r\n family = await AuthTokenFamily.ensure(\r\n {\r\n id: legacyToken.get(\"user_id\"),\r\n userType: legacyToken.get<string>(\"user_type\"),\r\n } as Auth,\r\n familyId,\r\n );\r\n }\r\n\r\n const user = {\r\n id: family.get(\"user_id\"),\r\n userType: family.get<string>(\"user_type\"),\r\n } as Auth;\r\n\r\n if (family.isRevoked) {\r\n // Logout and replay containment are idempotent. Re-run cleanup because a\r\n // request that lost the original race may have observed stale rows.\r\n const revokedTokens = await this.refreshTokenModel.revokeFamily(familyId);\r\n await this.accessTokenModel.deleteFamilyAndLegacyForUser(user, familyId);\r\n afterTokenFamilyOperation(() =>\r\n authEvents.emit(\"token.familyRevoked\", familyId, revokedTokens),\r\n );\r\n\r\n return;\r\n }\r\n\r\n let revokedTokens: RefreshToken[];\r\n try {\r\n revokedTokens = await runTokenFamilyOperation(familyId, async (currentFamily) => {\r\n const familyUser = {\r\n id: currentFamily.get(\"user_id\"),\r\n userType: currentFamily.get<string>(\"user_type\"),\r\n } as Auth;\r\n const revision = currentFamily.get<number>(\"revision\");\r\n const revoked = await AuthTokenFamily.revoke(familyId, revision);\r\n\r\n if (revoked !== 1) {\r\n throw new Error(\"Token family revoke lost its revision transition\");\r\n }\r\n\r\n const familyTokens = await this.refreshTokenModel.revokeFamily(familyId);\r\n await this.accessTokenModel.deleteFamilyAndLegacyForUser(familyUser, familyId);\r\n\r\n return familyTokens;\r\n });\r\n } catch (error) {\r\n if (!(error instanceof TokenFamilyUnavailableError)) throw error;\r\n\r\n // Another request committed the revoke between the read above and its\r\n // CAS. A second logout is successful after idempotent cleanup.\r\n revokedTokens = await this.refreshTokenModel.revokeFamily(familyId);\r\n await this.accessTokenModel.deleteFamilyAndLegacyForUser(user, familyId);\r\n }\r\n\r\n afterTokenFamilyOperation(() =>\r\n authEvents.emit(\"token.familyRevoked\", familyId, revokedTokens),\r\n );\r\n }\r\n\r\n /**\r\n * Delete expired tokens (refresh + access) and spent one-time tokens\r\n * (expired or consumed verification/reset/OTP/passkey rows). Emits `token.expired` per refresh\r\n * token and `cleanup.completed` with the refresh count. Drives the\r\n * `auth.cleanup` CLI command.\r\n */\r\n public async cleanupExpiredTokens(): Promise<number> {\r\n const expiredTokens = await this.refreshTokenModel.purgeExpired();\r\n\r\n for (const token of expiredTokens) {\r\n authEvents.emit(\"token.expired\", token);\r\n }\r\n\r\n await this.accessTokenModel.purgeExpired();\r\n await this.oneTimeTokenModel.purgeSpent();\r\n\r\n authEvents.emit(\"cleanup.completed\", expiredTokens.length);\r\n\r\n return expiredTokens.length;\r\n }\r\n\r\n /**\r\n * Find every persisted token — access and refresh — that can never retire\r\n * itself: an unusable `expires_at`, or a token carrying no `exp` claim.\r\n *\r\n * This is the remediation read for the pre-4.12.0 `expiresIn` defect (#25).\r\n * Nothing else surfaces these rows: `auth.cleanup` selects `expires_at < now`,\r\n * which an `Invalid Date` never satisfies, and a row whose date column is\r\n * perfectly fine can still hold a token with no deadline in it. Read-only —\r\n * pair with {@link purgeNeverExpiringTokens} to act on the answer.\r\n */\r\n public async findNeverExpiringTokens(): Promise<{\r\n accessTokens: AccessToken[];\r\n refreshTokens: RefreshToken[];\r\n }> {\r\n return {\r\n accessTokens: await this.accessTokenModel.findNeverExpiring(),\r\n refreshTokens: await this.refreshTokenModel.findNeverExpiring(),\r\n };\r\n }\r\n\r\n /**\r\n * Revoke every never-expiring token by deleting its row, emitting\r\n * `token.revoked` per refresh token. Drives `warlock auth.purge-never-expiring`.\r\n *\r\n * Access tokens first: deleting one is immediate revocation, while a refresh\r\n * token left in place for a moment can only mint an access token through a\r\n * verifier that now rejects it for the missing `exp`.\r\n */\r\n public async purgeNeverExpiringTokens(): Promise<{\r\n accessTokens: number;\r\n refreshTokens: number;\r\n }> {\r\n const accessTokens = await this.accessTokenModel.purgeNeverExpiring();\r\n const refreshTokens = await this.refreshTokenModel.purgeNeverExpiring();\r\n\r\n // `token.expired` (the event `auth.cleanup` already emits for a removed\r\n // refresh token) rather than `token.revoked`, which carries a loaded `Auth`\r\n // this batch path has no reason to fetch a user row for.\r\n for (const token of refreshTokens) {\r\n authEvents.emit(\"token.expired\", token);\r\n }\r\n\r\n return { accessTokens: accessTokens.length, refreshTokens: refreshTokens.length };\r\n }\r\n\r\n /**\r\n * Active, unexpired sessions for the user, newest first.\r\n */\r\n public async getActiveSessions(user: Auth): Promise<RefreshToken[]> {\r\n return this.refreshTokenModel.activeFor(user);\r\n }\r\n\r\n /**\r\n * `Max-Age` (seconds) `setAuthCookie` should apply, derived from an\r\n * `AccessTokenOutput`'s `expiresAt` — or `undefined` for a bare token\r\n * string (a session cookie) or an unparseable `expiresAt`.\r\n */\r\n private cookieMaxAgeFromToken(token: string | AccessTokenOutput): number | undefined {\r\n if (typeof token === \"string\") return undefined;\r\n\r\n const expiresAt = new Date(token.expiresAt).getTime();\r\n\r\n if (Number.isNaN(expiresAt)) return undefined;\r\n\r\n return Math.max(0, Math.round((expiresAt - Date.now()) / 1000));\r\n }\r\n\r\n /**\r\n * Write the auth cookie on `response` — the write side of the `cookie:<name>`\r\n * token source `authMiddleware([], \"cookie:<name>\")` already reads. An explicit\r\n * app-controller call, never a side effect of {@link login}, so an existing\r\n * bearer-only app never starts emitting `Set-Cookie` just by upgrading.\r\n *\r\n * Attribute flags (`HttpOnly`, `SameSite=Lax`, `Secure` outside dev) come\r\n * from `response.cookie()`'s own `secureCookieDefaults()` floor — this\r\n * method never overrides them, only `name`/`path`/`maxAge`. `raw: true`\r\n * writes the token unquoted, so the same string a `cookie:<name>` source\r\n * reads back is exactly what was issued, not a JSON-wrapped copy.\r\n *\r\n * @param token - the token string, or an `AccessTokenOutput` (e.g. from\r\n * {@link login}) whose `expiresAt` sets `Max-Age` automatically when\r\n * `options.maxAge` is not given. A bare string with no `options.maxAge`\r\n * produces a session cookie.\r\n *\r\n * @example\r\n * const { user, tokens } = await authService.login(User, credentials);\r\n * authService.setAuthCookie(response, tokens.accessToken);\r\n */\r\n public setAuthCookie(\r\n response: Response,\r\n token: string | AccessTokenOutput,\r\n options: SetAuthCookieOptions = {},\r\n ): void {\r\n const name = options.name ?? authConfig.cookie.name();\r\n const path = options.path ?? authConfig.cookie.path();\r\n const tokenValue = typeof token === \"string\" ? token : token.token;\r\n const maxAge = options.maxAge ?? this.cookieMaxAgeFromToken(token);\r\n\r\n response.cookie(name, tokenValue, {\r\n raw: true,\r\n path,\r\n ...(maxAge !== undefined ? { maxAge } : {}),\r\n });\r\n }\r\n\r\n /**\r\n * Clear the auth cookie {@link setAuthCookie} wrote. An explicit\r\n * app-controller call — pair it with {@link logout} after the token row is\r\n * revoked. `path` must match what the cookie was set with, or the browser\r\n * silently ignores the clear (see `response.clearCookie`'s own doc comment).\r\n *\r\n * @example\r\n * await authService.logout(user, accessToken, refreshToken);\r\n * authService.clearAuthCookie(response);\r\n */\r\n public clearAuthCookie(response: Response, options: ClearAuthCookieOptions = {}): void {\r\n const name = options.name ?? authConfig.cookie.name();\r\n const path = options.path ?? authConfig.cookie.path();\r\n\r\n response.clearCookie(name, { path });\r\n }\r\n}\r\n\r\nexport const authService = new AuthService();\r\n"],"mappings":";;;;;;;;;;;;;;;;;AAiCA,IAAM,cAAN,MAAkB;;;;;;;CAOhB,IAAY,mBAAuC;EACjD,OAAO,OAAO,IAAI,0BAA0B,WAAW;CACzD;;;;CAKA,IAAY,oBAAyC;EACnD,OAAO,OAAO,IAAI,2BAA2B,YAAY;CAC3D;;;;CAKA,IAAY,oBAAyC;EACnD,OAAO,OAAO,IAAI,2BAA2B,YAAY;CAC3D;;;;CAKA,AAAO,wBAAwB,MAAqC;EAClE,OAAO;GACL,IAAI,KAAK;GACT,UAAU,KAAK;GACf,YAAY,KAAK,IAAI;EACvB;CACF;;CAGA,MAAa,gBAAgB,MAA8B;EACzD,OAAO,WAAW,gBAAgB,IAAI;CACxC;CAEA,MAAc,sBAAsB,MAA2B;EAC7D,IAAI,CAAE,MAAM,KAAK,gBAAgB,IAAI,GACnC,MAAM,IAAI,eAAe,gCAAgC;CAE7D;CAEA,MAAc,iBACZ,MACA,SACA,UAC4B;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AQ,WAAW,OAAO,KAAK;EACpD,MAAM,OAAO,QAAQ,QAAQ,WAAW,OAAO,KAAK;EACpD,MAAM,aAAa,OAAO,UAAU,WAAW,QAAQ,MAAM;EAC7D,MAAM,SAAS,QAAQ,UAAU,KAAK,sBAAsB,KAAK;EAEjE,SAAS,OAAO,MAAM,YAAY;GAChC,KAAK;GACL;GACA,GAAI,WAAW,SAAY,EAAE,OAAO,IAAI,CAAC;EAC3C,CAAC;CACH;;;;;;;;;;;CAYA,AAAO,gBAAgB,UAAoB,UAAkC,CAAC,GAAS;EACrF,MAAM,OAAO,QAAQ,QAAQ,WAAW,OAAO,KAAK;EACpD,MAAM,OAAO,QAAQ,QAAQ,WAAW,OAAO,KAAK;EAEpD,SAAS,YAAY,MAAM,EAAE,KAAK,CAAC;CACrC;AACF;AAEA,MAAa,cAAc,IAAI,YAAY"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { AuthTokenFamily } from "../models/auth-token-family/auth-token-family.model.mjs";
|
|
2
|
+
import "../models/auth-token-family/index.mjs";
|
|
3
|
+
import { databaseTransactionContext, transaction } from "@warlock.js/cascade";
|
|
4
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
5
|
+
|
|
6
|
+
//#region ../auth/src/services/token-family-operation.ts
|
|
7
|
+
const operationEffects = new AsyncLocalStorage();
|
|
8
|
+
/** Defer events until this coordinator's owned transaction commits. */
|
|
9
|
+
function afterTokenFamilyOperation(effect) {
|
|
10
|
+
const effects = operationEffects.getStore();
|
|
11
|
+
if (effects) effects.push(effect);
|
|
12
|
+
else effect();
|
|
13
|
+
}
|
|
14
|
+
/** A CAS miss is retried only when this helper owns the transaction boundary. */
|
|
15
|
+
var TokenFamilyConflictError = class extends Error {
|
|
16
|
+
constructor() {
|
|
17
|
+
super("Token family changed concurrently");
|
|
18
|
+
this.name = "TokenFamilyConflictError";
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
/** A missing or revoked family cannot become usable through a retry. */
|
|
22
|
+
var TokenFamilyUnavailableError = class extends Error {
|
|
23
|
+
constructor() {
|
|
24
|
+
super("Token family is unavailable");
|
|
25
|
+
this.name = "TokenFamilyUnavailableError";
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const MAX_OWNED_TRANSACTION_ATTEMPTS = 3;
|
|
29
|
+
function isRetryableTransactionError(error) {
|
|
30
|
+
if (!(error instanceof Error)) return false;
|
|
31
|
+
const transactionError = error;
|
|
32
|
+
return transactionError.code === "40001" || transactionError.hasErrorLabel?.("TransientTransactionError") === true;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Perform one family state transition in a transaction and advance its durable
|
|
36
|
+
* revision with a conditional update. An existing Cascade transaction is
|
|
37
|
+
* joined, never retried here: PostgreSQL marks an outer transaction aborted on
|
|
38
|
+
* a serialization error, so only that transaction's owner can safely retry.
|
|
39
|
+
*
|
|
40
|
+
* The finite internal cap prevents request handlers from retrying forever.
|
|
41
|
+
* Transaction setup failures are deliberately propagated; this auth boundary
|
|
42
|
+
* has no non-transactional fallback.
|
|
43
|
+
*/
|
|
44
|
+
async function runTokenFamilyOperation(familyId, operation) {
|
|
45
|
+
const joinsExistingTransaction = databaseTransactionContext.hasActiveTransaction();
|
|
46
|
+
const attempts = joinsExistingTransaction ? 1 : MAX_OWNED_TRANSACTION_ATTEMPTS;
|
|
47
|
+
let lastError;
|
|
48
|
+
for (let attempt = 0; attempt < attempts; attempt++) {
|
|
49
|
+
const parentEffects = operationEffects.getStore();
|
|
50
|
+
const effects = [];
|
|
51
|
+
let result;
|
|
52
|
+
try {
|
|
53
|
+
result = await operationEffects.run(effects, () => transaction(async () => {
|
|
54
|
+
const family = await AuthTokenFamily.findByFamilyId(familyId);
|
|
55
|
+
if (!family || family.isRevoked) throw new TokenFamilyUnavailableError();
|
|
56
|
+
const revision = family.get("revision");
|
|
57
|
+
if (await AuthTokenFamily.advanceRevision(familyId, revision) !== 1) throw new TokenFamilyConflictError();
|
|
58
|
+
return operation(family);
|
|
59
|
+
}, { isolationLevel: "SERIALIZABLE" }));
|
|
60
|
+
} catch (error) {
|
|
61
|
+
lastError = error;
|
|
62
|
+
if (joinsExistingTransaction || !isRetryableTransactionError(error) && !(error instanceof TokenFamilyConflictError)) throw error;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (parentEffects) parentEffects.push(...effects);
|
|
66
|
+
else for (const effect of effects) effect();
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
throw lastError;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
//#endregion
|
|
73
|
+
export { TokenFamilyUnavailableError, afterTokenFamilyOperation, runTokenFamilyOperation };
|
|
74
|
+
//# sourceMappingURL=token-family-operation.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-family-operation.mjs","names":[],"sources":["../../../../../../../auth/src/services/token-family-operation.ts"],"sourcesContent":["import { databaseTransactionContext, transaction } from \"@warlock.js/cascade\";\nimport { AsyncLocalStorage } from \"node:async_hooks\";\nimport { AuthTokenFamily } from \"../models/auth-token-family\";\n\nconst operationEffects = new AsyncLocalStorage<Array<() => void>>();\n\n/** Defer events until this coordinator's owned transaction commits. */\nexport function afterTokenFamilyOperation(effect: () => void): void {\n const effects = operationEffects.getStore();\n if (effects) effects.push(effect);\n else effect();\n}\n\n/** A CAS miss is retried only when this helper owns the transaction boundary. */\nclass TokenFamilyConflictError extends Error {\n public constructor() {\n super(\"Token family changed concurrently\");\n this.name = \"TokenFamilyConflictError\";\n }\n}\n\n/** A missing or revoked family cannot become usable through a retry. */\nexport class TokenFamilyUnavailableError extends Error {\n public constructor() {\n super(\"Token family is unavailable\");\n this.name = \"TokenFamilyUnavailableError\";\n }\n}\n\ntype TransactionError = {\n code?: unknown;\n hasErrorLabel?: (label: string) => boolean;\n};\n\nconst MAX_OWNED_TRANSACTION_ATTEMPTS = 3;\n\nfunction isRetryableTransactionError(error: unknown): boolean {\n if (!(error instanceof Error)) return false;\n\n const transactionError = error as Error & TransactionError;\n\n return (\n transactionError.code === \"40001\" ||\n transactionError.hasErrorLabel?.(\"TransientTransactionError\") === true\n );\n}\n\n/**\n * Perform one family state transition in a transaction and advance its durable\n * revision with a conditional update. An existing Cascade transaction is\n * joined, never retried here: PostgreSQL marks an outer transaction aborted on\n * a serialization error, so only that transaction's owner can safely retry.\n *\n * The finite internal cap prevents request handlers from retrying forever.\n * Transaction setup failures are deliberately propagated; this auth boundary\n * has no non-transactional fallback.\n */\nexport async function runTokenFamilyOperation<T>(\n familyId: string,\n operation: (family: AuthTokenFamily) => Promise<T>,\n): Promise<T> {\n const joinsExistingTransaction = databaseTransactionContext.hasActiveTransaction();\n const attempts = joinsExistingTransaction ? 1 : MAX_OWNED_TRANSACTION_ATTEMPTS;\n let lastError: unknown;\n\n for (let attempt = 0; attempt < attempts; attempt++) {\n const parentEffects = operationEffects.getStore();\n const effects: Array<() => void> = [];\n let result: T;\n try {\n result = await operationEffects.run(effects, () =>\n transaction(\n async () => {\n const family = await AuthTokenFamily.findByFamilyId(familyId);\n\n if (!family || family.isRevoked) {\n throw new TokenFamilyUnavailableError();\n }\n\n const revision = family.get<number>(\"revision\");\n const advanced = await AuthTokenFamily.advanceRevision(familyId, revision);\n\n if (advanced !== 1) {\n throw new TokenFamilyConflictError();\n }\n\n return operation(family);\n },\n { isolationLevel: \"SERIALIZABLE\" },\n ),\n );\n } catch (error) {\n lastError = error;\n\n if (\n joinsExistingTransaction ||\n (!isRetryableTransactionError(error) && !(error instanceof TokenFamilyConflictError))\n ) {\n throw error;\n }\n continue;\n }\n\n // Listener failures must never retry a transaction that already committed.\n // Nested family operations join their parent's queue. An app-owned outer\n // transaction has no Cascade commit hook, so its existing event timing is\n // retained; its owner remains responsible for external side effects.\n if (parentEffects) parentEffects.push(...effects);\n else for (const effect of effects) effect();\n return result;\n }\n\n throw lastError;\n}\n\nexport { TokenFamilyConflictError };\n"],"mappings":";;;;;;AAIA,MAAM,mBAAmB,IAAI,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AAe,cAAc,KAAK,GAAG,OAAO;OAC3C,KAAK,MAAM,UAAU,SAAS,OAAO;EAC1C,OAAO;CACT;CAEA,MAAM;AACR"}
|
package/llms-full.txt
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
---
|
|
10
10
|
name: auth-basics
|
|
11
|
-
description:
|
|
11
|
+
description: "Start here for @warlock.js/auth configuration, user models, credentials, and application session flows."
|
|
12
12
|
---
|
|
13
13
|
|
|
14
14
|
# Auth basics
|
|
@@ -28,7 +28,7 @@ npm install @warlock.js/auth
|
|
|
28
28
|
1. **Users extend `Auth`.** Your `User`, `Admin`, etc. extend the shared base model that knows how to issue tokens and verify passwords. Multiple user types coexist (see [`@warlock.js/auth/customize-user-type/SKILL.md`](@warlock.js/auth/customize-user-type/SKILL.md)).
|
|
29
29
|
2. **`auth.userType.<name>` config maps a user-type slug to the model class.** The middleware uses this to hydrate the right model from a token.
|
|
30
30
|
3. **Tokens persist.** Both `AccessToken` and `RefreshToken` are Cascade models — issuing a token writes a row; logout / revoke deletes or marks-revoked. Stateless JWT verification + stateful revocation list.
|
|
31
|
-
4. **`authMiddleware
|
|
31
|
+
4. **`authMiddleware` gates or resolves routes.** Use `authMiddleware("user")` for a typed hard gate, `authMiddleware()` for the configured/default user type, or `authMiddleware({ source: "cookie", key: "token", optional: true })` for a public route that receives a valid user when present. The object form can set a page-local login redirect. Legacy `authMiddleware(userType, "cookie:name")` remains valid.
|
|
32
32
|
5. **`authService.login(Model, credentials, deviceInfo?)` is the full happy path.** Verifies credentials, creates token pair (access + refresh), emits events, returns `{ user, tokens }`.
|
|
33
33
|
6. **Refresh-token rotation is on by default.** Each refresh consumes the old token and issues new ones from the same "family" — replay detection revokes the family.
|
|
34
34
|
7. **JWT secret lives in the env.** Generate with `warlock jwt.generate` (see [`@warlock.js/auth/run-auth-commands/SKILL.md`](@warlock.js/auth/run-auth-commands/SKILL.md)).
|
|
@@ -79,6 +79,7 @@ export default {
|
|
|
79
79
|
user: User,
|
|
80
80
|
// admin: Admin, // for multi-user-type
|
|
81
81
|
},
|
|
82
|
+
defaultUserType: "user", // needed for authMiddleware() when multiple types exist
|
|
82
83
|
accessToken: {
|
|
83
84
|
secret: env("JWT_SECRET"),
|
|
84
85
|
expiresIn: "1h",
|
|
@@ -131,7 +132,7 @@ Two values that look right and are not:
|
|
|
131
132
|
|
|
132
133
|
---
|
|
133
134
|
name: customize-token-storage
|
|
134
|
-
description:
|
|
135
|
+
description: "Customize token storage in @warlock.js/auth; use when you need to customize token storage."
|
|
135
136
|
---
|
|
136
137
|
|
|
137
138
|
# Customize token storage
|
|
@@ -295,7 +296,7 @@ If you rename a column, override the statics **and these getters** so they map t
|
|
|
295
296
|
|
|
296
297
|
---
|
|
297
298
|
name: customize-user-type
|
|
298
|
-
description:
|
|
299
|
+
description: "Customize user type (multi-user-type auth) in @warlock.js/auth; use when you need to customize user type."
|
|
299
300
|
---
|
|
300
301
|
|
|
301
302
|
# Customize user type (multi-user-type auth)
|
|
@@ -467,7 +468,7 @@ abstract class Auth<TSchema> extends Model<TSchema> implements Authenticable {
|
|
|
467
468
|
|
|
468
469
|
---
|
|
469
470
|
name: handle-login-and-logout
|
|
470
|
-
description:
|
|
471
|
+
description: "Login + logout in @warlock.js/auth; use when you need to handle login and logout."
|
|
471
472
|
---
|
|
472
473
|
|
|
473
474
|
# Login + logout
|
|
@@ -650,7 +651,7 @@ Full event list: `login.attempt`, `login.success`, `login.failed`, `logout`, `lo
|
|
|
650
651
|
|
|
651
652
|
---
|
|
652
653
|
name: login-with-providers
|
|
653
|
-
description:
|
|
654
|
+
description: "Login with an OAuth provider, passkeys, or a phone code in @warlock.js/auth; use when you need to login with providers."
|
|
654
655
|
---
|
|
655
656
|
|
|
656
657
|
# Login with an OAuth provider, passkeys, or a phone code
|
|
@@ -937,7 +938,7 @@ router.post("/auth/otp/verify", async ({ request, response }) => {
|
|
|
937
938
|
|
|
938
939
|
---
|
|
939
940
|
name: manage-tokens
|
|
940
|
-
description:
|
|
941
|
+
description: "Manage tokens in @warlock.js/auth; use when you need to manage tokens."
|
|
941
942
|
---
|
|
942
943
|
|
|
943
944
|
# Manage tokens
|
|
@@ -973,7 +974,20 @@ const pair = await authService.createTokenPair(user, deviceInfo);
|
|
|
973
974
|
|
|
974
975
|
`createTokenPair` is the typical issuance path. It respects `config.auth.refreshToken.enabled` — if disabled, returns only `accessToken`.
|
|
975
976
|
|
|
976
|
-
## Refresh with rotation — `refreshTokens`
|
|
977
|
+
## Refresh with rotation — `refreshTokens`
|
|
978
|
+
|
|
979
|
+
Refresh and family revocation use durable family revision checks inside a
|
|
980
|
+
serializable Cascade transaction. Run the additive `authMigrations` before
|
|
981
|
+
using the upgraded APIs. Transaction support is required: the service fails
|
|
982
|
+
rather than falling back to uncoordinated writes. MongoDB deployments therefore
|
|
983
|
+
need a transaction-capable topology.
|
|
984
|
+
|
|
985
|
+
An operation that owns its transaction retries serialization conflicts at most
|
|
986
|
+
three times. Inside an application-owned transaction it joins once and leaves
|
|
987
|
+
retry/rollback to the caller. Auth notifications from owned attempts are emitted
|
|
988
|
+
only after successful commit; notifications inside an application-owned outer
|
|
989
|
+
transaction retain their existing timing before that outer commit. Keep external
|
|
990
|
+
side effects outside such a transaction.
|
|
977
991
|
|
|
978
992
|
```ts
|
|
979
993
|
const next = await authService.refreshTokens(oldRefreshToken, deviceInfo);
|
|
@@ -1006,7 +1020,7 @@ refresh (B) → revokes B; creates C in family X
|
|
|
1006
1020
|
refresh (A again)→ A is revoked → revoke family X entirely
|
|
1007
1021
|
```
|
|
1008
1022
|
|
|
1009
|
-
The family ties together "successive rotations of one session."
|
|
1023
|
+
The family ties together "successive rotations of one session." In 5.19 it is also a durable `AuthTokenFamily` row: family revoke marks that family, revokes its refresh rows, and deletes its associated access rows atomically. Access rows from before family association are conservatively deleted only for the same user and user type; they are never guessed into a family.
|
|
1010
1024
|
|
|
1011
1025
|
## Listing active sessions
|
|
1012
1026
|
|
|
@@ -1028,9 +1042,8 @@ Use this for "active sessions" UIs. Revoke a specific session by calling `.revok
|
|
|
1028
1042
|
// Specific access token
|
|
1029
1043
|
await authService.removeAccessToken(user, accessTokenString);
|
|
1030
1044
|
|
|
1031
|
-
// Specific
|
|
1032
|
-
|
|
1033
|
-
await rt?.revoke();
|
|
1045
|
+
// Specific session/family
|
|
1046
|
+
await authService.revokeTokenFamily(familyId);
|
|
1034
1047
|
|
|
1035
1048
|
// All access tokens for a user
|
|
1036
1049
|
await authService.removeAllAccessTokens(user);
|
|
@@ -1112,6 +1125,7 @@ The package signs access and refresh tokens with independent secrets — `config
|
|
|
1112
1125
|
- Don't disable rotation (`config.auth.refreshToken.rotation = false`) unless you genuinely understand the tradeoff — you lose replay detection.
|
|
1113
1126
|
- Don't increase `maxPerUser` to a huge number "to be safe." Each active refresh token is a revocation surface; fewer simultaneous tokens means less attack surface.
|
|
1114
1127
|
- Don't manually delete `AccessToken` rows in a service. The user might be hitting a request mid-revoke and get an inconsistent state. Use the `authService` helpers.
|
|
1128
|
+
- Run the additive `authMigrations` on upgrade. Do not alter or backfill token rows yourself; legacy access tokens are handled conservatively on family revoke.
|
|
1115
1129
|
|
|
1116
1130
|
## See also
|
|
1117
1131
|
|
|
@@ -1124,7 +1138,7 @@ The package signs access and refresh tokens with independent secrets — `config
|
|
|
1124
1138
|
|
|
1125
1139
|
---
|
|
1126
1140
|
name: overview
|
|
1127
|
-
description:
|
|
1141
|
+
description: "@warlock.js/auth overview for orientation and choosing the focused skill for the task."
|
|
1128
1142
|
---
|
|
1129
1143
|
|
|
1130
1144
|
# `@warlock.js/auth` — overview
|
|
@@ -1145,7 +1159,7 @@ Skip if you're not on `@warlock.js/core` (the package depends on it), or if you
|
|
|
1145
1159
|
|
|
1146
1160
|
## The mental model in one paragraph
|
|
1147
1161
|
|
|
1148
|
-
Your user model extends the `Auth` base model and declares its `userType`. A login flows through `authService.login(Model, credentials, deviceInfo?)`: it verifies the password, issues
|
|
1162
|
+
Your user model extends the `Auth` base model and declares its `userType`. A login flows through `authService.login(Model, credentials, deviceInfo?)`: it verifies the password, issues a persisted access + refresh pair, and fires events. `authMiddleware("user")` is a typed hard gate; `authMiddleware()` resolves the configured default type; the object form selects header/cookie credentials, `optional`, and a page-local redirect. Refresh rotation uses a durable token family: family revocation invalidates its refresh and associated access rows. CLI commands generate the JWT secret and clean up expired tokens.
|
|
1149
1163
|
|
|
1150
1164
|
## Skills index
|
|
1151
1165
|
|
|
@@ -1165,7 +1179,7 @@ Start here. The `Auth` base model, `authMiddleware` gate, `authService` (login/l
|
|
|
1165
1179
|
Sign up a new user and issue the first token pair — `User.create({ ...password: await hashPassword(plain) })` then `authService.createTokenPair(user)`. For `POST /register`.
|
|
1166
1180
|
|
|
1167
1181
|
#### [`protect-routes`](@warlock.js/auth/protect-routes/SKILL.md)
|
|
1168
|
-
`authMiddleware
|
|
1182
|
+
`authMiddleware` object and legacy overloads: typed/default hard gates, optional resolution, credential source, and page-local redirects. Sets `request.locals.user` + `request.decodedAccessToken` when a user resolves.
|
|
1169
1183
|
|
|
1170
1184
|
### Going deeper
|
|
1171
1185
|
|
|
@@ -1202,12 +1216,12 @@ Two CLI commands — `warlock jwt.generate` (strong JWT secret → `.env`) and `
|
|
|
1202
1216
|
|
|
1203
1217
|
---
|
|
1204
1218
|
name: protect-routes
|
|
1205
|
-
description:
|
|
1219
|
+
description: "Gate routes with authMiddleware in @warlock.js/auth; use when you need to protect routes."
|
|
1206
1220
|
---
|
|
1207
1221
|
|
|
1208
1222
|
# Gate routes with `authMiddleware`
|
|
1209
1223
|
|
|
1210
|
-
`authMiddleware
|
|
1224
|
+
`authMiddleware` returns a Warlock middleware. Attach it to routes or route groups. The legacy string overload is preserved; the object overload makes credential source, optional resolution, and a page-local redirect explicit.
|
|
1211
1225
|
|
|
1212
1226
|
```ts title="src/app/users/models/user/user.model.ts"
|
|
1213
1227
|
import { Auth } from "@warlock.js/auth";
|
|
@@ -1235,14 +1249,13 @@ const accountController: RequestHandler = async ({ response }) => response.succe
|
|
|
1235
1249
|
const adminController: RequestHandler = async ({ response }) => response.success({});
|
|
1236
1250
|
const staffController: RequestHandler = async ({ response }) => response.success({});
|
|
1237
1251
|
|
|
1238
|
-
//
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
middleware: [authMiddleware([])], // empty array = "any logged-in user"
|
|
1252
|
+
// Required default type (`auth.defaultUserType`, or the sole configured type)
|
|
1253
|
+
router.get("/account", accountController, {
|
|
1254
|
+
middleware: [authMiddleware()],
|
|
1242
1255
|
});
|
|
1243
1256
|
|
|
1244
1257
|
// Mode 2 — required, specific user type(s)
|
|
1245
|
-
// Rejects with 401
|
|
1258
|
+
// Rejects missing/invalid credentials with 401 and a disallowed user type with 403.
|
|
1246
1259
|
router.get("/admin", adminController, {
|
|
1247
1260
|
middleware: [authMiddleware("admin")],
|
|
1248
1261
|
});
|
|
@@ -1271,13 +1284,13 @@ A token passes three separate checks, in order — a token can fail any one of t
|
|
|
1271
1284
|
2. **The access-token row still exists** — deleting it (logout) invalidates the token immediately, before its JWT expiry.
|
|
1272
1285
|
3. **The row's own `expires_at` has not passed** (since 4.12.0). The database is the authority on the session: a row whose expiry has elapsed is rejected and deleted, even if the JWT itself is still within its lifetime. A row with a missing or unparseable `expires_at` is treated as expired, not as never-expiring.
|
|
1273
1286
|
|
|
1274
|
-
|
|
1287
|
+
Missing or invalid credentials return 401; an authenticated disallowed user type returns 403:
|
|
1275
1288
|
|
|
1276
1289
|
| Error code | When |
|
|
1277
1290
|
| --- | --- |
|
|
1278
|
-
| `MissingAccessToken` | No
|
|
1291
|
+
| `MissingAccessToken` | No credential at the configured source |
|
|
1279
1292
|
| `InvalidAccessToken` | Token doesn't verify (signature, missing/passed `exp`, wrong token type), has no DB row, or the row's `expires_at` has passed |
|
|
1280
|
-
| `Unauthorized` | Token valid but user-type isn't in the allowed list |
|
|
1293
|
+
| `Unauthorized` | Token valid but user-type isn't in the allowed list (403) |
|
|
1281
1294
|
|
|
1282
1295
|
## Page routes: redirect to login instead of the JSON 401
|
|
1283
1296
|
|
|
@@ -1305,7 +1318,7 @@ Behavior, precisely:
|
|
|
1305
1318
|
|
|
1306
1319
|
The page-vs-API signal is `request.route.isPage`. Config is typed as `PageAuthConfig` on `AuthConfigurations` (`loginPath?: string`, `returnUrlParam?: string`, default `"returnUrl"`); read at runtime through `authConfig.pageAuth`.
|
|
1307
1320
|
|
|
1308
|
-
> `
|
|
1321
|
+
> A local `redirect` object is preferred for a page that needs a different login destination; absent it, `auth.pageAuth` remains the compatibility fallback.
|
|
1309
1322
|
|
|
1310
1323
|
## Reading the user in a controller
|
|
1311
1324
|
|
|
@@ -1344,31 +1357,23 @@ router.group({ prefix: "/admin", middleware: [authMiddleware("admin")] }, () =>
|
|
|
1344
1357
|
|
|
1345
1358
|
Every route inside the group is gated — the group's `middleware` array applies to each route in the callback. Cleaner than repeating the middleware on each route.
|
|
1346
1359
|
|
|
1347
|
-
## Optional auth
|
|
1348
|
-
|
|
1349
|
-
`
|
|
1350
|
-
|
|
1351
|
-
```ts
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
// Middleware receives the context object ({ request, response }), like a handler.
|
|
1357
|
-
export const optionalAuth: Middleware = async ({ request }) => {
|
|
1358
|
-
const token = request.authorizationValue;
|
|
1359
|
-
if (!token) return; // absent → continue anonymously; the route decides
|
|
1360
|
-
try {
|
|
1361
|
-
const decoded = await jwt.verify(token);
|
|
1362
|
-
// hydrate request.locals.user from your token/user model when valid
|
|
1363
|
-
} catch {
|
|
1364
|
-
// invalid token on an optional route → treat as anonymous, don't reject
|
|
1365
|
-
}
|
|
1366
|
-
// returning nothing (undefined) === continue to the handler
|
|
1367
|
-
};
|
|
1368
|
-
```
|
|
1369
|
-
|
|
1370
|
-
Wire it like any middleware (`{ middleware: [optionalAuth] }`), then branch on `request.locals.user` in the controller. Use `authMiddleware` when the route must be gated; use this when the route is public but personalizes for a signed-in user. `auth.pageAuth` (above) is the built-in **adapter** over this same "unauthenticated" outcome for the common page-redirect case — it is not a competing mechanism.
|
|
1360
|
+
## Optional auth
|
|
1361
|
+
|
|
1362
|
+
Use `optional: true` in the object form when a public route should receive a verified user when one is present. Missing or invalid credentials continue anonymously; a hard gate remains the default.
|
|
1363
|
+
|
|
1364
|
+
```ts
|
|
1365
|
+
middleware: [authMiddleware({ source: "header", optional: true })]
|
|
1366
|
+
```
|
|
1367
|
+
|
|
1368
|
+
## Automatic cookie renewal
|
|
1371
1369
|
|
|
1370
|
+
Add `refresh: { source: "cookie", key: "refresh_token", overlapMs?: 5000 }`
|
|
1371
|
+
to a typed cookie middleware to renew an absent, invalid, or expired access
|
|
1372
|
+
cookie before the handler. It writes the successor access and refresh cookies
|
|
1373
|
+
through `authService.setAuthCookie`; it never replays a handler. `overlapMs` is
|
|
1374
|
+
clamped to 0–10,000 ms (default 5,000). Only the exact immediate successor is
|
|
1375
|
+
tolerated during that window. The legacy `refreshTokens` API stays strict; a
|
|
1376
|
+
later old-token presentation revokes its family.
|
|
1372
1377
|
## Custom error responses
|
|
1373
1378
|
|
|
1374
1379
|
The middleware uses the framework's `response.unauthorized({...})` shape. To override the response globally, hook the framework's error transformer to remap `AuthErrorCodes.*` codes.
|
|
@@ -1410,13 +1415,13 @@ router.post("/articles", articlesController, { middleware: [authMiddleware([]),
|
|
|
1410
1415
|
|
|
1411
1416
|
Use `authMiddleware` to establish *who the caller is*, and `access` to decide *what they may do*. They compose — `access` reads the user `authMiddleware` put on the request.
|
|
1412
1417
|
|
|
1413
|
-
## Cookie
|
|
1418
|
+
## Cookie credentials
|
|
1414
1419
|
|
|
1415
|
-
|
|
1420
|
+
Use `{ source: "cookie", key: "token" }` in the object form. The same checks and `request.locals.user` outcome apply; legacy `authMiddleware("user", "cookie:token")` is also supported.
|
|
1416
1421
|
|
|
1417
1422
|
```ts
|
|
1418
1423
|
router.get("/browser-account", accountController, {
|
|
1419
|
-
middleware: [authMiddleware(
|
|
1424
|
+
middleware: [authMiddleware({ source: "cookie", key: "token" })],
|
|
1420
1425
|
});
|
|
1421
1426
|
```
|
|
1422
1427
|
|
|
@@ -1514,7 +1519,7 @@ This is an Origin check, not a double-submit CSRF token — cheaper, and closes
|
|
|
1514
1519
|
|
|
1515
1520
|
---
|
|
1516
1521
|
name: register-user
|
|
1517
|
-
description:
|
|
1522
|
+
description: "Register-and-issue-tokens flow in @warlock.js/auth; use when you need to register user."
|
|
1518
1523
|
---
|
|
1519
1524
|
|
|
1520
1525
|
# Register-and-issue-tokens flow
|
|
@@ -1665,7 +1670,7 @@ Cleaner alternative: emit your own `user.registered` event from the controller a
|
|
|
1665
1670
|
|
|
1666
1671
|
---
|
|
1667
1672
|
name: run-auth-commands
|
|
1668
|
-
description:
|
|
1673
|
+
description: "Run auth commands in @warlock.js/auth; use when you need to run auth commands."
|
|
1669
1674
|
---
|
|
1670
1675
|
|
|
1671
1676
|
# Run auth commands
|
|
@@ -1828,7 +1833,7 @@ Register it the same way as the bundled commands — call the factory inside `de
|
|
|
1828
1833
|
|
|
1829
1834
|
---
|
|
1830
1835
|
name: throttle-login-attempts
|
|
1831
|
-
description:
|
|
1836
|
+
description: "Throttle login attempts with loginThrottleMiddleware in @warlock.js/auth; use when you need to throttle login attempts."
|
|
1832
1837
|
---
|
|
1833
1838
|
|
|
1834
1839
|
# Throttle login attempts with `loginThrottleMiddleware`
|
|
@@ -1902,7 +1907,7 @@ On lockout the middleware sends:
|
|
|
1902
1907
|
{ "error": "...", "errorCode": "EC004" } // AuthErrorCodes.TooManyAttempts
|
|
1903
1908
|
```
|
|
1904
1909
|
|
|
1905
|
-
|
|
1910
|
+
`@warlock.js/auth` ships built-in `en` and `ar` text for `auth.errors.tooManyAttempts` (and every other `auth.errors.*` key it uses); define the key in your own locale files, or pass `errorMessage`, to override it. Map `EC004` in your error transformer the same way you map the other `AuthErrorCodes`.
|
|
1906
1911
|
|
|
1907
1912
|
## Beyond login
|
|
1908
1913
|
|
|
@@ -1948,7 +1953,7 @@ They compose. For a hard ceiling on request volume *and* failure-aware account l
|
|
|
1948
1953
|
|
|
1949
1954
|
---
|
|
1950
1955
|
name: verify-email-and-reset-password
|
|
1951
|
-
description:
|
|
1956
|
+
description: "Verify email + reset password in @warlock.js/auth; use when you need to verify email and reset password."
|
|
1952
1957
|
---
|
|
1953
1958
|
|
|
1954
1959
|
# Verify email + reset password
|