@adonisjs/auth 9.5.0 → 10.0.0-next.0
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/build/{chunk-SAW2RPCA.js → chunk-MSPAYMZE.js} +94 -4
- package/build/{chunk-MUPAP5IP.js → chunk-S5G5RTJX.js} +40 -0
- package/build/index.d.ts +8 -8
- package/build/index.js +2 -2
- package/build/modules/access_tokens_guard/access_token.d.ts +103 -0
- package/build/modules/access_tokens_guard/crc32.d.ts +39 -0
- package/build/modules/access_tokens_guard/define_config.d.ts +4 -4
- package/build/modules/access_tokens_guard/guard.d.ts +82 -5
- package/build/modules/access_tokens_guard/main.d.ts +5 -5
- package/build/modules/access_tokens_guard/main.js +339 -4
- package/build/modules/access_tokens_guard/token_providers/db.d.ts +102 -4
- package/build/modules/access_tokens_guard/types.d.ts +2 -2
- package/build/modules/access_tokens_guard/user_providers/lucid.d.ts +77 -4
- package/build/modules/basic_auth_guard/define_config.d.ts +4 -4
- package/build/modules/basic_auth_guard/guard.d.ts +16 -3
- package/build/modules/basic_auth_guard/main.d.ts +3 -3
- package/build/modules/basic_auth_guard/main.js +11 -1
- package/build/modules/basic_auth_guard/types.d.ts +1 -1
- package/build/modules/basic_auth_guard/user_providers/lucid.d.ts +19 -2
- package/build/modules/session_guard/define_config.d.ts +4 -4
- package/build/modules/session_guard/guard.d.ts +42 -3
- package/build/modules/session_guard/main.d.ts +5 -5
- package/build/modules/session_guard/main.js +349 -124
- package/build/modules/session_guard/remember_me_token.d.ts +64 -0
- package/build/modules/session_guard/token_providers/db.d.ts +92 -2
- package/build/modules/session_guard/types.d.ts +2 -2
- package/build/modules/session_guard/user_providers/lucid.d.ts +78 -5
- package/build/providers/auth_provider.d.ts +22 -1
- package/build/providers/auth_provider.js +16 -3
- package/build/services/auth.d.ts +1 -1
- package/build/src/auth_manager.d.ts +25 -4
- package/build/src/authenticator.d.ts +55 -3
- package/build/src/authenticator_client.d.ts +18 -1
- package/build/src/define_config.d.ts +19 -1
- package/build/src/errors.d.ts +31 -0
- package/build/src/middleware/initialize_auth_middleware.d.ts +15 -2
- package/build/src/middleware/initialize_auth_middleware.js +10 -0
- package/build/src/mixins/lucid.d.ts +13 -0
- package/build/src/mixins/lucid.js +27 -2
- package/build/src/plugins/japa/api_client.d.ts +10 -1
- package/build/src/plugins/japa/browser_client.d.ts +10 -1
- package/build/src/types.d.ts +141 -29
- package/package.json +23 -22
|
@@ -1,130 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
E_UNAUTHORIZED_ACCESS
|
|
3
|
-
} from "../../chunk-
|
|
3
|
+
} from "../../chunk-S5G5RTJX.js";
|
|
4
4
|
import "../../chunk-UXA4FHST.js";
|
|
5
5
|
|
|
6
|
-
// modules/session_guard/remember_me_token.ts
|
|
7
|
-
import { createHash } from "crypto";
|
|
8
|
-
import string from "@adonisjs/core/helpers/string";
|
|
9
|
-
import { Secret, base64, safeEqual } from "@adonisjs/core/helpers";
|
|
10
|
-
var RememberMeToken = class {
|
|
11
|
-
/**
|
|
12
|
-
* Decodes a publicly shared token and return the series
|
|
13
|
-
* and the token value from it.
|
|
14
|
-
*
|
|
15
|
-
* Returns null when unable to decode the token because of
|
|
16
|
-
* invalid format or encoding.
|
|
17
|
-
*/
|
|
18
|
-
static decode(value) {
|
|
19
|
-
if (typeof value !== "string") {
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
if (!value) {
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
const [identifier, ...tokenValue] = value.split(".");
|
|
26
|
-
if (!identifier || tokenValue.length === 0) {
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
const decodedIdentifier = base64.urlDecode(identifier);
|
|
30
|
-
const decodedSecret = base64.urlDecode(tokenValue.join("."));
|
|
31
|
-
if (!decodedIdentifier || !decodedSecret) {
|
|
32
|
-
return null;
|
|
33
|
-
}
|
|
34
|
-
return {
|
|
35
|
-
identifier: decodedIdentifier,
|
|
36
|
-
secret: new Secret(decodedSecret)
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Creates a transient token that can be shared with the persistence
|
|
41
|
-
* layer.
|
|
42
|
-
*/
|
|
43
|
-
static createTransientToken(userId, size, expiresIn) {
|
|
44
|
-
const expiresAt = /* @__PURE__ */ new Date();
|
|
45
|
-
expiresAt.setSeconds(expiresAt.getSeconds() + string.seconds.parse(expiresIn));
|
|
46
|
-
return {
|
|
47
|
-
userId,
|
|
48
|
-
expiresAt,
|
|
49
|
-
...this.seed(size)
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Creates a secret opaque token and its hash.
|
|
54
|
-
*/
|
|
55
|
-
static seed(size) {
|
|
56
|
-
const seed = string.random(size);
|
|
57
|
-
const secret = new Secret(seed);
|
|
58
|
-
const hash = createHash("sha256").update(secret.release()).digest("hex");
|
|
59
|
-
return { secret, hash };
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Identifer is a unique sequence to identify the
|
|
63
|
-
* token within database. It should be the
|
|
64
|
-
* primary/unique key
|
|
65
|
-
*/
|
|
66
|
-
identifier;
|
|
67
|
-
/**
|
|
68
|
-
* Reference to the user id for whom the token
|
|
69
|
-
* is generated.
|
|
70
|
-
*/
|
|
71
|
-
tokenableId;
|
|
72
|
-
/**
|
|
73
|
-
* The value is a public representation of a token. It is created
|
|
74
|
-
* by combining the "identifier"."secret"
|
|
75
|
-
*/
|
|
76
|
-
value;
|
|
77
|
-
/**
|
|
78
|
-
* Hash is computed from the seed to later verify the validity
|
|
79
|
-
* of seed
|
|
80
|
-
*/
|
|
81
|
-
hash;
|
|
82
|
-
/**
|
|
83
|
-
* Date/time when the token instance was created
|
|
84
|
-
*/
|
|
85
|
-
createdAt;
|
|
86
|
-
/**
|
|
87
|
-
* Date/time when the token was updated
|
|
88
|
-
*/
|
|
89
|
-
updatedAt;
|
|
90
|
-
/**
|
|
91
|
-
* Timestamp at which the token will expire
|
|
92
|
-
*/
|
|
93
|
-
expiresAt;
|
|
94
|
-
constructor(attributes) {
|
|
95
|
-
this.identifier = attributes.identifier;
|
|
96
|
-
this.tokenableId = attributes.tokenableId;
|
|
97
|
-
this.hash = attributes.hash;
|
|
98
|
-
this.createdAt = attributes.createdAt;
|
|
99
|
-
this.updatedAt = attributes.updatedAt;
|
|
100
|
-
this.expiresAt = attributes.expiresAt;
|
|
101
|
-
if (attributes.secret) {
|
|
102
|
-
this.value = new Secret(
|
|
103
|
-
`${base64.urlEncode(String(this.identifier))}.${base64.urlEncode(
|
|
104
|
-
attributes.secret.release()
|
|
105
|
-
)}`
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Check if the token has been expired. Verifies
|
|
111
|
-
* the "expiresAt" timestamp with the current
|
|
112
|
-
* date.
|
|
113
|
-
*/
|
|
114
|
-
isExpired() {
|
|
115
|
-
return this.expiresAt < /* @__PURE__ */ new Date();
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Verifies the value of a token against the pre-defined hash
|
|
119
|
-
*/
|
|
120
|
-
verify(secret) {
|
|
121
|
-
const newHash = createHash("sha256").update(secret.release()).digest("hex");
|
|
122
|
-
return safeEqual(this.hash, newHash);
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
|
|
126
6
|
// modules/session_guard/guard.ts
|
|
127
|
-
import { Secret
|
|
7
|
+
import { Secret } from "@adonisjs/core/helpers";
|
|
128
8
|
import { RuntimeException } from "@adonisjs/core/exceptions";
|
|
129
9
|
var SessionGuard = class {
|
|
130
10
|
/**
|
|
@@ -191,16 +71,40 @@ var SessionGuard = class {
|
|
|
191
71
|
/**
|
|
192
72
|
* The key used to store the logged-in user id inside
|
|
193
73
|
* session
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* console.log('Session key:', guard.sessionKeyName) // 'auth_web'
|
|
194
77
|
*/
|
|
195
78
|
get sessionKeyName() {
|
|
196
79
|
return `auth_${this.#name}`;
|
|
197
80
|
}
|
|
198
81
|
/**
|
|
199
82
|
* The key used to store the remember me token cookie
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* console.log('Remember me key:', guard.rememberMeKeyName) // 'remember_web'
|
|
200
86
|
*/
|
|
201
87
|
get rememberMeKeyName() {
|
|
202
88
|
return `remember_${this.#name}`;
|
|
203
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Creates a new SessionGuard instance
|
|
92
|
+
*
|
|
93
|
+
* @param name - Unique name for the guard instance
|
|
94
|
+
* @param ctx - HTTP context for the current request
|
|
95
|
+
* @param options - Configuration options for the session guard
|
|
96
|
+
* @param emitter - Event emitter for guard events
|
|
97
|
+
* @param userProvider - User provider for authentication
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* const guard = new SessionGuard(
|
|
101
|
+
* 'web',
|
|
102
|
+
* ctx,
|
|
103
|
+
* { useRememberMeTokens: true, rememberMeTokensAge: '30d' },
|
|
104
|
+
* emitter,
|
|
105
|
+
* userProvider
|
|
106
|
+
* )
|
|
107
|
+
*/
|
|
204
108
|
constructor(name, ctx, options, emitter, userProvider) {
|
|
205
109
|
this.#name = name;
|
|
206
110
|
this.#ctx = ctx;
|
|
@@ -312,7 +216,7 @@ var SessionGuard = class {
|
|
|
312
216
|
*/
|
|
313
217
|
async #authenticateViaRememberCookie(rememberMeCookie, sessionId) {
|
|
314
218
|
const userProvider = this.#userProvider;
|
|
315
|
-
const token = await userProvider.verifyRememberToken(new
|
|
219
|
+
const token = await userProvider.verifyRememberToken(new Secret(rememberMeCookie));
|
|
316
220
|
if (!token) {
|
|
317
221
|
throw this.#authenticationFailed(sessionId);
|
|
318
222
|
}
|
|
@@ -385,7 +289,7 @@ var SessionGuard = class {
|
|
|
385
289
|
this.#ctx.response.clearCookie(this.rememberMeKeyName);
|
|
386
290
|
if (this.user && rememberMeCookie && this.#options.useRememberMeTokens) {
|
|
387
291
|
const userProvider = this.#userProvider;
|
|
388
|
-
const token = await userProvider.verifyRememberToken(new
|
|
292
|
+
const token = await userProvider.verifyRememberToken(new Secret(rememberMeCookie));
|
|
389
293
|
if (token) {
|
|
390
294
|
await userProvider.deleteRemeberToken(this.user, token.identifier);
|
|
391
295
|
}
|
|
@@ -457,10 +361,196 @@ var SessionGuard = class {
|
|
|
457
361
|
}
|
|
458
362
|
};
|
|
459
363
|
|
|
364
|
+
// modules/session_guard/remember_me_token.ts
|
|
365
|
+
import { createHash } from "crypto";
|
|
366
|
+
import string from "@adonisjs/core/helpers/string";
|
|
367
|
+
import { Secret as Secret2, base64, safeEqual } from "@adonisjs/core/helpers";
|
|
368
|
+
var RememberMeToken = class {
|
|
369
|
+
/**
|
|
370
|
+
* Decodes a publicly shared token and return the series
|
|
371
|
+
* and the token value from it.
|
|
372
|
+
*
|
|
373
|
+
* Returns null when unable to decode the token because of
|
|
374
|
+
* invalid format or encoding.
|
|
375
|
+
*
|
|
376
|
+
* @param value - The token value to decode
|
|
377
|
+
*
|
|
378
|
+
* @example
|
|
379
|
+
* const decoded = RememberMeToken.decode('abc123.def456')
|
|
380
|
+
* if (decoded) {
|
|
381
|
+
* console.log('Token ID:', decoded.identifier)
|
|
382
|
+
* console.log('Secret:', decoded.secret.release())
|
|
383
|
+
* }
|
|
384
|
+
*/
|
|
385
|
+
static decode(value) {
|
|
386
|
+
if (typeof value !== "string") {
|
|
387
|
+
return null;
|
|
388
|
+
}
|
|
389
|
+
if (!value) {
|
|
390
|
+
return null;
|
|
391
|
+
}
|
|
392
|
+
const [identifier, ...tokenValue] = value.split(".");
|
|
393
|
+
if (!identifier || tokenValue.length === 0) {
|
|
394
|
+
return null;
|
|
395
|
+
}
|
|
396
|
+
const decodedIdentifier = base64.urlDecode(identifier);
|
|
397
|
+
const decodedSecret = base64.urlDecode(tokenValue.join("."));
|
|
398
|
+
if (!decodedIdentifier || !decodedSecret) {
|
|
399
|
+
return null;
|
|
400
|
+
}
|
|
401
|
+
return {
|
|
402
|
+
identifier: decodedIdentifier,
|
|
403
|
+
secret: new Secret2(decodedSecret)
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Creates a transient token that can be shared with the persistence
|
|
408
|
+
* layer.
|
|
409
|
+
*
|
|
410
|
+
* @param userId - The ID of the user for whom the token is created
|
|
411
|
+
* @param size - The size of the random secret to generate
|
|
412
|
+
* @param expiresIn - Expiration time (seconds or duration string)
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* const transientToken = RememberMeToken.createTransientToken(123, 32, '30d')
|
|
416
|
+
* // Store transientToken in database
|
|
417
|
+
*/
|
|
418
|
+
static createTransientToken(userId, size, expiresIn) {
|
|
419
|
+
const expiresAt = /* @__PURE__ */ new Date();
|
|
420
|
+
expiresAt.setSeconds(expiresAt.getSeconds() + string.seconds.parse(expiresIn));
|
|
421
|
+
return {
|
|
422
|
+
userId,
|
|
423
|
+
expiresAt,
|
|
424
|
+
...this.seed(size)
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Creates a secret opaque token and its hash.
|
|
429
|
+
*
|
|
430
|
+
* @param size - The size of the random string to generate
|
|
431
|
+
*
|
|
432
|
+
* @example
|
|
433
|
+
* const { secret, hash } = RememberMeToken.seed(32)
|
|
434
|
+
* console.log('Secret:', secret.release())
|
|
435
|
+
* console.log('Hash:', hash)
|
|
436
|
+
*/
|
|
437
|
+
static seed(size) {
|
|
438
|
+
const seed = string.random(size);
|
|
439
|
+
const secret = new Secret2(seed);
|
|
440
|
+
const hash = createHash("sha256").update(secret.release()).digest("hex");
|
|
441
|
+
return { secret, hash };
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Identifer is a unique sequence to identify the
|
|
445
|
+
* token within database. It should be the
|
|
446
|
+
* primary/unique key
|
|
447
|
+
*/
|
|
448
|
+
identifier;
|
|
449
|
+
/**
|
|
450
|
+
* Reference to the user id for whom the token
|
|
451
|
+
* is generated.
|
|
452
|
+
*/
|
|
453
|
+
tokenableId;
|
|
454
|
+
/**
|
|
455
|
+
* The value is a public representation of a token. It is created
|
|
456
|
+
* by combining the "identifier"."secret"
|
|
457
|
+
*/
|
|
458
|
+
value;
|
|
459
|
+
/**
|
|
460
|
+
* Hash is computed from the seed to later verify the validity
|
|
461
|
+
* of seed
|
|
462
|
+
*/
|
|
463
|
+
hash;
|
|
464
|
+
/**
|
|
465
|
+
* Date/time when the token instance was created
|
|
466
|
+
*/
|
|
467
|
+
createdAt;
|
|
468
|
+
/**
|
|
469
|
+
* Date/time when the token was updated
|
|
470
|
+
*/
|
|
471
|
+
updatedAt;
|
|
472
|
+
/**
|
|
473
|
+
* Timestamp at which the token will expire
|
|
474
|
+
*/
|
|
475
|
+
expiresAt;
|
|
476
|
+
/**
|
|
477
|
+
* Creates a new RememberMeToken instance
|
|
478
|
+
*
|
|
479
|
+
* @param attributes - Token attributes including identifier, user ID, hash, etc.
|
|
480
|
+
*
|
|
481
|
+
* @example
|
|
482
|
+
* const token = new RememberMeToken({
|
|
483
|
+
* identifier: 1,
|
|
484
|
+
* tokenableId: 123,
|
|
485
|
+
* hash: 'sha256hash',
|
|
486
|
+
* createdAt: new Date(),
|
|
487
|
+
* updatedAt: new Date(),
|
|
488
|
+
* expiresAt: new Date(Date.now() + 86400000)
|
|
489
|
+
* })
|
|
490
|
+
*/
|
|
491
|
+
constructor(attributes) {
|
|
492
|
+
this.identifier = attributes.identifier;
|
|
493
|
+
this.tokenableId = attributes.tokenableId;
|
|
494
|
+
this.hash = attributes.hash;
|
|
495
|
+
this.createdAt = attributes.createdAt;
|
|
496
|
+
this.updatedAt = attributes.updatedAt;
|
|
497
|
+
this.expiresAt = attributes.expiresAt;
|
|
498
|
+
if (attributes.secret) {
|
|
499
|
+
this.value = new Secret2(
|
|
500
|
+
`${base64.urlEncode(String(this.identifier))}.${base64.urlEncode(
|
|
501
|
+
attributes.secret.release()
|
|
502
|
+
)}`
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Check if the token has been expired. Verifies
|
|
508
|
+
* the "expiresAt" timestamp with the current
|
|
509
|
+
* date.
|
|
510
|
+
*
|
|
511
|
+
* @example
|
|
512
|
+
* if (token.isExpired()) {
|
|
513
|
+
* console.log('Remember me token has expired')
|
|
514
|
+
* } else {
|
|
515
|
+
* console.log('Token is still valid')
|
|
516
|
+
* }
|
|
517
|
+
*/
|
|
518
|
+
isExpired() {
|
|
519
|
+
return this.expiresAt < /* @__PURE__ */ new Date();
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Verifies the value of a token against the pre-defined hash
|
|
523
|
+
*
|
|
524
|
+
* @param secret - The secret to verify against the stored hash
|
|
525
|
+
*
|
|
526
|
+
* @example
|
|
527
|
+
* const isValid = token.verify(new Secret('user-provided-secret'))
|
|
528
|
+
* if (isValid) {
|
|
529
|
+
* console.log('Remember me token is valid')
|
|
530
|
+
* }
|
|
531
|
+
*/
|
|
532
|
+
verify(secret) {
|
|
533
|
+
const newHash = createHash("sha256").update(secret.release()).digest("hex");
|
|
534
|
+
return safeEqual(this.hash, newHash);
|
|
535
|
+
}
|
|
536
|
+
};
|
|
537
|
+
|
|
460
538
|
// modules/session_guard/token_providers/db.ts
|
|
461
539
|
import { inspect } from "util";
|
|
462
540
|
import { RuntimeException as RuntimeException2 } from "@adonisjs/core/exceptions";
|
|
463
541
|
var DbRememberMeTokensProvider = class _DbRememberMeTokensProvider {
|
|
542
|
+
/**
|
|
543
|
+
* Creates a new DbRememberMeTokensProvider instance
|
|
544
|
+
*
|
|
545
|
+
* @param options - Configuration options for the provider
|
|
546
|
+
*
|
|
547
|
+
* @example
|
|
548
|
+
* const provider = new DbRememberMeTokensProvider({
|
|
549
|
+
* tokenableModel: () => import('#models/user'),
|
|
550
|
+
* table: 'remember_me_tokens',
|
|
551
|
+
* tokenSecretLength: 40
|
|
552
|
+
* })
|
|
553
|
+
*/
|
|
464
554
|
constructor(options) {
|
|
465
555
|
this.options = options;
|
|
466
556
|
this.table = options.table || "remember_me_tokens";
|
|
@@ -468,6 +558,15 @@ var DbRememberMeTokensProvider = class _DbRememberMeTokensProvider {
|
|
|
468
558
|
}
|
|
469
559
|
/**
|
|
470
560
|
* Create tokens provider instance for a given Lucid model
|
|
561
|
+
*
|
|
562
|
+
* @param model - The tokenable model factory function
|
|
563
|
+
* @param options - Optional configuration options
|
|
564
|
+
*
|
|
565
|
+
* @example
|
|
566
|
+
* const provider = DbRememberMeTokensProvider.forModel(
|
|
567
|
+
* () => import('#models/user'),
|
|
568
|
+
* { table: 'user_remember_tokens' }
|
|
569
|
+
* )
|
|
471
570
|
*/
|
|
472
571
|
static forModel(model, options) {
|
|
473
572
|
return new _DbRememberMeTokensProvider({
|
|
@@ -509,6 +608,16 @@ var DbRememberMeTokensProvider = class _DbRememberMeTokensProvider {
|
|
|
509
608
|
}
|
|
510
609
|
/**
|
|
511
610
|
* Maps a database row to an instance token instance
|
|
611
|
+
*
|
|
612
|
+
* @param dbRow - The database row containing token data
|
|
613
|
+
*
|
|
614
|
+
* @example
|
|
615
|
+
* const token = provider.dbRowToRememberMeToken({
|
|
616
|
+
* id: 1,
|
|
617
|
+
* tokenable_id: 123,
|
|
618
|
+
* hash: 'sha256hash',
|
|
619
|
+
* // ... other columns
|
|
620
|
+
* })
|
|
512
621
|
*/
|
|
513
622
|
dbRowToRememberMeToken(dbRow) {
|
|
514
623
|
return new RememberMeToken({
|
|
@@ -522,6 +631,10 @@ var DbRememberMeTokensProvider = class _DbRememberMeTokensProvider {
|
|
|
522
631
|
}
|
|
523
632
|
/**
|
|
524
633
|
* Returns a query client instance from the parent model
|
|
634
|
+
*
|
|
635
|
+
* @example
|
|
636
|
+
* const db = await provider.getDb()
|
|
637
|
+
* const tokens = await db.from('remember_me_tokens').select('*')
|
|
525
638
|
*/
|
|
526
639
|
async getDb() {
|
|
527
640
|
const model = this.options.tokenableModel;
|
|
@@ -529,6 +642,13 @@ var DbRememberMeTokensProvider = class _DbRememberMeTokensProvider {
|
|
|
529
642
|
}
|
|
530
643
|
/**
|
|
531
644
|
* Create a token for a user
|
|
645
|
+
*
|
|
646
|
+
* @param user - The user instance to create a token for
|
|
647
|
+
* @param expiresIn - Token expiration time
|
|
648
|
+
*
|
|
649
|
+
* @example
|
|
650
|
+
* const token = await provider.create(user, '30d')
|
|
651
|
+
* console.log('Remember token:', token.value.release())
|
|
532
652
|
*/
|
|
533
653
|
async create(user, expiresIn) {
|
|
534
654
|
this.#ensureIsPersisted(user);
|
|
@@ -564,6 +684,15 @@ var DbRememberMeTokensProvider = class _DbRememberMeTokensProvider {
|
|
|
564
684
|
}
|
|
565
685
|
/**
|
|
566
686
|
* Find a token for a user by the token id
|
|
687
|
+
*
|
|
688
|
+
* @param user - The user instance that owns the token
|
|
689
|
+
* @param identifier - The token identifier to search for
|
|
690
|
+
*
|
|
691
|
+
* @example
|
|
692
|
+
* const token = await provider.find(user, 123)
|
|
693
|
+
* if (token) {
|
|
694
|
+
* console.log('Found token with id:', token.identifier)
|
|
695
|
+
* }
|
|
567
696
|
*/
|
|
568
697
|
async find(user, identifier) {
|
|
569
698
|
this.#ensureIsPersisted(user);
|
|
@@ -576,6 +705,13 @@ var DbRememberMeTokensProvider = class _DbRememberMeTokensProvider {
|
|
|
576
705
|
}
|
|
577
706
|
/**
|
|
578
707
|
* Delete a token by its id
|
|
708
|
+
*
|
|
709
|
+
* @param user - The user instance that owns the token
|
|
710
|
+
* @param identifier - The token identifier to delete
|
|
711
|
+
*
|
|
712
|
+
* @example
|
|
713
|
+
* const deletedCount = await provider.delete(user, 123)
|
|
714
|
+
* console.log('Deleted tokens:', deletedCount)
|
|
579
715
|
*/
|
|
580
716
|
async delete(user, identifier) {
|
|
581
717
|
this.#ensureIsPersisted(user);
|
|
@@ -585,6 +721,13 @@ var DbRememberMeTokensProvider = class _DbRememberMeTokensProvider {
|
|
|
585
721
|
}
|
|
586
722
|
/**
|
|
587
723
|
* Returns all the tokens a given user
|
|
724
|
+
*
|
|
725
|
+
* @param user - The user instance to get tokens for
|
|
726
|
+
*
|
|
727
|
+
* @example
|
|
728
|
+
* const tokens = await provider.all(user)
|
|
729
|
+
* console.log('User has', tokens.length, 'remember tokens')
|
|
730
|
+
* tokens.forEach(token => console.log(token.identifier))
|
|
588
731
|
*/
|
|
589
732
|
async all(user) {
|
|
590
733
|
this.#ensureIsPersisted(user);
|
|
@@ -600,6 +743,14 @@ var DbRememberMeTokensProvider = class _DbRememberMeTokensProvider {
|
|
|
600
743
|
*
|
|
601
744
|
* Returns null when unable to verify the token or find it
|
|
602
745
|
* inside the storage
|
|
746
|
+
*
|
|
747
|
+
* @param tokenValue - The token value to verify
|
|
748
|
+
*
|
|
749
|
+
* @example
|
|
750
|
+
* const token = await provider.verify(new Secret('rmt_abc123.def456'))
|
|
751
|
+
* if (token && !token.isExpired()) {
|
|
752
|
+
* console.log('Valid remember token for user:', token.tokenableId)
|
|
753
|
+
* }
|
|
603
754
|
*/
|
|
604
755
|
async verify(tokenValue) {
|
|
605
756
|
const decodedToken = RememberMeToken.decode(tokenValue.release());
|
|
@@ -624,6 +775,14 @@ var DbRememberMeTokensProvider = class _DbRememberMeTokensProvider {
|
|
|
624
775
|
* Ideally, the recycle should update the existing token, but we
|
|
625
776
|
* skip that for now and come back to it later and handle race
|
|
626
777
|
* conditions as well.
|
|
778
|
+
*
|
|
779
|
+
* @param user - The user that owns the token
|
|
780
|
+
* @param identifier - The token identifier to recycle
|
|
781
|
+
* @param expiresIn - New expiration time
|
|
782
|
+
*
|
|
783
|
+
* @example
|
|
784
|
+
* const newToken = await provider.recycle(user, 123, '30d')
|
|
785
|
+
* console.log('Recycled token:', newToken.value.release())
|
|
627
786
|
*/
|
|
628
787
|
async recycle(user, identifier, expiresIn) {
|
|
629
788
|
await this.delete(user, identifier);
|
|
@@ -634,6 +793,16 @@ var DbRememberMeTokensProvider = class _DbRememberMeTokensProvider {
|
|
|
634
793
|
// modules/session_guard/user_providers/lucid.ts
|
|
635
794
|
import { RuntimeException as RuntimeException3 } from "@adonisjs/core/exceptions";
|
|
636
795
|
var SessionLucidUserProvider = class {
|
|
796
|
+
/**
|
|
797
|
+
* Creates a new SessionLucidUserProvider instance
|
|
798
|
+
*
|
|
799
|
+
* @param options - Configuration options for the user provider
|
|
800
|
+
*
|
|
801
|
+
* @example
|
|
802
|
+
* const provider = new SessionLucidUserProvider({
|
|
803
|
+
* model: () => import('#models/user')
|
|
804
|
+
* })
|
|
805
|
+
*/
|
|
637
806
|
constructor(options) {
|
|
638
807
|
this.options = options;
|
|
639
808
|
}
|
|
@@ -644,6 +813,10 @@ var SessionLucidUserProvider = class {
|
|
|
644
813
|
/**
|
|
645
814
|
* Imports the model from the provider, returns and caches it
|
|
646
815
|
* for further operations.
|
|
816
|
+
*
|
|
817
|
+
* @example
|
|
818
|
+
* const UserModel = await provider.getModel()
|
|
819
|
+
* const user = await UserModel.find(1)
|
|
647
820
|
*/
|
|
648
821
|
async getModel() {
|
|
649
822
|
if (this.model && !("hot" in import.meta)) {
|
|
@@ -655,6 +828,10 @@ var SessionLucidUserProvider = class {
|
|
|
655
828
|
}
|
|
656
829
|
/**
|
|
657
830
|
* Returns the tokens provider associated with the user model
|
|
831
|
+
*
|
|
832
|
+
* @example
|
|
833
|
+
* const tokensProvider = await provider.getTokensProvider()
|
|
834
|
+
* const token = await tokensProvider.create(user, '7d')
|
|
658
835
|
*/
|
|
659
836
|
async getTokensProvider() {
|
|
660
837
|
const model = await this.getModel();
|
|
@@ -667,6 +844,13 @@ var SessionLucidUserProvider = class {
|
|
|
667
844
|
}
|
|
668
845
|
/**
|
|
669
846
|
* Creates an adapter user for the guard
|
|
847
|
+
*
|
|
848
|
+
* @param user - The user model instance
|
|
849
|
+
*
|
|
850
|
+
* @example
|
|
851
|
+
* const guardUser = await provider.createUserForGuard(user)
|
|
852
|
+
* console.log('User ID:', guardUser.getId())
|
|
853
|
+
* console.log('Original user:', guardUser.getOriginal())
|
|
670
854
|
*/
|
|
671
855
|
async createUserForGuard(user) {
|
|
672
856
|
const model = await this.getModel();
|
|
@@ -691,6 +875,15 @@ var SessionLucidUserProvider = class {
|
|
|
691
875
|
}
|
|
692
876
|
/**
|
|
693
877
|
* Finds a user by their primary key value
|
|
878
|
+
*
|
|
879
|
+
* @param identifier - The user identifier to search for
|
|
880
|
+
*
|
|
881
|
+
* @example
|
|
882
|
+
* const guardUser = await provider.findById(123)
|
|
883
|
+
* if (guardUser) {
|
|
884
|
+
* const originalUser = guardUser.getOriginal()
|
|
885
|
+
* console.log('Found user:', originalUser.email)
|
|
886
|
+
* }
|
|
694
887
|
*/
|
|
695
888
|
async findById(identifier) {
|
|
696
889
|
const model = await this.getModel();
|
|
@@ -702,6 +895,13 @@ var SessionLucidUserProvider = class {
|
|
|
702
895
|
}
|
|
703
896
|
/**
|
|
704
897
|
* Creates a remember token for a given user
|
|
898
|
+
*
|
|
899
|
+
* @param user - The user to create a token for
|
|
900
|
+
* @param expiresIn - Token expiration time
|
|
901
|
+
*
|
|
902
|
+
* @example
|
|
903
|
+
* const token = await provider.createRememberToken(user, '30d')
|
|
904
|
+
* console.log('Remember token:', token.value.release())
|
|
705
905
|
*/
|
|
706
906
|
async createRememberToken(user, expiresIn) {
|
|
707
907
|
const tokensProvider = await this.getTokensProvider();
|
|
@@ -709,6 +909,16 @@ var SessionLucidUserProvider = class {
|
|
|
709
909
|
}
|
|
710
910
|
/**
|
|
711
911
|
* Verify a token by its publicly shared value
|
|
912
|
+
*
|
|
913
|
+
* @param tokenValue - The token value to verify
|
|
914
|
+
*
|
|
915
|
+
* @example
|
|
916
|
+
* const token = await provider.verifyRememberToken(
|
|
917
|
+
* new Secret('rmt_abc123.def456')
|
|
918
|
+
* )
|
|
919
|
+
* if (token && !token.isExpired()) {
|
|
920
|
+
* console.log('Valid remember token for user:', token.tokenableId)
|
|
921
|
+
* }
|
|
712
922
|
*/
|
|
713
923
|
async verifyRememberToken(tokenValue) {
|
|
714
924
|
const tokensProvider = await this.getTokensProvider();
|
|
@@ -716,6 +926,13 @@ var SessionLucidUserProvider = class {
|
|
|
716
926
|
}
|
|
717
927
|
/**
|
|
718
928
|
* Delete a token for a user by the token identifier
|
|
929
|
+
*
|
|
930
|
+
* @param user - The user that owns the token
|
|
931
|
+
* @param identifier - The token identifier to delete
|
|
932
|
+
*
|
|
933
|
+
* @example
|
|
934
|
+
* const deletedCount = await provider.deleteRemeberToken(user, 123)
|
|
935
|
+
* console.log('Deleted tokens:', deletedCount)
|
|
719
936
|
*/
|
|
720
937
|
async deleteRemeberToken(user, identifier) {
|
|
721
938
|
const tokensProvider = await this.getTokensProvider();
|
|
@@ -723,6 +940,14 @@ var SessionLucidUserProvider = class {
|
|
|
723
940
|
}
|
|
724
941
|
/**
|
|
725
942
|
* Recycle a token for a user by the token identifier
|
|
943
|
+
*
|
|
944
|
+
* @param user - The user that owns the token
|
|
945
|
+
* @param identifier - The token identifier to recycle
|
|
946
|
+
* @param expiresIn - New expiration time
|
|
947
|
+
*
|
|
948
|
+
* @example
|
|
949
|
+
* const newToken = await provider.recycleRememberToken(user, 123, '30d')
|
|
950
|
+
* console.log('Recycled token:', newToken.value.release())
|
|
726
951
|
*/
|
|
727
952
|
async recycleRememberToken(user, identifier, expiresIn) {
|
|
728
953
|
const tokensProvider = await this.getTokensProvider();
|