Skip to content

Commit

Permalink
IOS-2816 Reduced debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
megakoko committed Jan 12, 2023
1 parent 6a0217a commit b012f09
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 39 deletions.
30 changes: 7 additions & 23 deletions TangemSdk/TangemSdk/Common/Services/Secure/BiometricsStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ public class BiometricsStorage {
public init() {}

public func get(_ account: String, context: LAContext? = nil) throws -> Data? {
Log.debug("BiometricsStorage \(account) get - fetching key")

let query: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrAccount: account,
Expand All @@ -32,34 +30,26 @@ public class BiometricsStorage {
var result: AnyObject?

let status = SecItemCopyMatching(query as CFDictionary, &result)
Log.debug("BiometricsStorage \(account) get - status \(status.message) \(status)")
Log.debug("BiometricsStorage get - status \(status.message) \(status). Data size \((result as? Data)?.count ?? -1)")

switch status {
switch status {
case errSecSuccess:
guard let data = result as? Data else {
Log.debug("BiometricsStorage \(account) get - data nil")
return nil
}

Log.debug("BiometricsStorage \(account) get - data not nil")
return data
case errSecItemNotFound:
Log.debug("BiometricsStorage \(account) get - not found")
return nil
case errSecUserCanceled:
Log.debug("BiometricsStorage \(account) get - user cancelled")
throw TangemSdkError.userCancelled
case let status:
Log.debug("BiometricsStorage \(account) get - error \(status.message)")

let error = KeyStoreError("Keychain read failed: \(status.message)")
throw error
}
}

public func store(_ object: Data, forKey account: String, overwrite: Bool = true, context: LAContext? = nil) throws {
Log.debug("BiometricsStorage \(account) set - setting data")

let query: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrAccount: account,
Expand All @@ -71,6 +61,8 @@ public class BiometricsStorage {

var status = SecItemAdd(query as CFDictionary, nil)

Log.debug("BiometricsStorage store - status \(status.message) \(status)")

if status == errSecDuplicateItem && overwrite {
var searchQuery: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
Expand All @@ -80,32 +72,24 @@ public class BiometricsStorage {
kSecUseAuthenticationContext: context ?? self.context
]

Log.debug("BiometricsStorage \(account) set - failed to set a duplicate, overwriting")

let attributes = [kSecValueData: object] as [String: Any]
status = SecItemUpdate(searchQuery as CFDictionary, attributes as CFDictionary)

Log.debug("BiometricsStorage set - overwrite status \(status.message) \(status)")
}

Log.debug("BiometricsStorage \(account) set - status \(status.message) \(status)")

switch status {
case errSecSuccess:
Log.debug("BiometricsStorage \(account) set - OK")
break
case errSecUserCanceled:
Log.debug("BiometricsStorage \(account) set - user cancelled")
throw TangemSdkError.userCancelled
default:
Log.debug("BiometricsStorage \(account) set - error \(status.message)")

let error = KeyStoreError("Unable to store item: \(status.message)")
throw error
}
}

public func delete(_ account : String) throws {
Log.debug("BiometricsStorage \(account) delete")

let query: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecUseDataProtectionKeychain: true,
Expand All @@ -114,7 +98,7 @@ public class BiometricsStorage {

let status = SecItemDelete(query as CFDictionary)

Log.debug("BiometricsStorage \(account) delete - status \(status.message) \(status)")
Log.debug("BiometricsStorage delete - status \(status.message) \(status)")

switch status {
case errSecItemNotFound, errSecSuccess:
Expand Down
25 changes: 9 additions & 16 deletions TangemSdk/TangemSdk/Common/Services/Secure/SecureStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public struct SecureStorage {
public init() {}

public func get(_ account: String) throws -> Data? {
Log.debug("SecureStorage \(account) get")

let query: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrAccount: account,
Expand All @@ -28,28 +26,25 @@ public struct SecureStorage {

var result: AnyObject?

switch SecItemCopyMatching(query as CFDictionary, &result) {
let status = SecItemCopyMatching(query as CFDictionary, &result)

Log.debug("SecureStorage get - status \(status.message) \(status). Data size \((result as? Data)?.count ?? -1)")

switch status {
case errSecSuccess:
guard let data = result as? Data else {
Log.debug("SecureStorage \(account) get - data nil")
return nil
}

Log.debug("SecureStorage \(account) get - data not nil")

return data
case errSecItemNotFound:
Log.debug("SecureStorage \(account) get - not found")
return nil
case let status:
Log.debug("SecureStorage \(account) get - error \(status.message) \(status)")
throw KeyStoreError("Keychain read failed: \(status.message)")
}
}

public func store(_ object: Data, forKey account: String, overwrite: Bool = true) throws {
Log.debug("SecureStorage \(account) store - overwrite \(overwrite)")

let query: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecAttrAccount: account,
Expand All @@ -60,10 +55,10 @@ public struct SecureStorage {

var status = SecItemAdd(query as CFDictionary, nil)

Log.debug("SecureStorage \(account) store - status \(status.message) \(status)")
Log.debug("SecureStorage store - status \(status.message) \(status)")

if status == errSecDuplicateItem && overwrite {
Log.debug("SecureStorage \(account) store - failed to write, overwriting")
Log.debug("SecureStorage store - failed to write, overwriting")

let searchQuery: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
Expand All @@ -74,7 +69,7 @@ public struct SecureStorage {

status = SecItemUpdate(searchQuery as CFDictionary, attributes as CFDictionary)

Log.debug("SecureStorage \(account) store - status \(status.message) \(status)")
Log.debug("SecureStorage store - overwrite status \(status.message) \(status)")
}

guard status == errSecSuccess else {
Expand All @@ -83,8 +78,6 @@ public struct SecureStorage {
}

public func delete(_ account: String) throws {
Log.debug("SecureStorage \(account) delete")

let query: [CFString: Any] = [
kSecClass: kSecClassGenericPassword,
kSecUseDataProtectionKeychain: true,
Expand All @@ -94,7 +87,7 @@ public struct SecureStorage {

let status = SecItemDelete(query as CFDictionary)

Log.debug("SecureStorage \(account) delete - status \(status.message) \(status)")
Log.debug("SecureStorage delete - status \(status.message) \(status)")

switch status {
case errSecItemNotFound, errSecSuccess:
Expand Down

0 comments on commit b012f09

Please sign in to comment.