2017-11-14 10:31:26 -06:00
|
|
|
import * as express from 'express'
|
2019-07-25 09:23:44 -05:00
|
|
|
import { query } from 'express-validator'
|
2021-07-16 03:42:24 -05:00
|
|
|
import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
|
2018-08-16 08:25:20 -05:00
|
|
|
import { isWebfingerLocalResourceValid } from '../../helpers/custom-validators/webfinger'
|
2021-05-11 04:15:29 -05:00
|
|
|
import { getHostWithPort } from '../../helpers/express-utils'
|
2017-12-28 04:16:08 -06:00
|
|
|
import { logger } from '../../helpers/logger'
|
2021-05-11 04:15:29 -05:00
|
|
|
import { ActorModel } from '../../models/actor/actor'
|
2021-06-03 10:33:44 -05:00
|
|
|
import { areValidationErrors } from './shared'
|
2017-11-14 10:31:26 -06:00
|
|
|
|
|
|
|
const webfingerValidator = [
|
2018-08-16 08:25:20 -05:00
|
|
|
query('resource').custom(isWebfingerLocalResourceValid).withMessage('Should have a valid webfinger resource'),
|
2017-11-14 10:31:26 -06:00
|
|
|
|
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
|
2017-12-19 07:21:14 -06:00
|
|
|
const nameWithHost = getHostWithPort(req.query.resource.substr(5))
|
2017-11-27 10:30:46 -06:00
|
|
|
const [ name ] = nameWithHost.split('@')
|
|
|
|
|
2020-01-28 07:45:17 -06:00
|
|
|
const actor = await ActorModel.loadLocalUrlByName(name)
|
2017-12-14 10:38:41 -06:00
|
|
|
if (!actor) {
|
2021-05-31 18:36:53 -05:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
|
|
|
message: 'Actor not found'
|
|
|
|
})
|
2017-11-27 10:30:46 -06:00
|
|
|
}
|
|
|
|
|
2020-01-28 07:45:17 -06:00
|
|
|
res.locals.actorUrl = actor
|
2017-11-27 10:30:46 -06:00
|
|
|
return next()
|
2017-11-14 10:31:26 -06:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
webfingerValidator
|
|
|
|
}
|