Protecting some APIs with an APIKEY.
This commit is contained in:
21
server/lib/middlewares/apikey.ts
Normal file
21
server/lib/middlewares/apikey.ts
Normal file
@ -0,0 +1,21 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user