peertube-plugin-livechat/server/lib/middlewares/apikey.ts

27 lines
831 B
TypeScript
Raw Normal View History

2024-05-23 09:42:14 +00:00
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
//
// SPDX-License-Identifier: AGPL-3.0-only
import type { RegisterServerOptions } from '@peertube/peertube-types'
2021-05-05 15:06:19 +00:00
import type { Request, Response, NextFunction } from 'express'
import { getAPIKey } from '../apikey'
type CheckAPIKeyMiddlewareFunc = (req: Request, res: Response, next: NextFunction) => Promise<void>
function getCheckAPIKeyMiddleware (options: RegisterServerOptions): CheckAPIKeyMiddlewareFunc {
return async (req: Request, res: Response, next: NextFunction) => {
const key = req.query.apikey
const apikey = await getAPIKey(options)
if (key !== apikey) {
options.peertubeHelpers.logger.warn('Invalid APIKEY')
res.sendStatus(403)
return
}
next()
}
}
export {
getCheckAPIKeyMiddleware
}