Skip to content

Commit

Permalink
Update verify token
Browse files Browse the repository at this point in the history
  • Loading branch information
marklise committed Oct 3, 2024
1 parent 0e4d598 commit 0f710f4
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion handlers/authorizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ async function batchQueryWrapper(tableName, key, values) {
}

function validateToken(token) {
console.log("validateToken");
const headers = jwt.decode(token, { complete: true }).header;
console.log(headers);
const kid = headers.kid;
console.log(kid);

// search for the kid in the downloaded public keys
let keyIndex = -1;
Expand All @@ -105,22 +108,27 @@ function validateToken(token) {
break;
}
}
console.log(keyIndex);

if (keyIndex === -1) {
console.log('Public key not found in jwks.json');
throw 'Public key not found in jwks.json';
}

// construct the public key
console.log(keys[keyIndex]);
const publicKey = jwkToPem(keys[keyIndex]);

console.log(publicKey);

// get the last two sections of the token,
// message and signature (encoded in base64)
const [message, encodedSignature] = token.split('.').slice(0, 2);

console.log(message);
// decode the signature
const decodedSignature = Buffer.from(encodedSignature, 'base64');

console.log(decodedSignature);
// verify the signature
const verify = crypto.createVerify('SHA256');
verify.update(message);
Expand Down

0 comments on commit 0f710f4

Please sign in to comment.