diff options
author | Sebastian <sebasjm@gmail.com> | 2024-04-21 14:57:13 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2024-04-22 08:52:56 -0300 |
commit | d046653246d4b397bf81cfadbdfe2d91b46c7e7f (patch) | |
tree | 3cfb8e53eff69b17752767574e0b718fdfd2c974 /packages/challenger-ui/src/Routing.tsx | |
parent | 94b2530f2f9ea0e0efdf6e933f6160105265a2c6 (diff) | |
download | wallet-core-d046653246d4b397bf81cfadbdfe2d91b46c7e7f.tar.xz |
challenger spa
Diffstat (limited to 'packages/challenger-ui/src/Routing.tsx')
-rw-r--r-- | packages/challenger-ui/src/Routing.tsx | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/packages/challenger-ui/src/Routing.tsx b/packages/challenger-ui/src/Routing.tsx new file mode 100644 index 000000000..e1e9434e5 --- /dev/null +++ b/packages/challenger-ui/src/Routing.tsx @@ -0,0 +1,178 @@ +/* + 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 <http://www.gnu.org/licenses/> + */ + +import { + urlPattern, + useCurrentLocation, + useNavigationContext, +} from "@gnu-taler/web-util/browser"; +import { Fragment, VNode, h } from "preact"; + +import { assertUnreachable } from "@gnu-taler/taler-util"; +import { AnswerChallenge } from "./pages/AnswerChallenge.js"; +import { AskChallenge } from "./pages/AskChallenge.js"; +import { Frame } from "./pages/Frame.js"; +import { MissingParams } from "./pages/MissingParams.js"; +import { NonceNotFound } from "./pages/NonceNotFound.js"; +import { StartChallenge } from "./pages/StartChallenge.js"; +import { Setup } from "./pages/Setup.js"; +import { CallengeCompleted } from "./pages/CallengeCompleted.js"; + +export function Routing(): VNode { + // check session and defined if this is + // public routing or private + return ( + <Frame> + <PublicRounting /> + </Frame> + ); +} + +const publicPages = { + authorize: urlPattern<{ nonce: string }>( + /\/authorize\/(?<nonce>[a-zA-Z0-9]+)/, + ({ nonce }) => `#/authorize/${nonce}`, + ), + ask: urlPattern<{ nonce: string }>( + /\/ask\/(?<nonce>[a-zA-Z0-9]+)/, + ({ nonce }) => `#/ask/${nonce}`, + ), + answer: urlPattern<{ nonce: string }>( + /\/answer\/(?<nonce>[a-zA-Z0-9]+)/, + ({ nonce }) => `#/answer/${nonce}`, + ), + completed: urlPattern<{ nonce: string }>( + /\/completed\/(?<nonce>[a-zA-Z0-9]+)/, + ({ nonce }) => `#/completed/${nonce}`, + ), + setup: urlPattern<{ client: string }>( + /\/setup\/(?<client>[0-9]+)/, + ({ client }) => `#/setup/${client}`, + ), +}; + +function safeGetParam( + ps: Record<string, string[]>, + n: string, +): string | undefined { + if (!ps[n] || ps[n].length == 0) return undefined; + return ps[n][0]; +} + +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(); + + if (location === undefined) { + return <NonceNotFound />; + } + + switch (location.name) { + case "setup": { + return ( + <Setup + clientId={location.values.client} + onCreated={(nonce) => { + navigateTo(publicPages.ask.url({ nonce })) + //response_type=code + //client_id=1 + //redirect_uri=http://exchange.taler.test:1180/kyc-proof/kyc-provider-wallet + //state=123 + }} + /> + ); + } + case "authorize": { + const responseType = safeGetParam(location.params, "response_type"); + const clientId = safeGetParam(location.params, "client_id"); + const redirectURI = safeToURL( + safeGetParam(location.params, "redirect_uri"), + ); + const state = safeGetParam(location.params, "state"); + // http://localhost:8080/app/#/authorize/ASDASD123?response_type=code&client_id=1&redirect_uri=goog.ecom&state=123 + // + + // http://localhost:8080/app/?response_type=code&client_id=1&redirect_uri=http://exchange.taler.test:1180/kyc-proof/kyc-provider-wallet&state=123#/authorize/X9668AR2CFC26X55H0M87GJZXGM45VD4SZE05C5SNS5FADPWN220 + + if ( + !responseType || + !clientId || + !redirectURI || + !state || + responseType !== "code" + ) { + return <MissingParams />; + } + return ( + <StartChallenge + nonce={location.values.nonce} + clientId={clientId} + redirectURL={redirectURI} + state={state} + onSendSuccesful={() => { + navigateTo( + publicPages.ask.url({ + nonce: location.values.nonce, + }), + ); + }} + /> + ); + } + case "ask": { + return ( + <AskChallenge + nonce={location.values.nonce} + onSendSuccesful={() => { + navigateTo( + publicPages.answer.url({ + nonce: location.values.nonce, + }), + ); + }} + /> + ); + } + case "answer": { + return ( + <AnswerChallenge + nonce={location.values.nonce} + onComplete={() => { + navigateTo( + publicPages.completed.url({ + nonce: location.values.nonce, + }), + ); + }} + /> + ); + } + case "completed": { + return <CallengeCompleted nonce={location.values.nonce} />; + } + default: + assertUnreachable(location); + } +} |