Failing to create and sign a jwt with the apple key

123 Views Asked by At

I am trying to follow https://learn.microsoft.com/he-il/azure/app-service/configure-authentication-provider-apple to create a signed jwt with the p8 file i got from apple. I tried to do it using python code and with node.js code. Both are failing for me. The node.js code:

const jwt = require('jsonwebtoken');

const jwtPayload = {
  sub: '<app id>',
  nbf: Math.floor(Date.now() / 1000), // Unix timestamp in seconds
  exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 180, // 180 days expiration
  iss: '<team id>',
  aud: 'https://appleid.apple.com',
};

const jwtDecodeAlgorithm = 'ES256';
const jwtKid = '<key id>';

const privateKey = "<private key from the p8 file>";

//decode the private key from base64
const privateKeyDecoded = Buffer.from(privateKey, 'base64');
// Sign the JWT token
const jwtToken = jwt.sign(jwtPayload, privateKeyDecoded, {
  algorithm: jwtDecodeAlgorithm,
  header: { kid: jwtKid },
});

console.log(jwtToken);

The error i am hitting is: Error: secretOrPrivateKey must be an asymmetric key when using ES256

I expected to successfully create a signed jwt.

0

There are 0 best solutions below