@adonisjs/auth 9.4.2 → 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-JFTYQIKS.js → chunk-MSPAYMZE.js} +110 -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 +61 -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 +35 -2
- 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 +38 -37
|
@@ -1,6 +1,6 @@
|
|
|
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
6
|
// modules/access_tokens_guard/access_token.ts
|
|
@@ -295,13 +295,43 @@ var CRC32 = class {
|
|
|
295
295
|
}
|
|
296
296
|
return 4294967295 - num * -1 + 1;
|
|
297
297
|
}
|
|
298
|
+
/**
|
|
299
|
+
* Calculate CRC32 checksum for a string input
|
|
300
|
+
*
|
|
301
|
+
* @param input - The string to calculate checksum for
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* const crc = new CRC32()
|
|
305
|
+
* const checksum = crc.calculate('hello-world')
|
|
306
|
+
* console.log('CRC32:', checksum)
|
|
307
|
+
*/
|
|
298
308
|
calculate(input) {
|
|
299
309
|
return this.forString(input);
|
|
300
310
|
}
|
|
311
|
+
/**
|
|
312
|
+
* Calculate CRC32 checksum for a string
|
|
313
|
+
*
|
|
314
|
+
* @param input - The string to process
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* const crc = new CRC32()
|
|
318
|
+
* const result = crc.forString('test-string')
|
|
319
|
+
*/
|
|
301
320
|
forString(input) {
|
|
302
321
|
const bytes = this.#strToBytes(input);
|
|
303
322
|
return this.forBytes(bytes);
|
|
304
323
|
}
|
|
324
|
+
/**
|
|
325
|
+
* Calculate CRC32 checksum for byte array
|
|
326
|
+
*
|
|
327
|
+
* @param bytes - The byte array to process
|
|
328
|
+
* @param accumulator - Optional accumulator for chained calculations
|
|
329
|
+
*
|
|
330
|
+
* @example
|
|
331
|
+
* const crc = new CRC32()
|
|
332
|
+
* const bytes = new TextEncoder().encode('hello')
|
|
333
|
+
* const result = crc.forBytes(bytes)
|
|
334
|
+
*/
|
|
305
335
|
forBytes(bytes, accumulator) {
|
|
306
336
|
const crc = this.#calculateBytes(bytes, accumulator);
|
|
307
337
|
return this.#crcToUint(crc);
|
|
@@ -316,6 +346,16 @@ var AccessToken = class {
|
|
|
316
346
|
*
|
|
317
347
|
* Returns null when unable to decode the token because of
|
|
318
348
|
* invalid format or encoding.
|
|
349
|
+
*
|
|
350
|
+
* @param prefix - The token prefix to validate against
|
|
351
|
+
* @param value - The token value to decode
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* const decoded = AccessToken.decode('oat_', 'oat_abc123.def456')
|
|
355
|
+
* if (decoded) {
|
|
356
|
+
* console.log('Token ID:', decoded.identifier)
|
|
357
|
+
* console.log('Secret:', decoded.secret.release())
|
|
358
|
+
* }
|
|
319
359
|
*/
|
|
320
360
|
static decode(prefix, value) {
|
|
321
361
|
if (typeof value !== "string" || !value.startsWith(`${prefix}`)) {
|
|
@@ -342,6 +382,14 @@ var AccessToken = class {
|
|
|
342
382
|
/**
|
|
343
383
|
* Creates a transient token that can be shared with the persistence
|
|
344
384
|
* layer.
|
|
385
|
+
*
|
|
386
|
+
* @param userId - The ID of the user for whom the token is created
|
|
387
|
+
* @param size - The size of the random secret to generate
|
|
388
|
+
* @param expiresIn - Optional expiration time (seconds or duration string)
|
|
389
|
+
*
|
|
390
|
+
* @example
|
|
391
|
+
* const transientToken = AccessToken.createTransientToken(123, 32, '7d')
|
|
392
|
+
* // Store transientToken in database
|
|
345
393
|
*/
|
|
346
394
|
static createTransientToken(userId, size, expiresIn) {
|
|
347
395
|
let expiresAt;
|
|
@@ -359,6 +407,13 @@ var AccessToken = class {
|
|
|
359
407
|
* Creates a secret opaque token and its hash. The secret is
|
|
360
408
|
* suffixed with a crc32 checksum for secret scanning tools
|
|
361
409
|
* to easily identify the token.
|
|
410
|
+
*
|
|
411
|
+
* @param size - The size of the random string to generate
|
|
412
|
+
*
|
|
413
|
+
* @example
|
|
414
|
+
* const { secret, hash } = AccessToken.seed(32)
|
|
415
|
+
* console.log('Secret:', secret.release())
|
|
416
|
+
* console.log('Hash:', hash)
|
|
362
417
|
*/
|
|
363
418
|
static seed(size) {
|
|
364
419
|
const seed = string.random(size);
|
|
@@ -417,6 +472,25 @@ var AccessToken = class {
|
|
|
417
472
|
* is an array of abritary string values
|
|
418
473
|
*/
|
|
419
474
|
abilities;
|
|
475
|
+
/**
|
|
476
|
+
* Creates a new AccessToken instance
|
|
477
|
+
*
|
|
478
|
+
* @param attributes - Token attributes including identifier, user ID, type, hash, etc.
|
|
479
|
+
*
|
|
480
|
+
* @example
|
|
481
|
+
* const token = new AccessToken({
|
|
482
|
+
* identifier: 1,
|
|
483
|
+
* tokenableId: 123,
|
|
484
|
+
* type: 'api',
|
|
485
|
+
* hash: 'sha256hash',
|
|
486
|
+
* createdAt: new Date(),
|
|
487
|
+
* updatedAt: new Date(),
|
|
488
|
+
* lastUsedAt: null,
|
|
489
|
+
* expiresAt: new Date(Date.now() + 86400000),
|
|
490
|
+
* name: 'Mobile App Token',
|
|
491
|
+
* abilities: ['read:posts', 'write:posts']
|
|
492
|
+
* })
|
|
493
|
+
*/
|
|
420
494
|
constructor(attributes) {
|
|
421
495
|
this.identifier = attributes.identifier;
|
|
422
496
|
this.tokenableId = attributes.tokenableId;
|
|
@@ -441,18 +515,40 @@ var AccessToken = class {
|
|
|
441
515
|
}
|
|
442
516
|
/**
|
|
443
517
|
* Check if the token allows the given ability.
|
|
518
|
+
*
|
|
519
|
+
* @param ability - The ability to check
|
|
520
|
+
*
|
|
521
|
+
* @example
|
|
522
|
+
* if (token.allows('read:posts')) {
|
|
523
|
+
* console.log('User can read posts')
|
|
524
|
+
* }
|
|
444
525
|
*/
|
|
445
526
|
allows(ability) {
|
|
446
527
|
return this.abilities.includes(ability) || this.abilities.includes("*");
|
|
447
528
|
}
|
|
448
529
|
/**
|
|
449
530
|
* Check if the token denies the ability.
|
|
531
|
+
*
|
|
532
|
+
* @param ability - The ability to check
|
|
533
|
+
*
|
|
534
|
+
* @example
|
|
535
|
+
* if (token.denies('delete:posts')) {
|
|
536
|
+
* console.log('User cannot delete posts')
|
|
537
|
+
* }
|
|
450
538
|
*/
|
|
451
539
|
denies(ability) {
|
|
452
540
|
return !this.abilities.includes(ability) && !this.abilities.includes("*");
|
|
453
541
|
}
|
|
454
542
|
/**
|
|
455
543
|
* Authorize ability access using the current access token
|
|
544
|
+
*
|
|
545
|
+
* @param ability - The ability to authorize
|
|
546
|
+
*
|
|
547
|
+
* @throws {E_UNAUTHORIZED_ACCESS} When the token denies the ability
|
|
548
|
+
*
|
|
549
|
+
* @example
|
|
550
|
+
* token.authorize('write:posts') // Throws if not allowed
|
|
551
|
+
* console.log('Authorization successful')
|
|
456
552
|
*/
|
|
457
553
|
authorize(ability) {
|
|
458
554
|
if (this.denies(ability)) {
|
|
@@ -465,6 +561,13 @@ var AccessToken = class {
|
|
|
465
561
|
* date.
|
|
466
562
|
*
|
|
467
563
|
* Tokens with no expiry never expire
|
|
564
|
+
*
|
|
565
|
+
* @example
|
|
566
|
+
* if (token.isExpired()) {
|
|
567
|
+
* console.log('Token has expired')
|
|
568
|
+
* } else {
|
|
569
|
+
* console.log('Token is still valid')
|
|
570
|
+
* }
|
|
468
571
|
*/
|
|
469
572
|
isExpired() {
|
|
470
573
|
if (!this.expiresAt) {
|
|
@@ -474,11 +577,27 @@ var AccessToken = class {
|
|
|
474
577
|
}
|
|
475
578
|
/**
|
|
476
579
|
* Verifies the value of a token against the pre-defined hash
|
|
580
|
+
*
|
|
581
|
+
* @param secret - The secret to verify against the stored hash
|
|
582
|
+
*
|
|
583
|
+
* @example
|
|
584
|
+
* const isValid = token.verify(new Secret('user-provided-secret'))
|
|
585
|
+
* if (isValid) {
|
|
586
|
+
* console.log('Token is valid')
|
|
587
|
+
* }
|
|
477
588
|
*/
|
|
478
589
|
verify(secret) {
|
|
479
590
|
const newHash = createHash("sha256").update(secret.release()).digest("hex");
|
|
480
591
|
return safeEqual(this.hash, newHash);
|
|
481
592
|
}
|
|
593
|
+
/**
|
|
594
|
+
* Converts the token to a JSON representation suitable for API responses
|
|
595
|
+
*
|
|
596
|
+
* @example
|
|
597
|
+
* const tokenData = token.toJSON()
|
|
598
|
+
* console.log(tokenData.type) // 'bearer'
|
|
599
|
+
* console.log(tokenData.token) // 'oat_abc123.def456'
|
|
600
|
+
*/
|
|
482
601
|
toJSON() {
|
|
483
602
|
return {
|
|
484
603
|
type: "bearer",
|
|
@@ -536,6 +655,22 @@ var AccessTokensGuard = class {
|
|
|
536
655
|
* the request is not authenticated.
|
|
537
656
|
*/
|
|
538
657
|
user;
|
|
658
|
+
/**
|
|
659
|
+
* Creates a new AccessTokensGuard instance
|
|
660
|
+
*
|
|
661
|
+
* @param name - Unique name for the guard instance
|
|
662
|
+
* @param ctx - HTTP context for the current request
|
|
663
|
+
* @param emitter - Event emitter for guard events
|
|
664
|
+
* @param userProvider - User provider for token verification
|
|
665
|
+
*
|
|
666
|
+
* @example
|
|
667
|
+
* const guard = new AccessTokensGuard(
|
|
668
|
+
* 'api',
|
|
669
|
+
* ctx,
|
|
670
|
+
* emitter,
|
|
671
|
+
* new TokenUserProvider()
|
|
672
|
+
* )
|
|
673
|
+
*/
|
|
539
674
|
constructor(name, ctx, emitter, userProvider) {
|
|
540
675
|
this.#name = name;
|
|
541
676
|
this.#ctx = ctx;
|
|
@@ -571,6 +706,13 @@ var AccessTokensGuard = class {
|
|
|
571
706
|
/**
|
|
572
707
|
* Returns an instance of the authenticated user. Or throws
|
|
573
708
|
* an exception if the request is not authenticated.
|
|
709
|
+
*
|
|
710
|
+
* @throws {E_UNAUTHORIZED_ACCESS} When user is not authenticated
|
|
711
|
+
*
|
|
712
|
+
* @example
|
|
713
|
+
* const user = guard.getUserOrFail()
|
|
714
|
+
* console.log('User ID:', user.id)
|
|
715
|
+
* console.log('Current token:', user.currentAccessToken.name)
|
|
574
716
|
*/
|
|
575
717
|
getUserOrFail() {
|
|
576
718
|
if (!this.user) {
|
|
@@ -583,6 +725,17 @@ var AccessTokensGuard = class {
|
|
|
583
725
|
/**
|
|
584
726
|
* Authenticate the current HTTP request by verifying the bearer
|
|
585
727
|
* token or fails with an exception
|
|
728
|
+
*
|
|
729
|
+
* @throws {E_UNAUTHORIZED_ACCESS} When authentication fails
|
|
730
|
+
*
|
|
731
|
+
* @example
|
|
732
|
+
* try {
|
|
733
|
+
* const user = await guard.authenticate()
|
|
734
|
+
* console.log('Authenticated as:', user.email)
|
|
735
|
+
* console.log('Token abilities:', user.currentAccessToken.abilities)
|
|
736
|
+
* } catch (error) {
|
|
737
|
+
* console.log('Authentication failed')
|
|
738
|
+
* }
|
|
586
739
|
*/
|
|
587
740
|
async authenticate() {
|
|
588
741
|
if (this.authenticationAttempted) {
|
|
@@ -615,12 +768,27 @@ var AccessTokensGuard = class {
|
|
|
615
768
|
}
|
|
616
769
|
/**
|
|
617
770
|
* Create a token for a user (sign in)
|
|
771
|
+
*
|
|
772
|
+
* @param user - The user to create a token for
|
|
773
|
+
* @param abilities - Optional array of abilities the token should have
|
|
774
|
+
* @param options - Optional token configuration
|
|
775
|
+
*
|
|
776
|
+
* @example
|
|
777
|
+
* const token = await guard.createToken(user, ['read', 'write'], {
|
|
778
|
+
* name: 'Mobile App',
|
|
779
|
+
* expiresIn: '7d'
|
|
780
|
+
* })
|
|
781
|
+
* console.log('Token:', token.value.release())
|
|
618
782
|
*/
|
|
619
783
|
async createToken(user, abilities, options) {
|
|
620
784
|
return await this.#userProvider.createToken(user, abilities, options);
|
|
621
785
|
}
|
|
622
786
|
/**
|
|
623
787
|
* Invalidates the currently authenticated token (sign out)
|
|
788
|
+
*
|
|
789
|
+
* @example
|
|
790
|
+
* await guard.invalidateToken()
|
|
791
|
+
* console.log('Token invalidated successfully')
|
|
624
792
|
*/
|
|
625
793
|
async invalidateToken() {
|
|
626
794
|
const bearerToken = new Secret2(this.#getBearerToken());
|
|
@@ -629,6 +797,14 @@ var AccessTokensGuard = class {
|
|
|
629
797
|
/**
|
|
630
798
|
* Returns the Authorization header clients can use to authenticate
|
|
631
799
|
* the request.
|
|
800
|
+
*
|
|
801
|
+
* @param user - The user to authenticate as
|
|
802
|
+
* @param abilities - Optional array of abilities
|
|
803
|
+
* @param options - Optional token configuration
|
|
804
|
+
*
|
|
805
|
+
* @example
|
|
806
|
+
* const clientAuth = await guard.authenticateAsClient(user, ['read'])
|
|
807
|
+
* // Use clientAuth.headers.authorization in API tests
|
|
632
808
|
*/
|
|
633
809
|
async authenticateAsClient(user, abilities, options) {
|
|
634
810
|
const token = await this.#userProvider.createToken(user, abilities, options);
|
|
@@ -640,8 +816,15 @@ var AccessTokensGuard = class {
|
|
|
640
816
|
}
|
|
641
817
|
/**
|
|
642
818
|
* Silently check if the user is authenticated or not. The
|
|
643
|
-
* method is same the "authenticate" method but does not
|
|
819
|
+
* method is same as the "authenticate" method but does not
|
|
644
820
|
* throw any exceptions.
|
|
821
|
+
*
|
|
822
|
+
* @example
|
|
823
|
+
* const isAuthenticated = await guard.check()
|
|
824
|
+
* if (isAuthenticated) {
|
|
825
|
+
* const user = guard.user
|
|
826
|
+
* console.log('User is authenticated:', user.email)
|
|
827
|
+
* }
|
|
645
828
|
*/
|
|
646
829
|
async check() {
|
|
647
830
|
try {
|
|
@@ -660,6 +843,20 @@ var AccessTokensGuard = class {
|
|
|
660
843
|
import { inspect } from "util";
|
|
661
844
|
import { RuntimeException as RuntimeException2 } from "@adonisjs/core/exceptions";
|
|
662
845
|
var DbAccessTokensProvider = class _DbAccessTokensProvider {
|
|
846
|
+
/**
|
|
847
|
+
* Creates a new DbAccessTokensProvider instance
|
|
848
|
+
*
|
|
849
|
+
* @param options - Configuration options for the provider
|
|
850
|
+
*
|
|
851
|
+
* @example
|
|
852
|
+
* const provider = new DbAccessTokensProvider({
|
|
853
|
+
* tokenableModel: () => import('#models/user'),
|
|
854
|
+
* table: 'auth_access_tokens',
|
|
855
|
+
* tokenSecretLength: 40,
|
|
856
|
+
* type: 'auth_token',
|
|
857
|
+
* prefix: 'oat_'
|
|
858
|
+
* })
|
|
859
|
+
*/
|
|
663
860
|
constructor(options) {
|
|
664
861
|
this.options = options;
|
|
665
862
|
this.table = options.table || "auth_access_tokens";
|
|
@@ -669,6 +866,15 @@ var DbAccessTokensProvider = class _DbAccessTokensProvider {
|
|
|
669
866
|
}
|
|
670
867
|
/**
|
|
671
868
|
* Create tokens provider instance for a given Lucid model
|
|
869
|
+
*
|
|
870
|
+
* @param model - The tokenable model factory function
|
|
871
|
+
* @param options - Optional configuration options
|
|
872
|
+
*
|
|
873
|
+
* @example
|
|
874
|
+
* const provider = DbAccessTokensProvider.forModel(
|
|
875
|
+
* () => import('#models/user'),
|
|
876
|
+
* { prefix: 'api_', type: 'api_token' }
|
|
877
|
+
* )
|
|
672
878
|
*/
|
|
673
879
|
static forModel(model, options) {
|
|
674
880
|
return new _DbAccessTokensProvider({ tokenableModel: model, ...options || {} });
|
|
@@ -719,7 +925,18 @@ var DbAccessTokensProvider = class _DbAccessTokensProvider {
|
|
|
719
925
|
}
|
|
720
926
|
}
|
|
721
927
|
/**
|
|
722
|
-
* Maps a database row to an
|
|
928
|
+
* Maps a database row to an AccessToken instance
|
|
929
|
+
*
|
|
930
|
+
* @param dbRow - The database row containing token data
|
|
931
|
+
*
|
|
932
|
+
* @example
|
|
933
|
+
* const token = provider.dbRowToAccessToken({
|
|
934
|
+
* id: 1,
|
|
935
|
+
* tokenable_id: 123,
|
|
936
|
+
* type: 'auth_token',
|
|
937
|
+
* hash: 'sha256hash',
|
|
938
|
+
* // ... other columns
|
|
939
|
+
* })
|
|
723
940
|
*/
|
|
724
941
|
dbRowToAccessToken(dbRow) {
|
|
725
942
|
return new AccessToken({
|
|
@@ -737,6 +954,10 @@ var DbAccessTokensProvider = class _DbAccessTokensProvider {
|
|
|
737
954
|
}
|
|
738
955
|
/**
|
|
739
956
|
* Returns a query client instance from the parent model
|
|
957
|
+
*
|
|
958
|
+
* @example
|
|
959
|
+
* const db = await provider.getDb()
|
|
960
|
+
* const tokens = await db.from('auth_access_tokens').select('*')
|
|
740
961
|
*/
|
|
741
962
|
async getDb() {
|
|
742
963
|
const model = this.options.tokenableModel;
|
|
@@ -744,6 +965,17 @@ var DbAccessTokensProvider = class _DbAccessTokensProvider {
|
|
|
744
965
|
}
|
|
745
966
|
/**
|
|
746
967
|
* Create a token for a user
|
|
968
|
+
*
|
|
969
|
+
* @param user - The user instance to create a token for
|
|
970
|
+
* @param abilities - Array of abilities the token should have
|
|
971
|
+
* @param options - Optional token configuration
|
|
972
|
+
*
|
|
973
|
+
* @example
|
|
974
|
+
* const token = await provider.create(user, ['read', 'write'], {
|
|
975
|
+
* name: 'Mobile App Token',
|
|
976
|
+
* expiresIn: '7d'
|
|
977
|
+
* })
|
|
978
|
+
* console.log('Token:', token.value.release())
|
|
747
979
|
*/
|
|
748
980
|
async create(user, abilities = ["*"], options) {
|
|
749
981
|
this.#ensureIsPersisted(user);
|
|
@@ -788,6 +1020,15 @@ var DbAccessTokensProvider = class _DbAccessTokensProvider {
|
|
|
788
1020
|
}
|
|
789
1021
|
/**
|
|
790
1022
|
* Find a token for a user by the token id
|
|
1023
|
+
*
|
|
1024
|
+
* @param user - The user instance that owns the token
|
|
1025
|
+
* @param identifier - The token identifier to search for
|
|
1026
|
+
*
|
|
1027
|
+
* @example
|
|
1028
|
+
* const token = await provider.find(user, 123)
|
|
1029
|
+
* if (token) {
|
|
1030
|
+
* console.log('Found token:', token.name)
|
|
1031
|
+
* }
|
|
791
1032
|
*/
|
|
792
1033
|
async find(user, identifier) {
|
|
793
1034
|
this.#ensureIsPersisted(user);
|
|
@@ -800,6 +1041,13 @@ var DbAccessTokensProvider = class _DbAccessTokensProvider {
|
|
|
800
1041
|
}
|
|
801
1042
|
/**
|
|
802
1043
|
* Delete a token by its id
|
|
1044
|
+
*
|
|
1045
|
+
* @param user - The user instance that owns the token
|
|
1046
|
+
* @param identifier - The token identifier to delete
|
|
1047
|
+
*
|
|
1048
|
+
* @example
|
|
1049
|
+
* const deletedCount = await provider.delete(user, 123)
|
|
1050
|
+
* console.log('Deleted tokens:', deletedCount)
|
|
803
1051
|
*/
|
|
804
1052
|
async delete(user, identifier) {
|
|
805
1053
|
this.#ensureIsPersisted(user);
|
|
@@ -808,7 +1056,14 @@ var DbAccessTokensProvider = class _DbAccessTokensProvider {
|
|
|
808
1056
|
return affectedRows;
|
|
809
1057
|
}
|
|
810
1058
|
/**
|
|
811
|
-
* Returns all the tokens a given user
|
|
1059
|
+
* Returns all the tokens for a given user
|
|
1060
|
+
*
|
|
1061
|
+
* @param user - The user instance to get tokens for
|
|
1062
|
+
*
|
|
1063
|
+
* @example
|
|
1064
|
+
* const tokens = await provider.all(user)
|
|
1065
|
+
* console.log('User has', tokens.length, 'tokens')
|
|
1066
|
+
* tokens.forEach(token => console.log(token.name))
|
|
812
1067
|
*/
|
|
813
1068
|
async all(user) {
|
|
814
1069
|
this.#ensureIsPersisted(user);
|
|
@@ -840,6 +1095,14 @@ var DbAccessTokensProvider = class _DbAccessTokensProvider {
|
|
|
840
1095
|
*
|
|
841
1096
|
* Returns null when unable to verify the token or find it
|
|
842
1097
|
* inside the storage
|
|
1098
|
+
*
|
|
1099
|
+
* @param tokenValue - The token value to verify
|
|
1100
|
+
*
|
|
1101
|
+
* @example
|
|
1102
|
+
* const token = await provider.verify(new Secret('oat_abc123.def456'))
|
|
1103
|
+
* if (token && !token.isExpired()) {
|
|
1104
|
+
* console.log('Valid token for user:', token.tokenableId)
|
|
1105
|
+
* }
|
|
843
1106
|
*/
|
|
844
1107
|
async verify(tokenValue) {
|
|
845
1108
|
const decodedToken = AccessToken.decode(this.prefix, tokenValue.release());
|
|
@@ -861,6 +1124,14 @@ var DbAccessTokensProvider = class _DbAccessTokensProvider {
|
|
|
861
1124
|
}
|
|
862
1125
|
/**
|
|
863
1126
|
* Invalidates a token identified by its publicly shared token
|
|
1127
|
+
*
|
|
1128
|
+
* @param tokenValue - The token value to invalidate
|
|
1129
|
+
*
|
|
1130
|
+
* @example
|
|
1131
|
+
* const wasInvalidated = await provider.invalidate(new Secret('oat_abc123.def456'))
|
|
1132
|
+
* if (wasInvalidated) {
|
|
1133
|
+
* console.log('Token successfully invalidated')
|
|
1134
|
+
* }
|
|
864
1135
|
*/
|
|
865
1136
|
async invalidate(tokenValue) {
|
|
866
1137
|
const decodedToken = AccessToken.decode(this.prefix, tokenValue.release());
|
|
@@ -876,6 +1147,17 @@ var DbAccessTokensProvider = class _DbAccessTokensProvider {
|
|
|
876
1147
|
// modules/access_tokens_guard/user_providers/lucid.ts
|
|
877
1148
|
import { RuntimeException as RuntimeException3 } from "@adonisjs/core/exceptions";
|
|
878
1149
|
var AccessTokensLucidUserProvider = class {
|
|
1150
|
+
/**
|
|
1151
|
+
* Creates a new AccessTokensLucidUserProvider instance
|
|
1152
|
+
*
|
|
1153
|
+
* @param options - Configuration options for the user provider
|
|
1154
|
+
*
|
|
1155
|
+
* @example
|
|
1156
|
+
* const provider = new AccessTokensLucidUserProvider({
|
|
1157
|
+
* model: () => import('#models/user'),
|
|
1158
|
+
* tokens: 'accessTokens'
|
|
1159
|
+
* })
|
|
1160
|
+
*/
|
|
879
1161
|
constructor(options) {
|
|
880
1162
|
this.options = options;
|
|
881
1163
|
}
|
|
@@ -886,6 +1168,10 @@ var AccessTokensLucidUserProvider = class {
|
|
|
886
1168
|
/**
|
|
887
1169
|
* Imports the model from the provider, returns and caches it
|
|
888
1170
|
* for further operations.
|
|
1171
|
+
*
|
|
1172
|
+
* @example
|
|
1173
|
+
* const UserModel = await provider.getModel()
|
|
1174
|
+
* const user = await UserModel.find(1)
|
|
889
1175
|
*/
|
|
890
1176
|
async getModel() {
|
|
891
1177
|
if (this.model && !("hot" in import.meta)) {
|
|
@@ -897,6 +1183,10 @@ var AccessTokensLucidUserProvider = class {
|
|
|
897
1183
|
}
|
|
898
1184
|
/**
|
|
899
1185
|
* Returns the tokens provider associated with the user model
|
|
1186
|
+
*
|
|
1187
|
+
* @example
|
|
1188
|
+
* const tokensProvider = await provider.getTokensProvider()
|
|
1189
|
+
* const token = await tokensProvider.create(user, ['read'])
|
|
900
1190
|
*/
|
|
901
1191
|
async getTokensProvider() {
|
|
902
1192
|
const model = await this.getModel();
|
|
@@ -909,6 +1199,13 @@ var AccessTokensLucidUserProvider = class {
|
|
|
909
1199
|
}
|
|
910
1200
|
/**
|
|
911
1201
|
* Creates an adapter user for the guard
|
|
1202
|
+
*
|
|
1203
|
+
* @param user - The user model instance
|
|
1204
|
+
*
|
|
1205
|
+
* @example
|
|
1206
|
+
* const guardUser = await provider.createUserForGuard(user)
|
|
1207
|
+
* console.log('User ID:', guardUser.getId())
|
|
1208
|
+
* console.log('Original user:', guardUser.getOriginal())
|
|
912
1209
|
*/
|
|
913
1210
|
async createUserForGuard(user) {
|
|
914
1211
|
const model = await this.getModel();
|
|
@@ -933,6 +1230,17 @@ var AccessTokensLucidUserProvider = class {
|
|
|
933
1230
|
}
|
|
934
1231
|
/**
|
|
935
1232
|
* Create a token for a given user
|
|
1233
|
+
*
|
|
1234
|
+
* @param user - The user to create a token for
|
|
1235
|
+
* @param abilities - Optional array of abilities the token should have
|
|
1236
|
+
* @param options - Optional token configuration
|
|
1237
|
+
*
|
|
1238
|
+
* @example
|
|
1239
|
+
* const token = await provider.createToken(user, ['read', 'write'], {
|
|
1240
|
+
* name: 'API Token',
|
|
1241
|
+
* expiresIn: '30d'
|
|
1242
|
+
* })
|
|
1243
|
+
* console.log('Created token:', token.value.release())
|
|
936
1244
|
*/
|
|
937
1245
|
async createToken(user, abilities, options) {
|
|
938
1246
|
const tokensProvider = await this.getTokensProvider();
|
|
@@ -940,6 +1248,14 @@ var AccessTokensLucidUserProvider = class {
|
|
|
940
1248
|
}
|
|
941
1249
|
/**
|
|
942
1250
|
* Invalidates a token identified by its publicly shared token
|
|
1251
|
+
*
|
|
1252
|
+
* @param tokenValue - The token value to invalidate
|
|
1253
|
+
*
|
|
1254
|
+
* @example
|
|
1255
|
+
* const wasInvalidated = await provider.invalidateToken(
|
|
1256
|
+
* new Secret('oat_abc123.def456')
|
|
1257
|
+
* )
|
|
1258
|
+
* console.log('Token invalidated:', wasInvalidated)
|
|
943
1259
|
*/
|
|
944
1260
|
async invalidateToken(tokenValue) {
|
|
945
1261
|
const tokensProvider = await this.getTokensProvider();
|
|
@@ -947,6 +1263,15 @@ var AccessTokensLucidUserProvider = class {
|
|
|
947
1263
|
}
|
|
948
1264
|
/**
|
|
949
1265
|
* Finds a user by the user id
|
|
1266
|
+
*
|
|
1267
|
+
* @param identifier - The user identifier to search for
|
|
1268
|
+
*
|
|
1269
|
+
* @example
|
|
1270
|
+
* const guardUser = await provider.findById(123)
|
|
1271
|
+
* if (guardUser) {
|
|
1272
|
+
* const originalUser = guardUser.getOriginal()
|
|
1273
|
+
* console.log('Found user:', originalUser.email)
|
|
1274
|
+
* }
|
|
950
1275
|
*/
|
|
951
1276
|
async findById(identifier) {
|
|
952
1277
|
const model = await this.getModel();
|
|
@@ -959,6 +1284,16 @@ var AccessTokensLucidUserProvider = class {
|
|
|
959
1284
|
/**
|
|
960
1285
|
* Verifies a publicly shared access token and returns an
|
|
961
1286
|
* access token for it.
|
|
1287
|
+
*
|
|
1288
|
+
* @param tokenValue - The token value to verify
|
|
1289
|
+
*
|
|
1290
|
+
* @example
|
|
1291
|
+
* const token = await provider.verifyToken(
|
|
1292
|
+
* new Secret('oat_abc123.def456')
|
|
1293
|
+
* )
|
|
1294
|
+
* if (token && !token.isExpired()) {
|
|
1295
|
+
* console.log('Valid token with abilities:', token.abilities)
|
|
1296
|
+
* }
|
|
962
1297
|
*/
|
|
963
1298
|
async verifyToken(tokenValue) {
|
|
964
1299
|
const tokensProvider = await this.getTokensProvider();
|