]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/webfinger.ts
Stronger model typings
[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
453e83ea 21 // FIXME: we don't need the full actor
50d6de9c
C
22 const actor = await ActorModel.loadLocalByName(name)
23 if (!actor) {
a2431b7d 24 return res.status(404)
50d6de9c 25 .send({ error: 'Actor not found' })
a2431b7d
C
26 .end()
27 }
28
453e83ea 29 res.locals.actorFull = actor
a2431b7d 30 return next()
350e31d6
C
31 }
32]
33
34// ---------------------------------------------------------------------------
35
36export {
37 webfingerValidator
38}