This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented biometric authentication based on FaceID or TouchID, pass…
…word is stored in iOS encrypted keychain
- Loading branch information
Showing
10 changed files
with
209 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// | ||
// Biometry.swift | ||
// LibrePass | ||
// | ||
// Created by Zapomnij on 19/04/2024. | ||
// | ||
|
||
import Foundation | ||
import LocalAuthentication | ||
|
||
func setUpBiometricalAuthentication(password: String) async -> Bool { | ||
let context = LAContext() | ||
|
||
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil) { | ||
do { | ||
try await context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "Save password in keychain") | ||
|
||
guard let access = SecAccessControlCreateWithFlags(nil, kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly, .userPresence, nil) else { | ||
return false | ||
} | ||
|
||
guard let passwordData = password.data(using: .utf8) else { | ||
return false | ||
} | ||
|
||
let query: [String: Any] = | ||
[kSecClass as String: kSecClassGenericPassword, | ||
kSecAttrService as String: "LibrePass", | ||
kSecAttrAccessControl as String: access, | ||
kSecUseAuthenticationContext as String: context, | ||
kSecValueData as String: passwordData as Data] | ||
SecItemDelete(query as CFDictionary) | ||
|
||
let status = SecItemAdd(query as CFDictionary, nil) | ||
if status == errSecSuccess { | ||
return true | ||
} | ||
} catch { | ||
|
||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func accessKeychain() async -> String? { | ||
let context = LAContext() | ||
|
||
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil) { | ||
do { | ||
try await context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "Save password in keychain") | ||
|
||
let query: [String: Any] = | ||
[kSecClass as String: kSecClassGenericPassword, | ||
kSecAttrService as String: "LibrePass", | ||
kSecMatchLimit as String: kSecMatchLimitOne, | ||
kSecReturnAttributes as String: true, | ||
kSecUseAuthenticationContext as String: context, | ||
kSecReturnData as String: true] | ||
|
||
var item: CFTypeRef? | ||
let status = SecItemCopyMatching(query as CFDictionary, &item) | ||
print(status) | ||
if status == errSecSuccess { | ||
if let decoded = item as? [String: Any], let passwordData = decoded[kSecValueData as String] as? Data, let password = String(data: passwordData, encoding: .utf8) { | ||
return password | ||
} | ||
} | ||
} catch { | ||
|
||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func disableBiometricAuthentication() async -> Bool { | ||
let context = LAContext() | ||
|
||
if context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil) { | ||
do { | ||
try await context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "Save password in keychain") | ||
|
||
let query: [String: Any] = | ||
[kSecClass as String: kSecClassGenericPassword, | ||
kSecAttrService as String: "LibrePass", | ||
kSecReturnAttributes as String: true, | ||
kSecUseAuthenticationContext as String: context, | ||
kSecReturnData as String: true] | ||
|
||
let status = SecItemDelete(query as CFDictionary) | ||
if status == errSecSuccess { | ||
return true | ||
} | ||
} catch { | ||
|
||
} | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict/> | ||
</plist> |