implement Digest Auth
Godwin Asuquo committed
Feb 2, 2022 at 15:36 UTC
9e1753539f0161d97a41e7e31193d60cca1918e3
2 files changed
+49
-12
cw_core/lib/digest_auth.dart
new
+41
@@ -0,0 +1,41 @@
1
+import 'dart:convert';
2
+import 'dart:io';
3
+
4
+import 'package:http/http.dart' as http;
5
+import 'package:http/io_client.dart' as ioc;
6
+
7
+class DigestAuth{
8
+ Future<http.Response>request({
9
+ String uri,
10
+ String login = "",
11
+ String password = "",
12
+ }) async {
13
+ final path = '/json_rpc';
14
+ final rpcUri = Uri.http(uri, path);
15
+ final realm = 'monero-rpc';
16
+ final postMap = {
17
+ 'jsonrpc': '2.0',
18
+ 'id': '0',
19
+ 'method': 'get_info'
20
+ };
21
+ final authenticatingClient = HttpClient();
22
+
23
+ authenticatingClient.addCredentials(
24
+ rpcUri,
25
+ realm,
26
+ HttpClientDigestCredentials(login ?? "", password ?? ""),
27
+ );
28
+
29
+ final http.Client client = ioc.IOClient(authenticatingClient);
30
+
31
+ final response = await client.post(
32
+ rpcUri,
33
+ headers: {'Content-Type': 'application/json'},
34
+ body: json.encode(postMap),
35
+ );
36
+
37
+ client.close();
38
+
39
+ return response;
40
+ }
41
+}
\ No newline at end of file
cw_core/lib/node.dart
+8
-12
@@ -1,5 +1,6 @@
1
import 'dart:io';
2
3
+import 'package:cw_core/digest_auth.dart';
4
import 'package:cw_core/keyable.dart';
5
import 'package:flutter/foundation.dart';
6
import 'dart:convert';
@@ -95,20 +96,15 @@ class Node extends HiveObject with Keyable {
96
}
97
98
Future<bool> requestMoneroNode() async {
98
- try {
99
- Map<String, dynamic> resBody;
100
- final rpcUri = Uri.http(uri.authority, '/json_rpc');
101
- final headers = {'Content-type': 'application/json'};
102
- final body =
103
- json.encode({'jsonrpc': '2.0', 'id': '0', 'method': 'get_info'});
104
- final response =
105
- await http.post(rpcUri.toString(), headers: headers, body: body);
106
- resBody = json.decode(response.body) as Map<String, dynamic>;
107
- return !(resBody['result']['offline'] as bool);
108
- } catch (_) {
99
+ final digestAuth = DigestAuth();
100
+ try {
101
+ final response = await digestAuth.request(uri: uri.authority, login: login, password: password);
102
+ final resBody = json.decode(response.body) as Map<String, dynamic>;
103
+ return !(resBody['result']['offline'] as bool);
104
+ } catch (_) {
105
return false;
106
}
111
- }
107
+}
108
109
Future<bool> requestElectrumServer() async {
110
try {