-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathserver.js
More file actions
62 lines (55 loc) · 2.09 KB
/
Copy pathserver.js
File metadata and controls
62 lines (55 loc) · 2.09 KB
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
const request = require('request');
const controller = require('./controller/oauth2Controller');
const gitlabController = require('./controller/gitlabController');
const yapi = require('yapi.js');
module.exports = function (options) {
const {emailPostfix, emailKey, userKey, host, loginPath, redirectUri} = options;
this.bindHook('third_login', (ctx) => {
let token = ctx.request.body.token || ctx.request.query.token;
return new Promise((resolve, reject) => {
request(host + loginPath + "?access_token=" + token, function (error, response, body) {
if (!error && response.statusCode == 200) {
let result = JSON.parse(body);
if (result) {
let ret = {
email: (emailKey != undefined && emailKey.length > 0) ? result[emailKey] : (result[userKey] + emailPostfix),
username: result[userKey]
};
resolve(ret);
}
}
reject({message: host + loginPath + ' with access_token can not login!'});
});
});
});
this.bindHook('add_router', function(addRouter) {
let path = redirectUri.split('/api/plugin/');
if (!path[1]) {
throw new Error("oauth redirectUri must like 'http://domain.com:port/api/plugin/xxxxx'");
}
addRouter({
controller: controller,
method: 'get',
path: path[1],
action: 'oauth2Callback'
});
addRouter({
controller: gitlabController,
method: 'post',
path: 'gitlab/project',
action: 'addProject'
});
addRouter({
controller: gitlabController,
method: 'post',
path: 'gitlab/asyncGroup',
action: 'asyncGroup'
});
addRouter({
controller: gitlabController,
method: 'post',
path: 'gitlab/asyncProject',
action: 'asyncProjectGroup'
});
});
}