X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fmiddlewares%2Faccounts.ts;h=e9b98126273bd6250a9af6a60acfd8faeffe1938;hb=18490b07650d77d7fe376970b749af5a8c672fd6;hp=29b4ed1a6f56c737210f85817f5c6b97575df069;hpb=57f6896f67cfc570cf3605dd94b0778101b2d9b9;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/middlewares/accounts.ts b/server/helpers/middlewares/accounts.ts index 29b4ed1a6..e9b981262 100644 --- a/server/helpers/middlewares/accounts.ts +++ b/server/helpers/middlewares/accounts.ts @@ -2,6 +2,7 @@ import { Response } from 'express' import { AccountModel } from '../../models/account/account' import * as Bluebird from 'bluebird' import { MAccountDefault } from '../../types/models' +import { UserModel } from '@server/models/account/user' function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) { const promise = AccountModel.load(parseInt(id + '', 10)) @@ -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 }