ff-admin-server/src/controller/webapiController.ts

30 lines
1,007 B
TypeScript
Raw Normal View History

2025-01-21 13:54:52 +01:00
import { Request, Response } from "express";
import { JWTHelper } from "../helpers/jwtHelper";
2025-02-15 10:59:54 +01:00
import WebapiService from "../service/management/webapiService";
2025-01-21 13:54:52 +01:00
import ForbiddenRequestException from "../exceptions/forbiddenRequestException";
2025-02-15 10:59:54 +01:00
import WebapiCommandHandler from "../command/management/webapi/webapiCommandHandler";
2025-01-21 13:54:52 +01:00
/**
* @description Check authentication status by token
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
2025-01-22 09:27:15 +01:00
export async function getWebApiAccess(req: Request, res: Response): Promise<any> {
2025-01-21 13:54:52 +01:00
const bearer = req.headers.authorization?.split(" ")?.[1] ?? undefined;
2025-01-22 11:57:19 +01:00
let { id, expiry } = await WebapiService.getByToken(bearer);
2025-01-21 13:54:52 +01:00
2025-01-22 11:57:19 +01:00
if (expiry != null && new Date() > new Date(expiry)) {
2025-01-21 13:54:52 +01:00
throw new ForbiddenRequestException("api token expired");
}
2025-01-22 11:57:19 +01:00
await WebapiCommandHandler.updateUsage({ id });
let accessToken = await JWTHelper.buildWebapiToken(bearer, expiry);
2025-01-21 13:54:52 +01:00
res.json({
accessToken,
});
}