Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let package = Package(
.library(
name: "KeychainLibrary",
type: .dynamic,
targets: ["KeychainLibrary"]),
targets: ["KeychainLibrary"])
],
targets: [
.target(
Expand All @@ -23,4 +23,3 @@ let package = Package(
],
swiftLanguageModes: [.v5]
)

14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ npm install native-keychain
```typescript
import * as keychain from 'native-keychain';

keychain.setPassword({ service: 'my-service', password: 'my-password' });

const secret = await keychain.getPassword({ service: 'my-service', requireBiometrics: true }); // 'my-password'
keychain.setPassword({
service: 'my-service',
account: 'my-account',
password: 'my-password'
});

const secret = await keychain.getPassword({
service: 'my-service',
account: 'my-account',
requireBiometrics: true
}); // 'my-password'
```

## Available methods
Expand Down
35 changes: 24 additions & 11 deletions Sources/KeychainLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import Security

// Function to add data to keychain with biometrics protection
@_cdecl("addToKeychain")
public func addToKeychain(cStringData: UnsafePointer<Int8>, cStringService: UnsafePointer<Int8>)
-> Bool
{
public func addToKeychain(
cStringData: UnsafePointer<Int8>, cStringService: UnsafePointer<Int8>,
cStringAccount: UnsafePointer<Int8>
) -> Bool {
let data = String(cString: cStringData).data(using: .utf8)!
let service = String(cString: cStringService)
let account = String(cString: cStringAccount)

if #available(macOS 10.13.4, *) {
let context = LAContext()
Expand All @@ -25,6 +27,7 @@ public func addToKeychain(cStringData: UnsafePointer<Int8>, cStringService: Unsa
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: service,
kSecAttrAccount as String: account,
kSecValueData as String: data,
kSecUseAuthenticationContext as String: context,
]
Expand All @@ -45,6 +48,7 @@ public func addToKeychain(cStringData: UnsafePointer<Int8>, cStringService: Unsa
@_cdecl("getFromKeychain")
public func getFromKeychain(
cStringService: UnsafePointer<Int8>,
cStringAccount: UnsafePointer<Int8>,
requireBiometrics: Bool,
callback: @escaping @convention(c) (
UnsafePointer<Int8>?, UnsafePointer<Int8>?
Expand All @@ -54,7 +58,10 @@ public func getFromKeychain(

do {
let service = String(cString: cStringService)
try _getFromKeychain(service: service, requireBiometrics: requireBiometrics) { result in
let account = String(cString: cStringAccount)
try _getFromKeychain(
service: service, account: account, requireBiometrics: requireBiometrics
) { result in
switch result {
case .success(let data):
callback(nil, data)
Expand All @@ -79,8 +86,8 @@ enum BiometricAuthenticationError: Error {
}

func _getFromKeychain(
service: String, requireBiometrics: Bool,
completion: @escaping @Sendable (Result<String, BiometricAuthenticationError>) -> Void
service: String, account: String, requireBiometrics: Bool,
completion: @escaping (Result<String, BiometricAuthenticationError>) -> Void
) throws {
let context = LAContext()

Expand All @@ -105,7 +112,8 @@ func _getFromKeychain(
context.evaluatePolicy(policy, localizedReason: "Access to your secret") {
success, evaluateError in
if success {
_getPassword(context: context, service: service, completion: completion)
_getPassword(
context: context, service: service, account: account, completion: completion)
} else {
if let error = evaluateError {
completion(
Expand All @@ -118,17 +126,18 @@ func _getFromKeychain(
}
}
} else {
_getPassword(context: context, service: service, completion: completion)
_getPassword(context: context, service: service, account: account, completion: completion)
}
}

func _getPassword(
context: LAContext, service: String,
completion: @escaping @Sendable (Result<String, BiometricAuthenticationError>) -> Void
context: LAContext, service: String, account: String,
completion: @escaping (Result<String, BiometricAuthenticationError>) -> Void
) {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: service,
kSecAttrAccount as String: account,
kSecUseAuthenticationContext as String: context,
kSecReturnData as String: true,
]
Expand All @@ -148,12 +157,16 @@ func _getPassword(
}

@_cdecl("deleteFromKeychain")
public func deleteFromKeychain(cStringService: UnsafePointer<Int8>) -> Bool {
public func deleteFromKeychain(
cStringService: UnsafePointer<Int8>, cStringAccount: UnsafePointer<Int8>
) -> Bool {
let service = String(cString: cStringService)
let account = String(cString: cStringAccount)

let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: service,
kSecAttrAccount as String: account,
]

let status = SecItemDelete(query as CFDictionary)
Expand Down
Loading