Nostro/implementations/Nostro.App-Bunq.cpp

118 lines
8 KiB
C++
Raw Normal View History

feat(app): add the Nostro app, a window for phones and desktops Linux phones have no banking apps, and a command line is no answer on a phone. The app does everything the nostro command does and, like it, only talks to nostrod: it holds no key and no token. - Nostro.Ui, a toolkit whose screens are functions that fill a frame, ported from imsd-messages ca5cd72d; Nostro.App, the app's state, forms and screens; Nostro.Ui.Shell, which puts them on a Wayland window with Crafter.Graphics; nostro-app; and Nostro.DebugCommands for the --debug-api build. - Client: every method also in an asynchronous form for an sd-event loop, with DaemonMissing and OutcomeUnknown. - nostrod's Flatpak is net.catcrafts.Nostrod now and the app takes net.catcrafts.Nostro; install.sh moves the state. The app's Flatpak, both Flatpaks cross-built for aarch64, and the licence texts they carry. - nostro-fake-system, a made-up bus and bank with a real nostrod on them, to look at the app against. The README's screenshots are taken on it. - The nostro command: an API key's environment is the key's own to say, so --sandbox is for bunq-oauth alone, and a virtual card is listed as VIRTUAL. The published contract changes in three places, before any release (design rule 10): - Transactions' query takes `stored`: answered from what nostrod has stored, without asking the bank. The app searches with it; before, every search was a call to bunq. - AddBunq's `environment` no longer chooses over what the key says of itself: a key of the other environment is refused. - SetCardStatus refuses to freeze or unfreeze a virtual card. bunq reports one as DEACTIVATED however well it pays, and leaves it as it is when asked, without an error. The app is GPL-3.0-only, the library stays LGPL-3.0-only, and no code moved from one to the other (design rule 13). Transaction's schema is unchanged. Verified: the 17 tests pass with every line, function and region of what they link run, and the five lint runs are clean. Every screen was looked at on the made-up system, at a phone's and a desktop's sizes in the light and the dark look, on Plasma under Wayland. What is said of bunq's cards comes from its sandbox, with throwaway users, and from one real virtual card. Known to have been used against production: connecting, and reading accounts, payments and cards. Not known to have been tried: a payment from the app against bunq, OAuth against bunq, and anything on a phone. Building the app takes --graphics=, a Crafter.Graphics checkout on its branch claude/nostro-app, until that branch is merged and pinned here. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 12:27:14 +01:00
// 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<AddedConnector> 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<AddedConnector> then) { return bank.AddBunqOAuth(bunqOAuth.sandbox ? "sandbox" : "production", bunqOAuth.clientId.value, bunqOAuth.secret.value, bunqOAuth.redirectUri.value, code, std::move(then)); });
}
}