]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/webfinger.ts
Cleanup helpers
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / webfinger.ts
CommitLineData
350e31d6 1import * as express from 'express'
8d468a16
C
2import { query } from 'express-validator/check'
3import { isWebfingerResourceValid } from '../../helpers/custom-validators/webfinger'
350e31d6 4import { database as db } from '../../initializers'
8d468a16
C
5import { checkErrors } from './utils'
6import { logger } from '../../helpers/logger'
350e31d6
C
7
8const webfingerValidator = [
9 query('resource').custom(isWebfingerResourceValid).withMessage('Should have a valid webfinger resource'),
10
11 (req: express.Request, res: express.Response, next: express.NextFunction) => {
12 logger.debug('Checking webfinger parameters', { parameters: req.query })
13
14 checkErrors(req, res, () => {
15 // Remove 'acct:' from the beginning of the string
16 const nameWithHost = req.query.resource.substr(5)
79d5caf9 17 const [ name ] = nameWithHost.split('@')
350e31d6
C
18
19 db.Account.loadLocalByName(name)
20 .then(account => {
21 if (!account) {
22 return res.status(404)
23 .send({ error: 'Account not found' })
24 .end()
25 }
26
27 res.locals.account = account
28 return next()
29 })
30 .catch(err => {
31 logger.error('Error in webfinger validator.', err)
32 return res.sendStatus(500)
33 })
34 })
35 }
36]
37
38// ---------------------------------------------------------------------------
39
40export {
41 webfingerValidator
42}