Created
October 27, 2021 19:13
-
-
Save edinnen/354c4ae2de7ef711d58249c1cc9d1877 to your computer and use it in GitHub Desktop.
Generate an access token for Sign in With Apple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const jwt = require('jsonwebtoken'); | |
const fs = require('fs'); | |
const privateKey = fs.readFileSync('/Path/To/appleAuth/key.p8'); | |
const client_id = 'YOUR_CLIENT_ID'; | |
const team = 'YOUR_TEAM_ID'; | |
const key_id = 'YOUR_KEY_ID'; | |
function _generateToken(clientId, teamId, pk, exp, keyid) { | |
try { | |
const claims = { | |
iss: teamId, | |
iat: Math.floor(Date.now() / 1000), | |
exp, | |
aud: 'https://appleid.apple.com', | |
sub: clientId | |
}; | |
return jwt.sign(claims, pk, { | |
algorithm: 'ES256', | |
keyid | |
}); | |
} catch (err) { | |
throw new Error( | |
'AppleAuth Error - Error occurred while signing: ' + err | |
); | |
} | |
} | |
function generateToken() { | |
const sixMonthExpiry = Math.floor(Date.now() / 1000) + 86400 * 180; | |
return _generateToken( | |
client_id, | |
team, | |
privateKey, | |
sixMonthExpiry, | |
key_id | |
); | |
} | |
console.log(generateToken()); // eslint-disable-line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment