diff --git a/src/middleware/jwk/index.test.ts b/src/middleware/jwk/index.test.ts index 1d502eeef..deec08fd9 100644 --- a/src/middleware/jwk/index.test.ts +++ b/src/middleware/jwk/index.test.ts @@ -3,13 +3,14 @@ import { setupServer } from 'msw/node' import { setSignedCookie } from '../../helper/cookie' import { Hono } from '../../hono' import { HTTPException } from '../../http-exception' +import { encodeBase64Url } from '../../utils/encode' import { Jwt } from '../../utils/jwt' +import type { HonoJsonWebKey } from '../../utils/jwt/jws' +import { signing } from '../../utils/jwt/jws' +import type { JWTPayload } from '../../utils/jwt/types' +import { utf8Encoder } from '../../utils/jwt/utf8' import * as test_keys from './keys.test.json' import { jwk } from '.' -import { encodeBase64Url } from '../../utils/encode' -import { utf8Encoder } from '../../utils/jwt/utf8' -import { JWTPayload } from '../../utils/jwt/types' -import { HonoJsonWebKey, signing } from '../../utils/jwt/jws' const verify_keys = test_keys.public_keys @@ -142,17 +143,26 @@ describe('JWK', () => { }) it('Should not authorize a token with missing "kid" in header', async () => { - const encodeJwtPart = (part: unknown): string => encodeBase64Url(utf8Encoder.encode(JSON.stringify(part))).replace(/=/g, '') - const encodeSignaturePart = (buf: ArrayBufferLike): string => encodeBase64Url(buf).replace(/=/g, '') + const encodeJwtPart = (part: unknown): string => + encodeBase64Url(utf8Encoder.encode(JSON.stringify(part))).replace(/=/g, '') + const encodeSignaturePart = (buf: ArrayBufferLike): string => + encodeBase64Url(buf).replace(/=/g, '') const jwtSignWithoutKid = async (payload: JWTPayload, privateKey: HonoJsonWebKey) => { const encodedPayload = encodeJwtPart(payload) - let encodedHeader = encodeJwtPart({ alg: privateKey.alg, typ: 'JWT' }) + const encodedHeader = encodeJwtPart({ alg: privateKey.alg, typ: 'JWT' }) const partialToken = `${encodedHeader}.${encodedPayload}` - const signaturePart = await signing(privateKey, privateKey.alg as any, utf8Encoder.encode(partialToken)) + const signaturePart = await signing( + privateKey, + privateKey.alg as any, + utf8Encoder.encode(partialToken) + ) const signature = encodeSignaturePart(signaturePart) return `${partialToken}.${signature}` } - const credential = await jwtSignWithoutKid({ message: 'hello world' }, test_keys.private_keys[1]) + const credential = await jwtSignWithoutKid( + { message: 'hello world' }, + test_keys.private_keys[1] + ) const req = new Request('http://localhost/auth-with-keys/a') req.headers.set('Authorization', `Bearer ${credential}`) const res = await app.request(req)