Wednesday 24 April 2019

Google Cloud Key Management Service to sign JSON Web Tokens

crosspost from: (https://www.reddit.com/r/googlecloud/comments/bgtnv3/google_cloud_key_management_service_to_sign_json/)_Hey guys. Greetings from India!​First of all I tried the solution at: Using Google Cloud Key Management Service to sign JSON Web Tokens But it doesn't work.​Creating signature:​const TimeStamp = Math.floor(new Date().getTime() / 1000) let body = base64url( JSON.stringify({ alg: 'RS256', typ: 'JWT' }) ) body += '.' body += base64url( JSON.stringify({ iss: 'some-iss', aud: 'some-aud', iat: TimeStamp, exp: TimeStamp + parseInt(process.env.TOKEN_EXPIRY, 10) }) ) const hashedMessage = crypto .createHash('sha256') .update(body) .digest('base64') const digest = { sha256: hashedMessage } const [signatureObj] = await client .asymmetricSign({ name, digest }) .catch(console.error) const signature = base64url(signatureObj.signature) const token = `${body}.${signature}` ​Then verifying:const[publicKeyObject] = await client.getPublicKey({ name }).catch(console.error) const publicKey = publicKeyObject.pem const verify = crypto.createVerify('sha256') verify.write(body) verify.end() verify.verify(publicKey, base64url.decode(signature), 'base64') ​I'm not able to figure what is wrong with the code. Thanks!

Submitted April 24, 2019 at 01:04PM by Typical_Button_1

No comments:

Post a Comment