Permalink
Fetching contributors…
Cannot retrieve contributors at this time
74 lines (63 sloc) 2.16 KB
var oauthModule = require('./oauth2'),
url = require('url');
var github = module.exports =
oauthModule.submodule('github')
.configurable({
scope: 'specify types of access: (no scope), user, public_repo, repo, gist'
})
.oauthHost('https://github.com')
.apiHost('https://api.github.com')
.customHeaders({'User-Agent': 'everyauth-github'})
.authPath('/login/oauth/authorize')
.accessTokenPath('/login/oauth/access_token')
.entryPath('/auth/github')
.callbackPath('/auth/github/callback')
.customHeaders({'User-Agent': 'everyauth-github'})
.authQueryParam('scope', function () {
return this._scope && this.scope();
})
.authCallbackDidErr( function (req) {
var parsedUrl = url.parse(req.url, true);
return parsedUrl.query && !!parsedUrl.query.error;
})
.handleAuthCallbackError( function (req, res) {
var parsedUrl = url.parse(req.url, true),
errorDesc = parsedUrl.query.error + "; " + parsedUrl.query.error_description;
if (res.render) {
res.render(__dirname + '/../views/auth-fail.jade', {
errorDescription: errorDesc
});
} else {
// TODO Replace this with a nice fallback
throw new Error("You must configure handleAuthCallbackError if you are not using express");
}
})
.fetchOAuthUser( function (accessToken) {
var p = this.Promise();
this.oauth.get(this.apiHost() + '/user', accessToken, function (err, data) {
if (err) return p.fail(err);
var oauthUser = JSON.parse(data);
p.fulfill(oauthUser);
});
return p;
})
.moduleErrback( function (err, seqValues) {
if (err instanceof Error) {
var next = seqValues.next;
return next(err);
} else if (err.extra) {
var ghResponse = err.extra.res
, serverResponse = seqValues.res;
serverResponse.writeHead(
ghResponse.statusCode
, ghResponse.headers);
serverResponse.end(err.extra.data);
} else if (err.statusCode) {
var serverResponse = seqValues.res;
serverResponse.writeHead(err.statusCode);
serverResponse.end(err.data);
} else {
console.error(err);
throw new Error('Unsupported error type');
}
});