Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type safety and compatibility with Bun #2879

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

matthieusieben
Copy link
Contributor

No description provided.

@matthieusieben matthieusieben mentioned this pull request Oct 14, 2024

protected async getKey() {
export class JoseKey<J extends Jwk = Jwk> extends Key<J> {
protected async getKeyObj(alg: string) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the change that fixes compatibility with bun

Comment on lines +85 to 126
async verifyJwt<C extends string = never>(
token: SignedJwt,
options?: VerifyOptions<C>,
): Promise<VerifyResult<C>> {
try {
const keyObj = await this.getKey()
const result = await jwtVerify(token, keyObj, {
...options,
algorithms: this.algorithms,
} as JWTVerifyOptions)

return result as VerifyResult<P, C>
} catch (error) {
if (error instanceof JOSEError) {
throw new JwtVerifyError(error.message, error.code, { cause: error })
const result = await jwtVerify(
token,
async ({ alg }) => this.getKeyObj(alg),
{ ...options, algorithms: this.algorithms } as JWTVerifyOptions,
)

// @NOTE if all tokens are signed exclusively through createJwt(), then
// there should be no need to parse the payload and headers here. But
// since the JWT could have been signed with the same key from somewhere
// else, let's parse it to ensure the integrity (and type safety) of the
// data.
const headerParsed = jwtHeaderSchema.safeParse(result.protectedHeader)
if (!headerParsed.success) {
throw new JwtVerifyError('Invalid JWT header', undefined, {
cause: headerParsed.error,
})
}

const payloadParsed = jwtPayloadSchema.safeParse(result.payload)
if (!payloadParsed.success) {
throw new JwtVerifyError('Invalid JWT payload', undefined, {
cause: payloadParsed.error,
})
}

return {
protectedHeader: headerParsed.data,
// "requiredClaims" enforced by jwtVerify()
payload: payloadParsed.data as RequiredKey<JwtPayload, C>,
}
} catch (cause) {
if (cause instanceof JOSEError) {
throw new JwtVerifyError(cause.message, cause.code, { cause })
} else {
throw JwtVerifyError.from(error)
throw JwtVerifyError.from(cause)
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed type casting & replaced with actual validation to ensure type safety.

/** "crit" (Critical) Header Parameter */
crit: z.array(z.string()).optional(),
})
.passthrough()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid stripping unknown keys.

)
.optional(),
})
.passthrough()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid stripping unknown keys.

@@ -24,25 +26,28 @@ export abstract class Key {
return false
}

get privateJwk(): Jwk | undefined {
get privateJwk(): Readonly<J> | undefined {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returned value should not be altered

}

@cachedGetter
get bareJwk(): Jwk | undefined {
get bareJwk(): Readonly<Jwk> | undefined {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returned value should not be altered (because it is cached)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant