Skip to content

Commit

Permalink
added saving values to keychain with biometric authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmitrevski committed Dec 18, 2018
1 parent 66d6766 commit 6550005
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 110 deletions.
4 changes: 0 additions & 4 deletions GirdersSwift.xcodeproj/project.pbxproj
Expand Up @@ -40,7 +40,6 @@
CA7DAA051F866D140087BEA5 /* Configuration-env.plist in Resources */ = {isa = PBXBuildFile; fileRef = CA7DAA041F866D140087BEA5 /* Configuration-env.plist */; };
CAD5E2381F4C5DCD000B4DCE /* TestUtilURL.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAD5E2371F4C5DCD000B4DCE /* TestUtilURL.swift */; };
CAD5E23A1F4C5DDA000B4DCE /* TestUtilString.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAD5E2391F4C5DDA000B4DCE /* TestUtilString.swift */; };
CAD5E23C1F4C5DEB000B4DCE /* TestUtilDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAD5E23B1F4C5DEB000B4DCE /* TestUtilDictionary.swift */; };
CAEBA3201F4477A2003C9193 /* TestRequestGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAEBA31F1F4477A2003C9193 /* TestRequestGenerator.swift */; };
CAEBA3221F447CC1003C9193 /* TestHTTPUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = CAEBA3211F447CC1003C9193 /* TestHTTPUtil.swift */; };
E81F717220B5FEF5001B9B24 /* SecureStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = E81F717120B5FEF5001B9B24 /* SecureStorage.swift */; };
Expand Down Expand Up @@ -129,7 +128,6 @@
CA7DAA041F866D140087BEA5 /* Configuration-env.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Configuration-env.plist"; sourceTree = "<group>"; };
CAD5E2371F4C5DCD000B4DCE /* TestUtilURL.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestUtilURL.swift; sourceTree = "<group>"; };
CAD5E2391F4C5DDA000B4DCE /* TestUtilString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestUtilString.swift; sourceTree = "<group>"; };
CAD5E23B1F4C5DEB000B4DCE /* TestUtilDictionary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestUtilDictionary.swift; sourceTree = "<group>"; };
CAEBA31F1F4477A2003C9193 /* TestRequestGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestRequestGenerator.swift; sourceTree = "<group>"; };
CAEBA3211F447CC1003C9193 /* TestHTTPUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestHTTPUtil.swift; sourceTree = "<group>"; };
D3C42DCD874731A80E7D079E /* Pods-GirdersSwift.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-GirdersSwift.release.xcconfig"; path = "Pods/Target Support Files/Pods-GirdersSwift/Pods-GirdersSwift.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -286,7 +284,6 @@
CAEBA3211F447CC1003C9193 /* TestHTTPUtil.swift */,
CAD5E2371F4C5DCD000B4DCE /* TestUtilURL.swift */,
CAD5E2391F4C5DDA000B4DCE /* TestUtilString.swift */,
CAD5E23B1F4C5DEB000B4DCE /* TestUtilDictionary.swift */,
CA74F82D1F4ECF5B00B1318F /* TestEndpoint.swift */,
CA1C227E1F55565F00229381 /* TestRequest.swift */,
CA3F78761F5EE674007F52EF /* TestHttpClient.swift */,
Expand Down Expand Up @@ -732,7 +729,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
CAD5E23C1F4C5DEB000B4DCE /* TestUtilDictionary.swift in Sources */,
CAD5E23A1F4C5DDA000B4DCE /* TestUtilString.swift in Sources */,
CAD5E2381F4C5DCD000B4DCE /* TestUtilURL.swift in Sources */,
CA19213D1F5567C6002615ED /* TestSerialization.swift in Sources */,
Expand Down
49 changes: 49 additions & 0 deletions GirdersSwift/src/main/swift/storage/KeychainStorage.swift
Expand Up @@ -56,4 +56,53 @@ public class KeychainStorage: SecureStorage {
}
}

//MARK: - biometric protection
public func saveWithBiometricProtection(string: String?,
forKey key: String,
accessibility: Accessibility,
authenticationPolicy: AuthenticationPolicy) {
guard let string = string else {
do {
try keychain.remove(key)
} catch {
Log.error("Error removing keychain items")
}
return
}
DispatchQueue.global().async { [unowned self] in
do {
try self.keychain
.accessibility(accessibility, authenticationPolicy: authenticationPolicy)
.set(string, key: key)
} catch {
Log.error("Error saving value")
}
}
}

public func saveWithBiometricProtection(string: String?, forKey key: String) {
self.saveWithBiometricProtection(string: string,
forKey: key,
accessibility: .whenPasscodeSetThisDeviceOnly,
authenticationPolicy: .userPresence)
}

public func biometricProtectedString(forKey key: String,
withPrompt prompt: String,
result: @escaping (String?) -> Void) {
DispatchQueue.global().async { [unowned self] in
do {
let value = try self.keychain.authenticationPrompt(prompt).get(key)
DispatchQueue.main.async {
result(value)
}
} catch {
Log.error("Error retrieving the protected value")
DispatchQueue.main.async {
result(nil)
}
}
}
}

}
6 changes: 6 additions & 0 deletions GirdersSwift/src/main/swift/storage/SecureStorage.swift
Expand Up @@ -17,4 +17,10 @@ public protocol SecureStorage {
func data(forKey key: String) -> Data?
/// Clears the secure storage.
func clearSecureStorage()
/// Saves a string with biometric protection.
func saveWithBiometricProtection(string: String?, forKey key: String)
/// Retrieves a string saved with biometric protection.
func biometricProtectedString(forKey key: String,
withPrompt prompt: String,
result: @escaping (String?) -> Void)
}
106 changes: 0 additions & 106 deletions GirdersSwift/src/test/swift/http/TestUtilDictionary.swift

This file was deleted.

0 comments on commit 6550005

Please sign in to comment.