Skip to content

Commit

Permalink
lint & format
Browse files Browse the repository at this point in the history
  • Loading branch information
Beyondo committed Jan 31, 2025
1 parent c7fcfb2 commit b357e53
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/middleware/jwk/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,

Check warning on line 156 in src/middleware/jwk/index.test.ts

View workflow job for this annotation

GitHub Actions / Main

Unexpected any. Specify a different type
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)
Expand Down

0 comments on commit b357e53

Please sign in to comment.