commit 13425aab43376e4376b59189ad7c183e32c77d56
parent 5f0106be6bbb4fe9d3730646933eed36c26ca94a
Author: Sebastian <sebasjm@gmail.com>
Date: Tue, 6 May 2025 03:23:49 -0300
fix #9881
Diffstat:
4 files changed, 78 insertions(+), 78 deletions(-)
diff --git a/packages/challenger-ui/src/Routing.tsx b/packages/challenger-ui/src/Routing.tsx
@@ -25,12 +25,13 @@ 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 } from "./hooks/session.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 { Setup } from "./pages/Setup.js";
+import { usePreferences } from "./context/preferences.js";
export function Routing(): VNode {
// check session and defined if this is
@@ -71,8 +72,28 @@ export function safeToURL(s: string | undefined): URL | undefined {
}
}
+function getSession(params: Record<string, string[]>) {
+ const clientId = safeGetParam(params, "client_id");
+ const redirectURL = safeToURL(safeGetParam(params, "redirect_uri"));
+ const state = safeGetParam(params, "state");
+ const nonce = safeGetParam(params, "nonce");
+
+ const sessionId: SessionId | undefined =
+ !clientId || !redirectURL || !state || !nonce
+ ? undefined
+ : {
+ clientId,
+ nonce: nonce,
+ redirectURL: redirectURL.href,
+ state,
+ };
+ return sessionId;
+}
+
function PublicRounting(): VNode {
const loc = useCurrentLocation(publicPages);
+ const [{ showDebugInfo }] = usePreferences();
+ const { state } = useSessionState();
const { i18n } = useTranslationContext();
const { navigateTo } = useNavigationContext();
useErrorBoundary((e) => {
@@ -109,32 +130,16 @@ function PublicRounting(): VNode {
);
}
case "authorize": {
- const clientId = safeGetParam(location.params, "client_id");
- const redirectURL = safeToURL(
- safeGetParam(location.params, "redirect_uri"),
- );
- const state = safeGetParam(location.params, "state");
- const nonce = safeGetParam(location.params, "nonce");
-
- const sessionId: SessionId | undefined =
- !clientId || !redirectURL || !state || !nonce
- ? undefined
- : {
- clientId,
- nonce: nonce,
- redirectURL: redirectURL.href,
- state,
- };
+ const sessionId = getSession(location.params);
if (!sessionId) {
return (
<div>
- <i18n.Translate>The application needs to be loaded with 4 request parameters. One or more are missing:</i18n.Translate>
- {JSON.stringify(
- { client_id: clientId, redirect_url: redirectURL?.href, state, nonce },
- undefined,
- 2,
- )}
+ <i18n.Translate>
+ The application needs to be loaded with 4 request parameters. One
+ or more are missing:
+ </i18n.Translate>
+ {JSON.stringify({ params: location.params }, undefined, 2)}
</div>
);
}
@@ -168,32 +173,16 @@ function PublicRounting(): VNode {
);
}
case "ask": {
- const clientId = safeGetParam(location.params, "client_id");
- const redirectURL = safeToURL(
- safeGetParam(location.params, "redirect_uri"),
- );
- const state = safeGetParam(location.params, "state");
- const nonce = safeGetParam(location.params, "nonce");
-
- const sessionId: SessionId | undefined =
- !clientId || !redirectURL || !state || !nonce
- ? undefined
- : {
- clientId,
- nonce: nonce,
- redirectURL: redirectURL.href,
- state,
- };
+ const sessionId = getSession(location.params);
if (!sessionId) {
return (
<div>
- <i18n.Translate>The application needs to be loaded with 4 request parameters. One or more are missing:</i18n.Translate>
- {JSON.stringify(
- { client_id: clientId, redirect_url: redirectURL?.href, state, nonce },
- undefined,
- 2,
- )}
+ <i18n.Translate>
+ The application needs to be loaded with 4 request parameters. One
+ or more are missing:
+ </i18n.Translate>
+ {JSON.stringify({ params: location.params }, undefined, 2)}
</div>
);
}
@@ -220,36 +209,20 @@ function PublicRounting(): VNode {
);
}
case "answer": {
- const clientId = safeGetParam(location.params, "client_id");
- const redirectURL = safeToURL(
- safeGetParam(location.params, "redirect_uri"),
- );
- const state = safeGetParam(location.params, "state");
- const nonce = safeGetParam(location.params, "nonce");
-
- const sessionId: SessionId | undefined =
- !clientId || !redirectURL || !state || !nonce
- ? undefined
- : {
- clientId,
- nonce: nonce,
- redirectURL: redirectURL.href,
- state,
- };
+ const sessionId = getSession(location.params);
if (!sessionId) {
return (
<div>
- <i18n.Translate>The application needs to be loaded with 4 request parameters. One or more are missing:</i18n.Translate>
- {JSON.stringify(
- { client_id: clientId, redirect_url: redirectURL?.href, state, nonce },
- undefined,
- 2,
- )}
+ <i18n.Translate>
+ The application needs to be loaded with 4 request parameters. One
+ or more are missing:
+ </i18n.Translate>
+ {JSON.stringify({ params: location.params }, undefined, 2)}
</div>
);
}
-
+
return (
<AnswerChallenge
focus
diff --git a/packages/challenger-ui/src/hooks/session.ts b/packages/challenger-ui/src/hooks/session.ts
@@ -72,7 +72,7 @@ export const codecForSessionState = (): Codec<SessionState> =>
export interface SessionStateHandler {
state: SessionState | undefined;
- start(s: SessionId): void;
+ start(): void;
saveAddress(type: string, address: Record<string, string>): void;
removeAddress(index: number): void;
sent(info: ChallengerApi.ChallengeCreateResponse): void;
diff --git a/packages/challenger-ui/src/pages/AskChallenge.tsx b/packages/challenger-ui/src/pages/AskChallenge.tsx
@@ -138,14 +138,19 @@ export function AskChallenge({
const lastStatus = result.body;
+ const initial = lastStatus.last_address ?? {}
+ if (config.address_type === "postal-ch") {
+ initial[TalerFormAttributes.ADDRESS_COUNTRY] = "CH"
+ }
+
const design = getFormDesignBasedOnAddressType(
i18n,
config.address_type,
config.restrictions ?? {},
- lastStatus.last_address ?? {},
+ initial,
lastStatus.fix_address,
);
- const form = useForm(design, lastStatus.last_address ?? {});
+ const form = useForm(design, initial);
const prevAddr = !lastStatus?.last_address
? undefined
@@ -699,7 +704,8 @@ function getFormDesignBasedOnAddressType(
id: TalerFormAttributes.ADDRESS_LINES,
required: true,
disabled: read_only,
- label: i18n.str`Address`,
+ label: i18n.str`Swiss address`,
+ help: i18n.str`Make sure that this address is for Switzerland`,
placeholder: ADDRESS_EXAMPLE_CH,
validator(text) {
const restriction = getRestriction(
@@ -711,6 +717,24 @@ function getFormDesignBasedOnAddressType(
return undefined;
},
},
+ {
+ id: TalerFormAttributes.ADDRESS_COUNTRY,
+ label: i18n.str`Country`,
+ type: "selectOne",
+ choices: countryNameList(i18n),
+ required: true,
+ disabled: true,
+ preferredChoiceVals: ["CH"],
+ validator(text) {
+ const restriction = getRestriction(
+ restrictions[TalerFormAttributes.ADDRESS_COUNTRY],
+ );
+ if (restriction.regex && !restriction.regex.test(text)) {
+ return restriction.hint;
+ }
+ return undefined;
+ },
+ },
],
};
default: {
diff --git a/packages/challenger-ui/src/pages/Setup.tsx b/packages/challenger-ui/src/pages/Setup.tsx
@@ -75,12 +75,15 @@ export function Setup({
);
},
(ok) => {
- start({
- nonce: ok.body.nonce,
- clientId,
- redirectURL: url,
- state: encodeCrock(randomBytes(32)),
- });
+ start();
+
+ const redirect = new URL(window.location.href)
+ redirect.searchParams.set("client_id",clientId)
+ redirect.searchParams.set("redirect_uri",url)
+ redirect.searchParams.set("state",encodeCrock(randomBytes(32)))
+ redirect.searchParams.set("nonce",ok.body.nonce)
+ redirect.hash = ""
+ window.location.href = redirect.href
onCreated();
},