]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/middlewares/accounts.ts
allow private syndication feeds via a user feedToken
[github/Chocobozzz/PeerTube.git] / server / helpers / middlewares / accounts.ts
CommitLineData
3e753302
C
1import { Response } from 'express'
2import { AccountModel } from '../../models/account/account'
3import * as Bluebird from 'bluebird'
26d6bf65 4import { MAccountDefault } from '../../types/models'
afff310e 5import { UserModel } from '@server/models/account/user'
3e753302 6
57f6896f
C
7function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
8 const promise = AccountModel.load(parseInt(id + '', 10))
3e753302
C
9
10 return doesAccountExist(promise, res, sendNotFound)
11}
12
13function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
14 const promise = AccountModel.loadLocalByName(name)
15
16 return doesAccountExist(promise, res, sendNotFound)
17}
18
19function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
453e83ea
C
20 const promise = AccountModel.loadByNameWithHost(nameWithDomain)
21
22 return doesAccountExist(promise, res, sendNotFound)
3e753302
C
23}
24
453e83ea 25async function doesAccountExist (p: Bluebird<MAccountDefault>, res: Response, sendNotFound: boolean) {
3e753302
C
26 const account = await p
27
28 if (!account) {
29 if (sendNotFound === true) {
30 res.status(404)
31 .send({ error: 'Account not found' })
32 .end()
33 }
34
35 return false
36 }
37
38 res.locals.account = account
39
40 return true
41}
42
afff310e
RK
43async function doesUserFeedTokenCorrespond (id: number | string, token: string, res: Response) {
44 const user = await UserModel.loadById(parseInt(id + '', 10))
45
46 if (token !== user.feedToken) {
47 res.status(401)
48 .send({ error: 'User and token mismatch' })
49 .end()
50
51 return false
52 }
53
54 res.locals.user = user
55
56 return true
57}
58
3e753302
C
59// ---------------------------------------------------------------------------
60
61export {
62 doesAccountIdExist,
63 doesLocalAccountNameExist,
64 doesAccountNameWithHostExist,
afff310e
RK
65 doesAccountExist,
66 doesUserFeedTokenCorrespond
3e753302 67}