@c9up/warden 0.1.12 → 0.1.14

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 (96) hide show
  1. package/dist/AuthManager.d.ts +45 -9
  2. package/dist/AuthManager.d.ts.map +1 -1
  3. package/dist/AuthManager.js +75 -23
  4. package/dist/AuthManager.js.map +1 -1
  5. package/dist/Authenticator.d.ts +128 -0
  6. package/dist/Authenticator.d.ts.map +1 -0
  7. package/dist/Authenticator.js +296 -0
  8. package/dist/Authenticator.js.map +1 -0
  9. package/dist/WardenProvider.d.ts.map +1 -1
  10. package/dist/WardenProvider.js +51 -23
  11. package/dist/WardenProvider.js.map +1 -1
  12. package/dist/bouncer/AbilitiesBuilder.d.ts +20 -0
  13. package/dist/bouncer/AbilitiesBuilder.d.ts.map +1 -0
  14. package/dist/bouncer/AbilitiesBuilder.js +32 -0
  15. package/dist/bouncer/AbilitiesBuilder.js.map +1 -0
  16. package/dist/bouncer/AuthorizationResponse.d.ts +18 -8
  17. package/dist/bouncer/AuthorizationResponse.d.ts.map +1 -1
  18. package/dist/bouncer/AuthorizationResponse.js +20 -8
  19. package/dist/bouncer/AuthorizationResponse.js.map +1 -1
  20. package/dist/bouncer/BasePolicy.d.ts +4 -3
  21. package/dist/bouncer/BasePolicy.d.ts.map +1 -1
  22. package/dist/bouncer/BasePolicy.js.map +1 -1
  23. package/dist/bouncer/Bouncer.d.ts +9 -2
  24. package/dist/bouncer/Bouncer.d.ts.map +1 -1
  25. package/dist/bouncer/Bouncer.js +59 -10
  26. package/dist/bouncer/Bouncer.js.map +1 -1
  27. package/dist/bouncer/PolicyAuthorizer.d.ts +2 -1
  28. package/dist/bouncer/PolicyAuthorizer.d.ts.map +1 -1
  29. package/dist/bouncer/PolicyAuthorizer.js +11 -3
  30. package/dist/bouncer/PolicyAuthorizer.js.map +1 -1
  31. package/dist/bouncer/evaluate.d.ts +1 -1
  32. package/dist/bouncer/evaluate.d.ts.map +1 -1
  33. package/dist/bouncer/evaluate.js +3 -2
  34. package/dist/bouncer/evaluate.js.map +1 -1
  35. package/dist/bouncer/types.d.ts +22 -1
  36. package/dist/bouncer/types.d.ts.map +1 -1
  37. package/dist/config.d.ts +34 -3
  38. package/dist/config.d.ts.map +1 -1
  39. package/dist/config.js +15 -0
  40. package/dist/config.js.map +1 -1
  41. package/dist/configure.d.ts.map +1 -1
  42. package/dist/configure.js +15 -13
  43. package/dist/configure.js.map +1 -1
  44. package/dist/errors.d.ts +27 -0
  45. package/dist/errors.d.ts.map +1 -1
  46. package/dist/errors.js +37 -1
  47. package/dist/errors.js.map +1 -1
  48. package/dist/index.d.ts +7 -3
  49. package/dist/index.d.ts.map +1 -1
  50. package/dist/index.js +4 -2
  51. package/dist/index.js.map +1 -1
  52. package/dist/mfa/OtpProvider.d.ts.map +1 -1
  53. package/dist/mfa/OtpProvider.js +21 -3
  54. package/dist/mfa/OtpProvider.js.map +1 -1
  55. package/dist/mfa/WebauthnProvider.d.ts +24 -2
  56. package/dist/mfa/WebauthnProvider.d.ts.map +1 -1
  57. package/dist/mfa/WebauthnProvider.js +28 -8
  58. package/dist/mfa/WebauthnProvider.js.map +1 -1
  59. package/dist/middleware.d.ts +29 -6
  60. package/dist/middleware.d.ts.map +1 -1
  61. package/dist/middleware.js +85 -166
  62. package/dist/middleware.js.map +1 -1
  63. package/dist/standalone.d.ts.map +1 -1
  64. package/dist/standalone.js +21 -13
  65. package/dist/standalone.js.map +1 -1
  66. package/dist/strategies/ApiKeyStrategy.d.ts.map +1 -1
  67. package/dist/strategies/ApiKeyStrategy.js +4 -1
  68. package/dist/strategies/ApiKeyStrategy.js.map +1 -1
  69. package/dist/strategies/SessionStrategy.js +2 -2
  70. package/dist/strategies/SessionStrategy.js.map +1 -1
  71. package/index.darwin-arm64.node +0 -0
  72. package/index.darwin-x64.node +0 -0
  73. package/index.linux-arm64-gnu.node +0 -0
  74. package/index.linux-x64-gnu.node +0 -0
  75. package/index.win32-x64-msvc.node +0 -0
  76. package/package.json +2 -1
  77. package/src/AuthManager.ts +125 -28
  78. package/src/Authenticator.ts +385 -0
  79. package/src/WardenProvider.ts +51 -25
  80. package/src/bouncer/AbilitiesBuilder.ts +41 -0
  81. package/src/bouncer/AuthorizationResponse.ts +24 -16
  82. package/src/bouncer/BasePolicy.ts +4 -2
  83. package/src/bouncer/Bouncer.ts +77 -14
  84. package/src/bouncer/PolicyAuthorizer.ts +14 -4
  85. package/src/bouncer/evaluate.ts +4 -2
  86. package/src/bouncer/types.ts +24 -1
  87. package/src/config.ts +47 -3
  88. package/src/configure.ts +15 -13
  89. package/src/errors.ts +43 -1
  90. package/src/index.ts +22 -2
  91. package/src/mfa/OtpProvider.ts +31 -3
  92. package/src/mfa/WebauthnProvider.ts +58 -12
  93. package/src/middleware.ts +112 -207
  94. package/src/standalone.ts +22 -14
  95. package/src/strategies/ApiKeyStrategy.ts +4 -1
  96. package/src/strategies/SessionStrategy.ts +2 -2
@@ -5,6 +5,7 @@
5
5
  */
6
6
  import { RightsResolver } from "./rights/RightsResolver.js";
7
7
  import type { EffectivePermissions, Scope } from "./rights/types.js";
8
+ import type { SessionStore } from "./strategies/SessionStrategy.js";
8
9
  export interface UserPayload {
9
10
  id: string;
10
11
  roles?: string[];
@@ -29,15 +30,32 @@ export interface AuthStrategy {
29
30
  authenticate(credentials: Record<string, unknown>): Promise<AuthResult>;
30
31
  verify(token: string, context?: Record<string, unknown>): Promise<AuthResult>;
31
32
  }
33
+ /**
34
+ * AuthManager configuration. Two equivalent forms are accepted:
35
+ *
36
+ * - AdonisJS form (preferred): `{ default, guards }` — `default` names the
37
+ * guard used when none is specified, `guards` maps guard name → strategy.
38
+ * - Legacy form: `{ defaultStrategy, strategies }` — kept working so existing
39
+ * call sites and tests need no change.
40
+ *
41
+ * Exactly one of each pair must be supplied. Internally normalised to the
42
+ * AdonisJS names (`default` / `guards`).
43
+ */
32
44
  export interface AuthConfig {
33
- defaultStrategy: string;
34
- strategies: Record<string, AuthStrategy>;
45
+ /** AdonisJS name for the default guard. */
46
+ default?: string;
47
+ /** AdonisJS name for the guard map (name → strategy). */
48
+ guards?: Record<string, AuthStrategy>;
49
+ /** Legacy alias for {@link AuthConfig.default}. */
50
+ defaultStrategy?: string;
51
+ /** Legacy alias for {@link AuthConfig.guards}. */
52
+ strategies?: Record<string, AuthStrategy>;
35
53
  /**
36
54
  * The rights resolver backing the coarse RBAC helpers (Epic 56). When
37
55
  * absent, a default `RightsResolver(new MemoryRightsStore())` is used so
38
- * `new AuthManager({ defaultStrategy, strategies })` keeps working — with
39
- * an empty store, payload roles still fold in (D2) and permissions are
40
- * empty (token `user.permissions` is not an input — D1).
56
+ * `new AuthManager({ default, guards })` keeps working — with an empty
57
+ * store, payload roles still fold in (D2) and permissions are empty (token
58
+ * `user.permissions` is not an input — D1).
41
59
  */
42
60
  rights?: RightsResolver;
43
61
  }
@@ -45,8 +63,8 @@ export interface AuthConfig {
45
63
  * Manages authentication strategies and provides guard/permission checks.
46
64
  */
47
65
  export declare class AuthManager {
48
- private strategies;
49
- private defaultStrategy;
66
+ private guards;
67
+ private default;
50
68
  private readonly rights;
51
69
  constructor(config: AuthConfig);
52
70
  /** Authenticate with credentials using a specific or default strategy. */
@@ -95,14 +113,32 @@ export declare class AuthManager {
95
113
  /** Get a registered strategy by name. */
96
114
  getStrategy(name?: string): AuthStrategy;
97
115
  /**
98
- * Name of the default strategy (Adonis's "default guard"). Used by
99
- * `silentAuth` to pick which strategy to attempt when a route declares none.
116
+ * Name of the default guard (Adonis's "default guard"). Used by `silentAuth`
117
+ * and the {@link Authenticator} to pick which guard to attempt when a route
118
+ * declares none.
100
119
  */
101
120
  get defaultStrategyName(): string;
102
121
  /** Register a new strategy at runtime. */
103
122
  registerStrategy(name: string, strategy: AuthStrategy): void;
104
123
  /** Get all registered strategy names. */
105
124
  getStrategyNames(): string[];
125
+ /**
126
+ * Log a user in through a session-capable guard (AdonisJS
127
+ * `auth.use('web').login(user)` parity). Delegates to the guard's own
128
+ * `login()` — for `SessionStrategy` this rotates the session id (session
129
+ * fixation defence) and stores the user id. Throws if the resolved guard
130
+ * cannot log in (e.g. JWT / API-key guards, which are stateless).
131
+ *
132
+ * Fixes the misleading `SessionStrategy.authenticate()` sentinel that
133
+ * pointed at a then-nonexistent `authManager.login()` (bug-3261).
134
+ */
135
+ login(user: UserPayload, session: SessionStore, strategyName?: string): Promise<void>;
136
+ /**
137
+ * Log a user out of a session-capable guard (AdonisJS `auth.use('web').logout()`
138
+ * parity). Delegates to the guard's `logout()`. Throws if the resolved guard
139
+ * cannot log out.
140
+ */
141
+ logout(session: SessionStore, strategyName?: string): Promise<void>;
106
142
  }
107
143
  /** Strip dangerous prototype-pollution keys from user payload. */
108
144
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"AuthManager.d.ts","sourceRoot":"","sources":["../src/AuthManager.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,KAAK,EAAE,oBAAoB,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAErE,MAAM,WAAW,WAAW;IAC3B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,EAAE,CAAC;IACvB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACvB;AAED,MAAM,WAAW,UAAU;IAC1B,aAAa,EAAE,OAAO,CAAC;IACvB,IAAI,CAAC,EAAE,WAAW,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;;;OAMG;IACH,aAAa,CAAC,EAAE,IAAI,CAAC;CACrB;AAED,MAAM,WAAW,YAAY;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,CAAC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IACxE,MAAM,CAAC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IAAI,EAAE,WAAW,EACjB,KAAK,GAAE,KAAgB,GACrB,OAAO,CAAC,oBAAoB,CAAC;IAIhC;;;;OAIG;IACG,OAAO,CACZ,IAAI,EAAE,WAAW,EACjB,IAAI,EAAE,MAAM,EACZ,KAAK,GAAE,KAAgB,GACrB,OAAO,CAAC,OAAO,CAAC;IAInB;;;;OAIG;IACG,aAAa,CAClB,IAAI,EAAE,WAAW,EACjB,UAAU,EAAE,MAAM,EAClB,KAAK,GAAE,KAAgB,GACrB,OAAO,CAAC,OAAO,CAAC;IAInB;;;OAGG;IACG,iBAAiB,CACtB,IAAI,EAAE,WAAW,EACjB,WAAW,EAAE,MAAM,EAAE,EACrB,KAAK,GAAE,KAAgB,GACrB,OAAO,CAAC,OAAO,CAAC;IAInB,yCAAyC;IACzC,WAAW,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,YAAY;IAexC;;;;OAIG;IACH,IAAI,mBAAmB,IAAI,MAAM,CAEhC;IAED,0CAA0C;IAC1C,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,YAAY,GAAG,IAAI;IAI5D,yCAAyC;IACzC,gBAAgB,IAAI,MAAM,EAAE;IAI5B;;;;;;;;;OASG;IACG,KAAK,CACV,IAAI,EAAE,WAAW,EACjB,OAAO,EAAE,YAAY,EACrB,YAAY,CAAC,EAAE,MAAM,GACnB,OAAO,CAAC,IAAI,CAAC;IAchB;;;;OAIG;IACG,MAAM,CAAC,OAAO,EAAE,YAAY,EAAE,YAAY,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAUzE;AAwBD,kEAAkE;AAClE;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,WAAW,GAAG,IAAI,CAMvD"}
@@ -18,26 +18,35 @@ function isTokenIssuer(strategy) {
18
18
  * Manages authentication strategies and provides guard/permission checks.
19
19
  */
20
20
  export class AuthManager {
21
- strategies = new Map();
22
- defaultStrategy;
21
+ // AdonisJS names: `guards` (name → strategy) and `default` (the guard used
22
+ // when none is named). Renamed from the previous `strategies`/`defaultStrategy`.
23
+ guards = new Map();
24
+ default;
23
25
  rights;
24
26
  constructor(config) {
25
- this.defaultStrategy = config.defaultStrategy;
27
+ // Normalise the two accepted forms — AdonisJS `{ default, guards }` and
28
+ // the legacy `{ defaultStrategy, strategies }` — to the AdonisJS names.
29
+ const guards = config.guards ?? config.strategies;
30
+ const defaultGuard = config.default ?? config.defaultStrategy;
31
+ if (!guards || defaultGuard === undefined) {
32
+ throw new WardenError("INVALID_CONFIG", "AuthManager: config must supply `default` + `guards` (or the legacy `defaultStrategy` + `strategies`).");
33
+ }
34
+ this.default = defaultGuard;
26
35
  this.rights = config.rights ?? new RightsResolver(new MemoryRightsStore());
27
- for (const [name, strategy] of Object.entries(config.strategies)) {
28
- this.strategies.set(name, strategy);
36
+ for (const [name, strategy] of Object.entries(guards)) {
37
+ this.guards.set(name, strategy);
29
38
  }
30
- // Fail-fast at construction: an AuthManager with zero strategies
31
- // is a configuration bug. Previously, an empty `strategies: {}`
32
- // passed the constructor cleanly and the first protected request
33
- // crashed at runtime when `getStrategy('jwt')` threw — opaque 401
34
- // or 500 instead of a boot-time INVALID_CONFIG that points the
35
- // operator at the missing `config.auth.jwt` (or other strategy).
36
- if (Object.keys(config.strategies).length === 0) {
37
- throw new WardenError("INVALID_CONFIG", `AuthManager: no authentication strategies registered. Configure at least one strategy (e.g. config.warden.auth.jwt) before booting WardenProvider.`);
39
+ // Fail-fast at construction: an AuthManager with zero guards is a
40
+ // configuration bug. Previously, an empty `guards: {}` passed the
41
+ // constructor cleanly and the first protected request crashed at
42
+ // runtime when `getStrategy('jwt')` threw — opaque 401 or 500 instead
43
+ // of a boot-time INVALID_CONFIG that points the operator at the missing
44
+ // `config.auth.jwt` (or other guard).
45
+ if (this.guards.size === 0) {
46
+ throw new WardenError("INVALID_CONFIG", `AuthManager: no authentication guards registered. Configure at least one guard (e.g. config.warden.auth.jwt) before booting WardenProvider.`);
38
47
  }
39
- if (!this.strategies.has(config.defaultStrategy)) {
40
- throw new WardenError("INVALID_CONFIG", `defaultStrategy '${config.defaultStrategy}' is not present in strategies`);
48
+ if (!this.guards.has(defaultGuard)) {
49
+ throw new WardenError("INVALID_CONFIG", `default guard '${defaultGuard}' is not present in guards`);
41
50
  }
42
51
  }
43
52
  /** Authenticate with credentials using a specific or default strategy. */
@@ -98,7 +107,7 @@ export class AuthManager {
98
107
  issueFor(user, strategyName) {
99
108
  const strategy = this.getStrategy(strategyName);
100
109
  if (!isTokenIssuer(strategy)) {
101
- throw new WardenError("STRATEGY_CANNOT_ISSUE", `Auth strategy '${strategyName ?? this.defaultStrategy}' cannot issue tokens.`, {
110
+ throw new WardenError("STRATEGY_CANNOT_ISSUE", `Auth strategy '${strategyName ?? this.default}' cannot issue tokens.`, {
102
111
  hint: "issueFor() needs a token-minting strategy (e.g. JwtStrategy with signToken). Session / API-key strategies don't mint tokens.",
103
112
  });
104
113
  }
@@ -140,8 +149,8 @@ export class AuthManager {
140
149
  }
141
150
  /** Get a registered strategy by name. */
142
151
  getStrategy(name) {
143
- const strategyName = name ?? this.defaultStrategy;
144
- const strategy = this.strategies.get(strategyName);
152
+ const strategyName = name ?? this.default;
153
+ const strategy = this.guards.get(strategyName);
145
154
  if (!strategy) {
146
155
  throw new WardenError("STRATEGY_NOT_FOUND", `Auth strategy '${strategyName}' not registered`, {
147
156
  hint: "Call registerStrategy() before using this strategy name.",
@@ -150,20 +159,63 @@ export class AuthManager {
150
159
  return strategy;
151
160
  }
152
161
  /**
153
- * Name of the default strategy (Adonis's "default guard"). Used by
154
- * `silentAuth` to pick which strategy to attempt when a route declares none.
162
+ * Name of the default guard (Adonis's "default guard"). Used by `silentAuth`
163
+ * and the {@link Authenticator} to pick which guard to attempt when a route
164
+ * declares none.
155
165
  */
156
166
  get defaultStrategyName() {
157
- return this.defaultStrategy;
167
+ return this.default;
158
168
  }
159
169
  /** Register a new strategy at runtime. */
160
170
  registerStrategy(name, strategy) {
161
- this.strategies.set(name, strategy);
171
+ this.guards.set(name, strategy);
162
172
  }
163
173
  /** Get all registered strategy names. */
164
174
  getStrategyNames() {
165
- return [...this.strategies.keys()];
175
+ return [...this.guards.keys()];
176
+ }
177
+ /**
178
+ * Log a user in through a session-capable guard (AdonisJS
179
+ * `auth.use('web').login(user)` parity). Delegates to the guard's own
180
+ * `login()` — for `SessionStrategy` this rotates the session id (session
181
+ * fixation defence) and stores the user id. Throws if the resolved guard
182
+ * cannot log in (e.g. JWT / API-key guards, which are stateless).
183
+ *
184
+ * Fixes the misleading `SessionStrategy.authenticate()` sentinel that
185
+ * pointed at a then-nonexistent `authManager.login()` (bug-3261).
186
+ */
187
+ async login(user, session, strategyName) {
188
+ const strategy = this.getStrategy(strategyName);
189
+ if (!isLoginCapable(strategy)) {
190
+ throw new WardenError("STRATEGY_CANNOT_LOGIN", `Auth strategy '${strategyName ?? this.default}' does not support session login.`, {
191
+ hint: "login()/logout() need a stateful guard (e.g. SessionStrategy). JWT / API-key guards are stateless — mint a token with issueFor() instead.",
192
+ });
193
+ }
194
+ await strategy.login(user, session);
166
195
  }
196
+ /**
197
+ * Log a user out of a session-capable guard (AdonisJS `auth.use('web').logout()`
198
+ * parity). Delegates to the guard's `logout()`. Throws if the resolved guard
199
+ * cannot log out.
200
+ */
201
+ async logout(session, strategyName) {
202
+ const strategy = this.getStrategy(strategyName);
203
+ if (!isLoginCapable(strategy)) {
204
+ throw new WardenError("STRATEGY_CANNOT_LOGIN", `Auth strategy '${strategyName ?? this.default}' does not support session logout.`);
205
+ }
206
+ await strategy.logout(session);
207
+ }
208
+ }
209
+ /**
210
+ * Capability check for {@link AuthManager.login}/{@link AuthManager.logout}.
211
+ * `login`/`logout` are not on the base `AuthStrategy` contract (only stateful
212
+ * guards implement them), so narrow via `in` + `typeof` — no cast.
213
+ */
214
+ function isLoginCapable(strategy) {
215
+ return ("login" in strategy &&
216
+ typeof strategy.login === "function" &&
217
+ "logout" in strategy &&
218
+ typeof strategy.logout === "function");
167
219
  }
168
220
  /** Strip dangerous prototype-pollution keys from user payload. */
169
221
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"AuthManager.js","sourceRoot":"","sources":["../src/AuthManager.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,+BAA+B,CAAC;AAClE,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAgD5D;;;;GAIG;AACH,SAAS,aAAa,CACrB,QAAsB;IAEtB,OAAO,WAAW,IAAI,QAAQ,IAAI,OAAO,QAAQ,CAAC,SAAS,KAAK,UAAU,CAAC;AAC5E,CAAC;AAED;;GAEG;AACH,MAAM,OAAO,WAAW;IACf,UAAU,GAA8B,IAAI,GAAG,EAAE,CAAC;IAClD,eAAe,CAAS;IACf,MAAM,CAAiB;IAExC,YAAY,MAAkB;QAC7B,IAAI,CAAC,eAAe,GAAG,MAAM,CAAC,eAAe,CAAC;QAC9C,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,IAAI,IAAI,cAAc,CAAC,IAAI,iBAAiB,EAAE,CAAC,CAAC;QAC3E,KAAK,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC;YAClE,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;QACrC,CAAC;QACD,iEAAiE;QACjE,gEAAgE;QAChE,iEAAiE;QACjE,kEAAkE;QAClE,+DAA+D;QAC/D,iEAAiE;QACjE,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACjD,MAAM,IAAI,WAAW,CACpB,gBAAgB,EAChB,oJAAoJ,CACpJ,CAAC;QACH,CAAC;QACD,IAAI,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;AACF,CAAC"}
@@ -0,0 +1,128 @@
1
+ /**
2
+ * Authenticator — a PER-REQUEST authentication contract (AdonisJS
3
+ * `@adonisjs/auth` `Authenticator` parity), attached as `ctx.auth`.
4
+ *
5
+ * Why it exists: before this, `ctx.auth` was a plain data object with no
6
+ * methods, so an INLINE route handler (`router.post('/login', handler)`) had no
7
+ * way to authenticate on demand — only the `@Guard` decorator path worked, and
8
+ * that reads `ctx.route.controller/action`, which inline/functional routes don't
9
+ * expose. This contract makes `await ctx.auth.authenticate()` /
10
+ * `ctx.auth.use('session').login(user)` work anywhere, exactly like Adonis.
11
+ *
12
+ * Warden stays agnostic: the Authenticator is built from the {@link AuthManager}
13
+ * (resolved via `ctx.containerResolver`) + the request `ctx`; it never imports
14
+ * `@c9up/ream`.
15
+ */
16
+ import { type AuthManager, type AuthResult, type UserPayload } from "./AuthManager.js";
17
+ import type { WardenContext } from "./middleware.js";
18
+ import type { SessionStore } from "./strategies/SessionStrategy.js";
19
+ /**
20
+ * Guard names Warden accepts for the API-key / access-tokens driver. AdonisJS
21
+ * names the driver `access_tokens`; the legacy `api-key` spelling stays valid so
22
+ * existing `@Guard('api-key')` routes and configs keep working.
23
+ */
24
+ export declare const API_KEY_GUARD_NAMES: readonly string[];
25
+ /** The credentials Warden extracts from a request, independent of guard list. */
26
+ export interface ExtractedCredentials {
27
+ bearerToken: string;
28
+ apiKey: string;
29
+ session: SessionStore | undefined;
30
+ }
31
+ /**
32
+ * Extract the request's credentials once — a Bearer token, an API key (read from
33
+ * the configured header, matched case-insensitively), and the session store.
34
+ * Shared by `wardenMiddleware`, `silentAuth`, and the Authenticator so all three
35
+ * read credentials identically.
36
+ */
37
+ export declare function extractCredentials(ctx: WardenContext, auth: AuthManager): ExtractedCredentials;
38
+ /** Outcome of {@link tryAuthenticate}. */
39
+ export interface AuthAttempt {
40
+ result: AuthResult | null;
41
+ /** Name of the guard that authenticated (only when `result.authenticated`). */
42
+ viaGuard?: string;
43
+ attemptCount: number;
44
+ crashCount: number;
45
+ }
46
+ /**
47
+ * Try each declared guard in order — session guards via `verifyWithContext()`,
48
+ * others via `verify(token)` with native-first credential fallback.
49
+ * Distinguishes crashes (strategy threw / `strategyCrash`) from credential
50
+ * rejections so the caller can return 500 vs 401.
51
+ */
52
+ export declare function tryAuthenticate(auth: AuthManager, strategies: string[], creds: {
53
+ bearerToken: string;
54
+ apiKey: string;
55
+ session: SessionStore | undefined;
56
+ hasSessionStrategy: boolean;
57
+ }): Promise<AuthAttempt>;
58
+ /**
59
+ * A named-guard accessor (AdonisJS `auth.use('web')`). Exposes the stateful
60
+ * `login()`/`logout()` for session guards plus per-guard `authenticate()`.
61
+ */
62
+ export declare class GuardAccessor {
63
+ #private;
64
+ constructor(ctx: WardenContext, auth: AuthManager, name: string, parent: Authenticator);
65
+ /** The user, only if the request authenticated via THIS guard. */
66
+ get user(): UserPayload | undefined;
67
+ get isAuthenticated(): boolean;
68
+ /** Authenticate the request using only this guard (throws on failure). */
69
+ authenticate(): Promise<void>;
70
+ /** Log a user in through this guard (session guards). */
71
+ login(user: UserPayload): Promise<void>;
72
+ /** Log the current user out of this guard (session guards). */
73
+ logout(): Promise<void>;
74
+ }
75
+ /**
76
+ * Per-request Authenticator attached as `ctx.auth`. Mirrors AdonisJS's
77
+ * `Authenticator` surface: `authenticate` / `check` / `authenticateUsing` /
78
+ * `getUserOrFail` / `use`, plus the `user` / `isAuthenticated` /
79
+ * `authenticationAttempted` / `authenticatedViaGuard` getters.
80
+ */
81
+ export declare class Authenticator {
82
+ #private;
83
+ constructor(ctx: WardenContext, auth: AuthManager);
84
+ /** The authenticated user, or `undefined` for a guest. */
85
+ get user(): UserPayload | undefined;
86
+ /** Whether the request has an authenticated user (AdonisJS name). */
87
+ get isAuthenticated(): boolean;
88
+ /** Whether `authenticate`/`check`/`authenticateUsing` ran this request. */
89
+ get authenticationAttempted(): boolean;
90
+ /** Name of the guard that authenticated the request, if any. */
91
+ get authenticatedViaGuard(): string | undefined;
92
+ /**
93
+ * Roles of the authenticated user (Ream reads `ctx.auth.roles ??
94
+ * ctx.auth.user.roles`). Warden nests them under `user`; this getter mirrors
95
+ * them at the top level for the host's guard readers.
96
+ */
97
+ get roles(): string[] | undefined;
98
+ /** Permissions of the authenticated user (see {@link Authenticator.roles}). */
99
+ get permissions(): string[] | undefined;
100
+ /** Authenticate via the default guard. Throws `E_UNAUTHORIZED_ACCESS` on failure. */
101
+ authenticate(): Promise<void>;
102
+ /**
103
+ * Like {@link Authenticator.authenticate} but returns a boolean instead of
104
+ * throwing on a credential rejection. A strategy CRASH / config error still
105
+ * propagates (a server incident must not be silently swallowed as "guest").
106
+ */
107
+ check(): Promise<boolean>;
108
+ /**
109
+ * Authenticate by trying the given guards in order (default: the default
110
+ * guard). Sets the user on the first success; throws `E_UNAUTHORIZED_ACCESS`
111
+ * (401, carrying `redirectTo` for session guards) when all reject, or a
112
+ * `WARDEN_AUTH_STRATEGY_ERROR` (500) when every attempted guard crashed.
113
+ */
114
+ authenticateUsing(guards?: string[], options?: {
115
+ loginRoute?: string;
116
+ }): Promise<void>;
117
+ /** Return the authenticated user or throw `E_UNAUTHORIZED_ACCESS`. */
118
+ getUserOrFail(): UserPayload;
119
+ /** Access a named guard (AdonisJS `auth.use('web')`). Instances are cached per request. */
120
+ use(name: string): GuardAccessor;
121
+ /**
122
+ * @internal Seed the Authenticator from a result the enforcing middleware
123
+ * already computed, so `ctx.auth` reflects the authenticated user without a
124
+ * second verify pass.
125
+ */
126
+ adopt(result: AuthResult | null, viaGuard: string | undefined): void;
127
+ }
128
+ //# sourceMappingURL=Authenticator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Authenticator.d.ts","sourceRoot":"","sources":["../src/Authenticator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EACN,KAAK,WAAW,EAChB,KAAK,UAAU,EAGf,KAAK,WAAW,EAChB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,KAAK,EAAE,aA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