Skip to content

Commit cb552ce

Browse files
author
lucifer
committed
feat: 基础篇讲义
1 parent 913e057 commit cb552ce

File tree

8 files changed

+164
-92
lines changed

8 files changed

+164
-92
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11

2-
/node_modules
2+
/node_modules
3+
/static/*/*.md

app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ const onerror = require("koa-onerror");
77
const bodyparser = require("koa-bodyparser");
88
const logger = require("koa-logger");
99

10+
const passport = require("./middleware/passport");
1011
const index = require("./routes/index");
1112
const users = require("./routes/users");
13+
const lectures = require("./routes/lectures");
1214

1315
// error handler
1416
onerror(app);
@@ -22,6 +24,7 @@ app.use(
2224
app.use(cors({ credentials: true }));
2325
app.use(json());
2426
app.use(logger());
27+
app.use(passport);
2528
app.use(require("koa-static")(__dirname + "/public"));
2629

2730
app.use(
@@ -41,6 +44,7 @@ app.use(async (ctx, next) => {
4144
// routes
4245
app.use(index.routes(), index.allowedMethods());
4346
app.use(users.routes(), users.allowedMethods());
47+
app.use(lectures.routes(), lectures.allowedMethods());
4448

4549
// error-handling
4650
app.on("error", (err, ctx) => {

middleware/passport.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const fetch = require("node-fetch");
2+
const { encrypt, decrypt } = require("../utils/crypto");
3+
4+
const secret = process.env.secret;
5+
6+
const clientId = "c16b80e7b58a5a007157";
7+
const db = [
8+
{
9+
login: "azl397985856",
10+
},
11+
];
12+
13+
module.exports = async function checkAuth(ctx, next) {
14+
const token = ctx.cookies.get("token");
15+
16+
if (token) {
17+
const duserStr = decrypt(token);
18+
if (duserStr) {
19+
try {
20+
const duser = JSON.parse(duserStr);
21+
22+
if (db.find((q) => q.login === duser.login)) {
23+
ctx.body = duser;
24+
return;
25+
}
26+
} catch (err) {
27+
console.log("token 解析失败:", err);
28+
}
29+
}
30+
}
31+
const code = ctx.query.code;
32+
const { access_token } = await fetch(
33+
`https://github.com/login/oauth/access_token?code=${code}&client_id=${clientId}&client_secret=${secret}`,
34+
{
35+
method: "POST",
36+
headers: {
37+
Accept: "application/json",
38+
},
39+
}
40+
).then((res) => res.json());
41+
42+
const user = await fetch("https://api.github.com/user", {
43+
headers: {
44+
Accept: "application/json",
45+
Authorization: `token ${access_token}`,
46+
},
47+
}).then((res) => res.json());
48+
49+
if (db.find((q) => q.login === user.login)) {
50+
ctx.cookies.set(
51+
"token",
52+
encrypt(
53+
Buffer.from(
54+
JSON.stringify({
55+
...user,
56+
pay: true,
57+
}),
58+
"utf8"
59+
)
60+
),
61+
{
62+
httpOnly: false,
63+
expires: new Date(24 * 60 * 60 * 1000 + Date.now()),
64+
}
65+
);
66+
if (!ctx.session) {
67+
ctx.session = {};
68+
}
69+
ctx.session.user = {
70+
...user,
71+
pay: true,
72+
};
73+
} else {
74+
ctx.session.user = {
75+
...user,
76+
pay: false,
77+
};
78+
}
79+
80+
next();
81+
};

routes/lectures.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const router = require("koa-router")();
2+
const lectures = require("../static/lectures/lectures.json");
3+
const { decrypt } = require("../utils/crypto");
4+
5+
router.get("/api/v1/lectures/basic", async (ctx) => {
6+
ctx.body = {
7+
success: true,
8+
code: 0,
9+
data: [
10+
{
11+
title: "01. 数组,栈,队列",
12+
desc: "主要介绍了数组,栈,队列基本原理,基础 API 以及其对应的复杂度",
13+
image: {
14+
url:
15+
"https://tva1.sinaimg.cn/large/007S8ZIlly1gfbikq9ipmj30cd0a73yp.jpg",
16+
alt: "栈",
17+
},
18+
id: 1,
19+
},
20+
],
21+
};
22+
});
23+
24+
router.get("/api/v1/lectures/basic/:id", async (ctx) => {
25+
const id = ctx.params.id;
26+
const lecture = lectures[id];
27+
28+
ctx.body = {
29+
...lecture,
30+
content: decrypt(lectures[id].content),
31+
};
32+
});
33+
34+
module.exports = router;

routes/users.js

Lines changed: 6 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,14 @@
11
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-
}
352

363
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;
936
} else {
947
ctx.body = {
95-
...user,
96-
pay: false,
8+
success: false,
9+
code: 91,
10+
data: null,
11+
message: "您还没有登录,请先登录~",
9712
};
9813
}
9914
});

static/lectures/basic.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const fs = require("fs");
2+
3+
const { encrypt } = require("../../utils/crypto.js");
4+
const t = require("../../static/lectures/lectures.json");
5+
const lectures = {};
6+
7+
lectures[1] = {
8+
id: 1,
9+
content: encrypt(Buffer.from(fs.readFileSync("./1.basic-01.md"), "utf8")),
10+
};
11+
12+
fs.writeFileSync("./lectures.json", JSON.stringify(lectures));

static/lectures/lectures.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

utils/crypto.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const crypto = require("crypto");
2+
3+
const algorithm = "aes-256-ctr";
4+
const iv = Buffer.alloc(16, "-");
5+
const secret = process.env.secret;
6+
module.exports = {
7+
encrypt(text) {
8+
const cipher = crypto.createCipheriv(algorithm, secret.slice(0, 32), iv);
9+
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
10+
return encrypted.toString("hex");
11+
},
12+
decrypt(content) {
13+
const decipher = crypto.createDecipheriv(
14+
algorithm,
15+
secret.slice(0, 32),
16+
iv
17+
);
18+
const decrpyted = Buffer.concat([
19+
decipher.update(Buffer.from(content, "hex")),
20+
decipher.final(),
21+
]);
22+
return decrpyted.toString();
23+
},
24+
};

0 commit comments

Comments
 (0)