Skip to content

Commit

Permalink
validate takes an object for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
Ptroger committed Mar 28, 2024
1 parent 47eafd8 commit 00fab02
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
6 changes: 5 additions & 1 deletion packages/signature/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ const generateRsaPrivateKey = async (
}
jwk.kid = opts.keyId || rsaKeyToKid(jwk)

const pk = validate<RsaPrivateKey>(rsaPrivateKeySchema, jwk, 'Invalid RSA Private Key JWK')
const pk = validate<RsaPrivateKey>({
schema: rsaPrivateKeySchema,
jwk,
errorMessage: 'Invalid RSA Private Key'
})
return pk
}

Expand Down
12 changes: 10 additions & 2 deletions packages/signature/src/lib/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ import { ZodSchema } from 'zod'
import { JwtError } from './error'
import { Jwk } from './types'

export function validate<T>(schema: ZodSchema<T>, jwk: Jwk, errorMessage: string = 'Validation failed') {
export function validate<T>({
schema,
jwk,
errorMessage,
}: {
schema: ZodSchema<T>
jwk: Jwk
errorMessage?: string
}) {
return (function validate(input: Jwk): T {
const result = schema.safeParse(input)
if (!result.success) {
throw new JwtError({
message: errorMessage,
message: errorMessage || 'Invalid JWK',
context: { errors: result.error.flatten().fieldErrors }
})
}
Expand Down
28 changes: 7 additions & 21 deletions packages/signature/src/lib/verify.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { secp256k1 } from '@noble/curves/secp256k1'
import { importJWK, jwtVerify } from 'jose'
import { isAddressEqual, recoverAddress } from 'viem'
import { decode, decodeJwsd } from './decode'
import { decode } from './decode'
import { JwtError } from './error'
import { publicKeySchema } from './schemas'
import { eip191Hash } from './sign'
import { isSepc256k1PublicKeyJwk } from './typeguards'
import { Alg, EoaPublicKey, Hex, Jwk, Jwsd, Jwt, Payload, PublicKey, Secp256k1PublicKey, SigningAlg } from './types'
import { Alg, EoaPublicKey, Hex, Jwk, Jwt, Payload, PublicKey, Secp256k1PublicKey, SigningAlg } from './types'
import { base64UrlToHex, secp256k1PublicKeyToHex } from './utils'
import { validate } from './validate'

Expand Down Expand Up @@ -79,7 +79,11 @@ export const verifyEip191 = async (jwt: string, jwk: PublicKey): Promise<boolean

export async function verifyJwt(jwt: string, jwk: Jwk): Promise<Jwt> {
const { header, payload, signature } = decode(jwt)
const key = validate<PublicKey>(publicKeySchema, jwk, 'Invalid Public Key JWK')
const key = validate<PublicKey>({
schema: publicKeySchema,
jwk,
errorMessage: 'Invalid public key'
})

if (header.alg === SigningAlg.EIP191) {
await verifyEip191(jwt, key)
Expand All @@ -99,21 +103,3 @@ export async function verifyJwt(jwt: string, jwk: Jwk): Promise<Jwt> {
signature
}
}

export async function verifyJwsd(jws: string, jwk: PublicKey): Promise<Jwsd> {
const { header, payload, signature } = decodeJwsd(jws)

if (header.alg === SigningAlg.EIP191) {
await verifyEip191(jws, jwk)
} else {
// TODO: Implement other algs individually without jose
const joseJwk = await importJWK(jwk)
await jwtVerify(jws, joseJwk)
}

return {
header,
payload,
signature
}
}

0 comments on commit 00fab02

Please sign in to comment.