Skip to content

Instantly share code, notes, and snippets.

@kypflug
Created April 12, 2016 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kypflug/36d380f47d54196edf26cea6a829de9f to your computer and use it in GitHub Desktop.
Save kypflug/36d380f47d54196edf26cea6a829de9f to your computer and use it in GitHub Desktop.
var jwkToPem = require('jwk-to-pem')
var crypto = require('crypto');
var webAuthAuthenticator = {
validateSignature: function (publicKey, clientData, authnrData, signature, challenge) {
// Make sure the challenge in the client data
// matches the expected challenge
var c = new Buffer(clientData, 'base64');
var cc = JSON.parse(c.toString().replace(/\0/g,''));
if(cc.challenge != challenge) return false;
// Hash data with sha256
const hash = crypto.createHash('sha256');
hash.update(c);
var h = hash.digest();
// Verify signature is correct for authnrData + hash
var verify = crypto.createVerify('RSA-SHA256');
verify.update(new Buffer(authnrData,'base64'));
verify.update(h);
return verify.verify(jwkToPem(JSON.parse(publicKey)), signature, 'base64');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment