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