diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 17:30:46 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:43:01 +0100 |
commit | a2431b7dcbc72c05101dcdbe631ff84a823aeb51 (patch) | |
tree | 09278a822905622a70ff976a75e09d99bc45639a /server/middlewares/validators/webfinger.ts | |
parent | fcaf1e0aa84213a1b1f1b1a44a3276eae35ebe70 (diff) | |
download | PeerTube-a2431b7dcbc72c05101dcdbe631ff84a823aeb51.tar.gz PeerTube-a2431b7dcbc72c05101dcdbe631ff84a823aeb51.tar.zst PeerTube-a2431b7dcbc72c05101dcdbe631ff84a823aeb51.zip |
Refractor validators
Diffstat (limited to 'server/middlewares/validators/webfinger.ts')
-rw-r--r-- | server/middlewares/validators/webfinger.ts | 42 |
1 files changed, 18 insertions, 24 deletions
diff --git a/server/middlewares/validators/webfinger.ts b/server/middlewares/validators/webfinger.ts index 7852c1c2b..34e62c66d 100644 --- a/server/middlewares/validators/webfinger.ts +++ b/server/middlewares/validators/webfinger.ts | |||
@@ -1,37 +1,31 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { query } from 'express-validator/check' | 2 | import { query } from 'express-validator/check' |
3 | import { isWebfingerResourceValid } from '../../helpers/custom-validators/webfinger' | 3 | import { isWebfingerResourceValid } from '../../helpers/custom-validators/webfinger' |
4 | import { database as db } from '../../initializers' | ||
5 | import { checkErrors } from './utils' | ||
6 | import { logger } from '../../helpers/logger' | 4 | import { logger } from '../../helpers/logger' |
5 | import { database as db } from '../../initializers' | ||
6 | import { areValidationErrors } from './utils' | ||
7 | 7 | ||
8 | const webfingerValidator = [ | 8 | const webfingerValidator = [ |
9 | query('resource').custom(isWebfingerResourceValid).withMessage('Should have a valid webfinger resource'), | 9 | query('resource').custom(isWebfingerResourceValid).withMessage('Should have a valid webfinger resource'), |
10 | 10 | ||
11 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | 11 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
12 | logger.debug('Checking webfinger parameters', { parameters: req.query }) | 12 | logger.debug('Checking webfinger parameters', { parameters: req.query }) |
13 | 13 | ||
14 | checkErrors(req, res, () => { | 14 | if (areValidationErrors(req, res)) return |
15 | // Remove 'acct:' from the beginning of the string | 15 | |
16 | const nameWithHost = req.query.resource.substr(5) | 16 | // Remove 'acct:' from the beginning of the string |
17 | const [ name ] = nameWithHost.split('@') | 17 | const nameWithHost = req.query.resource.substr(5) |
18 | 18 | const [ name ] = nameWithHost.split('@') | |
19 | db.Account.loadLocalByName(name) | 19 | |
20 | .then(account => { | 20 | const account = await db.Account.loadLocalByName(name) |
21 | if (!account) { | 21 | if (!account) { |
22 | return res.status(404) | 22 | return res.status(404) |
23 | .send({ error: 'Account not found' }) | 23 | .send({ error: 'Account not found' }) |
24 | .end() | 24 | .end() |
25 | } | 25 | } |
26 | 26 | ||
27 | res.locals.account = account | 27 | res.locals.account = account |
28 | return next() | 28 | return next() |
29 | }) | ||
30 | .catch(err => { | ||
31 | logger.error('Error in webfinger validator.', err) | ||
32 | return res.sendStatus(500) | ||
33 | }) | ||
34 | }) | ||
35 | } | 29 | } |
36 | ] | 30 | ] |
37 | 31 | ||