aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/webfinger.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/middlewares/validators/webfinger.ts')
-rw-r--r--server/middlewares/validators/webfinger.ts42
1 files changed, 42 insertions, 0 deletions
diff --git a/server/middlewares/validators/webfinger.ts b/server/middlewares/validators/webfinger.ts
new file mode 100644
index 000000000..068e03ad7
--- /dev/null
+++ b/server/middlewares/validators/webfinger.ts
@@ -0,0 +1,42 @@
1import { query } from 'express-validator/check'
2import * as express from 'express'
3
4import { checkErrors } from './utils'
5import { logger, isWebfingerResourceValid } from '../../helpers'
6import { database as db } from '../../initializers'
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)
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
40export {
41 webfingerValidator
42}