This repository was archived by the owner on Nov 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
187 lines (182 loc) · 6.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const Koa = require("koa")
const Router = require("koa-router")
const rp = require("request-promise")
const crypto = require("crypto")
const uuid = require("uuid")
const knex = require("knex")({
client: "sqlite3",
connection: {
filename: "./development.db",
timezone: "UTC"
},
acquireConnectionTimeout: 1000,
useNullAsDefault: true
})
const app = new Koa()
const router = new Router({prefix: "/api/v1"})
const secretKey = crypto.randomBytes(32).hexSlice()
// Authorization control (generate UUID using OAuth response)
router
.get("/auth", async (ctx, next) => {
if (!!ctx.query.callback) {
const token = crypto.randomBytes(32).hexSlice()
ctx.cookies
.set("callback", ctx.query.callback, {expires: new Date(Date.now() + 300000)})
.set("token", crypto.createHmac("sha256", secretKey).update(token).digest("hex"), {expires: new Date(Date.now() + 300000)})
ctx.redirect(`https://qiita.com/api/v2/oauth/authorize?client_id=${process.env.client_id}&scope=read_qiita&state=${token}`)
} else {
throw new Error() // TODO: Response HTTP response error
}
})
.get("/auth/callback", async (ctx, next) => {
if (!!ctx.cookies.get("token") && !!ctx.cookies.get("callback") && !!ctx.query.code && !!ctx.query.state) {
if (crypto.createHmac("sha256", secretKey).update(ctx.query.state).digest("hex") !== ctx.cookies.get("token")) {
throw new Error() // TODO: Response HTTP response error
}
ctx.cookies.set("token")
await new Promise(resolve => setTimeout(resolve, 1000))
await rp({
method: "POST",
uri: "https://qiita.com/api/v2/access_tokens",
body: {
client_id: process.env.client_id,
client_secret: process.env.client_secret,
code: ctx.query.code
},
json: true
})
.then(auth => {
if (auth.client_id !== process.env.client_id) {
throw new Error() // TODO: Response HTTP response error
}
return Promise.all([
auth.token,
rp({
uri: "https://qiita.com/api/v2/authenticated_user",
headers: {
"Authorization": `Bearer ${auth.token}`
}
})
])
})
.then(([token, user]) => Promise.all([
user,
rp({
method: "DELETE",
uri: `https://qiita.com/api/v2/access_tokens/${token}`
})
])
)
.then(([user]) => {
const token = uuid.v1()
return Promise.all([
token,
knex("users").insert({name: user.id, token, source: "qiita" })
])
})
.then(([token]) => {
ctx.redirect(`${ctx.cookies.get("callback")}?token=${token}`)
ctx.cookies.set("callback")
})
.catch(err => {
console.error(err)
throw err
})
await next()
} else {
throw new Error() // TODO: Response HTTP response error
}
})
// Authentication
const checkAuth = async (ctx, next) => {
// Token should be "Authorization: Bearer <UUID>"
const auth = ctx.header.authorization
if (!auth) {
throw new Error() // TODO: Return HTTP error status
}
const token = auth.split(" ").pop()
// Authentication (token to user)
await knex.first("name").where("token", token).from("users")
.then(username => {
if (username === undefined) {
throw new Error() // TODO: Return HTTP error status
}
ctx.user = username.name
})
await next()
}
// Dislike API
router
.get("/:username/items/:id", checkAuth, async (ctx, next) => {
// Get disliked status and dislike count from DB
await knex.select("by_whom").where({id: ctx.params.id, state: true}).from("item_dislike")
.then(users => {
ctx.body = {
disliked: users.map(_=>_.by_whom).includes(ctx.user),
count: users.length
}
})
})
.post("/:username/items/:id", checkAuth, async (ctx, next) => {
// Set disliked status and get new dislike count
const {id, username} = ctx.params
await knex.first("state").where({id, by_whom: ctx.user}).from("item_dislike")
.then(disliked => {
if (disliked === undefined) {
return knex.transaction((trx) => knex("item_dislike")
.transacting(trx)
.insert({id, username, by_whom: ctx.user, state: true})
.then(trx.commit)
.catch(e => {
trx.rollback()
throw e
}))
}else if (!disliked.state) {
return knex.transaction((trx) => knex("item_dislike")
.transacting(trx)
.where({id, by_whom: ctx.user})
.update({state: true})
.then(trx.commit)
.catch(e => {
trx.rollback()
throw e
}))
} else {
throw new Error() // TODO: Response HTTP response error
}
})
.then(status => {
ctx.body = {complete: true}
})
})
.delete("/:username/items/:id", checkAuth, async (ctx, next) => {
// Unset disliked status and get new dislike count
const {id, username} = ctx.params
await knex.first("state").where({id, by_whom: ctx.user}).from("item_dislike")
.then(disliked => {
if (disliked === undefined || !disliked.state) {
throw new Error() // TODO: Response HTTP response error
}
})
.then(knex.transaction((trx) => knex("item_dislike")
.transacting(trx)
.where({id, username, by_whom: ctx.user})
.update({state: false})
.then(trx.commit)
.catch((e) => {
trx.rollback()
throw e
})
))
.then(status => {
ctx.body = {complete: true}
})
})
app
.use(async (ctx, next) => {
ctx.req.setTimeout(10000)
await next()
})
.use(router.routes())
.use(router.allowedMethods())
.listen(3000)