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