@c9up/warden 0.2.2 → 0.2.4

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.
Files changed (109) hide show
  1. package/README.md +5 -3
  2. package/dist/AttemptStore.d.ts +64 -0
  3. package/dist/AttemptStore.d.ts.map +1 -0
  4. package/dist/AttemptStore.js +117 -0
  5. package/dist/AttemptStore.js.map +1 -0
  6. package/dist/AuthManager.d.ts +5 -3
  7. package/dist/AuthManager.d.ts.map +1 -1
  8. package/dist/AuthManager.js +5 -4
  9. package/dist/AuthManager.js.map +1 -1
  10. package/dist/AuthRateLimiter.d.ts +44 -10
  11. package/dist/AuthRateLimiter.d.ts.map +1 -1
  12. package/dist/AuthRateLimiter.js +58 -73
  13. package/dist/AuthRateLimiter.js.map +1 -1
  14. package/dist/Authenticator.d.ts +8 -11
  15. package/dist/Authenticator.d.ts.map +1 -1
  16. package/dist/Authenticator.js +72 -9
  17. package/dist/Authenticator.js.map +1 -1
  18. package/dist/RememberMeToken.d.ts +15 -4
  19. package/dist/RememberMeToken.d.ts.map +1 -1
  20. package/dist/RememberMeToken.js +20 -7
  21. package/dist/RememberMeToken.js.map +1 -1
  22. package/dist/bouncer/evaluate.d.ts +0 -8
  23. package/dist/bouncer/evaluate.d.ts.map +1 -1
  24. package/dist/bouncer/evaluate.js +1 -1
  25. package/dist/bouncer/evaluate.js.map +1 -1
  26. package/dist/configure.d.ts +6 -0
  27. package/dist/configure.d.ts.map +1 -1
  28. package/dist/configure.js +2 -21
  29. package/dist/configure.js.map +1 -1
  30. package/dist/duration.d.ts.map +1 -1
  31. package/dist/duration.js +6 -0
  32. package/dist/duration.js.map +1 -1
  33. package/dist/errors.d.ts +6 -0
  34. package/dist/errors.d.ts.map +1 -1
  35. package/dist/errors.js +6 -0
  36. package/dist/errors.js.map +1 -1
  37. package/dist/index.d.ts +8 -6
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +2 -1
  40. package/dist/index.js.map +1 -1
  41. package/dist/mfa/BackupCodesProvider.d.ts +8 -1
  42. package/dist/mfa/BackupCodesProvider.d.ts.map +1 -1
  43. package/dist/mfa/BackupCodesProvider.js +5 -7
  44. package/dist/mfa/BackupCodesProvider.js.map +1 -1
  45. package/dist/mfa/MfaManager.d.ts +18 -2
  46. package/dist/mfa/MfaManager.d.ts.map +1 -1
  47. package/dist/mfa/MfaManager.js +38 -35
  48. package/dist/mfa/MfaManager.js.map +1 -1
  49. package/dist/mfa/TotpProvider.d.ts +30 -6
  50. package/dist/mfa/TotpProvider.d.ts.map +1 -1
  51. package/dist/mfa/TotpProvider.js +48 -19
  52. package/dist/mfa/TotpProvider.js.map +1 -1
  53. package/dist/mfa/WebauthnProvider.d.ts.map +1 -1
  54. package/dist/mfa/WebauthnProvider.js +4 -1
  55. package/dist/mfa/WebauthnProvider.js.map +1 -1
  56. package/dist/middleware.d.ts +2 -0
  57. package/dist/middleware.d.ts.map +1 -1
  58. package/dist/middleware.js +3 -0
  59. package/dist/middleware.js.map +1 -1
  60. package/dist/native/generated.d.ts +0 -5
  61. package/dist/native/generated.d.ts.map +1 -1
  62. package/dist/native.d.ts +0 -2
  63. package/dist/native.d.ts.map +1 -1
  64. package/dist/native.js +0 -4
  65. package/dist/native.js.map +1 -1
  66. package/dist/strategies/JwtStrategy.d.ts +11 -1
  67. package/dist/strategies/JwtStrategy.d.ts.map +1 -1
  68. package/dist/strategies/JwtStrategy.js +9 -2
  69. package/dist/strategies/JwtStrategy.js.map +1 -1
  70. package/dist/strategies/SessionStrategy.d.ts +22 -0
  71. package/dist/strategies/SessionStrategy.d.ts.map +1 -1
  72. package/dist/strategies/SessionStrategy.js +31 -1
  73. package/dist/strategies/SessionStrategy.js.map +1 -1
  74. package/dist/stubs.d.ts +14 -0
  75. package/dist/stubs.d.ts.map +1 -0
  76. package/dist/stubs.js +15 -0
  77. package/dist/stubs.js.map +1 -0
  78. package/dist/vendor/nodeEnv.d.ts +5 -0
  79. package/dist/vendor/nodeEnv.d.ts.map +1 -0
  80. package/dist/vendor/nodeEnv.js +38 -0
  81. package/dist/vendor/nodeEnv.js.map +1 -0
  82. package/index.darwin-arm64.node +0 -0
  83. package/index.darwin-x64.node +0 -0
  84. package/index.linux-arm64-gnu.node +0 -0
  85. package/index.linux-x64-gnu.node +0 -0
  86. package/index.win32-x64-msvc.node +0 -0
  87. package/package.json +4 -3
  88. package/src/AttemptStore.ts +158 -0
  89. package/src/AuthManager.ts +9 -7
  90. package/src/AuthRateLimiter.ts +85 -80
  91. package/src/Authenticator.ts +92 -13
  92. package/src/RememberMeToken.ts +36 -8
  93. package/src/bouncer/evaluate.ts +1 -1
  94. package/src/configure.ts +8 -24
  95. package/src/duration.ts +8 -0
  96. package/src/errors.ts +11 -1
  97. package/src/index.ts +24 -3
  98. package/src/mfa/BackupCodesProvider.ts +13 -14
  99. package/src/mfa/MfaManager.ts +58 -43
  100. package/src/mfa/TotpProvider.ts +90 -22
  101. package/src/mfa/WebauthnProvider.ts +4 -1
  102. package/src/middleware.ts +9 -0
  103. package/src/native/generated.ts +0 -14
  104. package/src/native.ts +0 -5
  105. package/src/strategies/JwtStrategy.ts +20 -2
  106. package/src/strategies/SessionStrategy.ts +52 -1
  107. package/src/stubs.ts +16 -0
  108. package/src/vendor/nodeEnv.ts +37 -0
  109. package/stubs/config/auth.stub +23 -0
@@ -60,6 +60,21 @@ export interface SessionStrategyConfig {
60
60
  * default matches the `web` guard an app is most likely migrating.
61
61
  */
62
62
  rememberMeCookieName?: string;
63
+ /**
64
+ * Attributes of the remember-me cookie, over the defaults: `path: "/"`,
65
+ * `sameSite: "lax"`, and `secure` in production. The cookie is a bearer
66
+ * credential that lives for years, so it never leaves over plain HTTP or
67
+ * rides a cross-site POST unless the app says so here.
68
+ */
69
+ rememberMeCookie?: RememberMeCookieAttributes;
70
+ }
71
+
72
+ /** What an app may set on the remember-me cookie. */
73
+ export interface RememberMeCookieAttributes {
74
+ path?: string;
75
+ domain?: string;
76
+ sameSite?: "lax" | "strict" | "none" | false;
77
+ secure?: boolean;
63
78
  }
64
79
 
65
80
  /** Two years, Adonis' `rememberMeTokensAge` default. */
@@ -102,6 +117,15 @@ export class SessionStrategy implements AuthStrategy {
102
117
  #sessionKey: string;
103
118
 
104
119
  constructor(config: SessionStrategyConfig) {
120
+ // A NaN age made every token's expiry NaN, which no "is it past?" test
121
+ // ever caught: the token never expired.
122
+ const age = config.rememberMeAge;
123
+ if (age !== undefined && (!Number.isSafeInteger(age) || age <= 0)) {
124
+ throw new WardenError(
125
+ "INVALID_CONFIG",
126
+ `rememberMeAge must be a positive whole number of seconds, got ${String(age)}`,
127
+ );
128
+ }
105
129
  this.#config = config;
106
130
  this.#sessionKey = config.sessionKey ?? "auth_user_id";
107
131
  }
@@ -111,6 +135,11 @@ export class SessionStrategy implements AuthStrategy {
111
135
  return this.#config.rememberMeCookieName ?? "remember_web";
112
136
  }
113
137
 
138
+ /** The remember-me cookie's attributes the app configured, if any. */
139
+ get rememberMeCookie(): RememberMeCookieAttributes | undefined {
140
+ return this.#config.rememberMeCookie;
141
+ }
142
+
114
143
  /** Whether "keep me signed in" is wired at all. */
115
144
  get usesRememberMeTokens(): boolean {
116
145
  return this.#config.rememberMeTokens !== undefined;
@@ -230,7 +259,12 @@ export class SessionStrategy implements AuthStrategy {
230
259
  return { authenticated: false, error: "Invalid session data" };
231
260
  const user = await this.#config.findUser(userId);
232
261
  if (!user) return { authenticated: false, error: "User not found" };
233
- return { authenticated: true, user };
262
+ // `mfa` is this SESSION's step-up, whatever `findUser` said (see
263
+ // markMfaVerified).
264
+ return {
265
+ authenticated: true,
266
+ user: { ...user, mfa: context.session.get(this.#mfaKey()) === true },
267
+ };
234
268
  }
235
269
 
236
270
  /**
@@ -254,6 +288,22 @@ export class SessionStrategy implements AuthStrategy {
254
288
  seatSession(user: UserPayload, session: SessionStore): void {
255
289
  session.regenerate();
256
290
  session.put(this.#sessionKey, user.id);
291
+ // A new sign-in has not stepped up yet, whatever the session held before.
292
+ session.forget(this.#mfaKey());
293
+ }
294
+
295
+ /**
296
+ * Record that this session completed an MFA step-up (`MfaManager.verify()`
297
+ * succeeded), which `@RequireMfa()` requires. Cleared by the next sign-in
298
+ * and by logout.
299
+ */
300
+ markMfaVerified(session: SessionStore): void {
301
+ session.put(this.#mfaKey(), true);
302
+ }
303
+
304
+ /** The session key of the step-up marker, beside the user id's. */
305
+ #mfaKey(): string {
306
+ return `${this.#sessionKey}_mfa`;
257
307
  }
258
308
 
259
309
  async login(
@@ -294,6 +344,7 @@ export class SessionStrategy implements AuthStrategy {
294
344
  state?: SessionGuardState,
295
345
  ): Promise<void> {
296
346
  session.forget(this.#sessionKey);
347
+ session.forget(this.#mfaKey());
297
348
  if (state) {
298
349
  state.viaRemember = false;
299
350
  state.attemptedViaRemember = false;
package/src/stubs.ts ADDED
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Where this package's stubs live.
3
+ *
4
+ * Its own module, as upstream keeps it: a stub root is a path a package
5
+ * publishes, and a caller importing it should not have to import the
6
+ * configure hook to get it.
7
+ *
8
+ * `import.meta.dirname` rather than a URL — upstream reads it straight off
9
+ * `import.meta`, and deriving it from `import.meta.url` breaks wherever that
10
+ * is not a `file://` URL, which is what a test runner serving modules over
11
+ * HTTP hands you.
12
+ */
13
+
14
+ import { join } from "node:path";
15
+
16
+ export const stubsRoot = join(import.meta.dirname, "..", "stubs");
@@ -0,0 +1,37 @@
1
+ // Generated from scripts/vendor/nodeEnv.ts — do not edit.
2
+ //
3
+ // This package is published and built from its own repository, so the file
4
+ // has to exist here rather than be imported. `pnpm vendor:sync` rewrites it,
5
+ // and `pnpm vendor:check` fails if this copy has drifted from the original.
6
+
7
+ /**
8
+ * `NODE_ENV`, read the way every runtime actually spells it.
9
+ *
10
+ * `NODE_ENV=prod` is ordinary in a Dockerfile or a platform dashboard, and a
11
+ * bare `=== "production"` answers "not production" for it. What that costs
12
+ * differs per package, so the consequence is stated where the decision is
13
+ * taken, not here.
14
+ *
15
+ * The tables mirror the framework's (`@c9up/ream`, src/env/nodeEnv.ts), which
16
+ * is the origin rather than a peer: it formats to different rules and exposes
17
+ * `currentNodeEnv()` instead of `inProduction()`.
18
+ */
19
+
20
+ const DEV_ENVS = ["dev", "develop", "development"];
21
+ const PROD_ENVS = ["prod", "production"];
22
+ const TEST_ENVS = ["test", "testing"];
23
+
24
+ /** The canonical name for whatever `NODE_ENV` holds. */
25
+ export function normalizeNodeEnv(value: string | undefined): string {
26
+ if (!value || typeof value !== "string") return "unknown";
27
+ const env = value.toLowerCase();
28
+ if (DEV_ENVS.includes(env)) return "development";
29
+ if (PROD_ENVS.includes(env)) return "production";
30
+ if (TEST_ENVS.includes(env)) return "test";
31
+ return env;
32
+ }
33
+
34
+ /** Whether this process is running in production, under any spelling. */
35
+ export function inProduction(): boolean {
36
+ return normalizeNodeEnv(process.env.NODE_ENV) === "production";
37
+ }
@@ -0,0 +1,23 @@
1
+ ---
2
+ to: config/auth.ts
3
+ ---
4
+ import { defineConfig, jwtGuard } from '@c9up/warden'
5
+
6
+ export default defineConfig({
7
+ default: 'jwt',
8
+ guards: {
9
+ jwt: jwtGuard({
10
+ secret: process.env.JWT_SECRET ?? '',
11
+ // Seconds, or a duration string — JWT_EXPIRY=1h works as written.
12
+ expiresIn: process.env.JWT_EXPIRY ?? 3600,
13
+ // TODO: wire these to your user model (e.g. via your ORM).
14
+ // The JWT guard needs both to issue and verify tokens.
15
+ findUser: async (_id) => {
16
+ throw new Error('TODO: implement findUser(id) for the JWT guard in config/auth.ts')
17
+ },
18
+ verifyCredentials: async (_email, _password) => {
19
+ throw new Error('TODO: implement verifyCredentials(email, password) in config/auth.ts')
20
+ },
21
+ }),
22
+ },
23
+ })