@@ -36,6 +36,7 @@ import { AsymmetricKeyFormatValues } from "@/features/common/values/asymmetric-k
36
36
import { useDebuggerStore } from "@/features/debugger/services/debugger.store" ;
37
37
import { SigningAlgCategoryValues } from "@/features/common/values/signing-alg-category.values" ;
38
38
import { EncoderInputsModel } from "@/features/debugger/models/encoder-inputs.model" ;
39
+ import { EncoderResult } from "@/features/common/models/encoder-result.model" ;
39
40
40
41
type EncodingHeaderErrors = {
41
42
headerErrors : string [ ] | null ;
@@ -183,7 +184,8 @@ class _TokenEncoderService {
183
184
}
184
185
185
186
if ( encodeJWTResult . isOk ( ) ) {
186
- stateUpdate . jwt = encodeJWTResult . value . trim ( ) ;
187
+ stateUpdate . jwt = encodeJWTResult . value . jwt . trim ( ) ;
188
+ stateUpdate . signingErrors = encodeJWTResult . value . signingErrors ;
187
189
}
188
190
189
191
return {
@@ -214,7 +216,7 @@ class _TokenEncoderService {
214
216
}
215
217
216
218
if ( encodeJWTResult . isOk ( ) ) {
217
- stateUpdate . jwt = encodeJWTResult . value . trim ( ) ;
219
+ stateUpdate . jwt = encodeJWTResult . value . jwt . trim ( ) ;
218
220
219
221
useDebuggerStore . getState ( ) . setStash$ ( {
220
222
asymmetricPublicKey : digitallySignedToken . publicKey ,
@@ -379,7 +381,7 @@ class _TokenEncoderService {
379
381
}
380
382
381
383
if ( encodeJWTResult . isOk ( ) ) {
382
- stateUpdate . jwt = encodeJWTResult . value . trim ( ) ;
384
+ stateUpdate . jwt = encodeJWTResult . value . jwt . trim ( ) ;
383
385
}
384
386
385
387
return {
@@ -409,7 +411,7 @@ class _TokenEncoderService {
409
411
}
410
412
411
413
if ( encodeJWTResult . isOk ( ) ) {
412
- stateUpdate . jwt = encodeJWTResult . value . trim ( ) ;
414
+ stateUpdate . jwt = encodeJWTResult . value . jwt . trim ( ) ;
413
415
}
414
416
415
417
return {
@@ -484,48 +486,61 @@ class _TokenEncoderService {
484
486
payload : DecodedJwtPayloadModel ,
485
487
key : string ,
486
488
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
- }
489
+ ) : Promise < Result < EncoderResult , DebuggerErrorModel > > {
490
+ if ( ! isHmacAlg ( header . alg ) ) {
491
+ return err ( {
492
+ task : DebuggerTaskValues . ENCODE ,
493
+ input : DebuggerInputValues . HEADER ,
494
+ 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).` ,
495
+ } ) ;
496
+ }
496
497
497
- const getAlgSizeResult = getAlgSize ( header . alg ) ;
498
+ if ( ! key ) {
499
+ return err ( {
500
+ task : DebuggerTaskValues . ENCODE ,
501
+ input : DebuggerInputValues . KEY ,
502
+ message : "Secret must not be empty." ,
503
+ } ) ;
504
+ }
498
505
499
- if ( getAlgSizeResult . isErr ( ) ) {
500
- return err ( {
501
- task : DebuggerTaskValues . ENCODE ,
502
- input : DebuggerInputValues . KEY ,
503
- message : getAlgSizeResult . error ,
504
- } ) ;
505
- }
506
+ const getAlgSizeResult = getAlgSize ( header . alg ) ;
506
507
507
- const checkHmacSecretLengthResult = checkHmacSecretLength (
508
- key ,
509
- getAlgSizeResult . value . size ,
510
- encodingFormat ,
511
- ) ;
508
+ if ( getAlgSizeResult . isErr ( ) ) {
509
+ return err ( {
510
+ task : DebuggerTaskValues . ENCODE ,
511
+ input : DebuggerInputValues . KEY ,
512
+ message : getAlgSizeResult . error ,
513
+ } ) ;
514
+ }
512
515
513
- if ( checkHmacSecretLengthResult . isErr ( ) ) {
514
- return err ( checkHmacSecretLengthResult . error ) ;
515
- }
516
+ const checkHmacSecretLengthResult = checkHmacSecretLength (
517
+ key ,
518
+ getAlgSizeResult . value . size ,
519
+ encodingFormat ,
520
+ ) ;
516
521
517
- return await signWithSymmetricSecretKey (
518
- header as CompactJWSHeaderParameters ,
519
- payload ,
520
- key ,
521
- encodingFormat ,
522
- ) ;
522
+ const signingError = checkHmacSecretLengthResult . isErr ( )
523
+ ? [ checkHmacSecretLengthResult . error . message ]
524
+ : null ;
525
+
526
+ const signWithSymmetricSecretKeyResult = await signWithSymmetricSecretKey (
527
+ header as CompactJWSHeaderParameters ,
528
+ payload ,
529
+ key ,
530
+ encodingFormat ,
531
+ ) ;
532
+
533
+ if ( signWithSymmetricSecretKeyResult . isErr ( ) ) {
534
+ return err ( {
535
+ task : DebuggerTaskValues . ENCODE ,
536
+ input : DebuggerInputValues . KEY ,
537
+ message : signWithSymmetricSecretKeyResult . error . message ,
538
+ } ) ;
523
539
}
524
540
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).` ,
541
+ return ok < EncoderResult > ( {
542
+ jwt : signWithSymmetricSecretKeyResult . value ,
543
+ signingErrors : signingError ,
529
544
} ) ;
530
545
}
531
546
@@ -534,7 +549,7 @@ class _TokenEncoderService {
534
549
payload : DecodedJwtPayloadModel ,
535
550
key : string ,
536
551
keyFormat : AsymmetricKeyFormatValues ,
537
- ) : Promise < Result < string , DebuggerErrorModel > > {
552
+ ) : Promise < Result < EncoderResult , DebuggerErrorModel > > {
538
553
if ( isDigitalSignatureAlg ( header . alg ) ) {
539
554
if ( ! key ) {
540
555
return err ( {
@@ -544,12 +559,25 @@ class _TokenEncoderService {
544
559
} ) ;
545
560
}
546
561
547
- return await signWithAsymmetricPrivateKey (
562
+ const jwt = await signWithAsymmetricPrivateKey (
548
563
header as CompactJWSHeaderParameters ,
549
564
payload ,
550
565
key ,
551
566
keyFormat ,
552
567
) ;
568
+
569
+ if ( jwt . isErr ( ) ) {
570
+ return err ( {
571
+ task : DebuggerTaskValues . ENCODE ,
572
+ input : DebuggerInputValues . KEY ,
573
+ message : "Private key must not be empty." ,
574
+ } )
575
+ }
576
+
577
+ return ok ( {
578
+ jwt : jwt . value ,
579
+ signingErrors : null ,
580
+ } ) ;
553
581
}
554
582
555
583
return err ( {
@@ -684,9 +712,7 @@ class _TokenEncoderService {
684
712
symmetricSecretKeyEncoding : EncodingValues ;
685
713
} ) : Promise <
686
714
Result <
687
- {
688
- jwt : string ;
689
- } ,
715
+ EncoderResult ,
690
716
EncodingSymmetricSecretKeyErrors
691
717
>
692
718
> {
@@ -767,6 +793,7 @@ class _TokenEncoderService {
767
793
768
794
return ok ( {
769
795
jwt : encodeJwtResult . value . jwt . trim ( ) ,
796
+ signingErrors : encodeJwtResult . value . signingErrors ,
770
797
} ) ;
771
798
}
772
799
@@ -861,17 +888,15 @@ class _TokenEncoderService {
861
888
} ,
862
889
) : Promise <
863
890
Result <
864
- {
865
- jwt : string ;
866
- } ,
891
+ EncoderResult ,
867
892
EncodingJwtErrors
868
893
>
869
894
> {
870
895
const algType = params . algType ;
871
896
const header = params . header ;
872
897
const payload = params . payload ;
873
898
874
- let encodeJWTResult : Result < string , DebuggerErrorModel > | null = null ;
899
+ let encodeJWTResult : Result < EncoderResult , DebuggerErrorModel > | null = null ;
875
900
876
901
if ( algType === SigningAlgCategoryValues . ANY ) {
877
902
const symmetricSecretKey = params . symmetricSecretKey ;
@@ -998,8 +1023,9 @@ class _TokenEncoderService {
998
1023
}
999
1024
}
1000
1025
1001
- return ok ( {
1002
- jwt : encodeJWTResult . value ,
1026
+ return ok < EncoderResult > ( {
1027
+ jwt : encodeJWTResult . value . jwt ,
1028
+ signingErrors : encodeJWTResult . value . signingErrors ,
1003
1029
} ) ;
1004
1030
}
1005
1031
@@ -1235,6 +1261,7 @@ class _TokenEncoderService {
1235
1261
}
1236
1262
1237
1263
stateUpdate . jwt = processSymmetricSecretKeyResult . value . jwt . trim ( ) ;
1264
+ stateUpdate . signingErrors = processSymmetricSecretKeyResult . value . signingErrors ;
1238
1265
1239
1266
return stateUpdate ;
1240
1267
}
@@ -1269,6 +1296,7 @@ class _TokenEncoderService {
1269
1296
}
1270
1297
1271
1298
stateUpdate . jwt = processSymmetricSecretKeyResult . value . jwt . trim ( ) ;
1299
+ stateUpdate . signingErrors = processSymmetricSecretKeyResult . value . signingErrors ;
1272
1300
1273
1301
return stateUpdate ;
1274
1302
}
0 commit comments