Set port if not specified in webfinger
[oweals/peertube.git] / server / middlewares / validators / webfinger.ts
1 import * as express from 'express'
2 import { query } from 'express-validator/check'
3 import { getHostWithPort, logger } from '../../helpers'
4 import { isWebfingerResourceValid } from '../../helpers/custom-validators/webfinger'
5 import { ActorModel } from '../../models/activitypub/actor'
6 import { areValidationErrors } from './utils'
7
8 const webfingerValidator = [
9   query('resource').custom(isWebfingerResourceValid).withMessage('Should have a valid webfinger resource'),
10
11   async (req: express.Request, res: express.Response, next: express.NextFunction) => {
12     logger.debug('Checking webfinger parameters', { parameters: req.query })
13
14     if (areValidationErrors(req, res)) return
15
16     // Remove 'acct:' from the beginning of the string
17     const nameWithHost = getHostWithPort(req.query.resource.substr(5))
18     const [ name ] = nameWithHost.split('@')
19
20     const actor = await ActorModel.loadLocalByName(name)
21     if (!actor) {
22       return res.status(404)
23         .send({ error: 'Actor not found' })
24         .end()
25     }
26
27     res.locals.actor = actor
28     return next()
29   }
30 ]
31
32 // ---------------------------------------------------------------------------
33
34 export {
35   webfingerValidator
36 }