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

Add sub claim to JWT standard fields #148

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/jwt-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type JwtHeader = JwtHeaderStandardFields & JsonObject;
interface JwtPayloadStandardFields {
exp?: number; // expires: https://tools.ietf.org/html/rfc7519#section-4.1.4
iss?: string; // issuer: https://tools.ietf.org/html/rfc7519#section-4.1.1
sub?: string; // subject: https://tools.ietf.org/html/rfc7519#section-4.1.2
aud?: string | string[]; // audience: https://tools.ietf.org/html/rfc7519#section-4.1.3
nbf?: number; // not before: https://tools.ietf.org/html/rfc7519#section-4.1.5
iat?: number; // issued at: https://tools.ietf.org/html/rfc7519#section-4.1.6
Expand Down
3 changes: 3 additions & 0 deletions src/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ function assertJwtPayload(
if (payload.iss !== undefined && typeof payload.iss !== "string") {
throw new JwtParseError("JWT payload iss claim is not a string");
}
if (payload.sub !== undefined && typeof payload.sub !== "string") {
throw new JwtParseError("JWT payload sub claim is not a string");
}
if (
payload.aud !== undefined &&
typeof payload.aud !== "string" &&
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/jwt-rsa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,18 @@ describe("unit tests jwt verifier", () => {
expect(statement).toThrow("JWT payload iss claim is not a string");
expect(statement).toThrow(JwtParseError);
});
test("JWT with sub that is not a string", () => {
const header = base64url('{"alg":"RS256"}');
const payload = base64url('{"sub":12345}');
const signedJwt = `${header}.${payload}.signature`;
const statement = () =>
verifyJwtSync(signedJwt, keypair.jwk, {
audience: null,
issuer: null,
});
expect(statement).toThrow("JWT payload sub claim is not a string");
expect(statement).toThrow(JwtParseError);
});
test("JWT with aud that is not a string", () => {
const header = base64url('{"alg":"RS256"}');
const payload = base64url('{"aud":12345}');
Expand Down