qiao-z-nuser 6.0.0 → 6.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +16 -0
- package/package.json +3 -3
- package/src/controller/GoogleController.js +17 -0
- package/src/service/GoogleService.js +87 -0
- package/src/util/google.js +23 -0
package/index.js
CHANGED
|
@@ -6,6 +6,7 @@ const logger = Logger('qiao-z-nuser');
|
|
|
6
6
|
const userController = require('./src/controller/UserController.js');
|
|
7
7
|
const userInfoController = require('./src/controller/UserInfoController.js');
|
|
8
8
|
const githubController = require('./src/controller/GithubController.js');
|
|
9
|
+
const googleController = require('./src/controller/GoogleController.js');
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* init
|
|
@@ -51,3 +52,18 @@ exports.initGithub = function (app) {
|
|
|
51
52
|
// init controller
|
|
52
53
|
githubController(app);
|
|
53
54
|
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* initGoogle
|
|
58
|
+
* @param {*} app qz app
|
|
59
|
+
*/
|
|
60
|
+
exports.initGoogle = function (app) {
|
|
61
|
+
// check app
|
|
62
|
+
if (!app) {
|
|
63
|
+
logger.info('init', 'need app');
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// init controller
|
|
68
|
+
googleController(app);
|
|
69
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qiao-z-nuser",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.2",
|
|
4
4
|
"description": "qiao-z new user module",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"qiao-z",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
},
|
|
20
20
|
"scripts": {},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"qiao-z-service": "^6.0.
|
|
22
|
+
"qiao-z-service": "^6.0.1"
|
|
23
23
|
},
|
|
24
|
-
"gitHead": "
|
|
24
|
+
"gitHead": "90d0d8a0ac77969aa38f5d4518a1461a8d2611f4"
|
|
25
25
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// service
|
|
2
|
+
const service = require('../service/GoogleService.js');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* google controller
|
|
6
|
+
*/
|
|
7
|
+
module.exports = (app) => {
|
|
8
|
+
// /google/auth
|
|
9
|
+
app.get('/google/auth', (req, res) => {
|
|
10
|
+
service.googleAuth(req, res);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
// /google/callback
|
|
14
|
+
app.get('/google/callback', (req, res) => {
|
|
15
|
+
service.googleCallback(req, res);
|
|
16
|
+
});
|
|
17
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// qiao
|
|
2
|
+
const { userGoogle } = require('qiao-z-service');
|
|
3
|
+
|
|
4
|
+
// google
|
|
5
|
+
const { getGoogleAuthUrl } = require('../util/google.js');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* googleAuth
|
|
9
|
+
* @param {*} req
|
|
10
|
+
* @param {*} res
|
|
11
|
+
*/
|
|
12
|
+
exports.googleAuth = (req, res) => {
|
|
13
|
+
// auth
|
|
14
|
+
const authObj = getGoogleAuthUrl();
|
|
15
|
+
|
|
16
|
+
// set cookie
|
|
17
|
+
res.setCookie('state', authObj.state);
|
|
18
|
+
|
|
19
|
+
// redirect
|
|
20
|
+
res.redirect(authObj.finalUrl);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* googleCallback
|
|
25
|
+
* @param {*} req
|
|
26
|
+
* @param {*} res
|
|
27
|
+
*/
|
|
28
|
+
exports.googleCallback = async (req, res) => {
|
|
29
|
+
const methodName = 'googleCallback';
|
|
30
|
+
|
|
31
|
+
// fallback url
|
|
32
|
+
const fallbackUrl = global.QZ_CONFIG.google.fallbackUrl;
|
|
33
|
+
|
|
34
|
+
// check
|
|
35
|
+
if (!req.cookies) {
|
|
36
|
+
req.logger.error(methodName, 'req.cookies is null');
|
|
37
|
+
res.redirect(fallbackUrl);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (!req.query) {
|
|
41
|
+
req.logger.error(methodName, 'req.query is null');
|
|
42
|
+
res.redirect(fallbackUrl);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// check error
|
|
47
|
+
if (req.query.error) {
|
|
48
|
+
req.logger.error(methodName, 'google auth error', req.query.error);
|
|
49
|
+
res.redirect(fallbackUrl);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// check state
|
|
54
|
+
const cookieState = req.cookies.state;
|
|
55
|
+
const queryState = req.query.state;
|
|
56
|
+
if (cookieState !== queryState) {
|
|
57
|
+
req.logger.info(methodName, 'cookieState', cookieState);
|
|
58
|
+
req.logger.info(methodName, 'queryState', queryState);
|
|
59
|
+
req.logger.error(methodName, 'cookieState !== queryState');
|
|
60
|
+
res.redirect(fallbackUrl);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// check code
|
|
65
|
+
const queryCode = req.query.code;
|
|
66
|
+
if (!queryCode) {
|
|
67
|
+
req.logger.error(methodName, 'queryCode is null');
|
|
68
|
+
res.redirect(fallbackUrl);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// send
|
|
73
|
+
const json = await userGoogle({
|
|
74
|
+
url: global.QZ_CONFIG.user.url,
|
|
75
|
+
from: global.QZ_CONFIG.user.from,
|
|
76
|
+
code: queryCode,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// r
|
|
80
|
+
if (json.type === 'success') {
|
|
81
|
+
// params
|
|
82
|
+
const params = new URLSearchParams(json.obj);
|
|
83
|
+
res.redirect(`${fallbackUrl}?${params.toString()}`);
|
|
84
|
+
} else {
|
|
85
|
+
res.redirect(fallbackUrl);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// crypto
|
|
2
|
+
const crypto = require('crypto');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* getGoogleAuthUrl
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
exports.getGoogleAuthUrl = () => {
|
|
9
|
+
const state = crypto.randomBytes(16).toString('hex');
|
|
10
|
+
const params = new URLSearchParams({
|
|
11
|
+
client_id: global.QZ_CONFIG.google.clientID,
|
|
12
|
+
redirect_uri: global.QZ_CONFIG.google.callbackUrl,
|
|
13
|
+
response_type: 'code',
|
|
14
|
+
scope: 'openid email profile',
|
|
15
|
+
state: state,
|
|
16
|
+
access_type: 'offline',
|
|
17
|
+
prompt: 'consent',
|
|
18
|
+
});
|
|
19
|
+
const finalUrl = `${global.QZ_CONFIG.google.authUrl}?${params.toString()}`;
|
|
20
|
+
|
|
21
|
+
// r
|
|
22
|
+
return { state, finalUrl };
|
|
23
|
+
};
|