PeerTube/server/middlewares/validators/webfinger.ts

37 lines
1.1 KiB
TypeScript
Raw Normal View History

2017-11-14 10:31:26 -06:00
import * as express from 'express'
2017-11-23 10:53:38 -06:00
import { query } from 'express-validator/check'
import { isWebfingerResourceValid } from '../../helpers/custom-validators/webfinger'
import { logger } from '../../helpers/logger'
2017-11-27 10:30:46 -06:00
import { database as db } from '../../initializers'
import { areValidationErrors } from './utils'
2017-11-14 10:31:26 -06:00
const webfingerValidator = [
query('resource').custom(isWebfingerResourceValid).withMessage('Should have a valid webfinger resource'),
2017-11-27 10:30:46 -06:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
2017-11-14 10:31:26 -06:00
logger.debug('Checking webfinger parameters', { parameters: req.query })
2017-11-27 10:30:46 -06:00
if (areValidationErrors(req, res)) return
// Remove 'acct:' from the beginning of the string
const nameWithHost = req.query.resource.substr(5)
const [ name ] = nameWithHost.split('@')
const account = await db.Account.loadLocalByName(name)
if (!account) {
return res.status(404)
.send({ error: 'Account not found' })
.end()
}
res.locals.account = account
return next()
2017-11-14 10:31:26 -06:00
}
]
// ---------------------------------------------------------------------------
export {
webfingerValidator
}