Skip to content

Commit

Permalink
ATO-1302: Set auth_time in ID token
Browse files Browse the repository at this point in the history
Also updated tests to use a static system time, so we can assert on the exact value of the time-related fields.
  • Loading branch information
cearl1 committed Jan 13, 2025
1 parent f188b5c commit 18daa9f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/components/token/helper/create-id-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const createIdTokenClaimSet = (
? randomBytes(32).toString()
: authRequestParams.nonce,
vtm: config.getTrustmarkUrl(),
auth_time: timeNow,
};
};

Expand Down
1 change: 1 addition & 0 deletions src/types/id-token-claims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export type IdTokenClaims = {
nonce: string;
vtm: string;
sid: string;
auth_time: number;
};
35 changes: 22 additions & 13 deletions tests/integration/token-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ import {
SESSION_ID,
VALID_CLAIMS,
RSA_PRIVATE_TOKEN_SIGNING_KEY,
ID_TOKEN_EXPIRY,
} from "../../src/constants";
import { decodeJwtNoVerify } from "./helper/decode-jwt-no-verify";

const TOKEN_ENDPOINT = "/token";

const TIME_NOW = 1736789549;
jest.useFakeTimers().setSystemTime(TIME_NOW);

const rsaKeyPair = generateKeyPairSync("rsa", {
modulusLength: 2048,
});
Expand Down Expand Up @@ -72,7 +76,7 @@ const createClientAssertionPayload = (
isExpired = false
) =>
new UnsecuredJWT(payload)
.setIssuedAt(Math.floor(Date.now() / 1000))
.setIssuedAt(Math.floor(TIME_NOW / 1000))
.setExpirationTime(isExpired ? "-1h" : "1h")
.setJti(randomUUID())
.setAudience(audience)
Expand Down Expand Up @@ -438,8 +442,8 @@ describe("/token endpoint tests, invalid client assertion", () => {
sub: knownClientId,
aud: "https://identity-provider.example.com/token",
jti: randomUUID(),
iat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000) + 3600,
iat: Math.floor(TIME_NOW / 1000),
exp: Math.floor(TIME_NOW / 1000) + 3600,
}) +
"." +
fakeSignature();
Expand Down Expand Up @@ -477,8 +481,8 @@ describe("/token endpoint, configured error responses", () => {
sub: knownClientId,
aud: audience,
jti: randomUUID(),
iat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000) + 3600,
iat: Math.floor(TIME_NOW / 1000),
exp: Math.floor(TIME_NOW / 1000) + 3600,
}),
};
});
Expand Down Expand Up @@ -538,7 +542,7 @@ describe("/token endpoint, configured error responses", () => {
const response = await request(app).post(TOKEN_ENDPOINT).send(validRequest);
const { id_token } = response.body;
const { payload } = decodeJwtNoVerify(id_token);
expect(payload.iat).toBeGreaterThan(Date.now() / 1000);
expect(payload.iat).toBe(Math.floor(TIME_NOW / 1000) + 86400);
});

it("returns an expired token if the client config has enabled TOKEN_EXPIRED", async () => {
Expand All @@ -551,7 +555,7 @@ describe("/token endpoint, configured error responses", () => {
const response = await request(app).post(TOKEN_ENDPOINT).send(validRequest);
const { id_token } = response.body;
const { payload } = decodeJwtNoVerify(id_token);
expect(payload.iat).toBeLessThan(Date.now() / 1000);
expect(payload.iat).toBe(Math.floor(TIME_NOW / 1000));
});

it("returns an invalid aud if the client config has enabled INVALID_AUD", async () => {
Expand Down Expand Up @@ -612,8 +616,8 @@ describe("/token endpoint valid client_assertion", () => {
sub: knownClientId,
aud: audience,
jti: randomUUID(),
iat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000) + 3600,
iat: Math.floor(TIME_NOW / 1000),
exp: Math.floor(TIME_NOW / 1000) + 3600,
});

const app = createApp();
Expand Down Expand Up @@ -643,8 +647,8 @@ describe("/token endpoint valid client_assertion", () => {
sub: knownClientId,
aud: audience,
jti: randomUUID(),
iat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000) + 3600,
iat: Math.floor(TIME_NOW / 1000),
exp: Math.floor(TIME_NOW / 1000) + 3600,
});

const app = createApp();
Expand Down Expand Up @@ -674,8 +678,8 @@ describe("/token endpoint valid client_assertion", () => {
sub: knownClientId,
aud: audience,
jti: randomUUID(),
iat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000) + 3600,
iat: Math.floor(TIME_NOW / 1000),
exp: Math.floor(TIME_NOW / 1000) + 3600,
});

const app = createApp();
Expand Down Expand Up @@ -722,5 +726,10 @@ describe("/token endpoint valid client_assertion", () => {
expect(decodedIdToken.payload.vot).toBe(
validAuthRequestParams.vtr.credentialTrust
);
expect(decodedIdToken.payload.iat).toBe(Math.floor(TIME_NOW / 1000));
expect(decodedIdToken.payload.exp).toBe(
Math.floor(TIME_NOW / 1000) + ID_TOKEN_EXPIRY
);
expect(decodedIdToken.payload.auth_time).toBe(Math.floor(TIME_NOW / 1000));
});
});

0 comments on commit 18daa9f

Please sign in to comment.