passport-apple
Advanced tools
Comparing version 1.0.0 to 1.1.0
{ | ||
"name": "passport-apple", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Passport strategy for Sign in with Apple", | ||
@@ -34,5 +34,5 @@ "main": "src/strategy.js", | ||
"dependencies": { | ||
"passport-oauth2": "^1.5.0", | ||
"jsonwebtoken": "^8.5.1" | ||
"jsonwebtoken": "^8.5.1", | ||
"passport-oauth2": "^1.5.0" | ||
} | ||
} |
@@ -1,10 +0,81 @@ | ||
# Sign in with Apple for Passportjs | ||
# Sign in with Apple for Passport.js | ||
<a href="https://twitter.com/intent/follow?screen_name=ananayarora"><img src="https://img.shields.io/twitter/follow/ananayarora.svg?label=Follow%20@ananayarora" alt="Follow @ananayarora"></img></a> | ||
<a href="https://npmjs.com/package/passport-apple"> | ||
<img src="https://img.shields.io/npm/dt/passport-apple.svg"></img> | ||
<img src="https://img.shields.io/npm/v/passport-apple.svg"></img> | ||
</a> | ||
</p> | ||
Passport strategy for the new Sign in with Apple feature! | ||
Passport strategy for the new Sign in with Apple feature, now with fetching profile information ✅! | ||
<b>This library is still under construction!</b><br /> | ||
<b>Check out the original apple-auth library that is fully working!</b> | ||
## Installation | ||
Install the package via npm / yarn: | ||
``` npm install --save passport-apple ``` | ||
Next, you need to configure your Apple Developer Account with Sign in with Apple. | ||
Steps for that are given here: | ||
https://github.com/ananay/apple-auth/blob/master/SETUP.md | ||
## Example | ||
**Live on https://passport-apple.ananay.dev** | ||
Example repo: https://github.com/ananay/passport-apple-example | ||
## Usage | ||
Initialize the strategy as follows: | ||
```js | ||
const AppleStrategy = require('passport-apple'); | ||
passport.use(new AppleStrategy({ | ||
clientID: "", | ||
teamID: "", | ||
callbackURL: "", | ||
keyID: "", | ||
privateKeyLocation: "", | ||
passReqToCallback: true | ||
}, function(req, accessToken, refreshToken, decodedIdToken, profile, cb) { | ||
// Here, check if the decodedIdToken.sub exists in your database! | ||
// decodedIdToken should contains email too if user authorized it but will not contain the name | ||
// `profile` parameter is REQUIRED for the sake of passport implementation | ||
// it should be profile in the future but apple hasn't implemented passing data | ||
// in access token yet https://developer.apple.com/documentation/sign_in_with_apple/tokenresponse | ||
cb(null, decodedIdToken); | ||
})); | ||
``` | ||
Add the login route: | ||
```js | ||
app.get("/login", passport.authenticate('apple')); | ||
``` | ||
Finally, add the callback route and handle the response: | ||
```js | ||
app.get("/auth", function(req, res, next) { | ||
passport.authenticate('apple', function(err, user, info) { | ||
if (err) { | ||
if (err == "AuthorizationError") { | ||
res.send("Oops! Looks like you didn't allow the app to proceed. Please sign in again! <br /> \ | ||
<a href=\"/login\">Sign in with Apple</a>"); | ||
} else if (err == "TokenError") { | ||
res.send("Oops! Couldn't get a valid token from Apple's servers! <br /> \ | ||
<a href=\"/login\">Sign in with Apple</a>"); | ||
} | ||
} else { | ||
res.json(user); | ||
} | ||
})(req, res, next); | ||
}); | ||
``` | ||
## Other Sign in with Apple repos | ||
Check out my other sign in with Apple Repos here. | ||
```apple-auth```: | ||
<a href="https://github.com/ananay/apple-auth">https://github.com/ananay/apple-auth</a><br /> | ||
@@ -14,2 +85,10 @@ <a href="https://npmjs.com/package/apple-auth">https://npmjs.com/package/apple-auth</a> | ||
## FAQ | ||
#### What's the difference between `apple-auth` and `passport-apple`? | ||
`apple-auth` is a standalone library for Sign in with Apple. It does not require you to use Passport.js where as passport-apple is used with Passport.js. | ||
#### How is this module different from [nicokaiser/passport-apple](https://github.com/nicokaiser/passport-apple)? | ||
`@nicokaiser/passport-apple` is a fork of `passport-apple` that was made when `passport-apple` couldn't support fetching profile information. `passport-apple` now **supports** fetching profile information as well by using a simpler workaround (shoutout to [@MotazAbuElnasr](https://github.com/MotazAbuElnasr) for this!) instead of rewriting all of `passport-oauth2`. | ||
## Questions / Contributing | ||
@@ -16,0 +95,0 @@ |
@@ -15,5 +15,5 @@ /** | ||
* Passport Strategy Constructor | ||
* | ||
* | ||
* Example: | ||
* | ||
* | ||
* passport.use(new AppleStrategy({ | ||
@@ -24,8 +24,12 @@ * clientID: "", | ||
* keyID: "", | ||
* privateKeyLocation: "" | ||
* }, function(accessToken, refreshToken, idToken, profile, cb) { | ||
* // Here, check if the idToken exists in your database! | ||
* privateKeyLocation: "", | ||
* passReqToCallback: true | ||
* }, function(req, accessToken, refreshToken, decodedIdToken, __ , cb) { | ||
* // Here, check if the decodedIdToken.sub exists in your database! | ||
* // __ parameter is REQUIRED for the sake of passport implementation | ||
* // it should be profile in the future but apple hasn't implemented passing data | ||
* // in access token yet https://developer.apple.com/documentation/sign_in_with_apple/tokenresponse | ||
* cb(null, idToken); | ||
* })); | ||
* | ||
* | ||
* @param {object} options - Configuration options | ||
@@ -41,2 +45,4 @@ * @param {string} options.clientID – Client ID (also known as the Services ID | ||
* @param {string} options.privateKeyLocation - Location to the private key | ||
* | ||
* @param {boolean} options.passReqToCallback - Determine if the req will be passed to passport cb function | ||
* @param {function} verify | ||
@@ -50,2 +56,3 @@ * @access public | ||
options.tokenURL = options.tokenURL || 'https://appleid.apple.com/auth/token'; | ||
options.passReqToCallback = options.passReqToCallback === undefined ? true : options.passReqToCallback | ||
@@ -89,7 +96,7 @@ // Make the OAuth call | ||
} else { | ||
let results = JSON.parse(data); | ||
let access_token = results.access_token; | ||
let refresh_token = results.refresh_token; | ||
let id_token = jwt.decode(results.id_token).sub; | ||
callback(null, access_token, refresh_token, id_token, results); | ||
const results = JSON.parse(data); | ||
const access_token = results.access_token; | ||
const refresh_token = results.refresh_token; | ||
const decodedIdToken = jwt.decode(results.id_token) | ||
callback(null, access_token, refresh_token, decodedIdToken); | ||
} | ||
@@ -99,4 +106,3 @@ } | ||
}).catch((error) => { | ||
// Log to the console if the token generation fails. | ||
console.log(error); | ||
callback(error); | ||
}); | ||
@@ -115,5 +121,10 @@ } | ||
*/ | ||
Strategy.prototype.authenticate = function(req, options) { | ||
Strategy.prototype.authenticate = function (req, options) { | ||
// Workaround instead of reimplementing authenticate function | ||
req.query = { ...req.query, ...req.body }; | ||
if(req.body && req.body.user){ | ||
req.appleProfile = JSON.parse(req.body.user) | ||
} | ||
OAuth2Strategy.prototype.authenticate.call(this, req, options); | ||
}; | ||
}; | ||
@@ -128,2 +139,5 @@ /** | ||
options.state = crypto.randomBytes(5).toString('hex'); | ||
options.response_type = "code id_token"; | ||
options.scope = "name email"; | ||
options.response_mode = "form_post"; | ||
return options; | ||
@@ -130,0 +144,0 @@ } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
12718
210
98
0