@@ -119,7 +119,7 @@ const getAccessToken = async (request: unknown, opts: object = {}) => {
119
119
return signJwt ( payload , enginePrivateJwk , { alg : SigningAlg . EIP191 } , signer )
120
120
}
121
121
122
- describe ( ' AuthorizationGuard' , ( ) => {
122
+ describe ( AuthorizationGuard . name , ( ) => {
123
123
let mockClientService = mock < ClientService > ( )
124
124
let mockConfigService = mock < ConfigService < Config > > ( )
125
125
@@ -134,11 +134,6 @@ describe('AuthorizationGuard', () => {
134
134
mockReflector = mock < Reflector > ( )
135
135
} )
136
136
137
- it ( 'should be defined' , ( ) => {
138
- const guard = new AuthorizationGuard ( mockClientService , mockConfigService , mockReflector , mockLogger )
139
- expect ( guard ) . toBeDefined ( )
140
- } )
141
-
142
137
describe ( 'canActivate' , ( ) => {
143
138
const mockExecutionContext = ( { request } : { request ?: unknown } = { } ) => {
144
139
const mockRequest = request || {
@@ -276,7 +271,7 @@ describe('AuthorizationGuard', () => {
276
271
await expect ( guard . canActivate ( context ) ) . resolves . toEqual ( true )
277
272
} )
278
273
279
- it ( 'should throw when token validation is enabled and missing accessToken' , async ( ) => {
274
+ it ( 'throws when token validation is enabled and missing accessToken' , async ( ) => {
280
275
expect . assertions ( 2 )
281
276
const client = getBaseClient ( )
282
277
mockClientService . findById . mockResolvedValue ( client )
@@ -331,7 +326,7 @@ describe('AuthorizationGuard', () => {
331
326
await expect ( guard . canActivate ( context ) ) . rejects . toThrow ( 'No engine key configured' )
332
327
} )
333
328
334
- it ( 'should throw when requieBoundTokens is true and token is not bound' , async ( ) => {
329
+ it ( 'throws when requieBoundTokens is true and token is not bound' , async ( ) => {
335
330
const userPrivateJwk = secp256k1PrivateKeyToJwk ( FIXTURE . UNSAFE_PRIVATE_KEY . Alice )
336
331
const client = getBaseClient ( )
337
332
mockClientService . findById . mockResolvedValue ( client )
@@ -417,5 +412,90 @@ describe('AuthorizationGuard', () => {
417
412
418
413
await expect ( guard . canActivate ( context ) ) . resolves . toEqual ( true )
419
414
} )
415
+
416
+ it ( 'passes when token contains required permissions' , async ( ) => {
417
+ const userPrivateJwk = secp256k1PrivateKeyToJwk ( FIXTURE . UNSAFE_PRIVATE_KEY . Alice )
418
+ const userJwk = secp256k1PublicKeyToJwk ( FIXTURE . VIEM_ACCOUNT . Alice . publicKey )
419
+ const client = getBaseClient ( )
420
+ mockClientService . findById . mockResolvedValue ( client )
421
+
422
+ // Mock the reflector to return permissions
423
+ mockReflector . get . mockReturnValue ( [ 'WALLET_READ' ] )
424
+
425
+ const guard = new AuthorizationGuard ( mockClientService , mockConfigService , mockReflector , mockLogger )
426
+ const payload = {
427
+ value : 'test-value'
428
+ }
429
+
430
+ const accessToken = await getAccessToken ( payload , {
431
+ sub : 'user-1' ,
432
+ cnf : userJwk ,
433
+ access : [
434
+ {
435
+ resource : 'vault' ,
436
+ permissions : [ 'WALLET_READ' ]
437
+ }
438
+ ]
439
+ } )
440
+ const jwsd = await getJwsd ( {
441
+ userPrivateJwk,
442
+ requestUrl : '/test' ,
443
+ payload,
444
+ accessToken
445
+ } )
446
+
447
+ const mockRequest = {
448
+ headers : { 'x-client-id' : 'test-client' , authorization : `GNAP ${ accessToken } ` , 'detached-jws' : jwsd } ,
449
+ body : payload ,
450
+ url : '/test' ,
451
+ method : 'POST'
452
+ }
453
+ const context = mockExecutionContext ( { request : mockRequest } )
454
+
455
+ await expect ( guard . canActivate ( context ) ) . resolves . toEqual ( true )
456
+ } )
457
+
458
+ it ( 'throws when token does not contain required permissions' , async ( ) => {
459
+ const userPrivateJwk = secp256k1PrivateKeyToJwk ( FIXTURE . UNSAFE_PRIVATE_KEY . Alice )
460
+ const userJwk = secp256k1PublicKeyToJwk ( FIXTURE . VIEM_ACCOUNT . Alice . publicKey )
461
+ const client = getBaseClient ( )
462
+ mockClientService . findById . mockResolvedValue ( client )
463
+
464
+ // Mock the reflector to return permissions
465
+ mockReflector . get . mockReturnValue ( [ 'WALLET_READ' ] )
466
+
467
+ const guard = new AuthorizationGuard ( mockClientService , mockConfigService , mockReflector , mockLogger )
468
+ const payload = {
469
+ value : 'test-value'
470
+ }
471
+
472
+ // Token with different permission than required
473
+ const accessToken = await getAccessToken ( payload , {
474
+ sub : 'user-1' ,
475
+ cnf : userJwk ,
476
+ access : [
477
+ {
478
+ resource : 'vault' ,
479
+ permissions : [ 'WALLET_WRITE' ]
480
+ }
481
+ ]
482
+ } )
483
+ const jwsd = await getJwsd ( {
484
+ userPrivateJwk,
485
+ requestUrl : '/test' ,
486
+ payload,
487
+ accessToken
488
+ } )
489
+
490
+ const mockRequest = {
491
+ headers : { 'x-client-id' : 'test-client' , authorization : `GNAP ${ accessToken } ` , 'detached-jws' : jwsd } ,
492
+ body : payload ,
493
+ url : '/test' ,
494
+ method : 'POST'
495
+ }
496
+ const context = mockExecutionContext ( { request : mockRequest } )
497
+
498
+ await expect ( guard . canActivate ( context ) ) . rejects . toThrow ( 'Invalid permissions' )
499
+ } )
420
500
} )
421
501
} )
0 commit comments