1616 * under the License.
1717 */
1818
19+ import TokenConstants from './constants/TokenConstants' ;
1920import { AsgardeoAuthException } from './errors/exception' ;
2021import { Crypto , JWKInterface } from './models/crypto' ;
21- import { IdToken } from './models/token' ;
22- import TokenConstants from './constants/TokenConstants' ;
2322
2423export class IsomorphicCrypto < T = any > {
25- private _cryptoUtils : Crypto < T > ;
24+ private cryptoUtils : Crypto < T > ;
2625
2726 public constructor ( cryptoUtils : Crypto < T > ) {
28- this . _cryptoUtils = cryptoUtils ;
27+ this . cryptoUtils = cryptoUtils ;
2928 }
3029
3130 /**
@@ -34,7 +33,7 @@ export class IsomorphicCrypto<T = any> {
3433 * @returns code verifier.
3534 */
3635 public getCodeVerifier ( ) : string {
37- return this . _cryptoUtils . base64URLEncode ( this . _cryptoUtils . generateRandomBytes ( 32 ) ) ;
36+ return this . cryptoUtils . base64URLEncode ( this . cryptoUtils . generateRandomBytes ( 32 ) ) ;
3837 }
3938
4039 /**
@@ -45,7 +44,7 @@ export class IsomorphicCrypto<T = any> {
4544 * @returns - code challenge.
4645 */
4746 public getCodeChallenge ( verifier : string ) : string {
48- return this . _cryptoUtils . base64URLEncode ( this . _cryptoUtils . hashSha256 ( verifier ) ) ;
47+ return this . cryptoUtils . base64URLEncode ( this . cryptoUtils . hashSha256 ( verifier ) ) ;
4948 }
5049
5150 /**
@@ -60,21 +59,22 @@ export class IsomorphicCrypto<T = any> {
6059 */
6160 /* eslint-disable @typescript-eslint/no-explicit-any */
6261 public getJWKForTheIdToken ( jwtHeader : string , keys : JWKInterface [ ] ) : JWKInterface {
63- const headerJSON : Record < string , string > = JSON . parse ( this . _cryptoUtils . base64URLDecode ( jwtHeader ) ) ;
62+ const headerJSON : Record < string , string > = JSON . parse ( this . cryptoUtils . base64URLDecode ( jwtHeader ) ) ;
63+
64+ const matchingKey : JWKInterface | undefined = keys . find (
65+ ( key : JWKInterface ) : boolean => headerJSON [ 'kid' ] === key . kid ,
66+ ) ;
6467
65- for ( const key of keys ) {
66- if ( headerJSON [ 'kid' ] === key . kid ) {
67- return key ;
68- }
68+ if ( matchingKey ) {
69+ return matchingKey ;
6970 }
7071
7172 throw new AsgardeoAuthException (
7273 'JS-CRYPTO_UTIL-GJFTIT-IV01' ,
7374 'kid not found.' ,
74- "Failed to find the 'kid' specified in the id_token. 'kid' found in the header : " +
75- headerJSON [ 'kid' ] +
76- ', Expected values: ' +
77- keys . map ( ( key : JWKInterface ) => key . kid ) . join ( ', ' ) ,
75+ `Failed to find the 'kid' specified in the id_token. 'kid' found in the header : ${
76+ headerJSON [ 'kid' ]
77+ } , Expected values: ${ keys . map ( ( key : JWKInterface ) => key . kid ) . join ( ', ' ) } `,
7878 ) ;
7979 }
8080
@@ -101,7 +101,7 @@ export class IsomorphicCrypto<T = any> {
101101 clockTolerance : number | undefined ,
102102 validateJwtIssuer : boolean | undefined ,
103103 ) : Promise < boolean > {
104- return this . _cryptoUtils
104+ return this . cryptoUtils
105105 . verifyJwt (
106106 idToken ,
107107 jwk ,
@@ -125,15 +125,13 @@ export class IsomorphicCrypto<T = any> {
125125 ) ,
126126 ) ;
127127 } )
128- . catch ( ( error : AsgardeoAuthException ) => {
129- return Promise . reject ( error ) ;
130- } ) ;
128+ . catch ( ( error : AsgardeoAuthException ) => Promise . reject ( error ) ) ;
131129 }
132130
133- public decodeJwtToken < T = Record < string , unknown > > ( token : string ) : T {
131+ public decodeJwtToken < R = Record < string , unknown > > ( token : string ) : R {
134132 try {
135- const utf8String : string = this . _cryptoUtils . base64URLDecode ( token ?. split ( '.' ) [ 1 ] ) ;
136- const payload : T = JSON . parse ( utf8String ) ;
133+ const utf8String : string = this . cryptoUtils . base64URLDecode ( token ?. split ( '.' ) [ 1 ] ) ;
134+ const payload : R = JSON . parse ( utf8String ) ;
137135
138136 return payload ;
139137 } catch ( error : any ) {
0 commit comments