Merge pull request #87 from calvinweb/system_privatekey

DRAFT:Add support of reading system privatekey (.ssh/id_rsa)
This commit is contained in:
lollipopkit
2023-08-04 18:50:52 +08:00
committed by GitHub
3 changed files with 38 additions and 1 deletions

View File

@@ -1,5 +1,4 @@
import 'dart:io'; import 'dart:io';
import 'package:file_picker/file_picker.dart'; import 'package:file_picker/file_picker.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
@@ -77,3 +76,14 @@ String getTime(int? unixMill) {
String pathJoin(String path1, String path2) { String pathJoin(String path1, String path2) {
return path1 + (path1.endsWith('/') ? '' : '/') + path2; return path1 + (path1.endsWith('/') ? '' : '/') + path2;
} }
String getHome() {
String? home = "";
Map<String, String> envVars = Platform.environment;
if (isMacOS ||isLinux) {
home = envVars['HOME'];
} else if (isWindows) {
home = envVars['UserProfile'];
}
return home??"";
}

View File

@@ -1,3 +1,6 @@
import 'dart:io';
import 'package:toolbox/core/utils/misc.dart' show getHome, pathJoin;
import 'package:toolbox/data/model/app/error.dart';
import 'package:hive_flutter/hive_flutter.dart'; import 'package:hive_flutter/hive_flutter.dart';
part 'private_key_info.g.dart'; part 'private_key_info.g.dart';
@@ -29,3 +32,15 @@ class PrivateKeyInfo {
return data; return data;
} }
} }
class SystemPrivateKeyInfo extends PrivateKeyInfo {
SystemPrivateKeyInfo() : super("System private key", "", "");
Future getKey() async {
File idRsaFile = File(pathJoin(getHome(), ".ssh/id_rsa"));
if (!await idRsaFile.exists()) {
this.privateKey="";
}
this.privateKey= await idRsaFile.readAsString();
}
}

View File

@@ -1,7 +1,10 @@
import 'dart:async';
import 'package:toolbox/core/persistant_store.dart'; import 'package:toolbox/core/persistant_store.dart';
import 'package:toolbox/data/model/server/private_key_info.dart'; import 'package:toolbox/data/model/server/private_key_info.dart';
import 'package:toolbox/core/utils/platform.dart';
class PrivateKeyStore extends PersistentStore { class PrivateKeyStore extends PersistentStore {
late SystemPrivateKeyInfo systemPrivateKeyInfo;
void put(PrivateKeyInfo info) { void put(PrivateKeyInfo info) {
box.put(info.id, info); box.put(info.id, info);
} }
@@ -15,10 +18,19 @@ class PrivateKeyStore extends PersistentStore {
ps.add(s); ps.add(s);
} }
} }
if (isLinux || isMacOS) {
SystemPrivateKeyInfo sysPk = SystemPrivateKeyInfo();
unawaited(sysPk.getKey());
systemPrivateKeyInfo = sysPk;
ps.add(sysPk);
}
return ps; return ps;
} }
PrivateKeyInfo? get(String? id) { PrivateKeyInfo? get(String? id) {
if (id == "System private key") {
return this.systemPrivateKeyInfo;
}
if (id == null) return null; if (id == null) return null;
return box.get(id); return box.get(id);
} }