@@ -55,7 +55,7 @@ export const cloudflareAccess = (accessTeamName: string): MiddlewareHandler => {
5555 let token
5656 try {
5757 token = decodeJwt ( encodedToken )
58- } catch ( err ) {
58+ } catch {
5959 return c . text ( 'Authentication error: Unable to decode Bearer token' , 401 )
6060 }
6161
@@ -108,7 +108,7 @@ async function getPublicKeys(accessTeamName: string) {
108108 } )
109109 }
110110
111- const data : any = await result . json ( )
111+ const data = await result . json < { keys : JsonWebKeyWithKid [ ] } > ( )
112112
113113 // Because we keep CryptoKey's in memory between requests, we need to make sure they are refreshed once in a while
114114 const cacheExpiration = Math . floor ( Date . now ( ) / 1000 ) + 3600 // 1h
@@ -142,20 +142,16 @@ function getJwt(c: Context) {
142142}
143143
144144function decodeJwt ( token : string ) : DecodedToken {
145- const parts = token . split ( '.' )
146- if ( parts . length !== 3 ) {
145+ const [ header , payload , signature ] = token . split ( '.' )
146+ if ( ! header || ! payload || ! signature ) {
147147 throw new Error ( 'Invalid token' )
148148 }
149149
150- const header = JSON . parse ( atob ( parts [ 0 ] as string ) )
151- const payload = JSON . parse ( atob ( parts [ 1 ] as string ) )
152- const signature = atob ( ( parts [ 2 ] as string ) . replace ( / _ / g, '/' ) . replace ( / - / g, '+' ) )
153-
154150 return {
155- header : header ,
156- payload : payload ,
157- signature : signature ,
158- raw : { header : parts [ 0 ] , payload : parts [ 1 ] , signature : parts [ 2 ] } ,
151+ header : JSON . parse ( atob ( header ) ) as object ,
152+ payload : JSON . parse ( atob ( payload ) ) as CloudflareAccessPayload ,
153+ signature : atob ( signature . replace ( / _ / g , '/' ) . replace ( / - / g , '+' ) ) ,
154+ raw : { header, payload, signature } ,
159155 }
160156}
161157
@@ -178,8 +174,8 @@ async function isValidJwtSignature(token: DecodedToken, keys: Record<string, Cry
178174
179175async function validateSingleKey (
180176 key : CryptoKey ,
181- signature : Uint8Array ,
182- data : Uint8Array
177+ signature : BufferSource ,
178+ data : BufferSource
183179) : Promise < boolean > {
184180 return crypto . subtle . verify ( 'RSASSA-PKCS1-v1_5' , key , signature , data )
185181}
0 commit comments