Saving private key

This commit is contained in:
Pengwius 2021-02-26 14:42:16 +01:00
parent 1483dd8592
commit ee65697324
6 changed files with 62 additions and 6 deletions

View file

@ -11,6 +11,7 @@
5C1794B825E8FE08007AD91A /* notes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C1794B725E8FE08007AD91A /* notes.swift */; };
5C1794BC25E8FE19007AD91A /* settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C1794BB25E8FE19007AD91A /* settings.swift */; };
5C1794C025E8FE27007AD91A /* about.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C1794BF25E8FE27007AD91A /* about.swift */; };
5C1794CD25E90DBD007AD91A /* KeychainAccess in Frameworks */ = {isa = PBXBuildFile; productRef = 5C1794CC25E90DBD007AD91A /* KeychainAccess */; };
5C2D331025E64F3C000253AC /* grades.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C2D330F25E64F3C000253AC /* grades.swift */; };
5C2D331425E650EC000253AC /* exams.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C2D331325E650EC000253AC /* exams.swift */; };
5C2D331825E651C4000253AC /* homework.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C2D331725E651C4000253AC /* homework.swift */; };
@ -106,6 +107,7 @@
files = (
5C9B6F4925D6C08D00C3F5F5 /* Sdk in Frameworks */,
5CCAE31625DA4CDD00D87580 /* OpenSSL in Frameworks */,
5C1794CD25E90DBD007AD91A /* KeychainAccess in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -275,6 +277,7 @@
packageProductDependencies = (
5C9B6F4825D6C08D00C3F5F5 /* Sdk */,
5CCAE31525DA4CDD00D87580 /* OpenSSL */,
5C1794CC25E90DBD007AD91A /* KeychainAccess */,
);
productName = wulkanowy;
productReference = F4C6D9042544E17400F8903A /* wulkanowy.app */;
@ -350,6 +353,7 @@
mainGroup = F4C6D8FB2544E17300F8903A;
packageReferences = (
5CCAE31025DA4CCA00D87580 /* XCRemoteSwiftPackageReference "OpenSSL" */,
5C1794CB25E90DBD007AD91A /* XCRemoteSwiftPackageReference "KeychainAccess" */,
);
productRefGroup = F4C6D9052544E17400F8903A /* Products */;
projectDirPath = "";
@ -736,6 +740,14 @@
/* End XCConfigurationList section */
/* Begin XCRemoteSwiftPackageReference section */
5C1794CB25E90DBD007AD91A /* XCRemoteSwiftPackageReference "KeychainAccess" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/kishikawakatsumi/KeychainAccess";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 4.2.1;
};
};
5CCAE31025DA4CCA00D87580 /* XCRemoteSwiftPackageReference "OpenSSL" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/krzyzanowskim/OpenSSL";
@ -747,6 +759,11 @@
/* End XCRemoteSwiftPackageReference section */
/* Begin XCSwiftPackageProductDependency section */
5C1794CC25E90DBD007AD91A /* KeychainAccess */ = {
isa = XCSwiftPackageProductDependency;
package = 5C1794CB25E90DBD007AD91A /* XCRemoteSwiftPackageReference "KeychainAccess" */;
productName = KeychainAccess;
};
5C9B6F4825D6C08D00C3F5F5 /* Sdk */ = {
isa = XCSwiftPackageProductDependency;
productName = Sdk;

View file

@ -1,6 +1,15 @@
{
"object": {
"pins": [
{
"package": "KeychainAccess",
"repositoryURL": "https://github.com/kishikawakatsumi/KeychainAccess",
"state": {
"branch": null,
"revision": "654d52d30f3dd4592e944c3e0bccb53178c992f6",
"version": "4.2.1"
}
},
{
"package": "OpenSSL",
"repositoryURL": "https://github.com/krzyzanowskim/OpenSSL",

View file

@ -11,6 +11,7 @@
"token" = "Token";
"symbol" = "Symbol";
"pin" = "Pin";
"deviceName" = "Device name";
"loginButton" = "Login";
"wrongToken" = "Wrong token";
"wrongSymbol" = "Wrong symbol";

View file

@ -11,6 +11,7 @@
"token" = "Token";
"symbol" = "Symbol";
"pin" = "Pin";
"deviceName" = "Nazwa urządzenia";
"loginButton" = "Zaloguj";
"wrongToken" = "Zły token";
"wrongSymbol" = "Zły symbol";

View file

@ -156,7 +156,7 @@ struct LoginView: View {
.stroke(setColor(input: "pin"), lineWidth: 2)
)
TextField("Device name", text: $deviceModel)
TextField("deviceName", text: $deviceModel)
.autocapitalization(.none)
.disableAutocorrection(true)
.font(Font.body.weight(Font.Weight.medium))

View file

@ -7,27 +7,55 @@
import Combine
import Sdk
import Foundation
import KeychainAccess
final class VulcanStore: ObservableObject {
static let shared: VulcanStore = VulcanStore()
var privateKey: String?
let sdk: Sdk?
private init() {
// Check for stored certificate
guard let certificate: X509 = try? X509(serialNumber: 1, certificateEntries: ["CN": "APP_CERTIFICATE CA Certificate"]) else {
sdk = nil
privateKey = nil
return
}
guard let privateKeyRawData = certificate.getPrivateKeyData(format: .DER),
let privateKeyString = String(data: privateKeyRawData, encoding: .utf8)?
.split(separator: "\n")
.dropFirst()
.dropLast()
.joined() else {
privateKey = nil
sdk = nil
return
}
privateKey = privateKeyString
sdk = Sdk(certificate: certificate)
}
public func login(token: String, symbol: String, pin: String, deviceModel: String, completionHandler: @escaping (Error?) -> Void) {
sdk?.login(token: token, symbol: symbol, pin: pin, deviceModel: deviceModel) { error in
if error == nil {
// Success - save certificate
sdk?.login(token: token, symbol: symbol, pin: pin, deviceModel: deviceModel) { [self] error in
if let error = error {
// Wyobraź sobie, że tutaj jest obsługa błędów. Wyobraź, bo mi sie jej robić nie chciało.
print(error)
} else {
// Error - discard or try again
let privateKeyToSave: String = privateKey ?? ""
let utf8str = privateKeyToSave.data(using: .utf8)
if let base64Encoded = utf8str?.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)) {
let keychain = Keychain()
keychain[string: "privateKey"] = base
let token = keychain["privateKey"]
print("Encoded: \(token)")
}
}
completionHandler(error)