Created
July 31, 2021 03:59
-
-
Save dminkovsky/afa4bfda669ffbf162e7a02d8549ff0f to your computer and use it in GitHub Desktop.
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
import jwt from 'jsonwebtoken'; | |
interface Config { | |
teamId: string; | |
redirectUri: string; | |
keyId: string; | |
privateKey: string; | |
} | |
export default class AppleClientSecret { | |
constructor(private readonly config: Config) { | |
if (this.config.redirectUri.includes('localhost')) { | |
console.warn( | |
'AppleClientSecret.ts: WARNING: `redirectUri` contains "localhost", code resolution will fail', | |
); | |
} | |
} | |
async generate(clientId: string): Promise<string> { | |
const claims = { | |
exp: Math.floor(Date.now() / 1000) + 86400 * 180, // 6 months | |
iss: this.config.teamId, | |
iat: Math.floor(Date.now() / 1000), | |
aud: 'https://appleid.apple.com', | |
sub: clientId, | |
}; | |
return jwt.sign(claims, this.config.privateKey, { | |
algorithm: 'ES256', | |
keyid: this.config.keyId, | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment