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