]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/webfinger.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / webfinger.ts
CommitLineData
350e31d6 1import * as express from 'express'
c8861d5d 2import { query } from 'express-validator'
06a05d5f 3import { isWebfingerLocalResourceValid } from '../../helpers/custom-validators/webfinger'
da854ddd 4import { logger } from '../../helpers/logger'
50d6de9c 5import { ActorModel } from '../../models/activitypub/actor'
a2431b7d 6import { areValidationErrors } from './utils'
0626e7af 7import { getHostWithPort } from '../../helpers/express-utils'
350e31d6
C
8
9const webfingerValidator = [
06a05d5f 10 query('resource').custom(isWebfingerLocalResourceValid).withMessage('Should have a valid webfinger resource'),
350e31d6 11
a2431b7d 12 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
350e31d6
C
13 logger.debug('Checking webfinger parameters', { parameters: req.query })
14
a2431b7d
C
15 if (areValidationErrors(req, res)) return
16
17 // Remove 'acct:' from the beginning of the string
0405ab52 18 const nameWithHost = getHostWithPort(req.query.resource.substr(5))
a2431b7d
C
19 const [ name ] = nameWithHost.split('@')
20
0374b6b5 21 const actor = await ActorModel.loadLocalUrlByName(name)
50d6de9c 22 if (!actor) {
a2431b7d 23 return res.status(404)
50d6de9c 24 .send({ error: 'Actor not found' })
a2431b7d
C
25 .end()
26 }
27
0374b6b5 28 res.locals.actorUrl = actor
a2431b7d 29 return next()
350e31d6
C
30 }
31]
32
33// ---------------------------------------------------------------------------
34
35export {
36 webfingerValidator
37}