Follow works
[oweals/peertube.git] / server / middlewares / validators / webfinger.ts
1 import { query } from 'express-validator/check'
2 import * as express from 'express'
3
4 import { checkErrors } from './utils'
5 import { logger, isWebfingerResourceValid } from '../../helpers'
6 import { database as db } from '../../initializers'
7
8 const 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)
17       const [ name, ] = nameWithHost.split('@')
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
40 export {
41   webfingerValidator
42 }