2021-11-12 20:48:26 +01:00
|
|
|
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";
|
2021-11-12 20:48:26 +01:00
|
|
|
|
2022-02-17 11:29:09 +01:00
|
|
|
export class OpenIdProfileController extends BaseHttpController {
|
|
|
|
routes() {
|
2021-11-12 20:48:26 +01:00
|
|
|
//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);
|
2021-11-12 20:48:26 +01:00
|
|
|
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) {
|
2022-01-06 10:49:44 +01:00
|
|
|
throw new Error("Email was not found");
|
2021-11-12 20:48:26 +01:00
|
|
|
}
|
2022-02-17 11:29:09 +01:00
|
|
|
res.send(
|
2021-11-12 20:48:26 +01:00
|
|
|
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);
|
2021-11-12 20:48:26 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
`;
|
2021-11-12 20:48:26 +01:00
|
|
|
}
|
|
|
|
}
|