@shun-js/webcc-server 0.6.7 → 0.6.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shun-js/webcc-server",
3
- "version": "0.6.7",
3
+ "version": "0.6.9",
4
4
  "description": "webcc.dev server",
5
5
  "keywords": [
6
6
  "web claude code"
@@ -23,7 +23,8 @@
23
23
  "dependencies": {
24
24
  "@shun-js/shun-config": "^0.3.1",
25
25
  "@shun-js/shun-service": "^0.3.1",
26
- "qiao-file": "^5.0.6",
26
+ "qiao-ajax": "^5.2.4",
27
+ "qiao-encode": "^5.0.6",
27
28
  "qiao-log": "^5.1.9",
28
29
  "qiao-z": "^5.8.9"
29
30
  },
@@ -31,5 +32,5 @@
31
32
  "access": "public",
32
33
  "registry": "https://registry.npmjs.org/"
33
34
  },
34
- "gitHead": "a43c95c66e0f7dc0a8cfed413baf2ccc5699c373"
35
+ "gitHead": "2e16481e703d73845eac724b043e641a56346041"
35
36
  }
@@ -10,6 +10,11 @@ module.exports = (app) => {
10
10
  service.index(req, res);
11
11
  });
12
12
 
13
+ // github auth
14
+ app.get('/github/auth', (req, res) => {
15
+ service.githubAuth(req, res);
16
+ });
17
+
13
18
  // github callback
14
19
  app.get('/github/callback', (req, res) => {
15
20
  service.githubCallback(req, res);
@@ -1,5 +1,5 @@
1
1
  // github
2
- const { getGitHubAuthUrl } = require('../util/github.js');
2
+ const { getGitHubAuthUrl, getGithubUserinfo } = require('../util/github.js');
3
3
 
4
4
  /**
5
5
  * index
@@ -7,19 +7,68 @@ const { getGitHubAuthUrl } = require('../util/github.js');
7
7
  * @param {*} res
8
8
  */
9
9
  exports.index = async (req, res) => {
10
- const url = getGitHubAuthUrl('1');
11
- console.log(url);
12
10
  res.send('1');
13
11
  };
14
12
 
13
+ /**
14
+ * githubAuth
15
+ * @param {*} req
16
+ * @param {*} res
17
+ */
18
+ exports.githubAuth = async (req, res) => {
19
+ // auth
20
+ const authObj = getGitHubAuthUrl();
21
+
22
+ // set cookie
23
+ res.setCookie('state', authObj.state);
24
+
25
+ // redirect
26
+ res.redirect(authObj.finalUrl);
27
+ };
28
+
15
29
  /**
16
30
  * githubCallback
17
31
  * @param {*} req
18
32
  * @param {*} res
19
33
  */
20
34
  exports.githubCallback = async (req, res) => {
21
- console.log(req.params);
22
- console.log(req.body);
23
- console.log(req.query);
35
+ const methodName = 'githubCallback';
36
+
37
+ // fallback url
38
+ const fallbackUrl = global.QZ_CONFIG.github.fallbackUrl;
39
+
40
+ // check
41
+ if (!req.cookies) {
42
+ req.logger.error(methodName, 'req.cookies is null');
43
+ res.redirect(fallbackUrl);
44
+ return;
45
+ }
46
+ if (!req.query) {
47
+ req.logger.error(methodName, 'req.query is null');
48
+ res.redirect(fallbackUrl);
49
+ return;
50
+ }
51
+
52
+ // check state
53
+ const cookieState = req.cookies.state;
54
+ const queryState = req.query.state;
55
+ if (cookieState !== queryState) {
56
+ req.logger.info(methodName, 'cookieState', cookieState);
57
+ req.logger.info(methodName, 'queryState', queryState);
58
+ req.logger.error(methodName, 'cookieState !== queryState');
59
+ res.redirect(fallbackUrl);
60
+ return;
61
+ }
62
+
63
+ // check code
64
+ const queryCode = req.query.code;
65
+ if (!queryCode) {
66
+ req.logger.error(methodName, 'queryCode is null');
67
+ res.redirect(fallbackUrl);
68
+ return;
69
+ }
70
+
71
+ // userinfo
72
+ await getGithubUserinfo(queryCode);
24
73
  res.send('1');
25
74
  };
@@ -1,15 +1,50 @@
1
+ // encode
2
+ const { uuid } = require('qiao-encode');
3
+
4
+ // ajax
5
+ const { get } = require('qiao-ajax');
6
+
1
7
  /**
2
8
  * getGitHubAuthUrl
3
- * @param {*} state
4
9
  * @returns
5
10
  */
6
- exports.getGitHubAuthUrl = (state) => {
11
+ exports.getGitHubAuthUrl = () => {
12
+ const state = uuid();
7
13
  const params = new URLSearchParams({
8
14
  client_id: global.QZ_CONFIG.github.clientID,
9
15
  redirect_uri: global.QZ_CONFIG.github.callbackUrl,
10
16
  scope: global.QZ_CONFIG.github.scope,
11
17
  state: state,
12
18
  });
19
+ const finalUrl = `${global.QZ_CONFIG.github.authUrl}?${params.toString()}`;
20
+
21
+ // r
22
+ return { state, finalUrl };
23
+ };
13
24
 
14
- return `${global.QZ_CONFIG.github.authUrl}?${params.toString()}`;
25
+ /**
26
+ * getGithubUserinfo
27
+ * @param {*} code
28
+ */
29
+ exports.getGithubUserinfo = async (code) => {
30
+ try {
31
+ // get token
32
+ const tokenUrl = global.QZ_CONFIG.github.tokenUrl;
33
+ const tokenConfig = {
34
+ params: {
35
+ client_id: global.QZ_CONFIG.github.clientID,
36
+ client_secret: global.QZ_CONFIG.github.clientSecret,
37
+ redirect_uri: global.QZ_CONFIG.github.callbackUrl,
38
+ code: code,
39
+ },
40
+ headers: {
41
+ Accept: 'application/json',
42
+ },
43
+ };
44
+ console.log(tokenConfig);
45
+ const tokenResponse = await get(tokenUrl, tokenConfig);
46
+ console.log(tokenResponse);
47
+ } catch (error) {
48
+ console.log(error);
49
+ }
15
50
  };