mirror of
https://github.com/tale/headplane.git
synced 2026-08-27 15:37:04 +00:00
chore(auth): add more informative logs
This commit is contained in:
@@ -8,7 +8,9 @@ import Code from "~/components/code";
|
|||||||
import Input from "~/components/input";
|
import Input from "~/components/input";
|
||||||
import Link from "~/components/link";
|
import Link from "~/components/link";
|
||||||
import { appConfigContext, authContext, oidcContext } from "~/server/context";
|
import { appConfigContext, authContext, oidcContext } from "~/server/context";
|
||||||
|
import type { OidcError, OidcService } from "~/server/oidc/provider";
|
||||||
import { useLiveData } from "~/utils/live-data";
|
import { useLiveData } from "~/utils/live-data";
|
||||||
|
import log from "~/utils/log";
|
||||||
|
|
||||||
import type { Route } from "./+types/page";
|
import type { Route } from "./+types/page";
|
||||||
import { loginAction } from "./action";
|
import { loginAction } from "./action";
|
||||||
@@ -30,12 +32,20 @@ export async function loader({ request, context, url }: Route.LoaderArgs) {
|
|||||||
const urlState = qp.get("s") ?? undefined;
|
const urlState = qp.get("s") ?? undefined;
|
||||||
|
|
||||||
const oidcService = oidc.state === "enabled" ? oidc.value : undefined;
|
const oidcService = oidc.state === "enabled" ? oidc.value : undefined;
|
||||||
const oidcStatus = oidcService
|
let oidcStatus: ReturnType<OidcService["status"]> | undefined;
|
||||||
? await oidcService.discover().then(
|
if (oidcService) {
|
||||||
(r) => (r.ok ? oidcService.status() : oidcService.status()),
|
try {
|
||||||
() => oidcService.status(),
|
const result = await oidcService.discover();
|
||||||
)
|
if (!result.ok) {
|
||||||
: undefined;
|
logLoginOidcError("OIDC discovery failed", result.error);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
log.error("auth", "OIDC discovery failed unexpectedly: %s", String(error));
|
||||||
|
log.debug("auth", "OIDC discovery error details: %o", error);
|
||||||
|
}
|
||||||
|
|
||||||
|
oidcStatus = oidcService.status();
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
oidcService &&
|
oidcService &&
|
||||||
@@ -59,6 +69,13 @@ export async function loader({ request, context, url }: Route.LoaderArgs) {
|
|||||||
|
|
||||||
export const action = loginAction;
|
export const action = loginAction;
|
||||||
|
|
||||||
|
function logLoginOidcError(context: string, error: OidcError): void {
|
||||||
|
log.error("auth", "%s [%s]: %s", context, error.code, error.message);
|
||||||
|
if (error.hint) {
|
||||||
|
log.error("auth", "Hint: %s", error.hint);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default function Page({ loaderData, actionData }: Route.ComponentProps) {
|
export default function Page({ loaderData, actionData }: Route.ComponentProps) {
|
||||||
const { isCookieSecureEnabled, isOidcConnectorEnabled, oidcErrorCodes, urlState } = loaderData;
|
const { isCookieSecureEnabled, isOidcConnectorEnabled, oidcErrorCodes, urlState } = loaderData;
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
headscaleContext,
|
headscaleContext,
|
||||||
oidcContext,
|
oidcContext,
|
||||||
} from "~/server/context";
|
} from "~/server/context";
|
||||||
|
import { logOidcError } from "~/server/oidc/provider";
|
||||||
import { findHeadscaleUserBySubject } from "~/server/web/headscale-identity";
|
import { findHeadscaleUserBySubject } from "~/server/web/headscale-identity";
|
||||||
import { Roles } from "~/server/web/roles";
|
import { Roles } from "~/server/web/roles";
|
||||||
import log from "~/utils/log";
|
import log from "~/utils/log";
|
||||||
@@ -27,6 +28,7 @@ export async function loader({ request, context, url }: Route.LoaderArgs) {
|
|||||||
const service = oidc.value;
|
const service = oidc.value;
|
||||||
|
|
||||||
if (url.searchParams.toString().length === 0) {
|
if (url.searchParams.toString().length === 0) {
|
||||||
|
log.warn("auth", "Called OIDC callback without query parameters");
|
||||||
return redirect("/login?s=error_no_query");
|
return redirect("/login?s=error_no_query");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,10 +55,7 @@ export async function loader({ request, context, url }: Route.LoaderArgs) {
|
|||||||
|
|
||||||
const result = await service.handleCallback(url.searchParams, flowState);
|
const result = await service.handleCallback(url.searchParams, flowState);
|
||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
log.error("auth", "OIDC callback failed [%s]: %s", result.error.code, result.error.message);
|
logOidcError("OIDC callback failed", result.error);
|
||||||
if (result.error.hint) {
|
|
||||||
log.error("auth", "Hint: %s", result.error.hint);
|
|
||||||
}
|
|
||||||
return redirect("/login?s=error_auth_failed");
|
return redirect("/login?s=error_auth_failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { data, redirect } from "react-router";
|
import { data, redirect } from "react-router";
|
||||||
|
|
||||||
import { appConfigContext, authContext, oidcContext } from "~/server/context";
|
import { appConfigContext, authContext, oidcContext } from "~/server/context";
|
||||||
|
import { logOidcError } from "~/server/oidc/provider";
|
||||||
import { createOidcStateCookie } from "~/utils/oidc-state";
|
import { createOidcStateCookie } from "~/utils/oidc-state";
|
||||||
|
|
||||||
import type { Route } from "./+types/oidc-start";
|
import type { Route } from "./+types/oidc-start";
|
||||||
@@ -22,6 +23,7 @@ export async function loader({ request, context }: Route.LoaderArgs) {
|
|||||||
|
|
||||||
const result = await service.startFlow();
|
const result = await service.startFlow();
|
||||||
if (!result.ok) {
|
if (!result.ok) {
|
||||||
|
logOidcError("OIDC start failed", result.error);
|
||||||
return redirect(`/login?s=${result.error.code}`);
|
return redirect(`/login?s=${result.error.code}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -75,6 +75,13 @@ export interface OidcError {
|
|||||||
hint?: string;
|
hint?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function logOidcError(context: string, error: OidcError): void {
|
||||||
|
log.error("auth", "%s [%s]: %s", context, error.code, error.message);
|
||||||
|
if (error.hint) {
|
||||||
|
log.error("auth", "Hint: %s", error.hint);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type JwksResolver = (
|
type JwksResolver = (
|
||||||
protectedHeader?: JWSHeaderParameters,
|
protectedHeader?: JWSHeaderParameters,
|
||||||
token?: FlattenedJWSInput,
|
token?: FlattenedJWSInput,
|
||||||
|
|||||||
Reference in New Issue
Block a user