Skip to content

Commit

Permalink
Merge pull request #4 from relivecc/support-string-key
Browse files Browse the repository at this point in the history
support privateKeyString
  • Loading branch information
ananay committed Jun 10, 2020
2 parents bc3163d + dd60b80 commit 1718fb7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const OAuth2Strategy = require('passport-oauth2'),
* @param {string} options.callbackURL – The identifier for the private key on the Apple
* Developer Account page
* @param {string} options.privateKeyLocation - Location to the private key
*
* @param {string} options.privateKeyString - Private key string
* @param {boolean} options.passReqToCallback - Determine if the req will be passed to passport cb function
* @param {function} verify
* @access public
Expand All @@ -61,7 +61,7 @@ function Strategy(options, verify) {
"client_id": options.clientID,
"team_id": options.teamID,
"key_id": options.keyID
}, options.privateKeyLocation);
}, options.privateKeyLocation, options.privateKeyString);

// Get the OAuth Access Token from Apple's server
// using the grant code / refresh token.
Expand Down
41 changes: 23 additions & 18 deletions src/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ class AppleClientSecret {
* @param {string} config.team_id
* @param {string} config.redirect_uri
* @param {string} config.key_id
* @param {*} privateKeyLocation
* @param {string} privateKeyLocation
* @param {string} privateKeyString
*/
constructor(config, privateKeyLocation) {
constructor(config, privateKeyLocation, privateKeyString) {
this._config = config;
this._privateKeyLocation = privateKeyLocation;
this._privateKeyString = privateKeyString;
this.generate = this.generate.bind(this);
this._generateToken = this._generateToken.bind(this);
}
Expand Down Expand Up @@ -66,23 +68,26 @@ class AppleClientSecret {
generate() {
return new Promise (
(resolve, reject) => {
fs.readFile(this._privateKeyLocation, (err, privateKey) => {
if (err) {
reject("AppleAuth Error - Couldn't read your Private Key file: " + err);
}
let exp = Math.floor(Date.now() / 1000) + ( 86400 * 180 ); // Make it expire within 6 months
this._generateToken(
this._config.client_id,
this._config.team_id,
privateKey,
exp,
this._config.key_id
).then((token) => {
resolve(token);
}).catch((err) => {
reject(err);
});
let privateKey;
try {
privateKey = this._privateKeyLocation ? fs.readFileSync(this._privateKeyLocation) : this._privateKeyString;
} catch (err) {
return reject("AppleAuth Error - Couldn't read your Private Key file: " + err);
}

let exp = Math.floor(Date.now() / 1000) + ( 86400 * 180 ); // Make it expire within 6 months
this._generateToken(
this._config.client_id,
this._config.team_id,
privateKey,
exp,
this._config.key_id
).then((token) => {
resolve(token);
}).catch((err) => {
reject(err);
});

}
);
}
Expand Down

0 comments on commit 1718fb7

Please sign in to comment.