Compare commits

...
1 Commits
Author SHA1 Message Date
NotAlexNoyle aed1734c40 Add log out support 2026-11-11 11:00:28 -05:00
10 changed files with 221 additions and 1 deletions

No files matched your search

+16
View File
@@ -5,6 +5,8 @@ export const dict = {
loading: "Loading login…",
failure: "Failed to log in",
tryAgain: "Try again",
signedOutNotice:
"You were signed out of Staff-Auth, so TrueOG apps ask you to sign in again.",
credentials: {
title: "Login",
username: "Minecraft username",
@@ -173,7 +175,21 @@ export const dict = {
},
logout: {
loggedOut: "Logged out",
explanation:
"You are signed out of Staff-Auth and of the TrueOG apps that use it (Gitea and WeKan).",
logBackIn: "Log back in",
loading: "Loading sign-out…",
confirmTitle: "Sign out",
confirmText:
"{{ client }} asked to sign you out. This signs you out of Staff-Auth and of every TrueOG app that uses it (Gitea and WeKan).",
anApp: "An app",
confirm: "Sign out",
cancel: "Stay signed in",
error: {
noChallenge: "No logout challenge was provided",
invalidChallenge: "An invalid logout challenge was provided",
challengeUsed: "This logout challenge has already been used",
},
},
error: {
unknownError: "Unknown error",
+76
View File
@@ -0,0 +1,76 @@
import type { TranslationKey } from "../contexts/I18nContext";
import { type ApiError, AppError, type ErrorData } from "../types/api";
import type { LogoutData } from "../types/logout";
import { BASE_URL, isApiError } from "./api";
export const logoutApi = {
logoutData: async (logoutChallenge: string): Promise<LogoutData> => {
const url = new URL(`${BASE_URL}/logout-request`);
url.searchParams.set("logout_challenge", logoutChallenge);
let response: Response;
try {
response = await fetch(url);
} catch (err) {
throw toAppError(err);
}
if (!response.ok)
throw toAppError({
status: response.status,
errorData: (await response.json()) as ErrorData,
});
return await response.json();
},
/**
* @returns redirect URL
*/
logout: async (logout: boolean, logoutChallenge: string): Promise<string> => {
let response: Response;
try {
response = await fetch(`${BASE_URL}/logout-request`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ logout, logoutChallenge }),
});
} catch (err) {
throw toAppError(err);
}
if (!response.ok)
throw toAppError({
status: response.status,
errorData: (await response.json()) as ErrorData,
});
return await response.text();
},
};
const fatal = ["INVALID_LOGOUT_CHALLENGE", "LOGOUT_REQUEST_USED"];
function toAppError(err: unknown): AppError {
if (isApiError(err)) {
throw new AppError(
fatal.includes(err.errorData.message) ? "fatal" : "local",
getApiErrorMessage(err),
err.status,
);
}
if (err instanceof TypeError) {
if (err.message.startsWith("NetworkError")) {
throw new AppError("local", "error.networkError", null);
}
}
throw new AppError("fatal", "error.unknownError", null);
}
function getApiErrorMessage(err: ApiError): TranslationKey {
switch (err.errorData.message) {
case "INVALID_LOGOUT_CHALLENGE":
return "logout.error.invalidChallenge";
case "LOGOUT_REQUEST_USED":
return "logout.error.challengeUsed";
default:
return "error.unknownError";
}
}
+29
View File
@@ -0,0 +1,29 @@
// Remembers in this browser that the person signed out of Staff-Auth, so the login page can say why an app sent
// them there. Set on /logged-out (the end of every logout), cleared when a sign-in reaches the consent step.
const KEY = "staffauth.signedOut";
const MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;
export const signedOut = {
mark(): void {
try {
localStorage.setItem(KEY, String(Date.now()));
} catch {
// Storage unavailable (private mode, blocked): the notice is a courtesy only.
}
},
clear(): void {
try {
localStorage.removeItem(KEY);
} catch {
// As above.
}
},
isMarked(): boolean {
try {
const at = Number(localStorage.getItem(KEY));
return at > 0 && Date.now() - at < MAX_AGE_MS;
} catch {
return false;
}
},
};
+3
View File
@@ -3,6 +3,7 @@ import { createEffect, For, Show, Suspense } from "solid-js";
import Loader from "../components/Loader";
import type { TranslationKey } from "../contexts/I18nContext";
import { consentApi } from "../lib/consent";
import { signedOut } from "../lib/signedOut";
import { useI18n } from "../providers/I18nProvider";
import { getConsentData } from "./consent.data";
@@ -21,6 +22,8 @@ export default function ConsentPage() {
createEffect(() => {
const data = consentData();
if (!data) return;
// Reaching consent means the sign-in succeeded: the "you were signed out" notice has done its job.
signedOut.clear();
if (data.skip) {
consent(true, params.consent_challenge as string);
}
+5 -1
View File
@@ -1,14 +1,18 @@
import { onMount } from "solid-js";
import CheckIcon from "~icons/mdi/check";
import { LOGIN_URL } from "../lib/api";
import { signedOut } from "../lib/signedOut";
import { useI18n } from "../providers/I18nProvider";
export default function LoggedOutPage() {
const { t } = useI18n();
onMount(() => signedOut.mark());
return (
<div class="flex flex-col min-h-screen items-center justify-center">
<div class="flex flex-col min-h-screen items-center justify-center p-4">
<CheckIcon class="h-16 w-full mb-8" />
<p class="text-xl text-center">{t("logout.loggedOut")}</p>
<p class="text-center mt-2 max-w-md">{t("logout.explanation")}</p>
<button
type="button"
onClick={() => (window.location.href = LOGIN_URL)}
+6
View File
@@ -12,6 +12,7 @@ import Credentials from "../components/login/Credentials";
import MinecraftCheck from "../components/login/MinecraftCheck";
import Totp from "../components/login/Totp";
import { getStage } from "../lib/login";
import { signedOut } from "../lib/signedOut";
import { useI18n } from "../providers/I18nProvider";
import { AppError } from "../types/api";
import { getLoginData } from "./login.data";
@@ -45,6 +46,11 @@ export default function LoginPage() {
<div class="card bg-base-200 border-2 border-base-300 w-full max-w-md shrink-0 shadow-2xl">
<div class="card-body">
<img src="/logo.webp" alt="TrueOG logo"></img>
<Show when={signedOut.isMarked()}>
<div role="status" class="alert alert-info">
{t("login.signedOutNotice")}
</div>
</Show>
<Suspense
fallback={
<Loader text={t("login.loading")} fillScreen={false} />
+14
View File
@@ -0,0 +1,14 @@
import { query } from "@solidjs/router";
import { logoutApi } from "../lib/logout";
import { AppError } from "../types/api";
export const getLogoutData = query(
async (logoutChallenge: string | string[] | undefined) => {
if (!logoutChallenge)
throw new AppError("fatal", "logout.error.noChallenge", null);
if (typeof logoutChallenge !== "string")
throw new AppError("fatal", "logout.error.invalidChallenge", null);
return await logoutApi.logoutData(logoutChallenge);
},
"logoutData",
);
+60
View File
@@ -0,0 +1,60 @@
import { createAsync, useSearchParams } from "@solidjs/router";
import { Suspense } from "solid-js";
import Loader from "../components/Loader";
import { logoutApi } from "../lib/logout";
import { useI18n } from "../providers/I18nProvider";
import { getLogoutData } from "./logout.data";
// Hydra's logout UI: an app (for example Gitea) asked Hydra to sign the person out. Such a request carries no proof
// of who sent it, so the person confirms; signing out ends Staff-Auth and every app that uses it.
export default function LogoutPage() {
const { t } = useI18n();
const [params] = useSearchParams();
const logoutData = createAsync(() => getLogoutData(params.logout_challenge));
async function logout(logout: boolean) {
const redirectUrl = await logoutApi.logout(
logout,
params.logout_challenge as string,
);
window.location.href = redirectUrl;
}
return (
<Suspense
fallback={<Loader text={t("logout.loading")} fillScreen={true} />}
>
<div class="flex min-h-screen items-center justify-center">
<div class="card bg-base-200 border-2 border-base-300 w-full max-w-md shrink-0 shadow-2xl">
<div class="card-body">
<img src="/logo.webp" alt="TrueOG"></img>
<div class="flex flex-col items-center gap-4">
<h1 class="text-2xl font-bold">{t("logout.confirmTitle")}</h1>
<p class="text-center">
{t("logout.confirmText", {
client: logoutData()?.clientName ?? t("logout.anApp"),
})}
</p>
<div class="w-full py-2 flex flex-col gap-2">
<button
type="button"
onClick={() => logout(true)}
class="btn btn-error"
>
{t("logout.confirm")}
</button>
<button
type="button"
onClick={() => logout(false)}
class="btn btn-soft"
>
{t("logout.cancel")}
</button>
</div>
</div>
</div>
</div>
</div>
</Suspense>
);
}
+9
View File
@@ -10,6 +10,7 @@ import { preloadUsers } from "./pages/panel/user/users.data";
const Login = lazy(() => import("./pages/login"));
const Consent = lazy(() => import("./pages/consent"));
const Logout = lazy(() => import("./pages/logout"));
const Setup = lazy(() => import("./pages/setup"));
@@ -60,6 +61,14 @@ export const routes: RouteDefinition[] = [
</BasicLayout>
),
},
{
path: "/logout",
component: () => (
<BasicLayout>
<Logout />
</BasicLayout>
),
},
{
path: "/setup",
component: () => (
+3
View File
@@ -0,0 +1,3 @@
export type LogoutData = {
clientName: string | null;
};