Skip to content

Commit

Permalink
fix: use sha512 for Ed25519 and shake256 for Ed448 ID Token _hash claims
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Nov 7, 2019
1 parent 7f80b8e commit 31f7a04
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
48 changes: 25 additions & 23 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,18 @@ module.exports = (issuer, aadIssValidation = false) => class Client extends Base
});
}

let key;

if (header.alg.startsWith('HS')) {
key = await this.joseSecret();
} else if (header.alg !== 'none') {
key = await this.issuer.key(header);
}

if (header.alg !== 'none' && !key) {
throw new RPError('could not find a key to validate the signature with');
}

if (returnedBy === 'authorization') {
if (!payload.at_hash && tokenSet.access_token) {
throw new RPError({
Expand All @@ -809,45 +821,35 @@ module.exports = (issuer, aadIssValidation = false) => class Client extends Base
if (!state) {
throw new TypeError('cannot verify s_hash, "checks.state" property not provided');
}
if (!tokenHash(payload.s_hash, state, header.alg)) {
throw new RPError({
printf: ['s_hash mismatch, expected %s, got: %s', tokenHash.generate(state, header.alg), payload.s_hash],
jwt: idToken,
});

try {
tokenHash.validate({ claim: 's_hash', source: 'state' }, payload.s_hash, state, header.alg, key && key.crv);
} catch (err) {
throw new RPError({ message: err.message, jwt: idToken });
}
}
}

if (tokenSet.access_token && payload.at_hash !== undefined) {
if (!tokenHash(payload.at_hash, tokenSet.access_token, header.alg)) {
throw new RPError({
printf: ['at_hash mismatch, expected %s, got: %s', tokenHash.generate(tokenSet.access_token, header.alg), payload.at_hash],
jwt: idToken,
});
try {
tokenHash.validate({ claim: 'at_hash', source: 'access_token' }, payload.at_hash, tokenSet.access_token, header.alg, key && key.crv);
} catch (err) {
throw new RPError({ message: err.message, jwt: idToken });
}
}

if (tokenSet.code && payload.c_hash !== undefined) {
if (!tokenHash(payload.c_hash, tokenSet.code, header.alg)) {
throw new RPError({
printf: ['c_hash mismatch, expected %s, got: %s', tokenHash.generate(tokenSet.code, header.alg), payload.c_hash],
jwt: idToken,
});
try {
tokenHash.validate({ claim: 'c_hash', source: 'code' }, payload.c_hash, tokenSet.code, header.alg, key && key.crv);
} catch (err) {
throw new RPError({ message: err.message, jwt: idToken });
}
}

if (header.alg === 'none') {
return tokenSet;
}

let key;

if (header.alg.startsWith('HS')) {
key = await this.joseSecret();
} else {
key = await this.issuer.key(header);
}

try {
jose.JWS.verify(idToken, key);
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"lru-cache": "^5.1.1",
"make-error": "^1.3.5",
"object-hash": "^2.0.0",
"oidc-token-hash": "^3.0.2",
"oidc-token-hash": "^4.0.0",
"p-any": "^2.1.0"
},
"devDependencies": {
Expand Down

0 comments on commit 31f7a04

Please sign in to comment.