@dotpress/auth 2.0.0-alpha.4 → 2.0.0-alpha.5
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 +1 -1
- package/dist/index.d.ts +93 -165
- package/dist/index.js +271 -160
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@ declare module '@dotpress/auth' {
|
|
|
22
22
|
|
|
23
23
|
const auth = createAuth({
|
|
24
24
|
keys: {
|
|
25
|
-
|
|
25
|
+
privateKey: process.env.JWT_PRIVATE_KEY!, // PKCS#8 PEM
|
|
26
26
|
publicKey: process.env.JWT_PUBLIC_KEY!, // SPKI PEM
|
|
27
27
|
},
|
|
28
28
|
issuer: 'https://api.example.com', // checked on every request
|
package/dist/index.d.ts
CHANGED
|
@@ -1,40 +1,7 @@
|
|
|
1
1
|
import * as _dotpress_core from '@dotpress/core';
|
|
2
|
-
import { DotpressRequest, HttpError,
|
|
2
|
+
import { Duration, RateLimitStore, DotpressRequest, HttpError, DotpressResponse } from '@dotpress/core';
|
|
3
3
|
import { JWK } from 'jose';
|
|
4
4
|
|
|
5
|
-
/** State of a rate limit counter. */
|
|
6
|
-
interface RateLimitState {
|
|
7
|
-
/** Number of hits in the current window, including the last one. */
|
|
8
|
-
count: number;
|
|
9
|
-
/** End of the current window. */
|
|
10
|
-
resetAt: Date;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Storage of rate limit counters (fixed windows). Use a shared store
|
|
14
|
-
* (Redis, database) when the app runs on several instances.
|
|
15
|
-
*/
|
|
16
|
-
interface RateLimitStore {
|
|
17
|
-
/**
|
|
18
|
-
* Atomically increments the counter of a key and returns its state. A
|
|
19
|
-
* missing or expired counter starts a new window of `windowSeconds`.
|
|
20
|
-
*/
|
|
21
|
-
increment(key: string, windowSeconds: number): Promise<RateLimitState>;
|
|
22
|
-
/** Deletes the counter of a key. */
|
|
23
|
-
reset(key: string): Promise<void>;
|
|
24
|
-
}
|
|
25
|
-
interface MemoryRateLimitStoreOptions {
|
|
26
|
-
/**
|
|
27
|
-
* Maximum number of counters kept in memory: expired counters are purged
|
|
28
|
-
* first, then the oldest ones. Defaults to 10 000.
|
|
29
|
-
*/
|
|
30
|
-
maxKeys?: number;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* In-memory rate limit store, for a single instance. With several
|
|
34
|
-
* instances, each one counts on its own: use a shared store.
|
|
35
|
-
*/
|
|
36
|
-
declare const memoryRateLimitStore: (options?: MemoryRateLimitStoreOptions) => RateLimitStore;
|
|
37
|
-
|
|
38
5
|
/**
|
|
39
6
|
* Authenticated user, as returned by `DataProvider.findUserById()`.
|
|
40
7
|
* Declare its shape through module augmentation:
|
|
@@ -56,14 +23,13 @@ interface AuthSession {
|
|
|
56
23
|
readonly userId: string;
|
|
57
24
|
/** `username` claim, with the `usernameClaim` setting only. */
|
|
58
25
|
readonly username: string | undefined;
|
|
59
|
-
/**
|
|
60
|
-
readonly
|
|
26
|
+
/** Identifier of the access token (`jti` claim), stored with its refresh token. */
|
|
27
|
+
readonly accessTokenId: string;
|
|
61
28
|
/**
|
|
62
29
|
* Session of the token (`sid` claim): shared by the tokens rotated from
|
|
63
|
-
* the same login.
|
|
64
|
-
* (`generateAccessToken()`).
|
|
30
|
+
* the same login.
|
|
65
31
|
*/
|
|
66
|
-
readonly sessionId: string
|
|
32
|
+
readonly sessionId: string;
|
|
67
33
|
/** Client the token was issued for (`client_id` claim). */
|
|
68
34
|
readonly clientId: string | undefined;
|
|
69
35
|
/** Roles of the user, used by `requireRole()`. */
|
|
@@ -71,9 +37,9 @@ interface AuthSession {
|
|
|
71
37
|
/** Decoded token claims. */
|
|
72
38
|
readonly claims: Readonly<Record<string, unknown>>;
|
|
73
39
|
}
|
|
74
|
-
interface
|
|
40
|
+
interface KeyPairOptions {
|
|
75
41
|
/** RSA private key, PEM encoded in PKCS#8 (`BEGIN PRIVATE KEY`). */
|
|
76
|
-
|
|
42
|
+
privateKey: string;
|
|
77
43
|
/** RSA public key, PEM encoded in SPKI (`BEGIN PUBLIC KEY`). */
|
|
78
44
|
publicKey: string;
|
|
79
45
|
/**
|
|
@@ -92,7 +58,7 @@ interface AdditionalPublicKey {
|
|
|
92
58
|
publicKey: string;
|
|
93
59
|
kid?: string;
|
|
94
60
|
}
|
|
95
|
-
interface
|
|
61
|
+
interface AuthCookieOptions {
|
|
96
62
|
/** Enables cookie mode. Defaults to `false`. */
|
|
97
63
|
enabled?: boolean;
|
|
98
64
|
/** Defaults to `true`. */
|
|
@@ -141,14 +107,13 @@ interface CookieSettings {
|
|
|
141
107
|
/** Also stores the access token in a cookie. Defaults to `false`. */
|
|
142
108
|
accessTokenInCookie?: boolean;
|
|
143
109
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
110
|
+
/** Why a login failed. */
|
|
111
|
+
type LoginErrorType = 'invalid_username' | 'invalid_password' | 'invalid_role'
|
|
112
|
+
/** Blocked by the login throttle, before checking the password. */
|
|
113
|
+
| 'too_many_attempts';
|
|
147
114
|
interface LoginFailedEvent {
|
|
148
115
|
username: string;
|
|
149
|
-
errorType:
|
|
150
|
-
/** Blocked by the login throttle, before checking the password. */
|
|
151
|
-
| 'too_many_attempts';
|
|
116
|
+
errorType: LoginErrorType;
|
|
152
117
|
/** `clientId` sent at login; empty when missing. */
|
|
153
118
|
clientId: string;
|
|
154
119
|
ipAddress: string;
|
|
@@ -161,16 +126,17 @@ interface BeforeLoginEvent {
|
|
|
161
126
|
ipAddress: string;
|
|
162
127
|
req: DotpressRequest;
|
|
163
128
|
}
|
|
164
|
-
/** At most `max` failed logins per `window
|
|
129
|
+
/** At most `max` failed logins per `window`. */
|
|
165
130
|
interface RateLimit {
|
|
166
131
|
max: number;
|
|
167
|
-
|
|
132
|
+
/** A number of seconds, or a duration like `'1m'` or `'1h'`. */
|
|
133
|
+
window: Duration;
|
|
168
134
|
}
|
|
169
135
|
/**
|
|
170
136
|
* Login throttle: failed logins are counted per username and IP address,
|
|
171
137
|
* and per username, whether the account exists or not.
|
|
172
138
|
*/
|
|
173
|
-
interface
|
|
139
|
+
interface LoginThrottleOptions {
|
|
174
140
|
/**
|
|
175
141
|
* Counters storage. Defaults to an in-memory store, for a single
|
|
176
142
|
* instance: use a shared store (Redis, database) with several instances.
|
|
@@ -189,19 +155,12 @@ interface LoginThrottleSettings {
|
|
|
189
155
|
username?: RateLimit | false;
|
|
190
156
|
};
|
|
191
157
|
}
|
|
192
|
-
interface ResolvedLoginThrottleSettings {
|
|
193
|
-
store: RateLimitStore;
|
|
194
|
-
limits: {
|
|
195
|
-
usernameAndIp: RateLimit | undefined;
|
|
196
|
-
username: RateLimit | undefined;
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
158
|
interface RefreshTokenReuseEvent {
|
|
200
159
|
userId: string;
|
|
201
|
-
/** Revoked session; undefined for tokens stored
|
|
202
|
-
|
|
160
|
+
/** Revoked session; undefined for tokens stored before sessions (V1). */
|
|
161
|
+
sessionId: string | undefined;
|
|
203
162
|
/** `jti` of the access token the reused refresh token was issued with. */
|
|
204
|
-
|
|
163
|
+
accessTokenId: string;
|
|
205
164
|
}
|
|
206
165
|
interface LoginSuccessEvent {
|
|
207
166
|
userId: string;
|
|
@@ -210,16 +169,20 @@ interface LoginSuccessEvent {
|
|
|
210
169
|
clientId: string;
|
|
211
170
|
ipAddress: string;
|
|
212
171
|
}
|
|
213
|
-
|
|
214
|
-
|
|
172
|
+
/**
|
|
173
|
+
* Refresh token to store. The token itself is never given: only its
|
|
174
|
+
* SHA-256 hash, which `findRefreshToken()` receives.
|
|
175
|
+
*/
|
|
176
|
+
interface NewRefreshToken {
|
|
215
177
|
tokenHash: string;
|
|
216
178
|
userId: string;
|
|
217
|
-
|
|
179
|
+
/** `jti` of the access token issued with the refresh token. */
|
|
180
|
+
accessTokenId: string;
|
|
218
181
|
/**
|
|
219
182
|
* Session the token belongs to: created at login, kept by every
|
|
220
183
|
* rotation. Used to revoke the whole session when a token is reused.
|
|
221
184
|
*/
|
|
222
|
-
|
|
185
|
+
sessionId: string;
|
|
223
186
|
expiresAt: Date;
|
|
224
187
|
}
|
|
225
188
|
/**
|
|
@@ -230,14 +193,14 @@ type LogoutScope = 'current' | 'all';
|
|
|
230
193
|
/** Session revoked by `revokeSession()`. */
|
|
231
194
|
interface RevokedSession {
|
|
232
195
|
userId: string;
|
|
233
|
-
/** `undefined` for tokens stored
|
|
196
|
+
/** `undefined` for tokens stored before sessions (V1). */
|
|
234
197
|
sessionId: string | undefined;
|
|
235
198
|
}
|
|
236
199
|
interface RefreshTokenData {
|
|
237
200
|
userId: string;
|
|
238
|
-
|
|
239
|
-
/** `null` or missing for tokens stored before
|
|
240
|
-
|
|
201
|
+
accessTokenId: string;
|
|
202
|
+
/** `null` or missing for tokens stored before sessions (V1). */
|
|
203
|
+
sessionId?: string | null;
|
|
241
204
|
expiresAt: Date;
|
|
242
205
|
isUsed: boolean;
|
|
243
206
|
/** When the token was used (see `refreshTokenReuseInterval`). */
|
|
@@ -259,14 +222,14 @@ interface UserIdentifiers {
|
|
|
259
222
|
* (Prisma, Drizzle, SQL…).
|
|
260
223
|
*/
|
|
261
224
|
interface DataProvider {
|
|
262
|
-
storeRefreshToken(payload:
|
|
225
|
+
storeRefreshToken(payload: NewRefreshToken): Promise<void>;
|
|
263
226
|
findRefreshToken(tokenHash: string): Promise<RefreshTokenData | undefined>;
|
|
264
227
|
/** Marks a token as used, storing `usedAt`. */
|
|
265
228
|
markRefreshTokenAsUsed(tokenHash: string, usedAt: Date): Promise<void>;
|
|
266
229
|
/** Revokes every refresh token of a user. */
|
|
267
|
-
|
|
268
|
-
/** Revokes every refresh token of a session
|
|
269
|
-
|
|
230
|
+
revokeUserTokens(userId: string): Promise<void>;
|
|
231
|
+
/** Revokes every refresh token of a session. */
|
|
232
|
+
revokeSessionTokens(sessionId: string): Promise<void>;
|
|
270
233
|
/**
|
|
271
234
|
* User of a token, on every authenticated request and every refresh.
|
|
272
235
|
* Return `undefined` for a deleted, disabled or banned account: its
|
|
@@ -276,7 +239,7 @@ interface DataProvider {
|
|
|
276
239
|
findUserById(userId: string): Promise<AuthUser | undefined>;
|
|
277
240
|
findUserIdentifiers(username: string): Promise<UserIdentifiers | undefined>;
|
|
278
241
|
/**
|
|
279
|
-
* Optional. Whether a session (
|
|
242
|
+
* Optional. Whether a session (the `sid` claim) was revoked.
|
|
280
243
|
* When defined, it is checked on every authenticated request, so that
|
|
281
244
|
* the access tokens of a revoked session (logout, refresh token reuse,
|
|
282
245
|
* password change) stop working at once, instead of at their expiry.
|
|
@@ -298,53 +261,54 @@ interface DataProvider {
|
|
|
298
261
|
* prove which application sends a request. It means that the token was
|
|
299
262
|
* issued under the policy of that client (see `isUserRoleAllowed`).
|
|
300
263
|
*/
|
|
301
|
-
interface
|
|
264
|
+
interface ClientOptions {
|
|
302
265
|
/** Accepted client identifiers. Others are rejected at login (`400`). */
|
|
303
266
|
allowed: readonly string[];
|
|
304
267
|
/** Requires a `clientId` at login. Defaults to `false`. */
|
|
305
268
|
required?: boolean;
|
|
306
269
|
/** Checks at login that a user may get a token for a client. */
|
|
307
|
-
isUserRoleAllowed?: (
|
|
270
|
+
isUserRoleAllowed?: (login: {
|
|
271
|
+
userId: string;
|
|
272
|
+
/** `role` of `findUserIdentifiers()`. */
|
|
273
|
+
role: string | undefined;
|
|
274
|
+
clientId: string;
|
|
275
|
+
}) => boolean;
|
|
308
276
|
/**
|
|
309
277
|
* Session lifetimes of some clients, overriding the `session` setting
|
|
310
278
|
* (an explicit `absoluteTimeout: undefined` removes the global limit).
|
|
311
279
|
*
|
|
312
280
|
* @example
|
|
313
|
-
* sessions: { backoffice: { idleTimeout:
|
|
281
|
+
* sessions: { backoffice: { idleTimeout: '1d', absoluteTimeout: '1d' } }
|
|
314
282
|
*/
|
|
315
|
-
sessions?: Readonly<Record<string,
|
|
283
|
+
sessions?: Readonly<Record<string, SessionOptions>>;
|
|
316
284
|
}
|
|
317
285
|
/**
|
|
318
|
-
* Session lifetimes
|
|
319
|
-
* by each refresh.
|
|
286
|
+
* Session lifetimes: numbers of seconds, or durations like `'14d'`. A
|
|
287
|
+
* session starts at login and is extended by each refresh.
|
|
320
288
|
*/
|
|
321
|
-
interface
|
|
289
|
+
interface SessionOptions {
|
|
322
290
|
/**
|
|
323
291
|
* Inactivity timeout: the session ends when it is not refreshed during
|
|
324
292
|
* this delay (lifetime of each refresh token). Defaults to 14 days.
|
|
325
293
|
*/
|
|
326
|
-
idleTimeout?:
|
|
294
|
+
idleTimeout?: Duration;
|
|
327
295
|
/**
|
|
328
296
|
* Absolute timeout: the session ends this delay after login, even when
|
|
329
297
|
* it is active. Defaults to none: active sessions never end.
|
|
330
298
|
*/
|
|
331
|
-
absoluteTimeout?:
|
|
299
|
+
absoluteTimeout?: Duration | undefined;
|
|
332
300
|
}
|
|
333
|
-
interface
|
|
334
|
-
|
|
335
|
-
absoluteTimeout: number | undefined;
|
|
336
|
-
}
|
|
337
|
-
interface AuthSettings {
|
|
338
|
-
keys: KeyPairSettings;
|
|
301
|
+
interface AuthOptions {
|
|
302
|
+
keys: KeyPairOptions;
|
|
339
303
|
dataProvider: DataProvider;
|
|
340
304
|
/**
|
|
341
|
-
* Access token lifetime
|
|
342
|
-
* longest delay during which the token of a
|
|
343
|
-
* works, without `DataProvider.isSessionRevoked()`.
|
|
305
|
+
* Access token lifetime: a number of seconds, or a duration like `'15m'`.
|
|
306
|
+
* Defaults to 15 minutes: the longest delay during which the token of a
|
|
307
|
+
* revoked session still works, without `DataProvider.isSessionRevoked()`.
|
|
344
308
|
*/
|
|
345
|
-
accessTokenTTL?:
|
|
309
|
+
accessTokenTTL?: Duration;
|
|
346
310
|
/** Session lifetimes (inactivity and absolute timeouts). */
|
|
347
|
-
session?:
|
|
311
|
+
session?: SessionOptions;
|
|
348
312
|
/**
|
|
349
313
|
* `iss` claim of the access tokens, checked on every request: for
|
|
350
314
|
* example the URL of the API. Use a different value per environment
|
|
@@ -356,16 +320,16 @@ interface AuthSettings {
|
|
|
356
320
|
* on every request. Defaults to `issuer`.
|
|
357
321
|
*/
|
|
358
322
|
audience?: string;
|
|
359
|
-
cookies?:
|
|
360
|
-
clients?:
|
|
361
|
-
onLoginFailed?: (event: LoginFailedEvent) => Promise<void>;
|
|
362
|
-
onLoginSuccess?: (event: LoginSuccessEvent) => Promise<void>;
|
|
323
|
+
cookies?: AuthCookieOptions;
|
|
324
|
+
clients?: ClientOptions;
|
|
325
|
+
onLoginFailed?: (event: LoginFailedEvent) => void | Promise<void>;
|
|
326
|
+
onLoginSuccess?: (event: LoginSuccessEvent) => void | Promise<void>;
|
|
363
327
|
/**
|
|
364
328
|
* Login throttle (brute force protection), enabled by default. `false`
|
|
365
329
|
* disables it, when the infrastructure (gateway, WAF) already limits
|
|
366
330
|
* `/auth/login`.
|
|
367
331
|
*/
|
|
368
|
-
loginThrottle?:
|
|
332
|
+
loginThrottle?: LoginThrottleOptions | false;
|
|
369
333
|
/**
|
|
370
334
|
* Called before checking the password, after the login throttle: return
|
|
371
335
|
* an error (`tooManyRequestsError()`, `forbiddenError()`…) to reject the
|
|
@@ -386,48 +350,25 @@ interface AuthSettings {
|
|
|
386
350
|
*/
|
|
387
351
|
usernameClaim?: boolean;
|
|
388
352
|
/**
|
|
389
|
-
* Delay
|
|
390
|
-
*
|
|
391
|
-
*
|
|
392
|
-
*
|
|
353
|
+
* Delay after its first use during which a refresh token can be
|
|
354
|
+
* presented again without being treated as stolen: concurrent refreshes
|
|
355
|
+
* (several tabs) and retries after a lost response get a new token pair
|
|
356
|
+
* in the same session. A number of seconds, or a duration like `'10s'`.
|
|
357
|
+
* Defaults to 10 seconds; 0 disables it.
|
|
393
358
|
*/
|
|
394
|
-
refreshTokenReuseInterval?:
|
|
359
|
+
refreshTokenReuseInterval?: Duration;
|
|
395
360
|
/**
|
|
396
361
|
* Called when a used refresh token is presented again after the reuse
|
|
397
362
|
* interval: the session has been revoked, as the token was probably
|
|
398
363
|
* stolen. Errors thrown by the hook are ignored.
|
|
399
364
|
*/
|
|
400
|
-
onRefreshTokenReuse?: (event: RefreshTokenReuseEvent) => Promise<void>;
|
|
365
|
+
onRefreshTokenReuse?: (event: RefreshTokenReuseEvent) => void | Promise<void>;
|
|
401
366
|
/**
|
|
402
367
|
* Roles of a user, checked by `requireRole()`. Defaults to its `roles`
|
|
403
368
|
* array, or to its `role` string.
|
|
404
369
|
*/
|
|
405
370
|
getUserRoles?: (user: AuthUser) => readonly string[];
|
|
406
371
|
}
|
|
407
|
-
/** Options of `auth.init()`. */
|
|
408
|
-
interface AuthInitOptions {
|
|
409
|
-
/**
|
|
410
|
-
* Browser path of the auth routes, used as the default path of the
|
|
411
|
-
* refresh token cookie. An auth service serves a single path.
|
|
412
|
-
*/
|
|
413
|
-
routesPath?: string;
|
|
414
|
-
/**
|
|
415
|
-
* CORS settings of the app (`api.cors`). When `cookies.trustedOrigins`
|
|
416
|
-
* is not set and CORS allows credentials, the origins allowed by CORS
|
|
417
|
-
* are trusted.
|
|
418
|
-
*/
|
|
419
|
-
cors?: PluginCors;
|
|
420
|
-
}
|
|
421
|
-
interface ResolvedAuthSettings extends Required<Omit<AuthSettings, 'cookies' | 'clients' | 'session' | 'onLoginFailed' | 'onLoginSuccess' | 'loginThrottle' | 'beforeLogin' | 'onRefreshTokenReuse'>> {
|
|
422
|
-
cookies: ResolvedCookieSettings;
|
|
423
|
-
clients: ClientSettings | undefined;
|
|
424
|
-
session: ResolvedSessionSettings;
|
|
425
|
-
onLoginFailed: ((event: LoginFailedEvent) => Promise<void>) | undefined;
|
|
426
|
-
onLoginSuccess: ((event: LoginSuccessEvent) => Promise<void>) | undefined;
|
|
427
|
-
loginThrottle: ResolvedLoginThrottleSettings | undefined;
|
|
428
|
-
beforeLogin: AuthSettings['beforeLogin'];
|
|
429
|
-
onRefreshTokenReuse: ((event: RefreshTokenReuseEvent) => Promise<void>) | undefined;
|
|
430
|
-
}
|
|
431
372
|
interface TokenPair {
|
|
432
373
|
accessToken: string;
|
|
433
374
|
refreshToken: string;
|
|
@@ -446,15 +387,6 @@ interface IssuedTokens extends TokenPair {
|
|
|
446
387
|
/** End of the session, when an absolute timeout applies. */
|
|
447
388
|
sessionExpiresAt: Date | undefined;
|
|
448
389
|
}
|
|
449
|
-
interface CreatedAccessToken {
|
|
450
|
-
jwtId: string;
|
|
451
|
-
accessToken: string;
|
|
452
|
-
expiresAt: Date;
|
|
453
|
-
}
|
|
454
|
-
interface CreatedRefreshToken {
|
|
455
|
-
token: string;
|
|
456
|
-
tokenHash: string;
|
|
457
|
-
}
|
|
458
390
|
|
|
459
391
|
declare module '@dotpress/core' {
|
|
460
392
|
interface RequestContext {
|
|
@@ -478,14 +410,6 @@ declare class AuthError extends Error {
|
|
|
478
410
|
constructor(code: AuthErrorCode, options?: ErrorOptions);
|
|
479
411
|
}
|
|
480
412
|
interface Auth {
|
|
481
|
-
readonly settings: ResolvedAuthSettings;
|
|
482
|
-
/**
|
|
483
|
-
* Imports the keys. Fails with an explicit message on invalid keys.
|
|
484
|
-
* `authPlugin()` calls it at startup, with the browser path of its routes.
|
|
485
|
-
*/
|
|
486
|
-
init(options?: AuthInitOptions): Promise<void>;
|
|
487
|
-
generateAccessToken(subject: TokenSubject): Promise<CreatedAccessToken>;
|
|
488
|
-
generateRefreshToken(): Promise<CreatedRefreshToken>;
|
|
489
413
|
/** Generates and stores a token pair, starting a session. */
|
|
490
414
|
generateTokensForUser(subject: TokenSubject): Promise<IssuedTokens>;
|
|
491
415
|
/**
|
|
@@ -496,7 +420,7 @@ interface Auth {
|
|
|
496
420
|
* `refreshTokenReuseInterval` after its first use, it may be presented
|
|
497
421
|
* again (concurrent refreshes, retries after a lost response): a new pair
|
|
498
422
|
* is issued in the same session. After, presenting it again means it was
|
|
499
|
-
* probably stolen: the whole session
|
|
423
|
+
* probably stolen: the whole session is revoked, and
|
|
500
424
|
* `REFRESH_TOKEN_REUSED` is thrown.
|
|
501
425
|
*
|
|
502
426
|
* Throws `EXPIRED_REFRESH_TOKEN` when the session reached its inactivity
|
|
@@ -516,7 +440,7 @@ interface Auth {
|
|
|
516
440
|
/**
|
|
517
441
|
* Revokes the session of a refresh token (logout). The token may be
|
|
518
442
|
* used, revoked or expired: it still designates its session. Tokens
|
|
519
|
-
* stored
|
|
443
|
+
* stored before sessions (V1) revoke every session of the user.
|
|
520
444
|
*
|
|
521
445
|
* With `scope: 'all'`, revokes every session of the user, on every
|
|
522
446
|
* device. The token must then be active (neither used, revoked nor
|
|
@@ -528,6 +452,13 @@ interface Auth {
|
|
|
528
452
|
revokeSession(refreshToken: string, options?: {
|
|
529
453
|
scope?: LogoutScope;
|
|
530
454
|
}): Promise<RevokedSession | undefined>;
|
|
455
|
+
/**
|
|
456
|
+
* Revokes every session of a user, on every device: after a password
|
|
457
|
+
* change, or when an account is disabled. Their refresh tokens stop
|
|
458
|
+
* working at once; their access tokens too with
|
|
459
|
+
* `DataProvider.isSessionRevoked()`, else at their expiry.
|
|
460
|
+
*/
|
|
461
|
+
revokeUserSessions(userId: string): Promise<void>;
|
|
531
462
|
/**
|
|
532
463
|
* End of a session, when an absolute timeout applies to it (computed
|
|
533
464
|
* from the `auth_time` claim).
|
|
@@ -550,7 +481,7 @@ interface Auth {
|
|
|
550
481
|
*/
|
|
551
482
|
setAuthCookies(res: DotpressResponse, tokens: TokenPair & {
|
|
552
483
|
refreshTokenExpiresAt?: Date;
|
|
553
|
-
}
|
|
484
|
+
}): void;
|
|
554
485
|
clearAuthCookies(res: DotpressResponse): void;
|
|
555
486
|
/**
|
|
556
487
|
* Whether a request may be authenticated by the cookies: protection
|
|
@@ -560,11 +491,11 @@ interface Auth {
|
|
|
560
491
|
*/
|
|
561
492
|
isCookieAuthenticationAllowed(req: DotpressRequest): Promise<boolean>;
|
|
562
493
|
/**
|
|
563
|
-
* Hashes a password with bcrypt
|
|
564
|
-
*
|
|
494
|
+
* Hashes a password with bcrypt, with `passwordHashRounds`. Throws a
|
|
495
|
+
* `RangeError` for passwords longer than 72 bytes (see
|
|
565
496
|
* `isPasswordTooLong()`).
|
|
566
497
|
*/
|
|
567
|
-
hashPassword(password: string
|
|
498
|
+
hashPassword(password: string): Promise<string>;
|
|
568
499
|
/** Whether a bcrypt hash has another cost than `passwordHashRounds`. */
|
|
569
500
|
passwordNeedsRehash(passwordHash: string): boolean;
|
|
570
501
|
/**
|
|
@@ -589,9 +520,7 @@ interface Auth {
|
|
|
589
520
|
* const auth = createAuth({ keys, dataProvider })
|
|
590
521
|
* await createApp({ adapter, plugins: [authPlugin(auth)] })
|
|
591
522
|
*/
|
|
592
|
-
declare const createAuth: (input:
|
|
593
|
-
/** Longest password bcrypt reads, in UTF-8 bytes. */
|
|
594
|
-
declare const MAX_PASSWORD_BYTES = 72;
|
|
523
|
+
declare const createAuth: (input: AuthOptions) => Auth;
|
|
595
524
|
/**
|
|
596
525
|
* Whether a password exceeds the 72 bytes (UTF-8) read by bcrypt. Check
|
|
597
526
|
* it in the sign-up and password change schemas: `hashPassword()` rejects
|
|
@@ -601,12 +530,16 @@ declare const MAX_PASSWORD_BYTES = 72;
|
|
|
601
530
|
* password: z.string().min(12).refine((p) => !isPasswordTooLong(p), 'Password too long')
|
|
602
531
|
*/
|
|
603
532
|
declare const isPasswordTooLong: (password: string) => boolean;
|
|
533
|
+
interface PasswordHashOptions {
|
|
534
|
+
/** bcrypt cost, from 4 to 31. Defaults to 12. */
|
|
535
|
+
rounds?: number;
|
|
536
|
+
}
|
|
604
537
|
/**
|
|
605
|
-
* Hashes a password with bcrypt
|
|
606
|
-
* `RangeError` for passwords longer than 72 bytes (see
|
|
538
|
+
* Hashes a password with bcrypt, out of an auth service (seeds, scripts).
|
|
539
|
+
* Throws a `RangeError` for passwords longer than 72 bytes (see
|
|
607
540
|
* `isPasswordTooLong()`).
|
|
608
541
|
*/
|
|
609
|
-
declare const generatePasswordHash: (password: string,
|
|
542
|
+
declare const generatePasswordHash: (password: string, options?: PasswordHashOptions) => Promise<string>;
|
|
610
543
|
|
|
611
544
|
/** Context guaranteed by `requireAuth`, `requireRole()` and `requireClient()`. */
|
|
612
545
|
interface AuthenticatedContext {
|
|
@@ -652,11 +585,6 @@ declare const AUTH_SECURITY_SCHEMES: {
|
|
|
652
585
|
readonly bearerFormat: "JWT";
|
|
653
586
|
};
|
|
654
587
|
};
|
|
655
|
-
/**
|
|
656
|
-
* Access token of a request: `Authorization: Bearer` header, then cookie
|
|
657
|
-
* (when enabled). The request body is never read.
|
|
658
|
-
*/
|
|
659
|
-
declare const extractAccessToken: (req: DotpressRequest, cookies: ResolvedCookieSettings) => string | undefined;
|
|
660
588
|
/**
|
|
661
589
|
* Plugs an authentication service into an app:
|
|
662
590
|
*
|
|
@@ -678,5 +606,5 @@ declare const authPlugin: (auth: Auth, options?: AuthPluginOptions) => {
|
|
|
678
606
|
readonly setup: (api: _dotpress_core.PluginAPI) => Promise<void>;
|
|
679
607
|
};
|
|
680
608
|
|
|
681
|
-
export { AUTH_SECURITY_SCHEMES, AuthError,
|
|
682
|
-
export type { AdditionalPublicKey, Auth, AuthErrorCode,
|
|
609
|
+
export { AUTH_SECURITY_SCHEMES, AuthError, authPlugin, createAuth, generatePasswordHash, isPasswordTooLong, requireAuth, requireClient, requireRole };
|
|
610
|
+
export type { AdditionalPublicKey, Auth, AuthCookieOptions, AuthErrorCode, AuthOptions, AuthPluginOptions, AuthSession, AuthUser, AuthenticatedContext, BeforeLoginEvent, ClientOptions, DataProvider, IssuedTokens, KeyPairOptions, LoginErrorType, LoginFailedEvent, LoginSuccessEvent, LoginThrottleOptions, LogoutScope, NewRefreshToken, PasswordHashOptions, RateLimit, RefreshTokenData, RefreshTokenReuseEvent, RevokedSession, SessionOptions, TokenPair, TokenSubject, UserIdentifiers };
|