@shun-js/webcc-server 0.6.7 → 0.6.8

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.8",
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": "b8d8674e7676cd3d152db0bd6e35c706e87d4c00"
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,69 @@ 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
+ const fallbackUrl = 'http://localhost:7008';
40
+
41
+ // check
42
+ if (!req.cookies) {
43
+ req.logger.error(methodName, 'req.cookies is null');
44
+ res.redirect(fallbackUrl);
45
+ return;
46
+ }
47
+ if (!req.query) {
48
+ req.logger.error(methodName, 'req.query is null');
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
+ // userinfo
73
+ await getGithubUserinfo(queryCode);
24
74
  res.send('1');
25
75
  };
@@ -1,15 +1,52 @@
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
- redirect_uri: global.QZ_CONFIG.github.callbackUrl,
15
+ // redirect_uri: global.QZ_CONFIG.github.callbackUrl,
16
+ redirect_uri: 'http://localhost:7008/github/callback',
10
17
  scope: global.QZ_CONFIG.github.scope,
11
18
  state: state,
12
19
  });
20
+ const finalUrl = `${global.QZ_CONFIG.github.authUrl}?${params.toString()}`;
21
+
22
+ // r
23
+ return { state, finalUrl };
24
+ };
13
25
 
14
- return `${global.QZ_CONFIG.github.authUrl}?${params.toString()}`;
26
+ /**
27
+ * getGithubUserinfo
28
+ * @param {*} code
29
+ */
30
+ exports.getGithubUserinfo = async (code) => {
31
+ try {
32
+ // get token
33
+ const tokenUrl = global.QZ_CONFIG.github.tokenUrl;
34
+ const tokenConfig = {
35
+ params: {
36
+ client_id: global.QZ_CONFIG.github.clientID,
37
+ client_secret: global.QZ_CONFIG.github.clientSecret,
38
+ // redirect_uri: global.QZ_CONFIG.github.callbackUrl,
39
+ redirect_uri: 'http://localhost:7008/github/callback',
40
+ code: code,
41
+ },
42
+ headers: {
43
+ Accept: 'application/json',
44
+ },
45
+ };
46
+ console.log(tokenConfig);
47
+ const tokenResponse = await get(tokenUrl, tokenConfig);
48
+ console.log(tokenResponse);
49
+ } catch (error) {
50
+ console.log(error);
51
+ }
15
52
  };