Skip to content

Commit

Permalink
IMN-522 Fixes in client-assertion-validation (#1084)
Browse files Browse the repository at this point in the history
  • Loading branch information
taglioni-r authored Oct 10, 2024
1 parent b54493c commit 96aa64e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
3 changes: 1 addition & 2 deletions packages/client-assertion-validation/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ export const Key = z
clientId: ClientId,
consumerId: TenantId,
kid: z.string(),
purposeId: PurposeId,
publicKey: z.string().min(1),
algorithm: z.literal("RS256"),
algorithm: z.string(),
})
.strict();
export type Key = z.infer<typeof Key>;
Expand Down
6 changes: 6 additions & 0 deletions packages/client-assertion-validation/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
validatePurposeId,
validateSub,
validatePlatformState,
ALLOWED_ALGORITHM,
} from "./utils.js";
import {
ApiKey,
Expand All @@ -48,6 +49,7 @@ import {
unexpectedClientAssertionPayload,
invalidSignature,
clientAssertionInvalidClaims,
algorithmNotAllowed,
} from "./errors.js";

export const validateRequestParameters = (
Expand Down Expand Up @@ -187,6 +189,10 @@ export const verifyClientAssertionSignature = (
key: Key
): ValidationResult<JwtPayload> => {
try {
if (key.algorithm !== ALLOWED_ALGORITHM) {
return failedValidation([algorithmNotAllowed(key.algorithm)]);
}

const result = verify(clientAssertionJws, key.publicKey, {
algorithms: [key.algorithm],
});
Expand Down
1 change: 0 additions & 1 deletion packages/client-assertion-validation/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export const getMockApiKey = (): ApiKey => ({
clientId: generateId<ClientId>(),
consumerId: generateId<TenantId>(),
kid: "kid",
purposeId: generateId<PurposeId>(),
publicKey: crypto
.generateKeyPairSync("rsa", {
modulusLength: 2048,
Expand Down
37 changes: 37 additions & 0 deletions packages/client-assertion-validation/test/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,43 @@ describe("validation test", () => {
expect(errors).toBeUndefined();
});

it("algorithmNotAllowed", () => {
const threeHourLater = new Date();
threeHourLater.setHours(threeHourLater.getHours() + 3);

const notAllowedAlg = "RS384";
const keySet = crypto.generateKeyPairSync("rsa", {
modulusLength: 2048,
});

const jws = getMockClientAssertion({
customHeader: {
alg: notAllowedAlg,
},
standardClaimsOverride: {
iat: new Date().getTime() / 1000,
exp: threeHourLater.getTime() / 1000,
},
customClaims: {},
keySet,
});
const publicKey = keySet.publicKey
.export({
type: "pkcs1",
format: "pem",
})
.toString();
const mockConsumerKey: ConsumerKey = {
...getMockConsumerKey(),
publicKey,
algorithm: notAllowedAlg,
};
const { errors } = verifyClientAssertionSignature(jws, mockConsumerKey);
expect(errors).toBeDefined();
expect(errors).toHaveLength(1);
expect(errors![0]).toEqual(algorithmNotAllowed(notAllowedAlg));
});

it.skip("invalidClientAssertionSignatureType", () => {
// it's not clear when the result of the verify function is a string
expect(1).toBe(1);
Expand Down

0 comments on commit 96aa64e

Please sign in to comment.