mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-03 09:29:48 +00:00
implement Digest Auth
This commit is contained in:
parent
b90e610327
commit
9e1753539f
2 changed files with 49 additions and 12 deletions
41
cw_core/lib/digest_auth.dart
Normal file
41
cw_core/lib/digest_auth.dart
Normal file
|
@ -0,0 +1,41 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http/io_client.dart' as ioc;
|
||||
|
||||
class DigestAuth{
|
||||
Future<http.Response>request({
|
||||
String uri,
|
||||
String login = "",
|
||||
String password = "",
|
||||
}) async {
|
||||
final path = '/json_rpc';
|
||||
final rpcUri = Uri.http(uri, path);
|
||||
final realm = 'monero-rpc';
|
||||
final postMap = {
|
||||
'jsonrpc': '2.0',
|
||||
'id': '0',
|
||||
'method': 'get_info'
|
||||
};
|
||||
final authenticatingClient = HttpClient();
|
||||
|
||||
authenticatingClient.addCredentials(
|
||||
rpcUri,
|
||||
realm,
|
||||
HttpClientDigestCredentials(login ?? "", password ?? ""),
|
||||
);
|
||||
|
||||
final http.Client client = ioc.IOClient(authenticatingClient);
|
||||
|
||||
final response = await client.post(
|
||||
rpcUri,
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: json.encode(postMap),
|
||||
);
|
||||
|
||||
client.close();
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:cw_core/digest_auth.dart';
|
||||
import 'package:cw_core/keyable.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'dart:convert';
|
||||
|
@ -95,20 +96,15 @@ class Node extends HiveObject with Keyable {
|
|||
}
|
||||
|
||||
Future<bool> requestMoneroNode() async {
|
||||
try {
|
||||
Map<String, dynamic> resBody;
|
||||
final rpcUri = Uri.http(uri.authority, '/json_rpc');
|
||||
final headers = {'Content-type': 'application/json'};
|
||||
final body =
|
||||
json.encode({'jsonrpc': '2.0', 'id': '0', 'method': 'get_info'});
|
||||
final response =
|
||||
await http.post(rpcUri.toString(), headers: headers, body: body);
|
||||
resBody = json.decode(response.body) as Map<String, dynamic>;
|
||||
return !(resBody['result']['offline'] as bool);
|
||||
} catch (_) {
|
||||
final digestAuth = DigestAuth();
|
||||
try {
|
||||
final response = await digestAuth.request(uri: uri.authority, login: login, password: password);
|
||||
final resBody = json.decode(response.body) as Map<String, dynamic>;
|
||||
return !(resBody['result']['offline'] as bool);
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> requestElectrumServer() async {
|
||||
try {
|
||||
|
|
Loading…
Reference in a new issue