Line data Source code
1 : // Copyright 2026 The terCAD team. All rights reserved.
2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.
3 :
4 : import 'dart:convert';
5 :
6 : import 'package:app_finance/_classes/controller/deep_link/deep_link_handler.dart';
7 : import 'package:app_finance/_classes/controller/encryption_handler.dart';
8 : import 'package:app_finance/_classes/integration/enablebanking/enablebanking_settings.dart';
9 : import 'package:app_finance/_classes/integration/monzo/monzo_client.dart';
10 : import 'package:app_finance/_classes/storage/app_preferences.dart';
11 : import 'package:flutter/foundation.dart';
12 : import 'package:uuid/uuid.dart';
13 :
14 : class MonzoSettings {
15 : // Native applications are opened by Monzo's redirect directly (deep link)
16 : static const callbackUrl = '${DeepLinkHandler.scheme}://integration${DeepLinkHandler.monzoPath}';
17 :
18 : // The web page "callback.html" is shared with Enable Banking: it recognizes Monzo by the prefix of the state
19 : static const statePrefix = 'monzo.';
20 :
21 0 : static String get defaultRedirectUrl => redirectUrlFor(kIsWeb ? Uri.base : null);
22 :
23 0 : @visibleForTesting
24 : static String redirectUrlFor(Uri? base) {
25 0 : if (base != null && (base.scheme == 'https' || base.scheme == 'http') && base.host.isNotEmpty) {
26 0 : return base.resolve(EnableBankingSettings.callbackPage).toString();
27 : }
28 : return callbackUrl;
29 : }
30 :
31 0 : static String newState() => '$statePrefix${const Uuid().v4()}';
32 :
33 : String clientId;
34 : String redirectUrl;
35 : String authorizationState;
36 :
37 : // First successful request after the approval in the Monzo app: the full history is readable for a few minutes
38 : DateTime? approvedAt;
39 :
40 : // Monzo account id to the uuid of the application's account
41 : final Map<String, String> links;
42 :
43 : // Monzo account id to the last day transactions were imported for
44 : final Map<String, DateTime> imported;
45 :
46 0 : MonzoSettings({
47 : this.clientId = '',
48 : this.redirectUrl = '',
49 : this.authorizationState = '',
50 : this.approvedAt,
51 : Map<String, String>? links,
52 : Map<String, DateTime>? imported,
53 0 : }) : links = links ?? {},
54 0 : imported = imported ?? {};
55 :
56 0 : DateTime? get fullHistoryUntil => approvedAt?.add(MonzoClient.fullHistoryWindow);
57 :
58 0 : Map<String, dynamic> toJson() => {
59 0 : 'clientId': clientId,
60 0 : 'redirectUrl': redirectUrl,
61 0 : 'authorizationState': authorizationState,
62 0 : 'approvedAt': approvedAt?.toIso8601String(),
63 0 : 'links': links,
64 0 : 'imported': imported.map((key, value) => MapEntry(key, value.toIso8601String())),
65 : };
66 :
67 0 : factory MonzoSettings.fromJson(Map<String, dynamic> json) => MonzoSettings(
68 0 : clientId: json['clientId'] ?? '',
69 0 : redirectUrl: json['redirectUrl'] ?? '',
70 0 : authorizationState: json['authorizationState'] ?? '',
71 0 : approvedAt: DateTime.tryParse((json['approvedAt'] ?? '').toString()),
72 0 : links: (json['links'] as Map<String, dynamic>? ?? {}).map((key, value) => MapEntry(key, value.toString())),
73 0 : imported: (json['imported'] as Map<String, dynamic>? ?? {}).map(
74 0 : (key, value) => MapEntry(key, DateTime.parse(value.toString())),
75 : ),
76 : );
77 :
78 0 : static MonzoSettings load() {
79 0 : final value = AppPreferences.get(AppPreferences.prefMonzo);
80 0 : if (value == null || value.isEmpty) {
81 0 : return MonzoSettings();
82 : }
83 : try {
84 0 : return MonzoSettings.fromJson(jsonDecode(value));
85 0 : } on FormatException {
86 0 : return MonzoSettings();
87 : }
88 : }
89 :
90 0 : Future<void> save() => AppPreferences.set(AppPreferences.prefMonzo, jsonEncode(toJson()));
91 :
92 0 : static String? _decrypt(String key) {
93 0 : final value = AppPreferences.get(key);
94 0 : if (value == null || value.isEmpty) {
95 : return null;
96 : }
97 : try {
98 0 : return EncryptionHandler.decrypt(value);
99 : } catch (_) {
100 : return null;
101 : }
102 : }
103 :
104 0 : static String? loadSecret() => _decrypt(AppPreferences.prefMonzoSecret);
105 :
106 0 : static Future<void> saveSecret(String secret) =>
107 0 : AppPreferences.set(AppPreferences.prefMonzoSecret, EncryptionHandler.encrypt(secret));
108 :
109 0 : static Future<void> clearSecret() => AppPreferences.clear(AppPreferences.prefMonzoSecret);
110 :
111 0 : static MonzoTokens? loadTokens() {
112 0 : final value = _decrypt(AppPreferences.prefMonzoTokens);
113 : if (value == null) {
114 : return null;
115 : }
116 : try {
117 0 : final tokens = MonzoTokens.fromJson(jsonDecode(value));
118 0 : return tokens.accessToken.isEmpty ? null : tokens;
119 : } catch (_) {
120 : return null;
121 : }
122 : }
123 :
124 0 : static Future<void> saveTokens(MonzoTokens tokens) =>
125 0 : AppPreferences.set(AppPreferences.prefMonzoTokens, EncryptionHandler.encrypt(jsonEncode(tokens.toJson())));
126 :
127 0 : static Future<void> clearTokens() => AppPreferences.clear(AppPreferences.prefMonzoTokens);
128 : }
|