Skip to content

Commit ca9a103

Browse files
fix: only add sub when provide
1 parent eb43846 commit ca9a103

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/wrapper/ReferralExchangeJwtClient.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,18 @@ function createSignedJwt({ privateKey, issuer, subject }: CreateSignedJwtArgs):
107107
} {
108108
const issuedAt = Math.floor(Date.now() / 1000);
109109
const expiresAtEpochSeconds = issuedAt + JWT_TTL_SECONDS;
110+
const signOptions: jwt.SignOptions = {
111+
algorithm: "ES256",
112+
issuer,
113+
expiresIn: JWT_TTL_SECONDS,
114+
};
115+
116+
// Only add the claim if a value is provided(not null or undefined)
117+
if (subject != null) {
118+
signOptions.subject = subject;
119+
}
110120

111-
const token = jwt.sign(
112-
{},
113-
privateKey,
114-
{
115-
algorithm: "ES256",
116-
issuer,
117-
expiresIn: JWT_TTL_SECONDS,
118-
subject,
119-
},
120-
);
121+
const token = jwt.sign({}, privateKey, signOptions);
121122

122123
return {
123124
token,

tests/unit/wrapper/ReferralExchangeJwtClient.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,28 @@ describe("ReferralExchangeJwtClient", () => {
5757
);
5858
});
5959

60+
it("omits subject claim when not provided", async () => {
61+
signMock.mockReturnValue("token-1");
62+
63+
const client = new ReferralExchangeJwtClient({
64+
privateKey: "fake-private-key",
65+
apiKeyName: "issuer",
66+
});
67+
68+
const fetcher = ((client as any)._options.fetcher) as (args: typeof requestArgs) => Promise<unknown>;
69+
70+
await fetcher(requestArgs);
71+
72+
const [, , options] = signMock.mock.calls[0];
73+
expect(options).toEqual(
74+
expect.objectContaining({
75+
issuer: "issuer",
76+
algorithm: "ES256",
77+
}),
78+
);
79+
expect(options).not.toHaveProperty("subject");
80+
});
81+
6082
it("includes subject claim when provided", async () => {
6183
signMock.mockReturnValue("token-1");
6284

0 commit comments

Comments
 (0)