Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import changes from cfsghost wrt. GitHub API v3, add option to provide organization base URL, API #70

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
ae891a4
Get email address via github API v3
cfsghost Aug 23, 2014
a0f7e43
modified testcase for email API
cfsghost Aug 28, 2014
96abbc2
* Disable node v0.4 on Travis CI
cfsghost Aug 28, 2014
fb7a415
original author do not keep maintaining this module, so fork.
cfsghost Dec 11, 2014
07a5b10
Update Readme
cfsghost Dec 11, 2014
91be6e9
update example to use passport-github2
cfsghost Dec 11, 2014
10d2ed5
bump version to 0.1.9
cfsghost Dec 11, 2014
c85f3a8
Update README.md
cfsghost Jan 7, 2015
a08377e
Update app.js
cfsghost Jan 7, 2015
2aad1b6
Update README.md
GregTurner Mar 27, 2015
5f3043f
Merge pull request #1 from BCDevExchange/GregTurner-patch-1
GregTurner Mar 27, 2015
9ee0c11
Merge pull request #1 from BCDevExchange/master
cfsghost Apr 28, 2015
14f87da
fix missing space in comment
colinterface Jun 9, 2015
be12a63
Merge pull request #2 from ultralame/patch-1
cfsghost Aug 22, 2015
a9953a8
update example to use express 4
hackreactor-students Sep 15, 2015
0e801c2
Remove repeat word in app.js comment
dougshamoo Sep 16, 2015
fbc2385
Merge pull request #3 from stevenlundy/master
cfsghost Sep 28, 2015
7773f34
Merge pull request #4 from dougshamoo/patch-1
cfsghost Sep 28, 2015
b54d090
Fixed error message for emails endpoint error.
Oct 12, 2015
8602081
Merge pull request #5 from bkonetzny/emails-error-message
cfsghost Oct 31, 2015
3ed2d48
README edit
csilzen Nov 22, 2015
2e93928
fix deprecated express middleware warnings
lipp Jan 4, 2016
1ca2a12
Update Travis NodeJS versions
simison Jan 4, 2016
2f4f222
Merge pull request #6 from casandrawith1s/patch-1
cfsghost Jan 5, 2016
5c0c88d
Merge pull request #7 from lipp/fix-express-middleware-deprecated-usage
cfsghost Jan 5, 2016
d6557e2
Merge pull request #8 from simison/patch-2
cfsghost Jan 5, 2016
5d66773
request _userEmailURL when has user:email permission
isayme Jan 20, 2016
d2a2866
Merge pull request #9 from isayme/master
cfsghost Mar 1, 2016
d572f18
bump version to 0.1.10
cfsghost Mar 1, 2016
a456666
Update README.md
cfsghost Aug 22, 2017
b274eb4
Add avatar_url from Github to Passport's profile.photos
skogsmaskin Aug 30, 2017
5848696
Merge pull request #10 from skogsmaskin/avatar-url
cfsghost Sep 27, 2017
e36a279
0.1.11
cfsghost Sep 27, 2017
93e2777
add option for name
guoshencheng Sep 30, 2017
87ac395
Merge pull request #11 from guoshencheng/master
cfsghost Sep 30, 2017
6e228c7
Resolve merge conflicts with passport-github
benjspriggs Jan 23, 2018
3fa562e
Update package.json with contributor, fork URL
benjspriggs Jan 23, 2018
fe27b3d
Update package for new minor
benjspriggs Jan 23, 2018
9a67da6
Update test to refer to avatar URL and assrt that photo is pulled int…
benjspriggs Jan 23, 2018
ee9b9ea
Update outdated dependencies
benjspriggs Jan 23, 2018
d183c9e
Update README
benjspriggs Jan 23, 2018
5f060f3
Remove errant symbols
benjspriggs Jan 23, 2018
af00d58
Remove reference to deleted example directory
benjspriggs Jan 23, 2018
b186019
Add options for providing a custom base and API URL
benjspriggs Jan 23, 2018
41c480e
Update package.json
benjspriggs Jan 23, 2018
436cd3e
Remove istanbul as dependency
benjspriggs Jan 23, 2018
bbca12e
Update travis config
benjspriggs Jan 23, 2018
711f9a8
Fix typo
benjspriggs Jan 23, 2018
8c26ba9
Update node js versions - deprecate sub-v1.0 node
benjspriggs Jan 23, 2018
753106c
Update node js versions - deprecate sub-v1.0 node
benjspriggs Jan 23, 2018
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
13 changes: 6 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
language: "node_js"
node_js:
- "9"
- "8"
- "7"
- "6"
- "5"
- "4"
- "3" # io.js
- "2" # io.js
- "1" # io.js
- "0.12"
- "0.10"
- "0.8"
- "0.6"


before_install:
- "npm install [email protected] -g"
- "npm install -g istanbul"
- "npm install -g coveralls"
- "preinstall-compat"

script:
Expand Down
36 changes: 14 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
[![Quality](https://img.shields.io/codeclimate/github/jaredhanson/passport-github.svg?label=quality)](https://codeclimate.com/github/jaredhanson/passport-github)
[![Dependencies](https://img.shields.io/david/jaredhanson/passport-github.svg)](https://david-dm.org/jaredhanson/passport-github)


[Passport](http://passportjs.org/) strategy for authenticating with [GitHub](https://github.com/)
using the OAuth 2.0 API.
using the OAuth 3.0 API.

This module lets you authenticate using GitHub in your Node.js applications.
By plugging into Passport, GitHub authentication can be easily and
unobtrusively integrated into any application or framework that supports
[Connect](http://www.senchalabs.org/connect/)-style middleware, including
[Express](http://expressjs.com/).

## Install
## Installation

```bash
$ npm install passport-github
Expand All @@ -35,24 +34,21 @@ configure a callback URL which matches the route in your application.
#### Configure Strategy

The GitHub authentication strategy authenticates users using a GitHub account
and OAuth 2.0 tokens. The client ID and secret obtained when creating an
application are supplied as options when creating the strategy. The strategy
also requires a `verify` callback, which receives the access token and optional
refresh token, as well as `profile` which contains the authenticated user's
GitHub profile. The `verify` callback must call `cb` providing a user to
complete authentication.
and OAuth 3.0 tokens. The strategy requires a `verify` callback, which accepts
these credentials and calls `done` providing a user, as well as `options`
specifying a client ID, client secret, and callback URL.

```js
```javascript
var GitHubStrategy = require('passport-github').Strategy;

passport.use(new GitHubStrategy({
clientID: GITHUB_CLIENT_ID,
clientSecret: GITHUB_CLIENT_SECRET,
callbackURL: "http://127.0.0.1:3000/auth/github/callback"
},
function(accessToken, refreshToken, profile, cb) {
function(accessToken, refreshToken, profile, done) {
User.findOrCreate({ githubId: profile.id }, function (err, user) {
return cb(err, user);
return done(err, user);
});
}
));
Expand Down Expand Up @@ -96,6 +92,7 @@ expected to have corresponding test cases. Ensure that the complete test suite
passes by executing:

```bash
$ npm install --only=dev
$ make test
```

Expand All @@ -109,18 +106,13 @@ $ make test-cov
$ make view-cov
```

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

#### Funding

This software is provided to you as open source, free of charge. The time and
effort to develop and maintain this project is dedicated by [@jaredhanson](https://github.com/jaredhanson).
If you (or your employer) benefit from this project, please consider a financial
contribution. Your contribution helps continue the efforts that produce this
and other open source software.
## Support

Funds are accepted via [PayPal](https://paypal.me/jaredhanson), [Venmo](https://venmo.com/jaredhanson),
and [other](http://jaredhanson.net/pay) methods. Any amount is appreciated.
- [Jared Hanson](http://github.com/jaredhanson)
- [Fred Chien](http://github.com/cfsghost)
- [Benjamin Spriggs](https://github.com/benjspriggs)

## License

Expand Down
44 changes: 24 additions & 20 deletions lib/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var OAuth2Strategy = require('passport-oauth2')
, InternalOAuthError = require('passport-oauth2').InternalOAuthError
, APIError = require('./errors/apierror');


/**
* `Strategy` constructor.
*
Expand All @@ -18,10 +17,12 @@ var OAuth2Strategy = require('passport-oauth2')
* credentials are not valid. If an exception occured, `err` should be set.
*
* Options:
* - `baseURL` your GitHub organization base url (defaults to 'https://github.com')
* - `apiURL` your GitHub organization api url (defaults to 'https://api.github.com', usually constructed with `${baseURL}/api/v3`)
* - `clientID` your GitHub application's Client ID
* - `clientSecret` your GitHub application's Client Secret
* - `callbackURL` URL to which GitHub will redirect the user after granting authorization
* - `scope` array of permission scopes to request. valid scopes include:
* - `scope` array of permission scopes to request. Valid scopes include:
* 'user', 'public_repo', 'repo', 'gist', or none.
* (see http://developer.github.com/v3/oauth/#scopes for more info)
* — `userAgent` All API requests MUST include a valid User Agent string.
Expand All @@ -32,7 +33,7 @@ var OAuth2Strategy = require('passport-oauth2')
*
* passport.use(new GitHubStrategy({
* clientID: '123-456-789',
* clientSecret: 'shhh-its-a-secret'
* clientSecret: 'shhh-its-a-secret',
* callbackURL: 'https://www.example.net/auth/github/callback',
* userAgent: 'myapp.com'
* },
Expand All @@ -50,8 +51,12 @@ var OAuth2Strategy = require('passport-oauth2')
*/
function Strategy(options, verify) {
options = options || {};
options.authorizationURL = options.authorizationURL || 'https://github.com/login/oauth/authorize';
options.tokenURL = options.tokenURL || 'https://github.com/login/oauth/access_token';


options.baseURL = options.baseURL || 'https://github.com';
options.apiURL = options.apiURL || 'https://api.github.com';
options.authorizationURL = options.authorizationURL || `${options.baseURL}/login/oauth/authorize`;
options.tokenURL = options.tokenURL || `${options.baseURL}/login/oauth/access_token`;
options.scopeSeparator = options.scopeSeparator || ',';
options.customHeaders = options.customHeaders || {};

Expand All @@ -60,10 +65,11 @@ function Strategy(options, verify) {
}

OAuth2Strategy.call(this, options, verify);
this.name = 'github';
this._userProfileURL = options.userProfileURL || 'https://api.github.com/user';
this.name = options.name || 'github';
this._userProfileURL = options.userProfileURL || `${options.apiURL}/user`;
this._userEmailURL = options.userEmailURL || `${options.apiURL}/user/emails`;
this._oauth2.useAuthorizationHeaderforGET(true);

// NOTE: GitHub returns an HTTP 200 OK on error responses. As a result, the
// underlying `oauth` implementation understandably does not parse the
// response as an error. This code swizzles the implementation to
Expand All @@ -87,7 +93,6 @@ function Strategy(options, verify) {
// Inherit from `OAuth2Strategy`.
util.inherits(Strategy, OAuth2Strategy);


/**
* Retrieve user profile from GitHub.
*
Expand All @@ -108,26 +113,26 @@ Strategy.prototype.userProfile = function(accessToken, done) {
var self = this;
this._oauth2.get(this._userProfileURL, accessToken, function (err, body, res) {
var json;

if (err) {
if (err.data) {
try {
json = JSON.parse(err.data);
} catch (_) {}
}

if (json && json.message) {
return done(new APIError(json.message));
}
return done(new InternalOAuthError('Failed to fetch user profile', err));
}

try {
json = JSON.parse(body);
} catch (ex) {
return done(new Error('Failed to parse user profile'));
}

var profile = Profile.parse(json);
profile.provider = 'github';
profile._raw = body;
Expand All @@ -141,7 +146,7 @@ Strategy.prototype.userProfile = function(accessToken, done) {
// information that was obtained.
return done(null, profile);
}

var json;
try {
json = JSON.parse(body);
Expand All @@ -150,15 +155,15 @@ Strategy.prototype.userProfile = function(accessToken, done) {
// information that was obtained.
return done(null, profile);
}


if (!json.length) {
return done(null, profile);
}

profile.emails = profile.emails || [];
var publicEmail = profile.emails[0];

(json).forEach(function(email) {
if (publicEmail && publicEmail.value == email.email) {
profile.emails[0].primary = email.primary;
Expand All @@ -174,8 +179,7 @@ Strategy.prototype.userProfile = function(accessToken, done) {
done(null, profile);
}
});
}

};

// Expose constructor.
module.exports = Strategy;
Loading