Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ appleSignin.getAuthorizationToken(code, options).then(tokenResponse => {
});
```

It's also possible to pass the private key directly as a string under `privateKey`, instead of passing `privateKeyPath`.

Result of ```getAuthorizationToken``` command is a JSON object representing Apple's [TokenResponse](https://developer.apple.com/documentation/signinwithapplerestapi/tokenresponse):
```javascript
{
Expand All @@ -89,6 +91,7 @@ appleSignin.verifyIdToken(tokenResponse.id_token, clientID).then(result => {
console.log(error);
});
```

### 4. Refresh access token after expiration
```javascript

Expand All @@ -111,6 +114,8 @@ appleSignin.refreshAuthorizationToken(refreshToken, options).then(result => {
})
```

It's also possible to pass the private key directly as a string under `privateKey`, instead of passing `privateKeyPath`.

## Examples
Developers using the popular [Express](http://expressjs.com) web framework can refer to an [example](https://github.com/Techofficer/express-apple-signin) as a starting point for their own web applications.

Expand Down
7 changes: 4 additions & 3 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ const getClientSecret = options => {
if (!options.clientID) throw new Error('clientID is empty');
if (!options.teamId) throw new Error('teamId is empty');
if (!options.keyIdentifier) throw new Error('keyIdentifier is empty');
if (!options.privateKeyPath) throw new Error('privateKeyPath is empty');
if (!fs.existsSync(options.privateKeyPath)) throw new Error("Can't find private key");
if (!options.privateKeyPath && !options.privateKey) throw new Error('privateKey and privateKeyPath are empty');
if (options.privateKeyPath && options.privateKey) throw new Error('privateKey and privateKeyPath cannot be passed together, choose one of them');
if (options.privateKeyPath && !fs.existsSync(options.privateKeyPath)) throw new Error("Can't find private key");

const timeNow = Math.floor(Date.now() / 1000);

Expand All @@ -47,7 +48,7 @@ const getClientSecret = options => {
};

const header = { alg: 'ES256', kid: options.keyIdentifier };
const key = fs.readFileSync(options.privateKeyPath);
const key = options.privateKeyPath ? fs.readFileSync(options.privateKeyPath) : options.privateKey;

return jwt.sign(claims, key, { algorithm: 'ES256', header });
};
Expand Down