2017-11-14 11:31:26 -05:00
|
|
|
import * as express from 'express'
|
2017-11-23 11:53:38 -05:00
|
|
|
import { query } from 'express-validator/check'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { logger } from '../../helpers'
|
2017-11-23 11:53:38 -05:00
|
|
|
import { isWebfingerResourceValid } from '../../helpers/custom-validators/webfinger'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { AccountModel } from '../../models/account/account'
|
2017-11-27 11:30:46 -05:00
|
|
|
import { areValidationErrors } from './utils'
|
2017-11-14 11:31:26 -05:00
|
|
|
|
|
|
|
const webfingerValidator = [
|
|
|
|
query('resource').custom(isWebfingerResourceValid).withMessage('Should have a valid webfinger resource'),
|
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2017-11-14 11:31:26 -05:00
|
|
|
logger.debug('Checking webfinger parameters', { parameters: req.query })
|
|
|
|
|
2017-11-27 11:30:46 -05: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('@')
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
const account = await AccountModel.loadLocalByName(name)
|
2017-11-27 11:30:46 -05:00
|
|
|
if (!account) {
|
|
|
|
return res.status(404)
|
|
|
|
.send({ error: 'Account not found' })
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.account = account
|
|
|
|
return next()
|
2017-11-14 11:31:26 -05:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
webfingerValidator
|
|
|
|
}
|