Skip to content

Instantly share code, notes, and snippets.

@dminkovsky
Created July 31, 2021 03:59
Show Gist options
  • Save dminkovsky/afa4bfda669ffbf162e7a02d8549ff0f to your computer and use it in GitHub Desktop.
Save dminkovsky/afa4bfda669ffbf162e7a02d8549ff0f to your computer and use it in GitHub Desktop.
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