forked from koajs/koa-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (160 loc) · 5.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**!
* koa-github - index.js
*
* MIT Licensed
*
* Authors:
* dead_horse <[email protected]> (http://deadhorse.me)
*/
'use strict';
/**
* Module dependencies.
*/
var urllib = require('co-urllib');
var debug = require('debug')('koa-github');
var utility = require('utility');
var util = require('util');
var qsParse = require('querystring').parse;
var urlParse = require('url').parse;
var assert = require('assert');
var defaultOptions = {
tokenKey: 'githubToken',
signinPath: '/github/auth',
timeout: 5000,
scope: ['user'],
redirect: 'redirect_uri'
};
/**
* auth with github
* need use session middleware before
* see http://developer.github.com/v3/oauth/#web-application-flow
*
* @param {Object} options
* - [String] clientID github client ID
* - [String] clientSecret github client secret
* - [String] callbackURL github callback url
* - [String] signinPath sign in with github's triggle path, default is /github/auth
* - [String] tokenKey session key, default is githubToken
* - [String] userKey user key, if set user key, will request github once to get the user info
* - [Array] scope A comma separated list of scopes
* - [Number] timeout request github api timeout
* - [String] redirect redirect key when call signinPath, so we can redirect after auth, default is redirect_uri
*
*/
module.exports = function (options) {
options = options || {};
if (!options.clientID || !options.clientSecret || !options.callbackURL) {
throw new Error('github auth need clientID, clientSecret and callbackURL');
}
for (var key in defaultOptions) {
if (!utility.has(options, key)) {
options[key] = defaultOptions[key];
}
}
options.callbackURL = options.callbackURL;
options.callbackPath = urlParse(options.callbackURL).path;
urllib.TIMEOUT = options.timeout;
debug('init github auth middleware with options %j', options);
return function *githubAuth(next) {
if (!this.session) {
return this.throw('github auth need session', 500);
}
// first step: redirect to github
if (this.path === options.signinPath) {
var state = utility.randomString();
var redirectUrl = 'https://github.com/login/oauth/authorize?';
redirectUrl = util.format('%sclient_id=%s&redirect_uri=%s&scope=%s&state=%s',
redirectUrl, options.clientID, options.callbackURL, options.scope, state);
this.session._githubstate = state;
//try to get the redirect url and set it to session
try {
var redirect = decodeURIComponent(urlParse(this.url, true).query[options.redirect] || '');
if (redirect[0] === '/') {
this.session._githubredirect = redirect;
debug('get github callback redirect uri: %s', redirect);
}
} catch (err) {
debug('decode redirect uri error');
}
debug('request github auth, redirect to %s', redirectUrl);
//if already signin
if (this.session[options.tokenKey]) {
debug('already has github token');
redirectUrl = this.session._githubredirect || '/';
delete this.session._githubredirect;
}
return this.redirect(redirectUrl);
}
// secound step: github callback
if (this.path === options.callbackPath) {
//if already signin
if (this.session[options.tokenKey]) {
debug('already has github token');
return this.redirect('/');
}
debug('after auth, jump from github.');
var url = urlParse(this.request.url, true);
// must have code
if (!url.query.code || !url.query.state) {
debug('request url need `code` and `state`');
return this.throw(400);
}
// check the state, protect against cross-site request forgery attacks
if (url.query.state !== this.session._githubstate) {
debug('request state is %s, but the state in session is %s',
url.query.state, this.session._githubstate);
delete this.session._githubstate;
return this.throw(403);
}
//step three: request to get the access token
var tokenUrl = 'https://github.com/login/oauth/access_token';
var requsetOptions = {
data: {
client_id: options.clientID,
client_secret: options.clientSecret,
code: url.query.code
}
};
debug('request the access token with data: %j', requsetOptions.data);
var token;
try {
var result = yield urllib.request(tokenUrl, requsetOptions);
assert.equal(result[1].statusCode, 200,
'response status ' + result[1].statusCode + ' not match 200');
token = qsParse(result[0].toString()).access_token;
assert(token, 'response without access_token');
} catch (err) {
return this.throw('request github token error: ' + err.message, 500);
}
this.session[options.tokenKey] = token;
debug('get access_token %s and store in session.%s', token, options.tokenKey);
delete this.session._githubstate;
//step four: if set userKey, get user
if (options.userKey) {
var result;
try {
var userUrl = 'https://api.github.com/user';
var authOptions = {
headers: {
Authorization: 'token ' + token,
'user-agent': 'koa-github'
},
dataType: 'json'
};
result = yield urllib.request(userUrl, authOptions);
assert.equal(result[1].statusCode, 200,
'response status ' + result[1].statusCode + ' not match 200');
assert(result[0], 'response without user info');
} catch (err) {
return this.throw('request github user info error: ' + err.message, 500);
}
debug('get user info %j and store in session.%s', result[0], options.userKey);
this.session[options.userKey] = result[0];
}
var githubredirect = this.session._githubredirect || '/';
delete this.session._githubredirect;
return this.redirect(githubredirect);
}
yield next;
};
};