@aponiajs/auth 0.0.0-stage → 1.0.0-beta.65
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/README.md +247 -2
- package/dist/index.d.mts +407 -0
- package/dist/index.mjs +837 -0
- package/llms.txt +39 -0
- package/package.json +60 -4
package/README.md
CHANGED
|
@@ -1,3 +1,248 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @aponiajs/auth
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
JWT authentication, role and permission guards, refresh-token rotation,
|
|
4
|
+
access-token revocation, and Bun-native password hashing for AponiaJS.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
bun add @aponiajs/auth@alpha
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Use it
|
|
11
|
+
|
|
12
|
+
### Register the module
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { Module } from "@aponiajs/common";
|
|
16
|
+
import { AuthModule } from "@aponiajs/auth";
|
|
17
|
+
|
|
18
|
+
@Module({
|
|
19
|
+
imports: [AuthModule.forRoot({ jwt: { secret: process.env.JWT_SECRET! } })],
|
|
20
|
+
})
|
|
21
|
+
export class AppModule {}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The registration is global and runs `AuthGuard` on every route by default, as
|
|
25
|
+
`@nestjs/authentication` does; `globalGuard: false` leaves the guard to each
|
|
26
|
+
route.
|
|
27
|
+
|
|
28
|
+
### Use it in a controller
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { Controller, Get, UseGuards } from "@aponiajs/common";
|
|
32
|
+
import {
|
|
33
|
+
AuthGuard,
|
|
34
|
+
AuthService,
|
|
35
|
+
CurrentUser,
|
|
36
|
+
Permissions,
|
|
37
|
+
PermissionsGuard,
|
|
38
|
+
Public,
|
|
39
|
+
Roles,
|
|
40
|
+
RolesGuard,
|
|
41
|
+
type AuthUser,
|
|
42
|
+
} from "@aponiajs/auth";
|
|
43
|
+
|
|
44
|
+
@Controller("users")
|
|
45
|
+
@UseGuards(AuthGuard, RolesGuard, PermissionsGuard)
|
|
46
|
+
export class UsersController {
|
|
47
|
+
constructor(private readonly auth: AuthService) {}
|
|
48
|
+
|
|
49
|
+
@Public()
|
|
50
|
+
@Get("health")
|
|
51
|
+
health() {
|
|
52
|
+
return "ok";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
@Roles("admin")
|
|
56
|
+
@Permissions("users:read")
|
|
57
|
+
@Get()
|
|
58
|
+
list(@CurrentUser() user: AuthUser | null) {
|
|
59
|
+
return { userId: user?.id, current: this.auth.current()?.id };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`AuthGuard` verifies the bearer token and writes the user onto the route
|
|
65
|
+
context; `@CurrentUser()` and `@CurrentSession()` read that value, and both
|
|
66
|
+
answer `null` on a `@Public()` route.
|
|
67
|
+
|
|
68
|
+
### Use it in a service
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { Injectable } from "@aponiajs/common";
|
|
72
|
+
import { AuthService, JwtService } from "@aponiajs/auth";
|
|
73
|
+
|
|
74
|
+
@Injectable()
|
|
75
|
+
export class AuthTokens {
|
|
76
|
+
constructor(
|
|
77
|
+
private readonly auth: AuthService,
|
|
78
|
+
private readonly jwt: JwtService,
|
|
79
|
+
) {}
|
|
80
|
+
|
|
81
|
+
async accessToken(): Promise<string> {
|
|
82
|
+
const user = this.auth.current();
|
|
83
|
+
if (user?.id === undefined) {
|
|
84
|
+
throw new Error("No authenticated user for this request.");
|
|
85
|
+
}
|
|
86
|
+
return await this.jwt.sign({ sub: user.id });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
`AuthService` is request-scoped, so it reads the user `AuthGuard` entered, and
|
|
92
|
+
`JwtService` signs with the configured secret and mints a `jti`.
|
|
93
|
+
|
|
94
|
+
### Async registration
|
|
95
|
+
|
|
96
|
+
`AuthModule` does not support an asynchronous registration, and the refusal is
|
|
97
|
+
structural: the `jwt`/`oidc` arm decides which verifier provider the
|
|
98
|
+
registration contributes, and a container factory cannot change a provider
|
|
99
|
+
list. Resolve the configuration before registering:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
const auth = AuthModule.forRoot({
|
|
103
|
+
jwt: { secret: await readJwtSecret() },
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
`register` remains the deprecated alias of `forRoot`.
|
|
108
|
+
|
|
109
|
+
## What it provides
|
|
110
|
+
|
|
111
|
+
- `JwtService` verifies HS256 tokens with issuer, audience, and algorithm checks, and signs test or service tokens.
|
|
112
|
+
- `AuthGuard` extracts the bearer token, verifies it, and stores the user in the request context.
|
|
113
|
+
- `AuthService.current()` reads the authenticated user inside handlers.
|
|
114
|
+
- `Roles()` requires at least one named role; `Permissions()` requires every named permission.
|
|
115
|
+
- `Public()` bypasses authentication for a route or controller.
|
|
116
|
+
- `PasswordService` uses `Bun.password.hash` and `Bun.password.verify`.
|
|
117
|
+
- `RefreshTokenService` issues, rotates, and revokes opaque refresh tokens; only a SHA-256 digest is stored.
|
|
118
|
+
- `RevocationService` records revoked access-token `jti` claims until their expiry, and `AuthGuard` checks them.
|
|
119
|
+
|
|
120
|
+
## Token transport
|
|
121
|
+
|
|
122
|
+
`AuthGuard` reads `Authorization: Bearer <token>`. A cookie transport is opted
|
|
123
|
+
in with `token: { cookieName: "aponia.at" }`; the header still wins when both are
|
|
124
|
+
present, and `token.scheme` renames the authorization scheme. A cookie token
|
|
125
|
+
needs CSRF protection on unsafe requests — pair it with `@aponiajs/security`.
|
|
126
|
+
|
|
127
|
+
## Key rotation
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
AuthModule.forRoot({
|
|
131
|
+
jwt: {
|
|
132
|
+
secret: process.env.JWT_SECRET!,
|
|
133
|
+
keys: [
|
|
134
|
+
{ kid: "2026-01", secret: process.env.JWT_SECRET_OLD! },
|
|
135
|
+
{ kid: "2026-06", secret: process.env.JWT_SECRET! },
|
|
136
|
+
],
|
|
137
|
+
activeKid: "2026-06",
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Tokens are signed with the active key and carry its `kid`. Verification accepts
|
|
143
|
+
any listed key, and a token with no `kid` is tried against the default secret and
|
|
144
|
+
each rotation key, so an old secret stops working only when its key leaves the
|
|
145
|
+
list.
|
|
146
|
+
|
|
147
|
+
## Refresh tokens and revocation
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
AuthModule.forRoot({
|
|
151
|
+
jwt: { secret: process.env.JWT_SECRET! },
|
|
152
|
+
refresh: { ttlMs: 30 * 24 * 60 * 60 * 1_000, maxTokens: 10_000 },
|
|
153
|
+
revocation: { maxEntries: 10_000 },
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
import { Injectable } from "@aponiajs/common";
|
|
159
|
+
import { RefreshTokenService, RevocationService } from "@aponiajs/auth";
|
|
160
|
+
|
|
161
|
+
@Injectable()
|
|
162
|
+
class Sessions {
|
|
163
|
+
constructor(
|
|
164
|
+
private readonly refresh: RefreshTokenService,
|
|
165
|
+
private readonly revocation: RevocationService,
|
|
166
|
+
) {}
|
|
167
|
+
|
|
168
|
+
async rotate(userId: string, jti: string, expiresAt: number): Promise<string> {
|
|
169
|
+
const issued = await this.refresh.issue(userId);
|
|
170
|
+
const rotated = await this.refresh.rotate(issued.token); // the old token is consumed
|
|
171
|
+
await this.refresh.revokeFamily(rotated.familyId); // sign the family out
|
|
172
|
+
await this.revocation.revoke(jti, expiresAt); // reject one access token
|
|
173
|
+
return rotated.token;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## OpenID Connect
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
AuthModule.forRoot({ oidc: { issuer: "https://login.example.com", audience: "my-api" } });
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
`OidcService` reads the provider's discovery document, pins the configured
|
|
185
|
+
issuer, and verifies tokens against the published key set. The document and key
|
|
186
|
+
set are cached; `clear()` re-reads them. A registration states exactly one of
|
|
187
|
+
`jwt` or `oidc`.
|
|
188
|
+
|
|
189
|
+
Rotation keeps the family id, so a replayed token is refused with
|
|
190
|
+
`INVALID_REFRESH_TOKEN`. `MemoryRefreshTokenStore` and `MemoryRevocationStore`
|
|
191
|
+
are bounded and process-local, including the tombstones reuse detection keeps;
|
|
192
|
+
an application that runs more than one instance supplies a store that shares
|
|
193
|
+
state:
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
AuthModule.forRoot({
|
|
197
|
+
jwt: { secret: process.env.JWT_SECRET! },
|
|
198
|
+
refresh: { store: sharedRefreshStore },
|
|
199
|
+
revocation: { store: sharedRevocationStore },
|
|
200
|
+
});
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
A store that does not implement its contract fails the registration with
|
|
204
|
+
`INVALID_AUTH_OPTIONS`.
|
|
205
|
+
|
|
206
|
+
## Security defaults
|
|
207
|
+
|
|
208
|
+
- The JWT secret must be at least 32 characters.
|
|
209
|
+
- A missing, invalid, or expired token answers `401`.
|
|
210
|
+
- A missing role or permission answers `403` Problem Details.
|
|
211
|
+
- The package mounts no route, reads no environment variable, and stores no secret outside the registration.
|
|
212
|
+
- `RolesGuard` and `PermissionsGuard` run after `AuthGuard`; declare them in that order.
|
|
213
|
+
- A rotated refresh token cannot be used twice; a reused token revokes its whole family, and revoking a family invalidates every descendant.
|
|
214
|
+
- `JwtService.sign()` mints a `jti` when none is supplied; a token minted elsewhere without one is accepted but not revocation-checked.
|
|
215
|
+
|
|
216
|
+
## Limits
|
|
217
|
+
|
|
218
|
+
This foundation covers bearer JWTs, role/permission checks, refresh rotation,
|
|
219
|
+
single-instance revocation, OIDC discovery, and password hashing. Distributed
|
|
220
|
+
revocation and distributed refresh stores are later layers.
|
|
221
|
+
|
|
222
|
+
## Bootstrap
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
import { AponiaFactory } from "@aponiajs/platform-elysia";
|
|
226
|
+
import { AppModule } from "./app.module.ts";
|
|
227
|
+
|
|
228
|
+
const application = await AponiaFactory.create(AppModule);
|
|
229
|
+
await application.listen(Number(Bun.env.PORT ?? 3000));
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Registration contract
|
|
233
|
+
|
|
234
|
+
`AuthModule.forRoot` is the only registration; `MODULE_OPTIONS_TOKEN`
|
|
235
|
+
(`AUTH_MODULE_OPTIONS`) is the options token it provides and exports.
|
|
236
|
+
`isGlobal` and `globalGuard` default to true, matching
|
|
237
|
+
`@nestjs/authentication`: the module is global and `AuthGuard` runs on every
|
|
238
|
+
route unless the registration states `globalGuard: false`. `@Public()` opts a
|
|
239
|
+
route out, and `@CurrentUser()`/`@CurrentSession()` read the authenticated user
|
|
240
|
+
and session (`{ user, token, claims }`). The `jwt`/`oidc` arm decides which
|
|
241
|
+
verifier provider exists, so there is no `forRootAsync`: resolve an asynchronous
|
|
242
|
+
configuration before registering. `register` remains as the deprecated alias.
|
|
243
|
+
|
|
244
|
+
## Documentation
|
|
245
|
+
|
|
246
|
+
- [Agent surface](../../docs/agent/README.md)
|
|
247
|
+
- [Published packages](../../docs/reference/packages.md)
|
|
248
|
+
- [npm](https://www.npmjs.com/package/@aponiajs/auth)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import { CanActivate, ConfigurableModuleAsyncOptions, DynamicModule, ExecutionContext, InjectionToken, Reflector, RolesGuard } from "@aponiajs/common";
|
|
2
|
+
import { RequestContextService } from "@aponiajs/core";
|
|
3
|
+
/** The discovery document fields the verifier reads. */
|
|
4
|
+
interface OidcProviderMetadata {
|
|
5
|
+
readonly issuer: string;
|
|
6
|
+
readonly jwks_uri: string;
|
|
7
|
+
readonly authorization_endpoint?: string;
|
|
8
|
+
readonly token_endpoint?: string;
|
|
9
|
+
readonly userinfo_endpoint?: string;
|
|
10
|
+
readonly end_session_endpoint?: string;
|
|
11
|
+
readonly [field: string]: unknown;
|
|
12
|
+
}
|
|
13
|
+
/** OpenID Connect discovery options. */
|
|
14
|
+
interface OidcOptions {
|
|
15
|
+
/** The issuer the discovery document must state and every token must carry. */
|
|
16
|
+
readonly issuer: string;
|
|
17
|
+
/** The audience a token must name, when the provider sets one. */
|
|
18
|
+
readonly audience?: string;
|
|
19
|
+
/** The accepted signing algorithms; defaults to `RS256`, `PS256`, and `ES256`. */
|
|
20
|
+
readonly algorithms?: readonly string[];
|
|
21
|
+
/** The discovery URL; defaults to `<issuer>/.well-known/openid-configuration`. */
|
|
22
|
+
readonly discoveryUrl?: string;
|
|
23
|
+
/** How long one discovery or key-set request may take; defaults to 5000 ms. */
|
|
24
|
+
readonly timeoutMs?: number;
|
|
25
|
+
/** Clock skew allowance in seconds; defaults to 30. */
|
|
26
|
+
readonly clockTolerance?: number;
|
|
27
|
+
/** @internal The fetch implementation; tests inject one. */
|
|
28
|
+
readonly fetch?: typeof globalThis.fetch;
|
|
29
|
+
}
|
|
30
|
+
/** The stored form of one refresh token; the raw token is never stored. */
|
|
31
|
+
interface RefreshTokenRecord {
|
|
32
|
+
readonly idHash: string;
|
|
33
|
+
readonly userId: string | number;
|
|
34
|
+
readonly familyId: string;
|
|
35
|
+
readonly createdAt: number;
|
|
36
|
+
readonly expiresAt: number;
|
|
37
|
+
}
|
|
38
|
+
/** The value `RefreshTokenService.issue` answers. */
|
|
39
|
+
interface IssuedRefreshToken {
|
|
40
|
+
readonly token: string;
|
|
41
|
+
readonly userId: string | number;
|
|
42
|
+
readonly familyId: string;
|
|
43
|
+
readonly expiresAt: number;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* What an atomic consume answers.
|
|
47
|
+
*
|
|
48
|
+
* A live token answers its record; a token that was already consumed answers the
|
|
49
|
+
* family it belonged to, which is what lets `rotate` revoke the family on reuse.
|
|
50
|
+
*/
|
|
51
|
+
type RefreshTokenConsumeResult = {
|
|
52
|
+
readonly record: RefreshTokenRecord;
|
|
53
|
+
} | {
|
|
54
|
+
readonly familyId: string;
|
|
55
|
+
};
|
|
56
|
+
/** The storage contract a refresh-token service accepts. */
|
|
57
|
+
interface RefreshTokenStore {
|
|
58
|
+
create(record: RefreshTokenRecord): void | Promise<void>;
|
|
59
|
+
find(idHash: string): RefreshTokenRecord | undefined | Promise<RefreshTokenRecord | undefined>;
|
|
60
|
+
delete(idHash: string): boolean | Promise<boolean>;
|
|
61
|
+
deleteFamily(familyId: string): number | Promise<number>;
|
|
62
|
+
clear(): void | Promise<void>;
|
|
63
|
+
/**
|
|
64
|
+
* Atomically consumes a token.
|
|
65
|
+
*
|
|
66
|
+
* Optional: a custom store without it falls back to `find` + `delete`, which
|
|
67
|
+
* still lets exactly one concurrent rotation win but cannot detect reuse after
|
|
68
|
+
* the record is gone. The built-in memory and Redis stores implement it.
|
|
69
|
+
*/
|
|
70
|
+
consume?(idHash: string): RefreshTokenConsumeResult | undefined | Promise<RefreshTokenConsumeResult | undefined>;
|
|
71
|
+
}
|
|
72
|
+
/** The storage contract a revocation service accepts. */
|
|
73
|
+
interface RevocationStore {
|
|
74
|
+
revoke(jti: string, expiresAt: number): void | Promise<void>;
|
|
75
|
+
isRevoked(jti: string): boolean | Promise<boolean>;
|
|
76
|
+
clear(): void | Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
/** Claims an authenticated user carries. */
|
|
79
|
+
interface AuthUser {
|
|
80
|
+
readonly id?: string | number;
|
|
81
|
+
readonly roles?: readonly string[];
|
|
82
|
+
readonly permissions?: readonly string[];
|
|
83
|
+
readonly [claim: string]: unknown;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The session the current request authenticated with.
|
|
87
|
+
*
|
|
88
|
+
* `@CurrentSession()` returns this value; `token` is the opaque token the
|
|
89
|
+
* request carried and `claims` is the verified payload.
|
|
90
|
+
*/
|
|
91
|
+
interface AuthSession {
|
|
92
|
+
readonly user: AuthUser;
|
|
93
|
+
readonly token: string;
|
|
94
|
+
readonly claims: AuthUser;
|
|
95
|
+
}
|
|
96
|
+
/** JWT verification and signing options. */
|
|
97
|
+
interface JwtOptions {
|
|
98
|
+
/** The HMAC secret; at least 32 characters. */
|
|
99
|
+
readonly secret: string;
|
|
100
|
+
/** The required issuer. */
|
|
101
|
+
readonly issuer?: string;
|
|
102
|
+
/** The required audience. */
|
|
103
|
+
readonly audience?: string;
|
|
104
|
+
/** The accepted algorithms; defaults to HS256. */
|
|
105
|
+
readonly algorithms?: readonly string[];
|
|
106
|
+
/** The lifetime `sign()` uses; defaults to one hour. */
|
|
107
|
+
readonly expiresIn?: string | number;
|
|
108
|
+
/**
|
|
109
|
+
* Additional signing keys, so a secret can be rotated without invalidating
|
|
110
|
+
* the tokens the previous one signed.
|
|
111
|
+
*
|
|
112
|
+
* `sign()` uses the active key and writes its `kid`; `verify()` accepts a
|
|
113
|
+
* token signed by any key in this list, and by `secret` when the token states
|
|
114
|
+
* no `kid`. A token naming a `kid` this list does not contain is refused.
|
|
115
|
+
*/
|
|
116
|
+
readonly keys?: readonly JwtKey[];
|
|
117
|
+
/** The `kid` `sign()` uses; defaults to the first entry of `keys`. */
|
|
118
|
+
readonly activeKid?: string;
|
|
119
|
+
}
|
|
120
|
+
/** One signing key a rotation list carries. */
|
|
121
|
+
interface JwtKey {
|
|
122
|
+
/** The identifier the token header names. */
|
|
123
|
+
readonly kid: string;
|
|
124
|
+
/** The HMAC secret; at least 32 characters. */
|
|
125
|
+
readonly secret: string;
|
|
126
|
+
}
|
|
127
|
+
/** The one token verifier a registration states: a local secret or an OIDC issuer. */
|
|
128
|
+
type AuthVerificationOptions = {
|
|
129
|
+
readonly jwt: JwtOptions;
|
|
130
|
+
readonly oidc?: undefined;
|
|
131
|
+
} | {
|
|
132
|
+
readonly oidc: OidcOptions;
|
|
133
|
+
readonly jwt?: undefined;
|
|
134
|
+
};
|
|
135
|
+
/** Options `AuthModule.register` accepts. */
|
|
136
|
+
type AuthModuleOptions = AuthVerificationOptions & {
|
|
137
|
+
readonly token?: AuthTokenOptions;
|
|
138
|
+
readonly refresh?: RefreshOptions;
|
|
139
|
+
readonly revocation?: RevocationOptions;
|
|
140
|
+
};
|
|
141
|
+
/** Where a request may carry its access token. */
|
|
142
|
+
interface AuthTokenOptions {
|
|
143
|
+
/**
|
|
144
|
+
* A cookie carrying the JWT, for an application that keeps it out of
|
|
145
|
+
* JavaScript. The header still wins when both are present.
|
|
146
|
+
*
|
|
147
|
+
* A cookie transport needs CSRF protection on unsafe requests; pair it with
|
|
148
|
+
* `@aponiajs/security`'s CSRF policy rather than relying on `SameSite` alone.
|
|
149
|
+
*/
|
|
150
|
+
readonly cookieName?: string;
|
|
151
|
+
/** The authorization scheme; defaults to `Bearer`. */
|
|
152
|
+
readonly scheme?: string;
|
|
153
|
+
}
|
|
154
|
+
/** The validated token transport the guard reads. */
|
|
155
|
+
interface ResolvedAuthTokenOptions {
|
|
156
|
+
readonly cookieName: string | undefined;
|
|
157
|
+
readonly scheme: string;
|
|
158
|
+
}
|
|
159
|
+
/** Refresh-token issuance options. */
|
|
160
|
+
interface RefreshOptions {
|
|
161
|
+
readonly ttlMs?: number;
|
|
162
|
+
/**
|
|
163
|
+
* The store to keep refresh tokens in.
|
|
164
|
+
*
|
|
165
|
+
* Defaults to a bounded process-local store. An application that runs more
|
|
166
|
+
* than one instance passes a store they share, so a token issued by one
|
|
167
|
+
* process rotates on another.
|
|
168
|
+
*/
|
|
169
|
+
readonly store?: RefreshTokenStore;
|
|
170
|
+
/** The entry cap of the default store; ignored when `store` is supplied. */
|
|
171
|
+
readonly maxTokens?: number;
|
|
172
|
+
/** The clock the default store and the service read. */
|
|
173
|
+
readonly now?: () => number;
|
|
174
|
+
}
|
|
175
|
+
/** Revocation-store options. */
|
|
176
|
+
interface RevocationOptions {
|
|
177
|
+
/**
|
|
178
|
+
* The set of revoked access tokens.
|
|
179
|
+
*
|
|
180
|
+
* Defaults to a bounded process-local set. A multi-instance deployment
|
|
181
|
+
* passes a shared set, so a token revoked by one process is refused by all.
|
|
182
|
+
*/
|
|
183
|
+
readonly store?: RevocationStore;
|
|
184
|
+
/** The entry cap of the default set; ignored when `store` is supplied. */
|
|
185
|
+
readonly maxEntries?: number;
|
|
186
|
+
/** The clock the default set reads. */
|
|
187
|
+
readonly now?: () => number;
|
|
188
|
+
}
|
|
189
|
+
/** The validated JWT options the service reads. */
|
|
190
|
+
interface ResolvedJwtOptions {
|
|
191
|
+
readonly secret: string;
|
|
192
|
+
readonly issuer: string | undefined;
|
|
193
|
+
readonly audience: string | undefined;
|
|
194
|
+
readonly algorithms: readonly string[];
|
|
195
|
+
readonly expiresIn: string | number;
|
|
196
|
+
readonly keys?: readonly JwtKey[] | undefined;
|
|
197
|
+
readonly activeKid?: string | undefined;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Options that shape the module definition rather than the injected auth
|
|
201
|
+
* options, mirroring `@nestjs/authentication`'s `isGlobal` and `globalGuard`.
|
|
202
|
+
*/
|
|
203
|
+
interface AuthModuleExtras {
|
|
204
|
+
/** Makes the registration's exports visible without an import. Defaults to true. */
|
|
205
|
+
readonly isGlobal?: boolean;
|
|
206
|
+
/** Registers `AuthGuard` as a global guard. Defaults to true. */
|
|
207
|
+
readonly globalGuard?: boolean;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* The generated registration class, options token, and type markers.
|
|
211
|
+
*
|
|
212
|
+
* `MODULE_OPTIONS_TOKEN` is the `AUTH_MODULE_OPTIONS` token; `OPTIONS_TYPE` and
|
|
213
|
+
* `ASYNC_OPTIONS_TYPE` are type markers that throw when read as values.
|
|
214
|
+
*
|
|
215
|
+
* This package's registration is shaped by its options: the verification arm
|
|
216
|
+
* decides which verifier provider exists, which a container factory cannot
|
|
217
|
+
* change. `forRoot` is therefore the only registration, and the generated
|
|
218
|
+
* `registerAsync` is not exposed as a public surface.
|
|
219
|
+
*/
|
|
220
|
+
declare const ConfigurableModuleClass: import("@aponiajs/common").ConfigurableModuleClass<AuthModuleOptions, "register", "create", AuthModuleExtras>, OPTIONS_TYPE: AuthModuleOptions & Partial<AuthModuleExtras>, ASYNC_OPTIONS_TYPE: import("@aponiajs/common").ConfigurableModuleAsyncOptions<AuthModuleOptions, "create"> & Partial<AuthModuleExtras>;
|
|
221
|
+
/** The options token, narrowed to the explicit token the builder mints. */
|
|
222
|
+
export declare const MODULE_OPTIONS_TOKEN: InjectionToken<AuthModuleOptions>;
|
|
223
|
+
/** The synchronous registration's parameter type. */
|
|
224
|
+
type AuthModuleSyncOptions = AuthModuleOptions & AuthModuleExtras;
|
|
225
|
+
/** Provides JWT authentication, authorization guards, and password hashing. */
|
|
226
|
+
export declare class AuthModule extends ConfigurableModuleClass {
|
|
227
|
+
/**
|
|
228
|
+
* Builds one authentication module.
|
|
229
|
+
*
|
|
230
|
+
* The registration has no `forRootAsync`: which providers exist — a JWT
|
|
231
|
+
* verifier or an OIDC one — is decided by the options arm, and a container
|
|
232
|
+
* factory cannot change a provider list. A configuration that must be built
|
|
233
|
+
* asynchronously is resolved before the module is registered.
|
|
234
|
+
*
|
|
235
|
+
* @param options - The verifier arm, token policy, refresh, revocation, and extras.
|
|
236
|
+
* @returns A frozen dynamic module an application imports.
|
|
237
|
+
* @throws An `AponiaError` with `INVALID_AUTH_OPTIONS` when an option is invalid.
|
|
238
|
+
*/
|
|
239
|
+
static forRoot(options: AuthModuleSyncOptions): DynamicModule;
|
|
240
|
+
/**
|
|
241
|
+
* Builds one authentication module.
|
|
242
|
+
*
|
|
243
|
+
* @deprecated Use {@link AuthModule.forRoot}; retained for the beta line.
|
|
244
|
+
*/
|
|
245
|
+
static register(options: AuthModuleSyncOptions): DynamicModule;
|
|
246
|
+
/**
|
|
247
|
+
* Refuses the asynchronous form, which this package's registration cannot serve.
|
|
248
|
+
*
|
|
249
|
+
* @throws An `AponiaError` with `INVALID_AUTH_OPTIONS`; a provider list that
|
|
250
|
+
* the options arm decides cannot be built from a factory result.
|
|
251
|
+
*/
|
|
252
|
+
static registerAsync(_options: ConfigurableModuleAsyncOptions<AuthModuleOptions> & Partial<AuthModuleExtras>): DynamicModule;
|
|
253
|
+
}
|
|
254
|
+
/** The request-scoped authenticated user. */
|
|
255
|
+
export declare class AuthService {
|
|
256
|
+
#private;
|
|
257
|
+
/** @internal Construct through `AuthModule.register`. */
|
|
258
|
+
constructor(requestContext: RequestContextService);
|
|
259
|
+
/** The user the current request authenticated, if any. */
|
|
260
|
+
current(): AuthUser | undefined;
|
|
261
|
+
/** @internal Enters the authenticated user into the request context. */
|
|
262
|
+
enter(user: AuthUser): void;
|
|
263
|
+
}
|
|
264
|
+
/** The one operation `AuthGuard` needs from whichever verifier the module registered. */
|
|
265
|
+
interface AuthVerifier {
|
|
266
|
+
verify(token: string): Promise<AuthUser>;
|
|
267
|
+
}
|
|
268
|
+
/** Checks whether an access token's `jti` was revoked. */
|
|
269
|
+
export declare class RevocationService {
|
|
270
|
+
#private;
|
|
271
|
+
/** @internal Construct through `AuthModule.register`. */
|
|
272
|
+
constructor(store: RevocationStore);
|
|
273
|
+
/** Revokes one `jti` until its token expiry. */
|
|
274
|
+
revoke(jti: string, expiresAt: number): Promise<void>;
|
|
275
|
+
/** Whether the token's `jti` is currently revoked. */
|
|
276
|
+
isRevoked(jti: string): Promise<boolean>;
|
|
277
|
+
/** Clears the revocation set. */
|
|
278
|
+
clear(): Promise<void>;
|
|
279
|
+
}
|
|
280
|
+
/** Verifies a bearer JWT and enters the user into the request context. */
|
|
281
|
+
export declare class AuthGuard implements CanActivate {
|
|
282
|
+
#private;
|
|
283
|
+
constructor(verifier: AuthVerifier, tokenOptions: ResolvedAuthTokenOptions, auth: AuthService, reflector: Reflector, revocation: RevocationService);
|
|
284
|
+
canActivate(context: ExecutionContext): Promise<boolean>;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* The authenticated user for the current request, or `null`.
|
|
288
|
+
*
|
|
289
|
+
* Mirrors `@nestjs/authentication`'s `@CurrentUser()`: the optional key picks
|
|
290
|
+
* one claim. `AuthGuard` writes the value onto the route context, so a handler
|
|
291
|
+
* decorated with `@Public()` reads `null` rather than throwing.
|
|
292
|
+
*
|
|
293
|
+
* @param key - One claim to read; omitted means the whole user.
|
|
294
|
+
* @returns The parameter decorator.
|
|
295
|
+
*/
|
|
296
|
+
export declare const CurrentUser: (dataOrPipe?: string | import("@aponiajs/common").PipeType | undefined, ...pipes: readonly import("@aponiajs/common").PipeType[]) => ParameterDecorator;
|
|
297
|
+
/**
|
|
298
|
+
* The session the current request authenticated with, or `null`.
|
|
299
|
+
*
|
|
300
|
+
* Mirrors `@nestjs/authentication`'s `@CurrentSession()`: the value carries the
|
|
301
|
+
* user, the opaque token, and its verified claims.
|
|
302
|
+
*
|
|
303
|
+
* @returns The parameter decorator.
|
|
304
|
+
*/
|
|
305
|
+
export declare const CurrentSession: (dataOrPipe?: unknown, ...pipes: readonly import("@aponiajs/common").PipeType[]) => ParameterDecorator;
|
|
306
|
+
/** The request-context token the authenticated user is stored under. */
|
|
307
|
+
export declare const AUTH_USER: import("@aponiajs/common").InjectionToken<AuthUser>;
|
|
308
|
+
/** The verifier `AuthGuard` uses; `JwtService` or `OidcService` behind one token. */
|
|
309
|
+
export declare const AUTH_VERIFIER: import("@aponiajs/common").InjectionToken<AuthVerifier>;
|
|
310
|
+
/** The header/cookie policy `AuthGuard` reads. */
|
|
311
|
+
export declare const AUTH_TOKEN_OPTIONS: import("@aponiajs/common").InjectionToken<ResolvedAuthTokenOptions>;
|
|
312
|
+
/** The JWT verifier and signer an application injects. */
|
|
313
|
+
export declare class JwtService implements AuthVerifier {
|
|
314
|
+
#private;
|
|
315
|
+
/** @internal Construct through `AuthModule.register`. */
|
|
316
|
+
constructor(options: ResolvedJwtOptions);
|
|
317
|
+
/**
|
|
318
|
+
* Signs one claims object.
|
|
319
|
+
*
|
|
320
|
+
* An access token needs a `jti` for revocation to reach it, so one is minted
|
|
321
|
+
* when the caller did not supply a non-empty string. A caller-supplied `jti`
|
|
322
|
+
* is preserved, which keeps deterministic test tokens and application-owned
|
|
323
|
+
* identifiers working.
|
|
324
|
+
*/
|
|
325
|
+
sign(claims: Record<string, unknown>, expiresIn?: string | number): Promise<string>;
|
|
326
|
+
/** Verifies one token and answers its user claims. */
|
|
327
|
+
verify(token: string): Promise<AuthUser>;
|
|
328
|
+
}
|
|
329
|
+
/** Verifies access tokens against an OpenID Connect provider's published keys. */
|
|
330
|
+
export declare class OidcService {
|
|
331
|
+
#private;
|
|
332
|
+
/** @internal Construct through `AuthModule.register`. */
|
|
333
|
+
constructor(options: OidcOptions);
|
|
334
|
+
/** Reads and validates the provider's discovery document. */
|
|
335
|
+
discover(): Promise<OidcProviderMetadata>;
|
|
336
|
+
/** Verifies one token against the provider's key set. */
|
|
337
|
+
verify(token: string): Promise<AuthUser>;
|
|
338
|
+
/** Drops the cached discovery document and key set. */
|
|
339
|
+
clear(): void;
|
|
340
|
+
}
|
|
341
|
+
/** Bun-native password hashing and verification. */
|
|
342
|
+
export declare class PasswordService {
|
|
343
|
+
/** Hashes a password with Bun's default password algorithm. */
|
|
344
|
+
hash(password: string): Promise<string>;
|
|
345
|
+
/** Verifies a password against a stored hash. */
|
|
346
|
+
verify(password: string, hash: string): Promise<boolean>;
|
|
347
|
+
}
|
|
348
|
+
/** Issues, rotates, and revokes opaque refresh tokens. */
|
|
349
|
+
export declare class RefreshTokenService {
|
|
350
|
+
#private;
|
|
351
|
+
/** @internal Construct through `AuthModule.register`. */
|
|
352
|
+
constructor(store: RefreshTokenStore, options?: RefreshOptions);
|
|
353
|
+
/** Issues one token, optionally in an existing rotation family. */
|
|
354
|
+
issue(userId: string | number, familyId?: string): Promise<IssuedRefreshToken>;
|
|
355
|
+
/**
|
|
356
|
+
* Rotates one token; the old token is consumed before the new one is issued.
|
|
357
|
+
*
|
|
358
|
+
* An already-consumed token revokes its whole family (OAuth reuse detection).
|
|
359
|
+
* A store without `consume` falls back to `find` + `delete`, which still lets
|
|
360
|
+
* exactly one concurrent rotation win.
|
|
361
|
+
*/
|
|
362
|
+
rotate(token: string): Promise<IssuedRefreshToken>;
|
|
363
|
+
/** Revokes one token. */
|
|
364
|
+
revoke(token: string): Promise<boolean>;
|
|
365
|
+
/** Revokes every token in a rotation family. */
|
|
366
|
+
revokeFamily(familyId: string): Promise<number>;
|
|
367
|
+
/** Clears the store. */
|
|
368
|
+
clear(): Promise<void>;
|
|
369
|
+
}
|
|
370
|
+
/** A bounded, process-local refresh-token store. */
|
|
371
|
+
export declare class MemoryRefreshTokenStore implements RefreshTokenStore {
|
|
372
|
+
#private;
|
|
373
|
+
constructor(options?: {
|
|
374
|
+
readonly maxTokens?: number;
|
|
375
|
+
readonly now?: () => number;
|
|
376
|
+
});
|
|
377
|
+
create(record: RefreshTokenRecord): void;
|
|
378
|
+
find(idHash: string): RefreshTokenRecord | undefined;
|
|
379
|
+
delete(idHash: string): boolean;
|
|
380
|
+
consume(idHash: string): RefreshTokenConsumeResult | undefined;
|
|
381
|
+
deleteFamily(familyId: string): number;
|
|
382
|
+
clear(): void;
|
|
383
|
+
}
|
|
384
|
+
/** A bounded, process-local revocation set. */
|
|
385
|
+
export declare class MemoryRevocationStore implements RevocationStore {
|
|
386
|
+
#private;
|
|
387
|
+
constructor(options?: {
|
|
388
|
+
readonly maxEntries?: number;
|
|
389
|
+
readonly now?: () => number;
|
|
390
|
+
});
|
|
391
|
+
revoke(jti: string, expiresAt: number): void;
|
|
392
|
+
isRevoked(jti: string): boolean;
|
|
393
|
+
clear(): void;
|
|
394
|
+
}
|
|
395
|
+
/** Requires every permission a route declares. */
|
|
396
|
+
export declare class PermissionsGuard implements CanActivate {
|
|
397
|
+
#private;
|
|
398
|
+
constructor(reflector: Reflector);
|
|
399
|
+
canActivate(context: ExecutionContext): boolean;
|
|
400
|
+
}
|
|
401
|
+
/** Requires at least one of the named roles. */
|
|
402
|
+
export declare function Roles(...roles: string[]): ClassDecorator & MethodDecorator;
|
|
403
|
+
/** Requires every named permission. */
|
|
404
|
+
export declare function Permissions(...permissions: string[]): ClassDecorator & MethodDecorator;
|
|
405
|
+
/** Allows a route without authentication. */
|
|
406
|
+
export declare function Public(): ClassDecorator & MethodDecorator;
|
|
407
|
+
export { type AuthModuleExtras, type AuthModuleOptions, type AuthModuleSyncOptions, type AuthSession, type AuthTokenOptions, type AuthUser, type AuthVerificationOptions, type AuthVerifier, type IssuedRefreshToken, type JwtKey, type JwtOptions, type OidcOptions, type OidcProviderMetadata, type RefreshOptions, type RefreshTokenRecord, type RefreshTokenStore, type RevocationOptions, type RevocationStore, RolesGuard };
|