Skip to content

Commit a2adbf1

Browse files
feat: make all API Requests require authorization (#686)
1 parent 3af7bd3 commit a2adbf1

File tree

9 files changed

+40
-4
lines changed

9 files changed

+40
-4
lines changed

pages/api/v2/bots/[id]/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ const patchLimiter = rateLimit({
3939
})
4040
const Bots = RequestHandler()
4141
.get(async (req: GetApiRequest, res) => {
42+
const auth = req.headers.authorization
43+
? await get.BotAuthorization(req.headers.authorization)
44+
: await get.Authorization(req.cookies.token)
45+
if (!auth) return ResponseWrapper(res, { code: 401 })
4246
const bot = await get.bot.load(req.query.id)
4347
if (!bot) return ResponseWrapper(res, { code: 404, message: '존재하지 않는 봇입니다.' })
4448
else {
@@ -200,7 +204,7 @@ const Bots = RequestHandler()
200204

201205
const isPerkAvailable =
202206
checkBotFlag(bot.flags, 'partnered') || checkBotFlag(bot.flags, 'trusted')
203-
207+
204208
const userInfo = await get.user.load(user)
205209
if (
206210
['reported', 'blocked', 'archived'].includes(bot.state) &&
@@ -219,7 +223,8 @@ const Bots = RequestHandler()
219223
const csrfValidated = checkToken(req, res, req.body._csrf)
220224
if (!csrfValidated) return
221225

222-
const validated: ManageBot = await getManageBotSchema(isPerkAvailable).validate(req.body, { abortEarly: false })
226+
const validated: ManageBot = await getManageBotSchema(isPerkAvailable)
227+
.validate(req.body, { abortEarly: false })
223228
.then((el) => el)
224229
.catch((e) => {
225230
ResponseWrapper(res, { code: 400, errors: e.errors })

pages/api/v2/list/bots/new.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import ResponseWrapper from '@utils/ResponseWrapper'
44

55
import { Bot, List } from '@types'
66

7-
const NewList = RequestHandler().get(async (_req, res) => {
7+
const NewList = RequestHandler().get(async (req, res) => {
8+
const auth = req.headers.authorization
9+
? await get.BotAuthorization(req.headers.authorization)
10+
: await get.Authorization(req.cookies.token)
11+
if (!auth) return ResponseWrapper(res, { code: 401 })
812
const result = await get.list.new.load(1)
913
return ResponseWrapper<List<Bot>>(res, { code: 200, data: result })
1014
})

pages/api/v2/list/bots/votes.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { Bot, List } from '@types'
66
import Yup from '@utils/Yup'
77

88
const VotesList = RequestHandler().get(async (req, res) => {
9+
const auth = req.headers.authorization
10+
? await get.BotAuthorization(req.headers.authorization)
11+
: await get.Authorization(req.cookies.token)
12+
if (!auth) return ResponseWrapper(res, { code: 401 })
913
const page = await Yup.number()
1014
.positive()
1115
.integer()

pages/api/v2/search/all.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import { SearchQuerySchema } from '@utils/Yup'
88
import { Bot, Server, List } from '@types'
99

1010
const Search = RequestHandler().get(async (req: ApiRequest, res) => {
11+
const auth = req.headers.authorization
12+
? await get.BotAuthorization(req.headers.authorization)
13+
: await get.Authorization(req.cookies.token)
14+
if (!auth) return ResponseWrapper(res, { code: 401 })
1115
const validated = await SearchQuerySchema.validate({ q: req.query.q || req.query.query, page: 1 })
1216
.then((el) => el)
1317
.catch((e) => {

pages/api/v2/search/bots.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import { SearchQuerySchema } from '@utils/Yup'
88
import { Bot, List } from '@types'
99

1010
const SearchBots = RequestHandler().get(async (req: ApiRequest, res: NextApiResponse) => {
11+
const auth = req.headers.authorization
12+
? await get.BotAuthorization(req.headers.authorization)
13+
: await get.Authorization(req.cookies.token)
14+
if (!auth) return ResponseWrapper(res, { code: 401 })
1115
const validated = await SearchQuerySchema.validate({
1216
q: req.query.q || req.query.query,
1317
page: req.query.page,

pages/api/v2/search/servers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import { SearchQuerySchema } from '@utils/Yup'
88
import { Server, List } from '@types'
99

1010
const SearchServers = RequestHandler().get(async (req: ApiRequest, res: NextApiResponse) => {
11+
const auth = req.headers.authorization
12+
? await get.BotAuthorization(req.headers.authorization)
13+
: await get.Authorization(req.cookies.token)
14+
if (!auth) return ResponseWrapper(res, { code: 401 })
1115
const validated = await SearchQuerySchema.validate({
1216
q: req.query.q || req.query.query,
1317
page: req.query.page,

pages/api/v2/servers/[id]/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ const patchLimiter = rateLimit({
3636
})
3737
const Servers = RequestHandler()
3838
.get(async (req: GetApiRequest, res) => {
39+
const auth = req.headers.authorization
40+
? await get.BotAuthorization(req.headers.authorization)
41+
: await get.Authorization(req.cookies.token)
42+
if (!auth) return ResponseWrapper(res, { code: 401 })
3943
const server = await get.server.load(req.query.id)
4044
if (!server) return ResponseWrapper(res, { code: 404, message: '존재하지 않는 서버 입니다.' })
4145
else {

pages/api/v2/servers/[id]/owners.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import ResponseWrapper from '@utils/ResponseWrapper'
55
import { get } from '@utils/Query'
66

77
const ServerOwners = RequestHandler().get(async (req: GetApiRequest, res) => {
8+
const auth = req.headers.authorization
9+
? await get.BotAuthorization(req.headers.authorization)
10+
: await get.Authorization(req.cookies.token)
11+
if (!auth) return ResponseWrapper(res, { code: 401 })
812
const owners = await get.serverOwners(req.query.id)
913
if (!owners) return ResponseWrapper(res, { code: 404 })
1014
return ResponseWrapper(res, { code: 200, data: owners })

pages/api/v2/users/[id]/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import ResponseWrapper from '@utils/ResponseWrapper'
55
import RequestHandler from '@utils/RequestHandler'
66

77
const Users = RequestHandler().get(async (req: ApiRequest, res) => {
8-
console.log(req.query)
8+
const auth = req.headers.authorization
9+
? await get.BotAuthorization(req.headers.authorization)
10+
: await get.Authorization(req.cookies.token)
11+
if (!auth) return ResponseWrapper(res, { code: 401 })
912
const user = await get.user.load(req.query?.id)
1013
if (!user) return ResponseWrapper(res, { code: 404, message: '존재하지 않는 유저 입니다.' })
1114
else return ResponseWrapper(res, { code: 200, data: user })

0 commit comments

Comments
 (0)