PeerTube/server/middlewares/validators/webfinger.ts

38 lines
1.2 KiB
TypeScript
Raw Normal View History

2017-11-14 17:31:26 +01:00
import * as express from 'express'
2017-11-23 17:53:38 +01:00
import { query } from 'express-validator/check'
import { isWebfingerResourceValid } from '../../helpers/custom-validators/webfinger'
2017-12-28 11:16:08 +01:00
import { logger } from '../../helpers/logger'
2017-12-14 17:38:41 +01:00
import { ActorModel } from '../../models/activitypub/actor'
2017-11-27 17:30:46 +01:00
import { areValidationErrors } from './utils'
2018-04-24 15:10:54 +02:00
import { getHostWithPort } from '../../helpers/express-utils'
2017-11-14 17:31:26 +01:00
const webfingerValidator = [
query('resource').custom(isWebfingerResourceValid).withMessage('Should have a valid webfinger resource'),
2017-11-27 17:30:46 +01:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
2017-11-14 17:31:26 +01:00
logger.debug('Checking webfinger parameters', { parameters: req.query })
2017-11-27 17:30:46 +01:00
if (areValidationErrors(req, res)) return
// Remove 'acct:' from the beginning of the string
2017-12-19 14:21:14 +01:00
const nameWithHost = getHostWithPort(req.query.resource.substr(5))
2017-11-27 17:30:46 +01:00
const [ name ] = nameWithHost.split('@')
2017-12-14 17:38:41 +01:00
const actor = await ActorModel.loadLocalByName(name)
if (!actor) {
2017-11-27 17:30:46 +01:00
return res.status(404)
2017-12-14 17:38:41 +01:00
.send({ error: 'Actor not found' })
2017-11-27 17:30:46 +01:00
.end()
}
2017-12-14 17:38:41 +01:00
res.locals.actor = actor
2017-11-27 17:30:46 +01:00
return next()
2017-11-14 17:31:26 +01:00
}
]
// ---------------------------------------------------------------------------
export {
webfingerValidator
}