]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/shared/accounts.ts
Don't inject untrusted input
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / shared / accounts.ts
CommitLineData
3e753302 1import { Response } from 'express'
10363c74 2import { AccountModel } from '@server/models/account/account'
7d9ba5c0 3import { UserModel } from '@server/models/user/user'
10363c74 4import { MAccountDefault } from '@server/types/models'
4638cd71 5import { forceNumber } from '@shared/core-utils'
c0e8b12e 6import { HttpStatusCode } from '@shared/models'
3e753302 7
57f6896f 8function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
4638cd71 9 const promise = AccountModel.load(forceNumber(id))
3e753302
C
10
11 return doesAccountExist(promise, res, sendNotFound)
12}
13
14function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
15 const promise = AccountModel.loadLocalByName(name)
16
17 return doesAccountExist(promise, res, sendNotFound)
18}
19
20function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
453e83ea
C
21 const promise = AccountModel.loadByNameWithHost(nameWithDomain)
22
23 return doesAccountExist(promise, res, sendNotFound)
3e753302
C
24}
25
b49f22d8 26async function doesAccountExist (p: Promise<MAccountDefault>, res: Response, sendNotFound: boolean) {
3e753302
C
27 const account = await p
28
29 if (!account) {
30 if (sendNotFound === true) {
76148b27
RK
31 res.fail({
32 status: HttpStatusCode.NOT_FOUND_404,
33 message: 'Account not found'
34 })
3e753302 35 }
3e753302
C
36 return false
37 }
38
39 res.locals.account = account
3e753302
C
40 return true
41}
42
18490b07 43async function doesUserFeedTokenCorrespond (id: number, token: string, res: Response) {
4638cd71 44 const user = await UserModel.loadByIdWithChannels(forceNumber(id))
afff310e
RK
45
46 if (token !== user.feedToken) {
76148b27
RK
47 res.fail({
48 status: HttpStatusCode.FORBIDDEN_403,
49 message: 'User and token mismatch'
50 })
afff310e
RK
51 return false
52 }
53
54 res.locals.user = user
afff310e
RK
55 return true
56}
57
3e753302
C
58// ---------------------------------------------------------------------------
59
60export {
61 doesAccountIdExist,
62 doesLocalAccountNameExist,
63 doesAccountNameWithHostExist,
afff310e
RK
64 doesAccountExist,
65 doesUserFeedTokenCorrespond
3e753302 66}