diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-09-06 13:31:30 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-09-06 13:31:30 +0200 |
commit | 93327f5274d3a081d56a41df0809f4c4e7535c99 (patch) | |
tree | 69543157f624db5dc9238451dacd268ea4f8bdb3 /lib/wallet/wxMessaging.ts | |
parent | dd2bd324918e7a2e70ba4532c78eb0e8b0543992 (diff) | |
download | wallet-core-93327f5274d3a081d56a41df0809f4c4e7535c99.tar.xz |
skeleton for webrequest
Diffstat (limited to 'lib/wallet/wxMessaging.ts')
-rw-r--r-- | lib/wallet/wxMessaging.ts | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/wallet/wxMessaging.ts b/lib/wallet/wxMessaging.ts index a8dc27524..68c744901 100644 --- a/lib/wallet/wxMessaging.ts +++ b/lib/wallet/wxMessaging.ts @@ -230,6 +230,50 @@ class ChromeNotifier implements Notifier { } } +function executePayment(contractHash: string, payUrl: string, offerUrl: string) { + +} + +function offerContractFromUrl(contractUrl: string) { + +} + + +function handleHttpPayment(headerList: chrome.webRequest.HttpHeader[], url) { + const headers = {}; + for (let kv of headerList) { + headers[kv.name.toLowerCase()] = kv.value; + } + + const contractUrl = headers["x-taler-contract-url"]; + if (contractUrl !== undefined) { + // The web shop is proposing a contract, we need to fetch it + // and show it to the user + offerContractFromUrl(contractUrl); + return; + } + + const contractHash = headers["x-taler-contract-hash"]; + + if (contractHash !== undefined) { + const payUrl = headers["x-taler-pay-url"]; + if (payUrl === undefined) { + console.log("malformed 402, X-Taler-Pay-Url missing"); + return; + } + + // Offer URL is optional + const offerUrl = headers["x-taler-offer-url"]; + executePayment(contractHash, payUrl, offerUrl); + return; + } + + // looks like it's not a taler request, it might be + // for a different payment system (or the shop is buggy) + console.log("ignoring non-taler 402 response"); + +} + export function wxMain() { chrome.browserAction.setBadgeText({text: ""}); @@ -260,6 +304,9 @@ export function wxMain() { let badge = new ChromeBadge(); let notifier = new ChromeNotifier(); let wallet = new Wallet(db, http, badge, notifier); + + // Handlers for messages coming directly from the content + // script on the page let handlers = makeHandlers(db, wallet); chrome.runtime.onMessage.addListener((req, sender, sendResponse) => { try { @@ -276,6 +323,17 @@ export function wxMain() { return false; } }); + + // Handlers for catching HTTP requests + chrome.webRequest.onHeadersReceived.addListener((details) => { + if (details.statusCode != 402) { + return; + } + return handleHttpPayment(details.responseHeaders, details.url); + details.responseHeaders + }, {urls: ["<all_urls>"]}, ["responseHeaders"]); + + }) .catch((e) => { console.error("could not initialize wallet messaging"); |