switch back to data - its just easier

need to do ui now
This commit is contained in:
neon443
2025-06-20 09:28:39 +01:00
parent 2e1c624bec
commit 5747fe5395
2 changed files with 21 additions and 14 deletions

View File

@@ -169,7 +169,7 @@ class SSHHandler: ObservableObject {
return return
} }
func authWithPubkey(pub: String, priv: String, pass: String) { func authWithPubkey(pub pubInp: Data, priv privInp: Data, pass: String) {
guard session != nil else { guard session != nil else {
withAnimation { authorized = false } withAnimation { authorized = false }
return return
@@ -184,8 +184,8 @@ class SSHHandler: ObservableObject {
fileManager.createFile(atPath: tempPubkey.path(), contents: nil) fileManager.createFile(atPath: tempPubkey.path(), contents: nil)
fileManager.createFile(atPath: tempKey.path(), contents: nil) fileManager.createFile(atPath: tempKey.path(), contents: nil)
try? Data(base64Encoded: pub)?.write(to: tempPubkey) try? pubInp.write(to: tempPubkey)
try? Data(base64Encoded: priv)?.write(to: tempKey) try? privInp.write(to: tempKey)
var pubkey: ssh_key? var pubkey: ssh_key?
ssh_pki_import_pubkey_file(tempPubkey.path(), &pubkey) ssh_pki_import_pubkey_file(tempPubkey.path(), &pubkey)

View File

@@ -12,8 +12,11 @@ struct ContentView: View {
@State var passphrase: String = "" @State var passphrase: String = ""
@State var pubkey: String = "" @State var pubkeyStr: String = ""
@State var privkey: String = "" @State var privkeyStr: String = ""
@State var pubkey: Data?
@State var privkey: Data?
@State var privPickerPresented: Bool = false @State var privPickerPresented: Bool = false
@State var pubPickerPresented: Bool = false @State var pubPickerPresented: Bool = false
@@ -22,7 +25,10 @@ struct ContentView: View {
NavigationStack { NavigationStack {
List { List {
HStack { HStack {
TextField("", text: $pubkey, prompt: Text("Public Key")) TextField("", text: $pubkeyStr, prompt: Text("Public Key"))
.onSubmit {
pubkey = Data(pubkeyStr.utf8)
}
Button() { Button() {
pubPickerPresented.toggle() pubPickerPresented.toggle()
} label: { } label: {
@@ -32,8 +38,7 @@ struct ContentView: View {
.fileImporter(isPresented: $pubPickerPresented, allowedContentTypes: [.item, .content, .data]) { (Result) in .fileImporter(isPresented: $pubPickerPresented, allowedContentTypes: [.item, .content, .data]) { (Result) in
do { do {
let fileURL = try Result.get() let fileURL = try Result.get()
let data = try! Data(contentsOf: fileURL) pubkey = try! Data(contentsOf: fileURL)
pubkey = data.base64EncodedString()
print(fileURL) print(fileURL)
} catch { } catch {
print(error.localizedDescription) print(error.localizedDescription)
@@ -42,7 +47,10 @@ struct ContentView: View {
} }
HStack { HStack {
TextField("", text: $pubkey, prompt: Text("Public Key")) TextField("", text: $privkeyStr, prompt: Text("Private Key"))
.onSubmit {
privkey = Data(privkeyStr.utf8)
}
Button() { Button() {
privPickerPresented.toggle() privPickerPresented.toggle()
} label: { } label: {
@@ -52,8 +60,7 @@ struct ContentView: View {
.fileImporter(isPresented: $privPickerPresented, allowedContentTypes: [.item, .content, .data]) { (Result) in .fileImporter(isPresented: $privPickerPresented, allowedContentTypes: [.item, .content, .data]) { (Result) in
do { do {
let fileURL = try Result.get() let fileURL = try Result.get()
let data = try! Data(contentsOf: fileURL) privkey = try! Data(contentsOf: fileURL)
privkey = data.base64EncodedString()
print(fileURL) print(fileURL)
} catch { } catch {
print(error.localizedDescription) print(error.localizedDescription)
@@ -106,8 +113,8 @@ struct ContentView: View {
} else { } else {
Button() { Button() {
handler.connect() handler.connect()
if !pubkey.isEmpty && !privkey.isEmpty { if pubkey != nil && privkey != nil {
handler.authWithPubkey(pub: pubkey, priv: privkey, pass: passphrase) handler.authWithPubkey(pub: pubkey!, priv: privkey!, pass: passphrase)
} else { } else {
let _ = handler.authWithPw() let _ = handler.authWithPw()
} }
@@ -116,7 +123,7 @@ struct ContentView: View {
Label("Connect", systemImage: "powerplug.portrait") Label("Connect", systemImage: "powerplug.portrait")
} }
.disabled( .disabled(
pubkey.isEmpty && privkey.isEmpty || pubkey == nil && privkey == nil ||
handler.host.username.isEmpty && handler.host.password.isEmpty handler.host.username.isEmpty && handler.host.password.isEmpty
) )
} }