]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/webfinger.ts
Add ability for admins to set default p2p policy
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / webfinger.ts
CommitLineData
41fb13c3 1import express from 'express'
c8861d5d 2import { query } from 'express-validator'
c0e8b12e 3import { HttpStatusCode } from '../../../shared/models/http/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'
10363c74 8import { areValidationErrors } from './shared'
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) {
76148b27
RK
24 return res.fail({
25 status: HttpStatusCode.NOT_FOUND_404,
26 message: 'Actor not found'
27 })
a2431b7d
C
28 }
29
0374b6b5 30 res.locals.actorUrl = actor
a2431b7d 31 return next()
350e31d6
C
32 }
33]
34
35// ---------------------------------------------------------------------------
36
37export {
38 webfingerValidator
39}