/*
This file is part of GNU Taler
(C) 2022-2024 Taler Systems S.A.
GNU Taler is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
GNU Taler; see the file COPYING. If not, see
*/
import {
Loading,
urlPattern,
useCurrentLocation,
useNavigationContext,
} from "@gnu-taler/web-util/browser";
import { Fragment, VNode, h } from "preact";
import { assertUnreachable } from "@gnu-taler/taler-util";
import { useErrorBoundary } from "preact/hooks";
import { CheckChallengeIsUpToDate } from "./components/CheckChallengeIsUpToDate.js";
import { SessionId, useSessionState } from "./hooks/session.js";
import { AnswerChallenge } from "./pages/AnswerChallenge.js";
import { AskChallenge } from "./pages/AskChallenge.js";
import { CallengeCompleted } from "./pages/CallengeCompleted.js";
import { Frame } from "./pages/Frame.js";
import { NonceNotFound } from "./pages/NonceNotFound.js";
import { Setup } from "./pages/Setup.js";
export function Routing(): VNode {
// check session and defined if this is
// public routing or private
return (
);
}
const publicPages = {
noinfo: urlPattern(/\/noinfo/, () => `#/noinfo`),
authorize: urlPattern(/\/authorize/, () => `#/authorize`),
ask: urlPattern(/\/ask/, () => `#/ask`),
answer: urlPattern(/\/answer/, () => `#/answer`),
completed: urlPattern(/\/completed/, () => `#/completed`),
setup: urlPattern<{ client: string }>(
/\/setup\/(?[0-9]+)/,
({ client }) => `#/setup/${client}`,
),
};
function safeGetParam(
ps: Record,
n: string,
): string | undefined {
if (!ps[n] || ps[n].length == 0) return undefined;
return ps[n][0];
}
export function safeToURL(s: string | undefined): URL | undefined {
if (s === undefined) return undefined;
try {
return new URL(s);
} catch (e) {
return undefined;
}
}
function PublicRounting(): VNode {
const location = useCurrentLocation(publicPages);
const { navigateTo } = useNavigationContext();
const { start } = useSessionState();
useErrorBoundary((e) => {
console.log("error", e);
});
if (location === undefined) {
return ;
}
switch (location.name) {
case "noinfo": {
return no info
;
}
case "setup": {
const secret = safeGetParam(location.params, "secret");
const redirectURL = safeToURL(
safeGetParam(location.params, "redirect_url"),
);
return (
{
navigateTo(publicPages.ask.url({}));
}}
/>
);
}
case "authorize": {
const clientId = safeGetParam(location.params, "client_id");
const redirectURL = safeToURL(
safeGetParam(location.params, "redirect_url"),
);
const state = safeGetParam(location.params, "state");
const sessionId: SessionId | undefined =
!clientId || !redirectURL || !state
? undefined
: {
clientId,
nonce: location.values.nonce,
redirectURL: redirectURL.href,
state,
};
return (
{
navigateTo(
publicPages.completed.url({
nonce: location.values.nonce,
}),
);
}}
onChangeLeft={() => {
navigateTo(
publicPages.ask.url({
nonce: location.values.nonce,
}),
);
}}
onNoMoreChanges={() => {
navigateTo(
publicPages.ask.url({
nonce: location.values.nonce,
}),
);
}}
>
);
}
case "ask": {
return (
{
navigateTo(
publicPages.answer.url({
nonce: location.values.nonce,
}),
);
}}
// onCompleted={() => {
// navigateTo(
// publicPages.completed.url({
// nonce: location.values.nonce,
// }),
// );
// }}
/>
);
}
case "answer": {
return (
{
navigateTo(
publicPages.completed.url({
nonce: location.values.nonce,
}),
);
}}
// onCompleted={() => {
// navigateTo(
// publicPages.completed.url({
// nonce: location.values.nonce,
// }),
// );
// }}
/>
);
}
case "completed": {
return (
);
}
default:
assertUnreachable(location);
}
}