Skip to content

Commit

Permalink
Merge be12a63 into 0009c72
Browse files Browse the repository at this point in the history
  • Loading branch information
cfsghost committed Aug 22, 2015
2 parents 0009c72 + be12a63 commit fcff5bb
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -3,7 +3,7 @@ node_js:
- "0.10"
- "0.8"
# - "0.6"
- "0.4"
# - "0.4"

before_install:
- "npm install istanbul -g"
Expand Down
13 changes: 8 additions & 5 deletions README.md
@@ -1,4 +1,6 @@
# Passport-GitHub
# Passport-GitHub2

The author of Passport-Github has not maintain module for a long time. Features in his module don't work since Github upgraded their API to version 3.0. We forked it and re-publish it to NPM with a new name `passport-github2`.

[Passport](http://passportjs.org/) strategy for authenticating with [GitHub](https://github.com/)
using the OAuth 2.0 API.
Expand All @@ -11,7 +13,7 @@ unobtrusively integrated into any application or framework that supports

## Install

$ npm install passport-github
$ npm install passport-github2

## Usage

Expand Down Expand Up @@ -43,7 +45,7 @@ For example, as route middleware in an [Express](http://expressjs.com/)
application:

app.get('/auth/github',
passport.authenticate('github'));
passport.authenticate('github', { scope: [ 'user:email' ] }));

app.get('/auth/github/callback',
passport.authenticate('github', { failureRedirect: '/login' }),
Expand All @@ -54,18 +56,19 @@ application:

## Examples

For a complete, working example, refer to the [login example](https://github.com/jaredhanson/passport-github/tree/master/examples/login).
For a complete, working example, refer to the [login example](https://github.com/cfsghost/passport-github/tree/master/examples/login).

## Tests

$ npm install --dev
$ make test

[![Build Status](https://secure.travis-ci.org/jaredhanson/passport-github.png)](http://travis-ci.org/jaredhanson/passport-github)
[![Build Status](https://secure.travis-ci.org/cfsghost/passport-github.png)](http://travis-ci.org/cfsghost/passport-github)

## Credits

- [Jared Hanson](http://github.com/jaredhanson)
- [Fred Chien](http://github.com/cfsghost)

## License

Expand Down
6 changes: 3 additions & 3 deletions examples/login/app.js
@@ -1,7 +1,7 @@
var express = require('express')
, passport = require('passport')
, util = require('util')
, GitHubStrategy = require('passport-github').Strategy;
, GitHubStrategy = require('passport-github2').Strategy;

var GITHUB_CLIENT_ID = "--insert-github-client-id-here--"
var GITHUB_CLIENT_SECRET = "--insert-github-client-secret-here--";
Expand Down Expand Up @@ -83,10 +83,10 @@ app.get('/login', function(req, res){
// GET /auth/github
// Use passport.authenticate() as route middleware to authenticate the
// request. The first step in GitHub authentication will involve redirecting
// the user to github.com. After authorization, GitHubwill redirect the user
// the user to github.com. After authorization, GitHub will redirect the user
// back to this application at /auth/github/callback
app.get('/auth/github',
passport.authenticate('github'),
passport.authenticate('github', { scope: [ 'user:email' ] }),
function(req, res){
// The request will be redirected to GitHub for authentication, so this
// function will not be called.
Expand Down
2 changes: 1 addition & 1 deletion examples/login/package.json
Expand Up @@ -5,6 +5,6 @@
"express": "3.x.x",
"ejs": ">= 0.0.0",
"passport": ">= 0.0.0",
"passport-github": ">= 0.0.0"
"passport-github2": ">= 0.0.0"
}
}
23 changes: 21 additions & 2 deletions lib/strategy.js
Expand Up @@ -62,6 +62,7 @@ function Strategy(options, verify) {
OAuth2Strategy.call(this, options, verify);
this.name = 'github';
this._userProfileURL = options.userProfileURL || 'https://api.github.com/user';
this._userEmailURL = options.userEmailURL || 'https://api.github.com/user/emails';
this._oauth2.useAuthorizationHeaderforGET(true);
}

Expand All @@ -88,6 +89,8 @@ util.inherits(Strategy, OAuth2Strategy);
* @api protected
*/
Strategy.prototype.userProfile = function(accessToken, done) {
var self = this;

this._oauth2.get(this._userProfileURL, accessToken, function (err, body, res) {
var json;

Expand All @@ -105,8 +108,24 @@ Strategy.prototype.userProfile = function(accessToken, done) {
profile.provider = 'github';
profile._raw = body;
profile._json = json;

done(null, profile);

// Getting emails
self._oauth2.get(self._userEmailURL, accessToken, function (err, body, res) {

if (err)
return done(new InternalOAuthError('Failed to fetch user profile', err));

var json = JSON.parse(body);

for (var index in json) {
if (json[index].primary) {
profile.emails = [{ value: json[index].email }];
break;
}
}

done(null, profile);
});
});
}

Expand Down
16 changes: 11 additions & 5 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "passport-github",
"version": "0.1.7",
"name": "passport-github2",
"version": "0.1.9",
"description": "GitHub authentication strategy for Passport.",
"keywords": [
"passport",
Expand All @@ -15,12 +15,18 @@
"email": "jaredhanson@gmail.com",
"url": "http://www.jaredhanson.net/"
},
"contributors": [
{
"name": "Fred Chien",
"email": "cfsghost@gmail.com"
}
],
"repository": {
"type": "git",
"url": "http://github.com/jaredhanson/passport-github.git"
"url": "http://github.com/cfsghost/passport-github.git"
},
"bugs": {
"url": "http://github.com/jaredhanson/passport-github/issues"
"url": "http://github.com/cfsghost/passport-github/issues"
},
"licenses": [
{
Expand All @@ -37,7 +43,7 @@
"chai": "1.x.x"
},
"engines": {
"node": ">= 0.4.0"
"node": ">= 0.8.0"
},
"scripts": {
"test": "node_modules/.bin/mocha --reporter spec --require test/bootstrap/node test/*.test.js"
Expand Down
17 changes: 12 additions & 5 deletions test/strategy.options.test.js
Expand Up @@ -10,16 +10,23 @@ describe('Strategy#userProfile', function() {
var strategy = new GitHubStrategy({
clientID: 'ABC123',
clientSecret: 'secret',
userProfileURL: 'https://github.corpDomain/api/v3/user'
userProfileURL: 'https://github.corpDomain/api/v3/user',
userEmailURL: 'https://github.corpDomain/api/v3/emails'
},
function() {});

// mock
strategy._oauth2.get = function(url, accessToken, callback) {
if (url != 'https://github.corpDomain/api/v3/user') { return callback(new Error('wrong url argument')); }
var testcases = {
'https://github.corpDomain/api/v3/user': '{ "login": "octocat", "id": 1, "name": "monalisa octocat", "email": "octocat@github.com", "html_url": "https://github.com/octocat" }',
'https://github.corpDomain/api/v3/emails': '[ { "email": "octocat@github.com", "verified": true, "primary": true } ]'
};

var body = testcases[url] || null;
if (!body)
return callback(new Error('wrong url argument'));

if (accessToken != 'token') { return callback(new Error('wrong token argument')); }

var body = '{ "login": "octocat", "id": 1, "name": "monalisa octocat", "email": "octocat@github.com", "html_url": "https://github.com/octocat" }';

callback(null, body, undefined);
};
Expand Down
14 changes: 10 additions & 4 deletions test/strategy.profile.test.js
Expand Up @@ -14,10 +14,16 @@ describe('Strategy#userProfile', function() {

// mock
strategy._oauth2.get = function(url, accessToken, callback) {
if (url != 'https://api.github.com/user') { return callback(new Error('wrong url argument')); }
if (accessToken != 'token') { return callback(new Error('wrong token argument')); }

var body = '{ "login": "octocat", "id": 1, "name": "monalisa octocat", "email": "octocat@github.com", "html_url": "https://github.com/octocat" }';
var testcases = {
'https://api.github.com/user': '{ "login": "octocat", "id": 1, "name": "monalisa octocat", "email": "octocat@github.com", "html_url": "https://github.com/octocat" }',
'https://api.github.com/user/emails': '[ { "email": "octocat@github.com", "verified": true, "primary": true } ]'
};

var body = testcases[url] || null;
if (!body)
return callback(new Error('wrong url argument'));

if (accessToken != 'token') { return callback(new Error('wrong token argument')); }

callback(null, body, undefined);
};
Expand Down

0 comments on commit fcff5bb

Please sign in to comment.