X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fmiddlewares%2Faccounts.ts;h=e9b98126273bd6250a9af6a60acfd8faeffe1938;hb=18490b07650d77d7fe376970b749af5a8c672fd6;hp=f5aa0badad321601f5e8f10579e2008a1add64fb;hpb=453e83ea5d81d203ba34bc43cd5c2c750ba40568;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/middlewares/accounts.ts b/server/helpers/middlewares/accounts.ts index f5aa0bada..e9b981262 100644 --- a/server/helpers/middlewares/accounts.ts +++ b/server/helpers/middlewares/accounts.ts @@ -1,10 +1,11 @@ import { Response } from 'express' import { AccountModel } from '../../models/account/account' import * as Bluebird from 'bluebird' -import { MAccountDefault } from '../../typings/models' +import { MAccountDefault } from '../../types/models' +import { UserModel } from '@server/models/account/user' -function doesAccountIdExist (id: number, res: Response, sendNotFound = true) { - const promise = AccountModel.load(id) +function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) { + const promise = AccountModel.load(parseInt(id + '', 10)) return doesAccountExist(promise, res, sendNotFound) } @@ -27,8 +28,7 @@ async function doesAccountExist (p: Bluebird, res: Response, se if (!account) { if (sendNotFound === true) { res.status(404) - .send({ error: 'Account not found' }) - .end() + .json({ error: 'Account not found' }) } return false @@ -39,11 +39,27 @@ async function doesAccountExist (p: Bluebird, res: Response, se return true } +async function doesUserFeedTokenCorrespond (id: number, token: string, res: Response) { + const user = await UserModel.loadByIdWithChannels(parseInt(id + '', 10)) + + if (token !== user.feedToken) { + res.status(403) + .json({ error: 'User and token mismatch' }) + + return false + } + + res.locals.user = user + + return true +} + // --------------------------------------------------------------------------- export { doesAccountIdExist, doesLocalAccountNameExist, doesAccountNameWithHostExist, - doesAccountExist + doesAccountExist, + doesUserFeedTokenCorrespond }