@@ -68,6 +68,11 @@ type EncodingJwtErrors = {
68
68
encodingErrors : string [ ] | null ;
69
69
} ;
70
70
71
+ type EncodingResult = {
72
+ jwt : string ;
73
+ signingErrors : string [ ] | null ;
74
+ }
75
+
71
76
class _TokenEncoderService {
72
77
async selectEncodingExample (
73
78
algorithmPickerOptionValue : string ,
@@ -183,7 +188,8 @@ class _TokenEncoderService {
183
188
}
184
189
185
190
if ( encodeJWTResult . isOk ( ) ) {
186
- stateUpdate . jwt = encodeJWTResult . value . trim ( ) ;
191
+ stateUpdate . jwt = encodeJWTResult . value . jwt . trim ( ) ;
192
+ stateUpdate . signingErrors = encodeJWTResult . value . signingErrors ;
187
193
}
188
194
189
195
return {
@@ -214,7 +220,7 @@ class _TokenEncoderService {
214
220
}
215
221
216
222
if ( encodeJWTResult . isOk ( ) ) {
217
- stateUpdate . jwt = encodeJWTResult . value . trim ( ) ;
223
+ stateUpdate . jwt = encodeJWTResult . value . jwt . trim ( ) ;
218
224
219
225
useDebuggerStore . getState ( ) . setStash$ ( {
220
226
asymmetricPublicKey : digitallySignedToken . publicKey ,
@@ -379,7 +385,7 @@ class _TokenEncoderService {
379
385
}
380
386
381
387
if ( encodeJWTResult . isOk ( ) ) {
382
- stateUpdate . jwt = encodeJWTResult . value . trim ( ) ;
388
+ stateUpdate . jwt = encodeJWTResult . value . jwt . trim ( ) ;
383
389
}
384
390
385
391
return {
@@ -409,7 +415,7 @@ class _TokenEncoderService {
409
415
}
410
416
411
417
if ( encodeJWTResult . isOk ( ) ) {
412
- stateUpdate . jwt = encodeJWTResult . value . trim ( ) ;
418
+ stateUpdate . jwt = encodeJWTResult . value . jwt . trim ( ) ;
413
419
}
414
420
415
421
return {
@@ -484,48 +490,61 @@ class _TokenEncoderService {
484
490
payload : DecodedJwtPayloadModel ,
485
491
key : string ,
486
492
encodingFormat : EncodingValues ,
487
- ) : Promise < Result < string , DebuggerErrorModel > > {
488
- if ( isHmacAlg ( header . alg ) ) {
489
- if ( ! key ) {
490
- return err ( {
491
- task : DebuggerTaskValues . ENCODE ,
492
- input : DebuggerInputValues . KEY ,
493
- message : "Secret must not be empty." ,
494
- } ) ;
495
- }
493
+ ) : Promise < Result < EncodingResult , DebuggerErrorModel > > {
494
+ if ( ! isHmacAlg ( header . alg ) ) {
495
+ return err ( {
496
+ task : DebuggerTaskValues . ENCODE ,
497
+ input : DebuggerInputValues . HEADER ,
498
+ message : `Invalid MAC algorithm. Only use MAC "alg" parameter values in the header as defined by [RFC 7518 (JSON Web Algorithms)](https://datatracker.ietf.org/doc/html/rfc7518#section-3.1).` ,
499
+ } ) ;
500
+ }
496
501
497
- const getAlgSizeResult = getAlgSize ( header . alg ) ;
502
+ if ( ! key ) {
503
+ return err ( {
504
+ task : DebuggerTaskValues . ENCODE ,
505
+ input : DebuggerInputValues . KEY ,
506
+ message : "Secret must not be empty." ,
507
+ } ) ;
508
+ }
498
509
499
- if ( getAlgSizeResult . isErr ( ) ) {
500
- return err ( {
501
- task : DebuggerTaskValues . ENCODE ,
502
- input : DebuggerInputValues . KEY ,
503
- message : getAlgSizeResult . error ,
504
- } ) ;
505
- }
510
+ const getAlgSizeResult = getAlgSize ( header . alg ) ;
506
511
507
- const checkHmacSecretLengthResult = checkHmacSecretLength (
508
- key ,
509
- getAlgSizeResult . value . size ,
510
- encodingFormat ,
511
- ) ;
512
+ if ( getAlgSizeResult . isErr ( ) ) {
513
+ return err ( {
514
+ task : DebuggerTaskValues . ENCODE ,
515
+ input : DebuggerInputValues . KEY ,
516
+ message : getAlgSizeResult . error ,
517
+ } ) ;
518
+ }
512
519
513
- if ( checkHmacSecretLengthResult . isErr ( ) ) {
514
- return err ( checkHmacSecretLengthResult . error ) ;
515
- }
520
+ const checkHmacSecretLengthResult = checkHmacSecretLength (
521
+ key ,
522
+ getAlgSizeResult . value . size ,
523
+ encodingFormat ,
524
+ ) ;
516
525
517
- return await signWithSymmetricSecretKey (
518
- header as CompactJWSHeaderParameters ,
519
- payload ,
520
- key ,
521
- encodingFormat ,
522
- ) ;
526
+ const signingError = checkHmacSecretLengthResult . isErr ( )
527
+ ? [ checkHmacSecretLengthResult . error . message ]
528
+ : null ;
529
+
530
+ const signWithSymmetricSecretKeyResult = await signWithSymmetricSecretKey (
531
+ header as CompactJWSHeaderParameters ,
532
+ payload ,
533
+ key ,
534
+ encodingFormat ,
535
+ ) ;
536
+
537
+ if ( signWithSymmetricSecretKeyResult . isErr ( ) ) {
538
+ return err ( {
539
+ task : DebuggerTaskValues . ENCODE ,
540
+ input : DebuggerInputValues . KEY ,
541
+ message : signWithSymmetricSecretKeyResult . error . message ,
542
+ } ) ;
523
543
}
524
544
525
- return err ( {
526
- task : DebuggerTaskValues . ENCODE ,
527
- input : DebuggerInputValues . HEADER ,
528
- message : `Invalid MAC algorithm. Only use MAC "alg" parameter values in the header as defined by [RFC 7518 (JSON Web Algorithms)](https://datatracker.ietf.org/doc/html/rfc7518#section-3.1).` ,
545
+ return ok < EncodingResult > ( {
546
+ jwt : signWithSymmetricSecretKeyResult . value ,
547
+ signingErrors : signingError ,
529
548
} ) ;
530
549
}
531
550
@@ -534,7 +553,7 @@ class _TokenEncoderService {
534
553
payload : DecodedJwtPayloadModel ,
535
554
key : string ,
536
555
keyFormat : AsymmetricKeyFormatValues ,
537
- ) : Promise < Result < string , DebuggerErrorModel > > {
556
+ ) : Promise < Result < EncodingResult , DebuggerErrorModel > > {
538
557
if ( isDigitalSignatureAlg ( header . alg ) ) {
539
558
if ( ! key ) {
540
559
return err ( {
@@ -544,12 +563,25 @@ class _TokenEncoderService {
544
563
} ) ;
545
564
}
546
565
547
- return await signWithAsymmetricPrivateKey (
566
+ const jwt = await signWithAsymmetricPrivateKey (
548
567
header as CompactJWSHeaderParameters ,
549
568
payload ,
550
569
key ,
551
570
keyFormat ,
552
571
) ;
572
+
573
+ if ( jwt . isErr ( ) ) {
574
+ return err ( {
575
+ task : DebuggerTaskValues . ENCODE ,
576
+ input : DebuggerInputValues . KEY ,
577
+ message : "Private key must not be empty." ,
578
+ } )
579
+ }
580
+
581
+ return ok ( {
582
+ jwt : jwt . value ,
583
+ signingErrors : null ,
584
+ } ) ;
553
585
}
554
586
555
587
return err ( {
@@ -684,9 +716,7 @@ class _TokenEncoderService {
684
716
symmetricSecretKeyEncoding : EncodingValues ;
685
717
} ) : Promise <
686
718
Result <
687
- {
688
- jwt : string ;
689
- } ,
719
+ EncodingResult ,
690
720
EncodingSymmetricSecretKeyErrors
691
721
>
692
722
> {
@@ -767,6 +797,7 @@ class _TokenEncoderService {
767
797
768
798
return ok ( {
769
799
jwt : encodeJwtResult . value . jwt . trim ( ) ,
800
+ signingErrors : encodeJwtResult . value . signingErrors ,
770
801
} ) ;
771
802
}
772
803
@@ -861,17 +892,15 @@ class _TokenEncoderService {
861
892
} ,
862
893
) : Promise <
863
894
Result <
864
- {
865
- jwt : string ;
866
- } ,
895
+ EncodingResult ,
867
896
EncodingJwtErrors
868
897
>
869
898
> {
870
899
const algType = params . algType ;
871
900
const header = params . header ;
872
901
const payload = params . payload ;
873
902
874
- let encodeJWTResult : Result < string , DebuggerErrorModel > | null = null ;
903
+ let encodeJWTResult : Result < EncodingResult , DebuggerErrorModel > | null = null ;
875
904
876
905
if ( algType === SigningAlgCategoryValues . ANY ) {
877
906
const symmetricSecretKey = params . symmetricSecretKey ;
@@ -998,8 +1027,9 @@ class _TokenEncoderService {
998
1027
}
999
1028
}
1000
1029
1001
- return ok ( {
1002
- jwt : encodeJWTResult . value ,
1030
+ return ok < EncodingResult > ( {
1031
+ jwt : encodeJWTResult . value . jwt ,
1032
+ signingErrors : encodeJWTResult . value . signingErrors ,
1003
1033
} ) ;
1004
1034
}
1005
1035
@@ -1235,6 +1265,7 @@ class _TokenEncoderService {
1235
1265
}
1236
1266
1237
1267
stateUpdate . jwt = processSymmetricSecretKeyResult . value . jwt . trim ( ) ;
1268
+ stateUpdate . signingErrors = processSymmetricSecretKeyResult . value . signingErrors ;
1238
1269
1239
1270
return stateUpdate ;
1240
1271
}
@@ -1269,6 +1300,7 @@ class _TokenEncoderService {
1269
1300
}
1270
1301
1271
1302
stateUpdate . jwt = processSymmetricSecretKeyResult . value . jwt . trim ( ) ;
1303
+ stateUpdate . signingErrors = processSymmetricSecretKeyResult . value . signingErrors ;
1272
1304
1273
1305
return stateUpdate ;
1274
1306
}
0 commit comments