-
Notifications
You must be signed in to change notification settings - Fork 460
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
base: main
Are you sure you want to change the base?
Conversation
|
||
protected async getKey() { | ||
export class JoseKey<J extends Jwk = Jwk> extends Key<J> { | ||
protected async getKeyObj(alg: string) { |
There was a problem hiding this comment.
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
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) | ||
} | ||
} |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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)
No description provided.