// SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® module Nostro.App:Bunq_impl; import std; import Nostro; import Nostro.Ui; import :App; import :Banks; import :Format; import :Forms; import :Errors; // bunq's part of the app: the two forms a connection to it is made with, which are the last step of adding a // connection in the settings, and what their buttons do. What bunq is called, which ways it has and what each lets a // connection do is in :Banks; what is filled in here, and how it is checked, in :Forms. Once the connection is made // the app treats it like any bank's. namespace Nostro::App { namespace { using namespace Ui; } // With an API key, made in bunq's own app. It is the user's own key, and does whatever bunq lets the user do. void App::BunqKeyPage(Ui::Frame& frame, Ui::Column& column) { column.Advance(Paragraph(frame, Padded(frame, column.Next()), "Make an API key in the bunq app, under Profile, Security, API keys, and enter it here. Nostro's service keeps it; this window forgets it once the connection is made.", Body(frame))); // What was typed is never shown, only enough of it to tell one key from another. column.Advance(TextField(frame, Padded(frame, column.Next()), bunqKey.key, {.key = "key.key", .label = "API key", .error = bunqKey.tried ? bunqKey.Check() : std::string(), .helper = bunqKey.key.value.empty() ? std::string() : std::format("Typed: {}", Fingerprint(bunqKey.key.value))})); // Which of bunq's environments the key is of is the key's own to say, not something to choose here. column.Advance(Paragraph(frame, Padded(frame, column.Next()), "A key that starts with sandbox_ connects to bunq's sandbox, where no real money moves.", Muted(frame))); Button(frame, Padded(frame, column.Take(frame.Dp(Size::control - Size::unit * 2))), {.key = "key.advanced", .label = bunqKey.advanced ? "Fewer settings" : "More settings", .kind = ButtonKind::Plain, .onTap = [this] { bunqKey.advanced = !bunqKey.advanced; context.Invalidate(); }}); if(bunqKey.advanced) { column.Advance(TextField(frame, Padded(frame, column.Next()), bunqKey.ips, {.key = "key.ips", .label = "Addresses the key may be used from", .helper = "* for any address; empty for this computer's address as it is now"})); } Button(frame, Padded(frame, column.Take(frame.Dp(Size::control))), {.key = "key.connect", .label = "Connect", .onTap = [this] { ConnectBunqKey(); }}); } void App::ConnectBunqKey() { bunqKey.tried = true; context.Invalidate(); if(!bunqKey.Check().empty()) { return; } // The key is read out of its field when the call is made, and kept there until the connection is: a mistyped // password at the dialog should not cost typing it again. Connect("AddBunq", [this](Client& bank, Client::Then then) { return bank.AddBunq(bunqKey.key.value, bunqKey.ips.value, {}, std::move(then)); }); } // Through OAuth, for an OAuth client the user registered with bunq: the client's registration first, then the // address to approve at, and the address the browser ends up at once that is done. void App::BunqOAuthPage(Ui::Frame& frame, Ui::Column& column) { if(bunqOAuth.address.empty()) { const BunqOAuthForm::Errors errors = bunqOAuth.tried ? bunqOAuth.Check() : BunqOAuthForm::Errors(); column.Advance(Paragraph(frame, Padded(frame, column.Next()), "For an OAuth client registered with bunq: enter what bunq gave it. A connection made this way can pay your own accounts and make draft payments, and nothing else.", Body(frame))); column.Advance(TextField(frame, Padded(frame, column.Next()), bunqOAuth.clientId, {.key = "oauth.client", .label = "Client id", .error = errors.clientId})); column.Advance(TextField(frame, Padded(frame, column.Next()), bunqOAuth.redirectUri, {.key = "oauth.redirect", .label = "Redirect address", .placeholder = "https://…", .error = errors.redirectUri, .purpose = TextPurpose::Url})); column.Advance(TextField(frame, Padded(frame, column.Next()), bunqOAuth.secret, {.key = "oauth.secret", .label = "Client secret", .error = errors.secret, .helper = bunqOAuth.secret.value.empty() ? std::string() : std::format("Typed: {}", Fingerprint(bunqOAuth.secret.value))})); Label(frame, Padded(frame, column.Take(frame.Dp(20))), "Environment", Muted(frame)); Segmented(frame, Padded(frame, column.Take(frame.Dp(Size::control))), {.key = "oauth.environment", .labels = {"Production", "Sandbox"}, .selected = bunqOAuth.sandbox ? std::size_t{1} : std::size_t{0}, .onSelect = [this](std::size_t chosen) { bunqOAuth.sandbox = chosen == 1; context.Invalidate(); }}); Button(frame, Padded(frame, column.Take(frame.Dp(Size::control))), {.key = "oauth.continue", .label = "Continue", .onTap = [this] { BeginBunqOAuth(); }}); return; } column.Advance(Paragraph(frame, Padded(frame, column.Next()), "Open this address and approve the connection with the bunq app, which may be on another device:", Body(frame))); column.Advance(Paragraph(frame, Padded(frame, column.Next()), bunqOAuth.address, Muted(frame))); Row open(Padded(frame, column.Take(frame.Dp(Size::control))), frame.Dp(Size::unit * 3)); Button(frame, open.TakeLeft((open.area.w - open.gap) / 2), {.key = "oauth.open", .label = "Open in browser", .enabled = host.openUri != nullptr, .onTap = [this] { host.openUri(bunqOAuth.address, [this](bool taken) { if(!taken) { Tell("No browser could be opened: copy the address instead"); } }); }}); Button(frame, open.Rest(), {.key = "oauth.copy", .label = "Copy the address", .kind = ButtonKind::Tonal, .onTap = [this] { context.Copy(bunqOAuth.address); Tell("Address copied"); }}); column.Advance(Paragraph(frame, Padded(frame, column.Next()), "Once you have approved, your browser is sent to the redirect address. Copy the whole address it ends up at and enter it here:", Body(frame))); column.Advance(TextField(frame, Padded(frame, column.Next()), bunqOAuth.redirected, {.key = "oauth.redirected", .label = "The address your browser was sent to", .error = bunqOAuthError, .purpose = TextPurpose::Url, .onSubmit = [this] { ConnectBunqOAuth(); }})); Button(frame, Padded(frame, column.Take(frame.Dp(Size::control))), {.key = "oauth.connect", .label = "Connect", .onTap = [this] { ConnectBunqOAuth(); }}); Button(frame, Padded(frame, column.Take(frame.Dp(Size::control - Size::unit * 2))), {.key = "oauth.restart", .label = "Start over", .kind = ButtonKind::Plain, .onTap = [this] { bunqOAuth.address.clear(); bunqOAuth.redirected.Clear(); bunqOAuthError.clear(); context.Invalidate(); }}); } void App::BeginBunqOAuth() { bunqOAuth.tried = true; context.Invalidate(); if(bunqOAuth.Check().Any()) { return; } bunqOAuth.Begin(host.oauthState ? host.oauthState() : Crypto::RandomHex(16)); bunqOAuthError.clear(); } void App::ConnectBunqOAuth() { context.Invalidate(); std::string code; try { code = bunqOAuth.Code(); } catch(const std::runtime_error& e) { // bunq did not authorize, the address has no code, or it answers another request than this one. bunqOAuthError = e.what(); return; } bunqOAuthError.clear(); // nostrod trades the code for its token and keeps it: the token never comes here. Connect("AddBunqOAuth", [this, code](Client& bank, Client::Then then) { return bank.AddBunqOAuth(bunqOAuth.sandbox ? "sandbox" : "production", bunqOAuth.clientId.value, bunqOAuth.secret.value, bunqOAuth.redirectUri.value, code, std::move(then)); }); } }