partey_workadventure/pusher/src/Controller/OpenIdProfileController.ts

68 lines
2.6 KiB
TypeScript
Raw Normal View History

import { parse } from "query-string";
import { openIDClient } from "../Services/OpenIDClient";
import { OPID_CLIENT_ISSUER } from "../Enum/EnvironmentVariable";
2022-02-17 11:29:09 +01:00
import { BaseHttpController } from "./BaseHttpController";
2022-02-17 11:29:09 +01:00
export class OpenIdProfileController extends BaseHttpController {
routes() {
//eslint-disable-next-line @typescript-eslint/no-misused-promises
2022-02-17 11:29:09 +01:00
this.app.get("/profile", async (req, res) => {
const { accessToken } = parse(req.path_query);
if (!accessToken) {
throw Error("Access token expected cannot to be check on Hydra");
}
try {
const resCheckTokenAuth = await openIDClient.checkTokenAuth(accessToken as string);
if (!resCheckTokenAuth.email) {
throw new Error("Email was not found");
}
2022-02-17 11:29:09 +01:00
res.send(
this.buildHtml(
OPID_CLIENT_ISSUER,
resCheckTokenAuth.email as string,
resCheckTokenAuth.picture as string | undefined
)
);
} catch (error) {
console.error("profileCallback => ERROR", error);
2022-02-17 11:29:09 +01:00
this.castErrorToResponse(error, res);
}
});
}
buildHtml(domain: string, email: string, pictureUrl?: string) {
2021-11-15 15:58:08 +01:00
return `
<!DOCTYPE>
<html>
<head>
<style>
*{
font-family: PixelFont-7, monospace;
}
body{
text-align: center;
color: white;
}
section{
margin: 20px;
}
</style>
</head>
<body>
<div class="container">
<section>
2022-02-17 11:29:09 +01:00
<img src="${pictureUrl ? pictureUrl : "/images/profile"}">
2021-11-15 15:58:08 +01:00
</section>
<section>
Profile validated by domain: <span style="font-weight: bold">${domain}</span>
2022-02-17 11:29:09 +01:00
</section>
2021-11-15 15:58:08 +01:00
<section>
2022-02-17 11:29:09 +01:00
Your email: <span style="font-weight: bold">${email}</span>
2021-11-15 15:58:08 +01:00
</section>
</div>
</body>
</html>
`;
}
}