29 lines
1,007 B
TypeScript
29 lines
1,007 B
TypeScript
import { Request, Response } from "express";
|
|
import { JWTHelper } from "../helpers/jwtHelper";
|
|
import WebapiService from "../service/management/webapiService";
|
|
import ForbiddenRequestException from "../exceptions/forbiddenRequestException";
|
|
import WebapiCommandHandler from "../command/management/webapi/webapiCommandHandler";
|
|
|
|
/**
|
|
* @description Check authentication status by token
|
|
* @param req {Request} Express req object
|
|
* @param res {Response} Express res object
|
|
* @returns {Promise<*>}
|
|
*/
|
|
export async function getWebApiAccess(req: Request, res: Response): Promise<any> {
|
|
const bearer = req.headers.authorization?.split(" ")?.[1] ?? undefined;
|
|
|
|
let { id, expiry } = await WebapiService.getByToken(bearer);
|
|
|
|
if (expiry != null && new Date() > new Date(expiry)) {
|
|
throw new ForbiddenRequestException("api token expired");
|
|
}
|
|
|
|
await WebapiCommandHandler.updateUsage({ id });
|
|
|
|
let accessToken = await JWTHelper.buildWebapiToken(bearer, expiry);
|
|
|
|
res.json({
|
|
accessToken,
|
|
});
|
|
}
|