X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmiddlewares%2Fvalidators%2Fwebfinger.ts;h=5fe864f8b6a62233d0352be6c6a11e4fa41727d2;hb=444c0a0e017824fb4ce526281a22c4abe0a13c50;hp=7852c1c2b43de881de9b72f8f66dacef3943775b;hpb=8d468a16fd33ec2660c3c59b3f7def53eb0cc4a1;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/middlewares/validators/webfinger.ts b/server/middlewares/validators/webfinger.ts index 7852c1c2b..5fe864f8b 100644 --- a/server/middlewares/validators/webfinger.ts +++ b/server/middlewares/validators/webfinger.ts @@ -1,37 +1,32 @@ import * as express from 'express' -import { query } from 'express-validator/check' -import { isWebfingerResourceValid } from '../../helpers/custom-validators/webfinger' -import { database as db } from '../../initializers' -import { checkErrors } from './utils' +import { query } from 'express-validator' +import { isWebfingerLocalResourceValid } from '../../helpers/custom-validators/webfinger' import { logger } from '../../helpers/logger' +import { ActorModel } from '../../models/activitypub/actor' +import { areValidationErrors } from './utils' +import { getHostWithPort } from '../../helpers/express-utils' const webfingerValidator = [ - query('resource').custom(isWebfingerResourceValid).withMessage('Should have a valid webfinger resource'), + query('resource').custom(isWebfingerLocalResourceValid).withMessage('Should have a valid webfinger resource'), - (req: express.Request, res: express.Response, next: express.NextFunction) => { + async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking webfinger parameters', { parameters: req.query }) - checkErrors(req, res, () => { - // Remove 'acct:' from the beginning of the string - const nameWithHost = req.query.resource.substr(5) - const [ name ] = nameWithHost.split('@') - - db.Account.loadLocalByName(name) - .then(account => { - if (!account) { - return res.status(404) - .send({ error: 'Account not found' }) - .end() - } - - res.locals.account = account - return next() - }) - .catch(err => { - logger.error('Error in webfinger validator.', err) - return res.sendStatus(500) - }) - }) + if (areValidationErrors(req, res)) return + + // Remove 'acct:' from the beginning of the string + const nameWithHost = getHostWithPort(req.query.resource.substr(5)) + const [ name ] = nameWithHost.split('@') + + const actor = await ActorModel.loadLocalUrlByName(name) + if (!actor) { + return res.status(404) + .send({ error: 'Actor not found' }) + .end() + } + + res.locals.actorUrl = actor + return next() } ]