@paroicms/server
Version:
The ParoiCMS server
99 lines (95 loc) • 3.71 kB
JavaScript
import { makeUrl } from "@paroicms/internal-server-lib";
import { ApiError, escapeHtml } from "@paroicms/public-server-lib";
import { generatePlatformToken, googleAuthGuard } from "../admin-backend/auth/auth.helper.js";
import { appConf, platformAuthUrl, registeredSites } from "../context.js";
import { getRouteParameter } from "../express/http-helpers.js";
export async function authController(req, res) {
if (!appConf.googleAuth || appConf.googleAuth.disabled || !platformAuthUrl) {
throw new Error("Google auth is disabled");
}
if (req.hostname !== appConf.googleAuth.fqdn) {
throw new ApiError(`Invalid hostname: ${req.hostname}`, 404);
}
res.send(generateAuthPageHtml(platformAuthUrl));
}
export async function googleLoginController(req, res) {
await googleAuthGuard(req, res);
}
export async function googleLoginCallbackController(req, res) {
const user = await googleAuthGuard(req, res);
if (!appConf.googleAuth || appConf.googleAuth.disabled || !platformAuthUrl) {
throw new Error("Google auth is disabled");
}
const platformToken = generatePlatformToken(user);
res.send(generateGoogleRedirectPageHtml({ platformToken, platformAuthUrl }));
}
export async function siteUrlController(req, _res) {
const redirectTo = getRouteParameter(req.params, "redirectTo");
if (!redirectTo)
throw new ApiError("Missing redirectTo", 404);
if (!appConf.googleAuth || appConf.googleAuth.disabled || !platformAuthUrl) {
throw new Error("Google auth is disabled");
}
const regSite = registeredSites.get(redirectTo);
if (!regSite)
throw new ApiError("Site not found", 404);
return makeUrl({
protocol: appConf.publicProtocol,
port: appConf.adminUiPort,
fqdn: regSite.fqdn,
});
}
function generateGoogleRedirectPageHtml({ platformToken, platformAuthUrl, }) {
return `
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Successful authentication</title>
</head>
<body>
<p>Authentication successful, redirect now to the website.</p>
<script>
async function redirectToBo () {
const redirectTo = sessionStorage.getItem("redirectTo");
const redirectUrl = await getRedirectUrl(redirectTo);
window.location.replace(redirectUrl + "/adm/login/?pltok=${escapeHtml(platformToken)}");
}
async function getRedirectUrl (redirectTo) {
const res = await fetch("${escapeHtml(platformAuthUrl)}/site-url/" + redirectTo);
if (!res.ok) {
throw new Error(\`redirect url, \${res.status} \${res.statusText}\`)
}
return await res.text();
}
redirectToBo().catch(error => {
const node = document.createElement("p");
node.textContent = \`You have been successfully authenticated, but we cannot redirect you to the website: \${error.message}\`;
node.style = "color: red;";
document.body.appendChild(node);
console.error(error);
});
</script>
</body>
</html>`;
}
function generateAuthPageHtml(platformAuthUrl) {
return `
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Authentication</title>
</head>
<body>
<p>Initiating authentification on Google…</p>
<script>
const urlParams = new URLSearchParams(window.location.search);
const redirectTo = urlParams.get("redirectTo");
sessionStorage.setItem("redirectTo", redirectTo);
window.location.replace("${escapeHtml(platformAuthUrl)}/google");
</script>
</body>
</html>`;
}
//# sourceMappingURL=oauth2-client.controller.js.map