Switching between accounts

This commit is contained in:
Pengwius 2021-03-08 22:55:41 +01:00
parent ee8f6e240a
commit e584eb51a6
3 changed files with 35 additions and 11 deletions

View file

@ -20,7 +20,7 @@ public func apiRequest(endpointURL: String) -> URLRequest {
let account = keychain[id] ?? "[]"
let data = Data(account.utf8)
let accountJSON = try! JSON(data: data)
let fingerprint: String = "\(accountJSON["fingerprint"])"
let privateKeyStringString: String = "\(accountJSON["privateKeyString"])"
let privateKeyString: Data = Data(privateKeyStringString.utf8)

View file

@ -15,18 +15,19 @@ func getSignatures(request: URLRequest, fingerprint: String, privateKeyString: D
}
// Create SecKey
let attributes = [
let attributes = [
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecAttrKeyClass: kSecAttrKeyClassPrivate,
]
guard let privateKeyData = Data(base64Encoded: privateKeyString),
]
guard let privateKeyData = Data(base64Encoded: privateKeyString),
let secKey = SecKeyCreateWithData(privateKeyData as NSData, attributes as NSDictionary, nil) else {
return "\(Sdk.APIError.noPrivateKey)"
}
}
// Get fingerprint
guard let signatureValues = Sdk.Signer.getSignatureValues(body: request.httpBody, url: urlString, privateKey: secKey, fingerprint: fingerprint) else {
return "\(Sdk.APIError.noPrivateKey)"
return "noFingerprint"
}
// Headers

View file

@ -8,13 +8,13 @@
import SwiftUI
import KeychainAccess
import SwiftyJSON
import Sdk
struct AccountManagerView: View {
@State private var showLoginModal = false
@State private var showEditAccountModal = false
@AppStorage("isLogged") private var isLogged: Bool = false
private func getStudentsNames() -> [String] {
//getting all accounts
let keychain = Keychain()
@ -52,8 +52,31 @@ struct AccountManagerView: View {
return json
}
private func setActualAccount() {
print("setting...")
private func setActualAccount(id: String) {
let keychain = Keychain()
let accountData = keychain["\(id)"] ?? "{}"
let data: Data = Data(accountData.utf8)
let accountJSON = try! JSON(data: data)
let RestURL = "\(accountJSON["account"]["RestURL"])api/mobile/register/hebe"
keychain["actualAccountId"] = "\(id)"
keychain["actualAccountEmail"] = "\(accountJSON["account"]["UserName"])"
let apiResponseRequest = apiRequest(endpointURL: "\(RestURL)")
let session = URLSession.shared
session.dataTask(with: apiResponseRequest) { (data, response, error) in
if let error = error {
// Handle HTTP request error
print(error)
} else if let data = data {
// Handle HTTP request response
let responseBody = String(data: data, encoding: String.Encoding.utf8)
keychain["actualStudentHebe"] = "\(responseBody!)"
} else {
// Handle unexpected error
}
}.resume()
}
var body: some View {
@ -68,7 +91,7 @@ struct AccountManagerView: View {
let data: Data = Data(studentString.utf8)
let studentJSON = try! JSON(data: data)
let studentEmail = "\(studentJSON["account"]["UserName"])"
Button("\(studentEmail)") { setActualAccount() }
Button("\(studentEmail)") { setActualAccount(id: student) }
.foregroundColor(Color("customControlColor"))
if("\(keychain["actualAccountId"] ?? "0")" == "\(student)") {
Image(systemName: "checkmark.circle")