|
1 | 1 | const router = require("koa-router")(); |
2 | | -const fetch = require("node-fetch"); |
3 | | -const crypto = require("crypto"); |
4 | | - |
5 | | -const secret = process.env.secret; |
6 | | - |
7 | | -const clientId = "c16b80e7b58a5a007157"; |
8 | | -const algorithm = "aes-256-ctr"; |
9 | | -const iv = crypto.randomBytes(16); |
10 | | - |
11 | | -const db = [ |
12 | | - { |
13 | | - login: "azl397985856", |
14 | | - }, |
15 | | -]; |
16 | | - |
17 | | -function encrypt(text) { |
18 | | - const cipher = crypto.createCipheriv(algorithm, secret.slice(0, 32), iv); |
19 | | - |
20 | | - const encrypted = Buffer.concat([cipher.update(text), cipher.final()]); |
21 | | - |
22 | | - return encrypted.toString("hex"); |
23 | | -} |
24 | | - |
25 | | -function decrypt(content) { |
26 | | - const decipher = crypto.createDecipheriv(algorithm, secret.slice(0, 32), iv); |
27 | | - |
28 | | - const decrpyted = Buffer.concat([ |
29 | | - decipher.update(Buffer.from(content, "hex")), |
30 | | - decipher.final(), |
31 | | - ]); |
32 | | - |
33 | | - return decrpyted.toString(); |
34 | | -} |
35 | 2 |
|
36 | 3 | router.get("/api/v1/user", async (ctx) => { |
37 | | - const token = ctx.cookies.get("token"); |
38 | | - |
39 | | - if (token) { |
40 | | - const duserStr = decrypt(token); |
41 | | - if (duserStr) { |
42 | | - try { |
43 | | - const duser = JSON.parse(duserStr); |
44 | | - |
45 | | - if (db.find((q) => q.login === duser.login)) { |
46 | | - ctx.body = duser; |
47 | | - return; |
48 | | - } |
49 | | - } catch (err) { |
50 | | - console.log("token 解析失败:", err); |
51 | | - } |
52 | | - } |
53 | | - } |
54 | | - const code = ctx.query.code; |
55 | | - const { access_token } = await fetch( |
56 | | - `https://github.com/login/oauth/access_token?code=${code}&client_id=${clientId}&client_secret=${secret}`, |
57 | | - { |
58 | | - method: "POST", |
59 | | - headers: { |
60 | | - Accept: "application/json", |
61 | | - }, |
62 | | - } |
63 | | - ).then((res) => res.json()); |
64 | | - |
65 | | - const user = await fetch("https://api.github.com/user", { |
66 | | - headers: { |
67 | | - Accept: "application/json", |
68 | | - Authorization: `token ${access_token}`, |
69 | | - }, |
70 | | - }).then((res) => res.json()); |
71 | | - |
72 | | - if (db.find((q) => q.login === user.login)) { |
73 | | - ctx.cookies.set( |
74 | | - "token", |
75 | | - encrypt( |
76 | | - Buffer.from( |
77 | | - JSON.stringify({ |
78 | | - ...user, |
79 | | - pay: true, |
80 | | - }), |
81 | | - "utf8" |
82 | | - ) |
83 | | - ), |
84 | | - { |
85 | | - httpOnly: false, |
86 | | - expires: new Date(24 * 60 * 60 * 1000 + Date.now()), |
87 | | - } |
88 | | - ); |
89 | | - ctx.body = { |
90 | | - ...user, |
91 | | - pay: true, |
92 | | - }; |
| 4 | + if (ctx.session && ctx.session.user) { |
| 5 | + ctx.body = ctx.session.body; |
93 | 6 | } else { |
94 | 7 | ctx.body = { |
95 | | - ...user, |
96 | | - pay: false, |
| 8 | + success: false, |
| 9 | + code: 91, |
| 10 | + data: null, |
| 11 | + message: "您还没有登录,请先登录~", |
97 | 12 | }; |
98 | 13 | } |
99 | 14 | }); |
|
0 commit comments