-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (65 loc) · 2.38 KB
/
server.js
File metadata and controls
71 lines (65 loc) · 2.38 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
63
64
65
66
67
68
69
70
71
const request = require('request');
const yapi = require('yapi.js');
module.exports = function (options) {
// Get access_token
const getAccessToken = async (oauthcode, clientId, secret, redirectUri) => new Promise((resolve, reject) => {
const tokenPath = 'https://www.googleapis.com/oauth2/v4/token'
const data = {
"code": oauthcode,
"grant_type": "authorization_code",
"client_id": clientId,
"client_secret": secret,
"redirect_uri": redirectUri
}
request.post(tokenPath, { json: data }, function (error, response, body) {
if (!error && response.statusCode == 200) {
resolve(body);
}
reject(error);
})
})
// Get user info
const getUserInfo = async (token) => new Promise((resolve, reject) => {
const userInfoUrl = 'https://www.googleapis.com/oauth2/v1/userinfo'
request(userInfoUrl + "?access_token=" + token.access_token, function (error, response, body) {
yapi.commons.log(body, "user info")
if (!error && response.statusCode == 200) {
let result = JSON.parse(body);
if (result) {
let ret = {
email: result.email,
username: result.name
};
resolve(ret);
} else {
reject(result);
}
}
reject(error);
});
});
// Google login: get userInfo by access_token
this.bindHook('third_login', async (ctx) => {
let oauthcode = ctx.request.query.code;
yapi.commons.log(oauthcode, "google login")
if (!oauthcode) {
throw new Error('google login: code不能为空');
}
let oauthstate = ctx.request.query.state;
if (!oauthstate) {
throw new Error('google login: state不能为空');
}
const { clientId, secret, redirectUri } = options
const token = await getAccessToken(oauthcode, clientId, secret, redirectUri)
const userInfo = await getUserInfo(token)
if (options.allowedDomains) {
let domain = userInfo.email.split('@')[1];
yapi.commons.log('Email: ' + userInfo.email + ', domain: ' + domain, 'info');
if (options.allowedDomains.indexOf(domain) === -1) {
yapi.commons.log('Email: ' + userInfo.email + ', login failed because domain not allowed', 'error');
throw new Error('Email domain not allowed to login! Please using email with domain: ' + options.allowedDomains.join(', '));
}
}
return userInfo
});
};