38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
import { Request, Response } from "express";
|
|
import Stacker from "../middlewares/stacker";
|
|
import { GetUserMiddleware } from "../middlewares/user";
|
|
import { URL } from "url";
|
|
import Client from "../../models/client";
|
|
import RequestError, { HttpStatusCode } from "../../helper/request_error";
|
|
import { getAccessTokenJWT } from "../../helper/jwt";
|
|
|
|
export const GetJWTByUser = Stacker(
|
|
GetUserMiddleware(true, false),
|
|
async (req: Request, res: Response) => {
|
|
const { client_id, origin } = req.query as { [key: string]: string };
|
|
|
|
const client = await Client.findOne({
|
|
client_id,
|
|
});
|
|
|
|
const clientNotFoundError = new RequestError(
|
|
"Client not found!",
|
|
HttpStatusCode.BAD_REQUEST
|
|
);
|
|
|
|
if (!client) throw clientNotFoundError;
|
|
|
|
const clientUrl = new URL(client.redirect_url);
|
|
|
|
if (clientUrl.hostname !== origin) throw clientNotFoundError;
|
|
|
|
const jwt = await getAccessTokenJWT({
|
|
user: req.user,
|
|
client: client,
|
|
permissions: [],
|
|
});
|
|
|
|
res.json({ jwt });
|
|
}
|
|
);
|