2025-01-21 13:54:52 +01:00
|
|
|
import { Request, Response } from "express";
|
|
|
|
import { JWTHelper } from "../helpers/jwtHelper";
|
|
|
|
import { JWTToken } from "../type/jwtTypes";
|
|
|
|
import InternalException from "../exceptions/internalException";
|
|
|
|
import RefreshCommandHandler from "../command/refreshCommandHandler";
|
|
|
|
import { CreateRefreshCommand, DeleteRefreshCommand } from "../command/refreshCommand";
|
|
|
|
import UserService from "../service/user/userService";
|
|
|
|
import speakeasy from "speakeasy";
|
|
|
|
import UnauthorizedRequestException from "../exceptions/unauthorizedRequestException";
|
|
|
|
import RefreshService from "../service/refreshService";
|
2025-01-22 09:39:31 +01:00
|
|
|
import WebapiService from "../service/user/webapiService";
|
2025-01-21 13:54:52 +01:00
|
|
|
import ForbiddenRequestException from "../exceptions/forbiddenRequestException";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 09:39:31 +01:00
|
|
|
let { expiry } = await WebapiService.getByToken(bearer);
|
2025-01-21 13:54:52 +01:00
|
|
|
|
|
|
|
if (new Date() > new Date(expiry)) {
|
|
|
|
throw new ForbiddenRequestException("api token expired");
|
|
|
|
}
|
|
|
|
|
2025-01-22 09:39:31 +01:00
|
|
|
let accessToken = await JWTHelper.buildWebapiToken(bearer);
|
2025-01-21 13:54:52 +01:00
|
|
|
|
|
|
|
res.json({
|
|
|
|
accessToken,
|
|
|
|
});
|
|
|
|
}
|