diff options
author | Florian Dold <florian.dold@gmail.com> | 2019-12-06 12:47:28 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2019-12-06 12:47:28 +0100 |
commit | ee1fc03ae82f2f4662041af3d4243113e92ffeaf (patch) | |
tree | b01ab0adf1b2d551db3a4f4ee89b41379d945a54 /src/util/taleruri.ts | |
parent | b4d36fca180602d6d90440f9ba257b92fc626e6c (diff) | |
download | wallet-core-ee1fc03ae82f2f4662041af3d4243113e92ffeaf.tar.xz |
case-insensitive URIs
Diffstat (limited to 'src/util/taleruri.ts')
-rw-r--r-- | src/util/taleruri.ts | 81 |
1 files changed, 66 insertions, 15 deletions
diff --git a/src/util/taleruri.ts b/src/util/taleruri.ts index 50886a916..f34b82a59 100644 --- a/src/util/taleruri.ts +++ b/src/util/taleruri.ts @@ -15,7 +15,8 @@ */ export interface PayUriResult { - downloadUrl: string; + merchantBaseUrl: string; + orderId: string; sessionId?: string; } @@ -36,7 +37,7 @@ export interface TipUriResult { export function parseWithdrawUri(s: string): WithdrawUriResult | undefined { const pfx = "taler://withdraw/"; - if (!s.startsWith(pfx)) { + if (!s.toLowerCase().startsWith(pfx)) { return undefined; } @@ -44,6 +45,20 @@ export function parseWithdrawUri(s: string): WithdrawUriResult | undefined { let [host, path, withdrawId] = rest.split("/"); + if (!host) { + return undefined; + } + + host = host.toLowerCase(); + + if (!path) { + return undefined; + } + + if (!withdrawId) { + return undefined; + } + if (path === "-") { path = "api/withdraw-operation"; } @@ -53,15 +68,45 @@ export function parseWithdrawUri(s: string): WithdrawUriResult | undefined { }; } -export function parsePayUri(s: string): PayUriResult | undefined { - if (s.startsWith("https://") || s.startsWith("http://")) { - return { - downloadUrl: s, - sessionId: undefined, - }; +export const enum TalerUriType { + TalerPay = "taler-pay", + TalerWithdraw = "taler-withdraw", + TalerTip = "taler-tip", + TalerRefund = "taler-refund", + TalerNotifyReserve = "taler-notify-reserve", + Unknown = "unknown", +} + +export function classifyTalerUri(s: string): TalerUriType { + const sl = s.toLowerCase(); + if (sl.startsWith("taler://pay/")) { + return TalerUriType.TalerPay; } + if (sl.startsWith("taler://tip/")) { + return TalerUriType.TalerTip; + } + if (sl.startsWith("taler://refund/")) { + return TalerUriType.TalerRefund; + } + if (sl.startsWith("taler://withdraw/")) { + return TalerUriType.TalerWithdraw; + } + if (sl.startsWith("taler://notify-reserve/")) { + return TalerUriType.TalerWithdraw; + } + return TalerUriType.Unknown; + +} + +export function getOrderDownloadUrl(merchantBaseUrl: string, orderId: string) { + const u = new URL("proposal", merchantBaseUrl); + u.searchParams.set("order_id", orderId); + return u.href +} + +export function parsePayUri(s: string): PayUriResult | undefined { const pfx = "taler://pay/"; - if (!s.startsWith(pfx)) { + if (!s.toLowerCase().startsWith(pfx)) { return undefined; } @@ -75,6 +120,8 @@ export function parsePayUri(s: string): PayUriResult | undefined { return undefined; } + host = host.toLowerCase(); + if (!maybePath) { return undefined; } @@ -99,21 +146,21 @@ export function parsePayUri(s: string): PayUriResult | undefined { protocol = "http"; } - const downloadUrl = + const merchantBaseUrl = `${protocol}://${host}/` + decodeURIComponent(maybePath) + - maybeInstancePath + - `proposal?order_id=${orderId}`; + maybeInstancePath; return { - downloadUrl, + merchantBaseUrl, + orderId, sessionId: maybeSessionid, }; } export function parseTipUri(s: string): TipUriResult | undefined { const pfx = "taler://tip/"; - if (!s.startsWith(pfx)) { + if (!s.toLowerCase().startsWith(pfx)) { return undefined; } @@ -125,6 +172,8 @@ export function parseTipUri(s: string): TipUriResult | undefined { return undefined; } + host = host.toLowerCase(); + if (!maybePath) { return undefined; } @@ -155,7 +204,7 @@ export function parseTipUri(s: string): TipUriResult | undefined { export function parseRefundUri(s: string): RefundUriResult | undefined { const pfx = "taler://refund/"; - if (!s.startsWith(pfx)) { + if (!s.toLowerCase().startsWith(pfx)) { return undefined; } @@ -167,6 +216,8 @@ export function parseRefundUri(s: string): RefundUriResult | undefined { return undefined; } + host = host.toLowerCase(); + if (!maybePath) { return undefined; } |