Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| afe8c773bd | |||
| 776f67ec89 | |||
| bb3d349993 | |||
|
|
cea23bb06e | ||
|
|
45727cf6cc | ||
|
|
54446ff314 | ||
|
|
774a58c5fb | ||
|
|
b9c0749257 | ||
|
|
d9814c2cc8 | ||
|
|
6d0ba0d767 | ||
|
|
0604692010 | ||
|
|
d2bc31815a | ||
|
|
15284371dc | ||
|
|
32ba85936c | ||
|
|
0c902f0842 | ||
|
|
7c4e5c205d | ||
|
|
40c4396b7e | ||
|
|
a85afe2413 | ||
|
|
b294163866 | ||
|
|
2f4fa2e61a | ||
|
|
51ee0ec8d7 | ||
|
|
ebd2234deb |
88
.gitignore
vendored
88
.gitignore
vendored
@@ -1,18 +1,9 @@
|
|||||||
# Xcode
|
## General
|
||||||
#
|
.DS_Store
|
||||||
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
|
.swiftpm
|
||||||
|
.build/
|
||||||
|
|
||||||
## User settings
|
## Various settings
|
||||||
xcuserdata/
|
|
||||||
|
|
||||||
## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
|
|
||||||
*.xcscmblueprint
|
|
||||||
*.xccheckout
|
|
||||||
|
|
||||||
## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
|
|
||||||
build/
|
|
||||||
DerivedData/
|
|
||||||
*.moved-aside
|
|
||||||
*.pbxuser
|
*.pbxuser
|
||||||
!default.pbxuser
|
!default.pbxuser
|
||||||
*.mode1v3
|
*.mode1v3
|
||||||
@@ -21,70 +12,5 @@ DerivedData/
|
|||||||
!default.mode2v3
|
!default.mode2v3
|
||||||
*.perspectivev3
|
*.perspectivev3
|
||||||
!default.perspectivev3
|
!default.perspectivev3
|
||||||
|
xcuserdata/
|
||||||
## Obj-C/Swift specific
|
*.xcuserdatad/
|
||||||
*.hmap
|
|
||||||
|
|
||||||
## App packaging
|
|
||||||
*.ipa
|
|
||||||
*.dSYM.zip
|
|
||||||
*.dSYM
|
|
||||||
|
|
||||||
## Playgrounds
|
|
||||||
timeline.xctimeline
|
|
||||||
playground.xcworkspace
|
|
||||||
|
|
||||||
# Swift Package Manager
|
|
||||||
#
|
|
||||||
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
|
|
||||||
# Packages/
|
|
||||||
# Package.pins
|
|
||||||
# Package.resolved
|
|
||||||
# *.xcodeproj
|
|
||||||
#
|
|
||||||
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
|
|
||||||
# hence it is not needed unless you have added a package configuration file to your project
|
|
||||||
# .swiftpm
|
|
||||||
|
|
||||||
.build/
|
|
||||||
|
|
||||||
# CocoaPods
|
|
||||||
#
|
|
||||||
# We recommend against adding the Pods directory to your .gitignore. However
|
|
||||||
# you should judge for yourself, the pros and cons are mentioned at:
|
|
||||||
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
|
||||||
#
|
|
||||||
# Pods/
|
|
||||||
#
|
|
||||||
# Add this line if you want to avoid checking in source code from the Xcode workspace
|
|
||||||
# *.xcworkspace
|
|
||||||
|
|
||||||
# Carthage
|
|
||||||
#
|
|
||||||
# Add this line if you want to avoid checking in source code from Carthage dependencies.
|
|
||||||
# Carthage/Checkouts
|
|
||||||
|
|
||||||
Carthage/Build/
|
|
||||||
|
|
||||||
# Accio dependency management
|
|
||||||
Dependencies/
|
|
||||||
.accio/
|
|
||||||
|
|
||||||
# fastlane
|
|
||||||
#
|
|
||||||
# It is recommended to not store the screenshots in the git repo.
|
|
||||||
# Instead, use fastlane to re-generate the screenshots whenever they are needed.
|
|
||||||
# For more information about the recommended setup visit:
|
|
||||||
# https://docs.fastlane.tools/best-practices/source-control/#source-control
|
|
||||||
|
|
||||||
fastlane/report.xml
|
|
||||||
fastlane/Preview.html
|
|
||||||
fastlane/screenshots/**/*.png
|
|
||||||
fastlane/test_output
|
|
||||||
|
|
||||||
# Code Injection
|
|
||||||
#
|
|
||||||
# After new code Injection tools there's a generated folder /iOSInjectionProject
|
|
||||||
# https://github.com/johnno1962/injectionforxcode
|
|
||||||
|
|
||||||
iOSInjectionProject/
|
|
||||||
|
|||||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2020 Aleksey Zgurskiy
|
Copyright (c) 2025 ANGD Dev
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|||||||
15
Package.swift
Normal file
15
Package.swift
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
// swift-tools-version: 5.10
|
||||||
|
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||||
|
|
||||||
|
import PackageDescription
|
||||||
|
|
||||||
|
let package = Package(
|
||||||
|
name: "KeychainKit",
|
||||||
|
platforms: [.macOS(.v10_15), .iOS(.v13)],
|
||||||
|
products: [
|
||||||
|
.library(name: "KeychainKit", targets: ["KeychainKit"]),
|
||||||
|
],
|
||||||
|
targets: [
|
||||||
|
.target(name: "KeychainKit")
|
||||||
|
]
|
||||||
|
)
|
||||||
61
README.md
61
README.md
@@ -1 +1,60 @@
|
|||||||
# keychain-kit
|
# KeychainKit
|
||||||
|
|
||||||
|
KeychainKit is a type-safe, easy-to-use wrapper around Apple’s Keychain service that supports storing, retrieving, and deleting data with optional local authentication.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
This library enables working with Keychain without losing control over security settings while simplifying type-safe access to data types like `Data`, `String`, `UUID`, and any `Codable` types.
|
||||||
|
|
||||||
|
It supports optional authentication via `LAContext`, allowing integration with Face ID, Touch ID, or device passcode.
|
||||||
|
|
||||||
|
KeychainKit does not hide the complexity of Keychain operations but provides a clean API and convenient error handling via a custom `KeychainError` type.
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
- **Swift**: 5.10+
|
||||||
|
- **Platforms**: macOS 10.15+, iOS 13.0+
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
To add KeychainKit to your project, use Swift Package Manager (SPM).
|
||||||
|
|
||||||
|
### Adding to an Xcode Project
|
||||||
|
|
||||||
|
1. Open your project in Xcode.
|
||||||
|
2. Navigate to the `File` menu and select `Add Package Dependencies`.
|
||||||
|
3. Enter the repository URL: `https://github.com/angd-dev/keychain-kit.git`
|
||||||
|
4. Choose the version to install (e.g., `2.0.0`).
|
||||||
|
5. Add the library to your target module.
|
||||||
|
|
||||||
|
### Adding to Package.swift
|
||||||
|
|
||||||
|
If you are using Swift Package Manager with a `Package.swift` file, add the dependency like this:
|
||||||
|
|
||||||
|
```swift
|
||||||
|
// swift-tools-version: 5.10
|
||||||
|
import PackageDescription
|
||||||
|
|
||||||
|
let package = Package(
|
||||||
|
name: "YourProject",
|
||||||
|
dependencies: [
|
||||||
|
.package(url: "https://github.com/angd-dev/keychain-kit.git", from: "2.0.0")
|
||||||
|
],
|
||||||
|
targets: [
|
||||||
|
.target(
|
||||||
|
name: "YourTarget",
|
||||||
|
dependencies: [
|
||||||
|
.product(name: "KeychainKit", package: "keychain-kit")
|
||||||
|
]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Additional Resources
|
||||||
|
|
||||||
|
For more information and usage examples, see the [documentation](https://docs.angd.dev/?package=keychain-kit&version=2.0.0).
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
This project is licensed under the MIT License. See the `LICENSE` file for details.
|
||||||
|
|||||||
272
Sources/KeychainKit/Classes/KeychainStorage.swift
Normal file
272
Sources/KeychainKit/Classes/KeychainStorage.swift
Normal file
@@ -0,0 +1,272 @@
|
|||||||
|
import Foundation
|
||||||
|
import LocalAuthentication
|
||||||
|
import Security
|
||||||
|
|
||||||
|
/// A type-safe storage abstraction over the Keychain service.
|
||||||
|
///
|
||||||
|
/// Supports storing, retrieving, and deleting generic data associated with
|
||||||
|
/// accounts and services, with optional local authentication context support.
|
||||||
|
///
|
||||||
|
/// ## Topics
|
||||||
|
///
|
||||||
|
/// ### Initializers
|
||||||
|
///
|
||||||
|
/// - ``init(service:context:)``
|
||||||
|
///
|
||||||
|
/// ### Instance Properties
|
||||||
|
///
|
||||||
|
/// - ``service``
|
||||||
|
/// - ``context``
|
||||||
|
///
|
||||||
|
/// ### Retrieving Values
|
||||||
|
///
|
||||||
|
/// - ``get(_:)-5u61a``
|
||||||
|
/// - ``get(_:)-502rt``
|
||||||
|
/// - ``get(_:)-63a3x``
|
||||||
|
/// - ``get(_:decoder:)``
|
||||||
|
///
|
||||||
|
/// ### Storing Values
|
||||||
|
///
|
||||||
|
/// - ``set(_:for:)-7053g``
|
||||||
|
/// - ``set(_:for:)-99s6o``
|
||||||
|
/// - ``set(_:for:)-2e1p6``
|
||||||
|
/// - ``set(_:for:encoder:)``
|
||||||
|
///
|
||||||
|
/// ### Deleting Values
|
||||||
|
///
|
||||||
|
/// - ``delete(_:)``
|
||||||
|
public final class KeychainStorage<
|
||||||
|
Account: KeychainAccountProtocol,
|
||||||
|
Service: KeychainServiceProtocol
|
||||||
|
>: KeychainStorageProtocol {
|
||||||
|
// MARK: - Properties
|
||||||
|
|
||||||
|
/// The service metadata associated with this Keychain storage instance.
|
||||||
|
public let service: Service?
|
||||||
|
|
||||||
|
/// An optional local authentication context used for biometric or passcode protection.
|
||||||
|
public let context: LAContext?
|
||||||
|
|
||||||
|
// MARK: - Inits
|
||||||
|
|
||||||
|
/// Creates a new `KeychainStorage` instance with the given service and authentication context.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - service: An optional `Service` instance representing the keychain service metadata.
|
||||||
|
/// - context: An optional `LAContext` instance for authentication protection.
|
||||||
|
public init(service: Service?, context: LAContext?) {
|
||||||
|
self.service = service
|
||||||
|
self.context = context
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Methods
|
||||||
|
|
||||||
|
/// Retrieves raw `Data` stored in Keychain for the specified account.
|
||||||
|
///
|
||||||
|
/// - Parameter account: The account identifier conforming to `KeychainAccountProtocol`.
|
||||||
|
/// - Returns: The raw data associated with the given account.
|
||||||
|
/// - Throws: ``KeychainError/itemNotFound`` when no keychain item matches the query.
|
||||||
|
/// - Throws: ``KeychainError/authenticationFailed`` if biometric or device authentication fails.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedData`` if the stored data is missing or corrupted.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedCode(_:)`` for any other OSStatus error returned by the Keychain API.
|
||||||
|
public func get(_ account: Account) throws(KeychainError) -> Data {
|
||||||
|
var query: [CFString: Any] = [
|
||||||
|
kSecClass: kSecClassGenericPassword,
|
||||||
|
kSecAttrAccount: account.identifier,
|
||||||
|
kSecAttrSynchronizable: account.synchronizable,
|
||||||
|
kSecUseDataProtectionKeychain: true,
|
||||||
|
kSecMatchLimit: kSecMatchLimitOne,
|
||||||
|
kSecReturnAttributes: true,
|
||||||
|
kSecReturnData: true
|
||||||
|
]
|
||||||
|
|
||||||
|
query[kSecAttrService] = service?.identifier
|
||||||
|
query[kSecAttrAccessGroup] = service?.accessGroup
|
||||||
|
query[kSecUseAuthenticationContext] = context
|
||||||
|
|
||||||
|
var queryResult: AnyObject?
|
||||||
|
let status = withUnsafeMutablePointer(to: &queryResult) {
|
||||||
|
SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0))
|
||||||
|
}
|
||||||
|
|
||||||
|
switch status {
|
||||||
|
case errSecSuccess:
|
||||||
|
guard
|
||||||
|
let item = queryResult as? [CFString : AnyObject],
|
||||||
|
let data = item[kSecValueData] as? Data
|
||||||
|
else { throw KeychainError.unexpectedData }
|
||||||
|
return data
|
||||||
|
case errSecItemNotFound:
|
||||||
|
throw KeychainError.itemNotFound
|
||||||
|
case errSecAuthFailed:
|
||||||
|
throw KeychainError.authenticationFailed
|
||||||
|
default:
|
||||||
|
throw KeychainError.unexpectedCode(status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Retrieves a UTF-8 encoded string stored in Keychain for the specified account.
|
||||||
|
///
|
||||||
|
/// - Parameter account: The account identifier conforming to `KeychainAccountProtocol`.
|
||||||
|
/// - Returns: The stored string value associated with the account.
|
||||||
|
/// - Throws: ``KeychainError/itemNotFound`` when no keychain item matches the query.
|
||||||
|
/// - Throws: ``KeychainError/authenticationFailed`` if biometric or device authentication fails.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedData`` if the stored data cannot be decoded as UTF-8.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedCode(_:)`` for any other OSStatus error returned by the Keychain API.
|
||||||
|
public func get(_ account: Account) throws(KeychainError) -> String {
|
||||||
|
guard let value = String(data: try get(account), encoding: .utf8) else {
|
||||||
|
throw KeychainError.unexpectedData
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Retrieves a `UUID` stored in Keychain for the specified account.
|
||||||
|
///
|
||||||
|
/// - Parameter account: The account identifier conforming to `KeychainAccountProtocol`.
|
||||||
|
/// - Returns: The stored UUID value associated with the account.
|
||||||
|
/// - Throws: ``KeychainError/itemNotFound`` when no keychain item matches the query.
|
||||||
|
/// - Throws: ``KeychainError/authenticationFailed`` if biometric or device authentication fails.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedData`` if the stored string is missing or is not a valid UUID.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedCode(_:)`` for any other OSStatus error returned by the Keychain API.
|
||||||
|
public func get(_ account: Account) throws(KeychainError) -> UUID {
|
||||||
|
guard let value = UUID(uuidString: try get(account)) else {
|
||||||
|
throw KeychainError.unexpectedData
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Retrieves a value of type `T` stored in Keychain, decoded from JSON using the provided decoder.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - account: The account identifier conforming to `KeychainAccountProtocol`.
|
||||||
|
/// - decoder: The `JSONDecoder` instance used to decode the data (default is a new instance).
|
||||||
|
/// - Returns: The decoded value of type `T`.
|
||||||
|
/// - Throws: ``KeychainError/itemNotFound`` when no keychain item matches the query.
|
||||||
|
/// - Throws: ``KeychainError/authenticationFailed`` if biometric or device authentication fails.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedData`` if the stored data is missing or corrupted.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedCode(_:)`` for any OSStatus error returned by the Keychain API.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedError(_:)`` if decoding the data into `T` fails.
|
||||||
|
public func get<T: Decodable>(
|
||||||
|
_ account: Account,
|
||||||
|
decoder: JSONDecoder = .init()
|
||||||
|
) throws(KeychainError) -> T {
|
||||||
|
let value: Data = try get(account)
|
||||||
|
do {
|
||||||
|
return try decoder.decode(T.self, from: value)
|
||||||
|
} catch {
|
||||||
|
throw KeychainError.unexpectedError(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stores raw `Data` in the Keychain for the specified account, replacing any existing value.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - value: The raw data to store in the Keychain.
|
||||||
|
/// - account: The account identifier conforming to `KeychainAccountProtocol`.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedError(_:)`` if access control creation fails.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedCode(_:)`` if adding the item to the Keychain fails.
|
||||||
|
/// - Throws: Any error thrown by ``delete(_:)`` if the previous value cannot be removed.
|
||||||
|
public func set(_ value: Data, for account: Account) throws(KeychainError) {
|
||||||
|
try delete(account)
|
||||||
|
|
||||||
|
var error: Unmanaged<CFError>?
|
||||||
|
let access = SecAccessControlCreateWithFlags(
|
||||||
|
nil, account.protection, account.accessFlags, &error
|
||||||
|
)
|
||||||
|
|
||||||
|
guard let access else {
|
||||||
|
throw KeychainError.unexpectedError(error?.takeUnretainedValue())
|
||||||
|
}
|
||||||
|
|
||||||
|
var query: [CFString: Any] = [
|
||||||
|
kSecClass: kSecClassGenericPassword,
|
||||||
|
kSecAttrAccount: account.identifier,
|
||||||
|
kSecAttrSynchronizable: account.synchronizable,
|
||||||
|
kSecUseDataProtectionKeychain: true,
|
||||||
|
kSecAttrAccessControl: access,
|
||||||
|
kSecValueData: value
|
||||||
|
]
|
||||||
|
|
||||||
|
query[kSecAttrService] = service?.identifier
|
||||||
|
query[kSecAttrAccessGroup] = service?.accessGroup
|
||||||
|
query[kSecUseAuthenticationContext] = context
|
||||||
|
|
||||||
|
let status = SecItemAdd(query as CFDictionary, nil)
|
||||||
|
guard status == noErr else {
|
||||||
|
throw KeychainError.unexpectedCode(status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stores a UTF-8 encoded string in the Keychain for the specified account.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - value: The string value to store.
|
||||||
|
/// - account: The account identifier conforming to `KeychainAccountProtocol`.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedError(_:)`` if access control creation fails.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedCode(_:)`` if adding the item to the Keychain fails.
|
||||||
|
/// - Throws: Any error thrown by ``set(_:for:)-7053g`` if encoding or insertion fails.
|
||||||
|
public func set(_ value: String, for account: Account) throws(KeychainError) {
|
||||||
|
try set(value.data(using: .utf8)!, for: account)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stores a `UUID` value as a string in the Keychain for the specified account.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - value: The UUID value to store.
|
||||||
|
/// - account: The account identifier conforming to `KeychainAccountProtocol`.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedError(_:)`` if access control creation fails.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedCode(_:)`` if adding the item to the Keychain fails.
|
||||||
|
/// - Throws: Any error thrown by ``set(_:for:)-7053g`` if encoding or insertion fails.
|
||||||
|
public func set(_ value: UUID, for account: Account) throws(KeychainError) {
|
||||||
|
try set(value.uuidString, for: account)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Stores an `Encodable` value in the Keychain as JSON-encoded data for the specified account.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - value: The value to encode and store.
|
||||||
|
/// - account: The account identifier conforming to `KeychainAccountProtocol`.
|
||||||
|
/// - encoder: The `JSONEncoder` to use for encoding the value (default is a new instance).
|
||||||
|
/// - Throws: ``KeychainError/unexpectedError(_:)`` if encoding fails.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedError(_:)`` if access control creation fails.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedCode(_:)`` if adding the item to the Keychain fails.
|
||||||
|
/// - Throws: Any error thrown by ``set(_:for:)-7053g`` if insertion fails.
|
||||||
|
public func set<T: Encodable>(
|
||||||
|
_ value: T,
|
||||||
|
for account: Account,
|
||||||
|
encoder: JSONEncoder = .init()
|
||||||
|
) throws(KeychainError) {
|
||||||
|
do {
|
||||||
|
let data = try encoder.encode(value)
|
||||||
|
try set(data, for: account)
|
||||||
|
} catch let error as KeychainError {
|
||||||
|
throw error
|
||||||
|
} catch {
|
||||||
|
throw KeychainError.unexpectedError(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deletes the item associated with the specified account from the Keychain.
|
||||||
|
///
|
||||||
|
/// If no item exists for the given account, the method does nothing and does not throw an error.
|
||||||
|
///
|
||||||
|
/// - Parameter account: The account identifier conforming to `KeychainAccountProtocol`.
|
||||||
|
/// - Throws: ``KeychainError/unexpectedCode(_:)`` if deletion fails with an unexpected OSStatus.
|
||||||
|
public func delete(_ account: Account) throws(KeychainError) {
|
||||||
|
var query: [CFString: Any] = [
|
||||||
|
kSecClass: kSecClassGenericPassword,
|
||||||
|
kSecAttrAccount: account.identifier,
|
||||||
|
kSecAttrSynchronizable: account.synchronizable,
|
||||||
|
kSecUseDataProtectionKeychain: true
|
||||||
|
]
|
||||||
|
|
||||||
|
query[kSecAttrService] = service?.identifier
|
||||||
|
query[kSecAttrAccessGroup] = service?.accessGroup
|
||||||
|
query[kSecUseAuthenticationContext] = context
|
||||||
|
|
||||||
|
let status = SecItemDelete(query as CFDictionary)
|
||||||
|
guard status == errSecSuccess || status == errSecItemNotFound else {
|
||||||
|
throw KeychainError.unexpectedCode(status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
Sources/KeychainKit/Enums/KeychainError.swift
Normal file
15
Sources/KeychainKit/Enums/KeychainError.swift
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import Foundation
|
||||||
|
|
||||||
|
/// Errors that can occur during Keychain operations.
|
||||||
|
public enum KeychainError: Error {
|
||||||
|
/// Authentication failed, e.g., due to biometric or passcode denial.
|
||||||
|
case authenticationFailed
|
||||||
|
/// No item found matching the query.
|
||||||
|
case itemNotFound
|
||||||
|
/// Unexpected or corrupted data found in Keychain item.
|
||||||
|
case unexpectedData
|
||||||
|
/// An unexpected OSStatus error code returned by Keychain API.
|
||||||
|
case unexpectedCode(OSStatus)
|
||||||
|
/// A generic unexpected error, with optional underlying error info.
|
||||||
|
case unexpectedError(Error?)
|
||||||
|
}
|
||||||
47
Sources/KeychainKit/Protocols/KeychainAccountProtocol.swift
Normal file
47
Sources/KeychainKit/Protocols/KeychainAccountProtocol.swift
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
import Foundation
|
||||||
|
|
||||||
|
/// A protocol that defines the required properties for a keychain account descriptor.
|
||||||
|
///
|
||||||
|
/// Types conforming to this protocol provide metadata for configuring secure storage
|
||||||
|
/// and access behavior for keychain items.
|
||||||
|
public protocol KeychainAccountProtocol {
|
||||||
|
/// A unique string used to identify the keychain account.
|
||||||
|
var identifier: String { get }
|
||||||
|
|
||||||
|
/// The keychain data protection level for the account.
|
||||||
|
///
|
||||||
|
/// Defaults to `kSecAttrAccessibleAfterFirstUnlock`. You may override it to use other
|
||||||
|
/// accessibility levels, such as `kSecAttrAccessibleWhenUnlocked`
|
||||||
|
/// or `kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly`.
|
||||||
|
var protection: CFString { get }
|
||||||
|
|
||||||
|
/// The access control flags used to define authentication requirements.
|
||||||
|
///
|
||||||
|
/// Defaults to `[]` (no additional access control). Can be overridden to specify
|
||||||
|
/// constraints such as `.userPresence`, `.biometryAny`, or `.devicePasscode`.
|
||||||
|
var accessFlags: SecAccessControlCreateFlags { get }
|
||||||
|
|
||||||
|
/// Whether the item should be marked as synchronizable via iCloud Keychain.
|
||||||
|
///
|
||||||
|
/// Defaults to `false`. Set to `true` if the item should sync across devices.
|
||||||
|
var synchronizable: Bool { get }
|
||||||
|
}
|
||||||
|
|
||||||
|
public extension KeychainAccountProtocol {
|
||||||
|
/// Default value for `protection`: accessible after first unlock.
|
||||||
|
var protection: CFString { kSecAttrAccessibleAfterFirstUnlock }
|
||||||
|
|
||||||
|
/// Default value for `accessFlags`: no access control constraints.
|
||||||
|
var accessFlags: SecAccessControlCreateFlags { [] }
|
||||||
|
|
||||||
|
/// Default value for `synchronizable`: not synchronized across devices.
|
||||||
|
var synchronizable: Bool { false }
|
||||||
|
}
|
||||||
|
|
||||||
|
public extension KeychainAccountProtocol where Self: RawRepresentable, Self.RawValue == String {
|
||||||
|
/// Provides a default `identifier` implementation for `RawRepresentable` types
|
||||||
|
/// whose `RawValue` is `String`.
|
||||||
|
///
|
||||||
|
/// The `identifier` is derived from the raw string value.
|
||||||
|
var identifier: String { rawValue }
|
||||||
|
}
|
||||||
28
Sources/KeychainKit/Protocols/KeychainServiceProtocol.swift
Normal file
28
Sources/KeychainKit/Protocols/KeychainServiceProtocol.swift
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
import Foundation
|
||||||
|
|
||||||
|
/// A protocol that defines the required properties for a keychain service descriptor.
|
||||||
|
///
|
||||||
|
/// Types conforming to this protocol provide an identifier used to distinguish stored items
|
||||||
|
/// and may optionally specify an access group to enable keychain sharing between apps.
|
||||||
|
public protocol KeychainServiceProtocol {
|
||||||
|
/// A unique string used to identify the keychain service.
|
||||||
|
var identifier: String { get }
|
||||||
|
|
||||||
|
/// An optional keychain access group identifier to support shared access between apps.
|
||||||
|
///
|
||||||
|
/// The default implementation returns `nil`, indicating no access group is specified.
|
||||||
|
var accessGroup: String? { get }
|
||||||
|
}
|
||||||
|
|
||||||
|
public extension KeychainServiceProtocol {
|
||||||
|
/// The default implementation returns `nil`, indicating that no access group is specified.
|
||||||
|
var accessGroup: String? { nil }
|
||||||
|
}
|
||||||
|
|
||||||
|
public extension KeychainServiceProtocol where Self: RawRepresentable, Self.RawValue == String {
|
||||||
|
/// Provides a default `identifier` implementation for `RawRepresentable` types
|
||||||
|
/// whose `RawValue` is `String`.
|
||||||
|
///
|
||||||
|
/// The `identifier` is derived from the raw string value.
|
||||||
|
var identifier: String { rawValue }
|
||||||
|
}
|
||||||
124
Sources/KeychainKit/Protocols/KeychainStorageProtocol.swift
Normal file
124
Sources/KeychainKit/Protocols/KeychainStorageProtocol.swift
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
import Foundation
|
||||||
|
|
||||||
|
/// A protocol that defines a type-safe interface for storing and retrieving values
|
||||||
|
/// in the system keychain.
|
||||||
|
///
|
||||||
|
/// This protocol provides generic support for `Data`, `String`, `UUID`, and `Codable` types.
|
||||||
|
/// It allows configuring the associated account and service context for each operation.
|
||||||
|
///
|
||||||
|
/// Types conforming to this protocol must specify concrete types for `Account`
|
||||||
|
/// and `Service`, which describe keychain item identity and service grouping.
|
||||||
|
///
|
||||||
|
/// ## Topics
|
||||||
|
///
|
||||||
|
/// ### Associated Types
|
||||||
|
///
|
||||||
|
/// - ``Account``
|
||||||
|
/// - ``Service``
|
||||||
|
///
|
||||||
|
/// ### Instance Properties
|
||||||
|
///
|
||||||
|
/// - ``service``
|
||||||
|
///
|
||||||
|
/// ### Retrieving Values
|
||||||
|
///
|
||||||
|
/// - ``get(_:)-2gcee``
|
||||||
|
/// - ``get(_:)-23z7h``
|
||||||
|
/// - ``get(_:)-4xbe6``
|
||||||
|
/// - ``get(_:decoder:)``
|
||||||
|
///
|
||||||
|
/// ### Storing Values
|
||||||
|
///
|
||||||
|
/// - ``set(_:for:)-21dla``
|
||||||
|
/// - ``set(_:for:)-6nzkf``
|
||||||
|
/// - ``set(_:for:)-2smpc``
|
||||||
|
/// - ``set(_:for:encoder:)``
|
||||||
|
///
|
||||||
|
/// ### Deleting Values
|
||||||
|
///
|
||||||
|
/// - ``delete(_:)``
|
||||||
|
public protocol KeychainStorageProtocol {
|
||||||
|
/// A type that describes a keychain account and its security configuration.
|
||||||
|
associatedtype Account: KeychainAccountProtocol
|
||||||
|
|
||||||
|
/// A type that identifies a keychain service context (e.g., app or subsystem).
|
||||||
|
associatedtype Service: KeychainServiceProtocol
|
||||||
|
|
||||||
|
/// The service associated with this keychain storage instance.
|
||||||
|
///
|
||||||
|
/// This value is used as the `kSecAttrService` when interacting with the keychain.
|
||||||
|
/// If `nil`, the default service behavior is used.
|
||||||
|
var service: Service? { get }
|
||||||
|
|
||||||
|
/// Retrieves the value stored in the keychain for the specified account as raw `Data`.
|
||||||
|
///
|
||||||
|
/// - Parameter account: The keychain account whose value should be retrieved.
|
||||||
|
/// - Returns: The data associated with the given account.
|
||||||
|
/// - Throws: An error if the item is not found, access is denied, or another keychain error occurs.
|
||||||
|
func get(_ account: Account) throws(KeychainError) -> Data
|
||||||
|
|
||||||
|
/// Retrieves the value stored in the keychain for the specified account as a UTF-8 string.
|
||||||
|
///
|
||||||
|
/// - Parameter account: The keychain account whose value should be retrieved.
|
||||||
|
/// - Returns: A string decoded from the stored data using UTF-8 encoding.
|
||||||
|
/// - Throws: An error if the item is not found, the data is not valid UTF-8,
|
||||||
|
/// or a keychain access error occurs.
|
||||||
|
func get(_ account: Account) throws(KeychainError) -> String
|
||||||
|
|
||||||
|
/// Retrieves the value stored in the keychain for the specified account as a `UUID`.
|
||||||
|
///
|
||||||
|
/// - Parameter account: The keychain account whose value should be retrieved.
|
||||||
|
/// - Returns: A UUID decoded from a 16-byte binary representation stored in the keychain.
|
||||||
|
/// - Throws: An error if the item is not found, the data is not exactly 16 bytes,
|
||||||
|
/// or a keychain access error occurs.
|
||||||
|
func get(_ account: Account) throws(KeychainError) -> UUID
|
||||||
|
|
||||||
|
/// Retrieves and decodes a value of type `T` stored in the keychain for the specified account.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - account: The keychain account whose value should be retrieved.
|
||||||
|
/// - decoder: The `JSONDecoder` instance used to decode the stored data.
|
||||||
|
/// - Returns: A decoded instance of type `T`.
|
||||||
|
/// - Throws: An error if the item is not found, decoding fails, or a keychain access error occurs.
|
||||||
|
func get<T: Decodable>(_ account: Account, decoder: JSONDecoder) throws(KeychainError) -> T
|
||||||
|
|
||||||
|
/// Stores raw `Data` in the keychain for the specified account.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - value: The data to store in the keychain.
|
||||||
|
/// - account: The keychain account under which the data will be saved.
|
||||||
|
/// - Throws: An error if storing the data fails.
|
||||||
|
func set(_ value: Data, for account: Account) throws(KeychainError)
|
||||||
|
|
||||||
|
/// Stores a UTF-8 encoded `String` in the keychain for the specified account.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - value: The string to store in the keychain.
|
||||||
|
/// - account: The keychain account under which the string will be saved.
|
||||||
|
/// - Throws: An error if storing the string fails.
|
||||||
|
func set(_ value: String, for account: Account) throws(KeychainError)
|
||||||
|
|
||||||
|
/// Stores a `UUID` in the keychain for the specified account.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - value: The UUID to store in the keychain (stored in 16-byte binary format).
|
||||||
|
/// - account: The keychain account under which the UUID will be saved.
|
||||||
|
/// - Throws: An error if storing the UUID fails.
|
||||||
|
func set(_ value: UUID, for account: Account) throws(KeychainError)
|
||||||
|
|
||||||
|
/// Encodes and stores a value of type `T` in the keychain for the specified account.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - value: The value to encode and store.
|
||||||
|
/// - account: The keychain account under which the encoded data will be saved.
|
||||||
|
/// - encoder: The `JSONEncoder` used to encode the value.
|
||||||
|
/// - Throws: An error if encoding or storing the value fails.
|
||||||
|
func set<T: Encodable>(_ value: T, for account: Account, encoder: JSONEncoder) throws(KeychainError)
|
||||||
|
|
||||||
|
/// Deletes the keychain item associated with the specified account.
|
||||||
|
///
|
||||||
|
/// - Parameter account: The keychain account whose stored value should be deleted.
|
||||||
|
/// - Note: If the item does not exist, the method completes silently without error.
|
||||||
|
/// - Throws: An error only if the item exists but removal fails.
|
||||||
|
func delete(_ account: Account) throws(KeychainError)
|
||||||
|
}
|
||||||
@@ -1,362 +0,0 @@
|
|||||||
// !$*UTF8*$!
|
|
||||||
{
|
|
||||||
archiveVersion = 1;
|
|
||||||
classes = {
|
|
||||||
};
|
|
||||||
objectVersion = 50;
|
|
||||||
objects = {
|
|
||||||
|
|
||||||
/* Begin PBXBuildFile section */
|
|
||||||
4B08E1FC23E73380003504E1 /* keychain_kit.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B08E1FA23E73380003504E1 /* keychain_kit.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
|
||||||
4B08E20823E73CAF003504E1 /* Keychain.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B08E20723E73CAF003504E1 /* Keychain.swift */; };
|
|
||||||
/* End PBXBuildFile section */
|
|
||||||
|
|
||||||
/* Begin PBXFileReference section */
|
|
||||||
4B08E1F723E73380003504E1 /* KeychainKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = KeychainKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
|
||||||
4B08E1FA23E73380003504E1 /* keychain_kit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = keychain_kit.h; sourceTree = "<group>"; };
|
|
||||||
4B08E1FB23E73380003504E1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
|
||||||
4B08E20323E733CD003504E1 /* Config.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Config.xcconfig; sourceTree = "<group>"; };
|
|
||||||
4B08E20423E733F3003504E1 /* Debug.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Debug.xcconfig; sourceTree = "<group>"; };
|
|
||||||
4B08E20523E733F9003504E1 /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Release.xcconfig; sourceTree = "<group>"; };
|
|
||||||
4B08E20723E73CAF003504E1 /* Keychain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Keychain.swift; sourceTree = "<group>"; };
|
|
||||||
/* End PBXFileReference section */
|
|
||||||
|
|
||||||
/* Begin PBXFrameworksBuildPhase section */
|
|
||||||
4B08E1F423E73380003504E1 /* Frameworks */ = {
|
|
||||||
isa = PBXFrameworksBuildPhase;
|
|
||||||
buildActionMask = 2147483647;
|
|
||||||
files = (
|
|
||||||
);
|
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
|
||||||
};
|
|
||||||
/* End PBXFrameworksBuildPhase section */
|
|
||||||
|
|
||||||
/* Begin PBXGroup section */
|
|
||||||
4B08E1ED23E7337F003504E1 = {
|
|
||||||
isa = PBXGroup;
|
|
||||||
children = (
|
|
||||||
4B08E1F923E73380003504E1 /* keychain-kit */,
|
|
||||||
4B08E1F823E73380003504E1 /* Products */,
|
|
||||||
);
|
|
||||||
sourceTree = "<group>";
|
|
||||||
};
|
|
||||||
4B08E1F823E73380003504E1 /* Products */ = {
|
|
||||||
isa = PBXGroup;
|
|
||||||
children = (
|
|
||||||
4B08E1F723E73380003504E1 /* KeychainKit.framework */,
|
|
||||||
);
|
|
||||||
name = Products;
|
|
||||||
sourceTree = "<group>";
|
|
||||||
};
|
|
||||||
4B08E1F923E73380003504E1 /* keychain-kit */ = {
|
|
||||||
isa = PBXGroup;
|
|
||||||
children = (
|
|
||||||
4B08E20623E73C92003504E1 /* Sources */,
|
|
||||||
4B08E20223E733BF003504E1 /* Support Files */,
|
|
||||||
);
|
|
||||||
path = "keychain-kit";
|
|
||||||
sourceTree = "<group>";
|
|
||||||
};
|
|
||||||
4B08E20223E733BF003504E1 /* Support Files */ = {
|
|
||||||
isa = PBXGroup;
|
|
||||||
children = (
|
|
||||||
4B08E1FA23E73380003504E1 /* keychain_kit.h */,
|
|
||||||
4B08E1FB23E73380003504E1 /* Info.plist */,
|
|
||||||
4B08E20323E733CD003504E1 /* Config.xcconfig */,
|
|
||||||
4B08E20423E733F3003504E1 /* Debug.xcconfig */,
|
|
||||||
4B08E20523E733F9003504E1 /* Release.xcconfig */,
|
|
||||||
);
|
|
||||||
path = "Support Files";
|
|
||||||
sourceTree = "<group>";
|
|
||||||
};
|
|
||||||
4B08E20623E73C92003504E1 /* Sources */ = {
|
|
||||||
isa = PBXGroup;
|
|
||||||
children = (
|
|
||||||
4B08E20723E73CAF003504E1 /* Keychain.swift */,
|
|
||||||
);
|
|
||||||
path = Sources;
|
|
||||||
sourceTree = "<group>";
|
|
||||||
};
|
|
||||||
/* End PBXGroup section */
|
|
||||||
|
|
||||||
/* Begin PBXHeadersBuildPhase section */
|
|
||||||
4B08E1F223E73380003504E1 /* Headers */ = {
|
|
||||||
isa = PBXHeadersBuildPhase;
|
|
||||||
buildActionMask = 2147483647;
|
|
||||||
files = (
|
|
||||||
4B08E1FC23E73380003504E1 /* keychain_kit.h in Headers */,
|
|
||||||
);
|
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
|
||||||
};
|
|
||||||
/* End PBXHeadersBuildPhase section */
|
|
||||||
|
|
||||||
/* Begin PBXNativeTarget section */
|
|
||||||
4B08E1F623E73380003504E1 /* keychain-kit */ = {
|
|
||||||
isa = PBXNativeTarget;
|
|
||||||
buildConfigurationList = 4B08E1FF23E73380003504E1 /* Build configuration list for PBXNativeTarget "keychain-kit" */;
|
|
||||||
buildPhases = (
|
|
||||||
4B08E1F223E73380003504E1 /* Headers */,
|
|
||||||
4B08E1F323E73380003504E1 /* Sources */,
|
|
||||||
4B08E1F423E73380003504E1 /* Frameworks */,
|
|
||||||
4B08E1F523E73380003504E1 /* Resources */,
|
|
||||||
);
|
|
||||||
buildRules = (
|
|
||||||
);
|
|
||||||
dependencies = (
|
|
||||||
);
|
|
||||||
name = "keychain-kit";
|
|
||||||
productName = "keychain-kit";
|
|
||||||
productReference = 4B08E1F723E73380003504E1 /* KeychainKit.framework */;
|
|
||||||
productType = "com.apple.product-type.framework";
|
|
||||||
};
|
|
||||||
/* End PBXNativeTarget section */
|
|
||||||
|
|
||||||
/* Begin PBXProject section */
|
|
||||||
4B08E1EE23E73380003504E1 /* Project object */ = {
|
|
||||||
isa = PBXProject;
|
|
||||||
attributes = {
|
|
||||||
LastUpgradeCheck = 1130;
|
|
||||||
ORGANIZATIONNAME = mr.noone;
|
|
||||||
TargetAttributes = {
|
|
||||||
4B08E1F623E73380003504E1 = {
|
|
||||||
CreatedOnToolsVersion = 11.3.1;
|
|
||||||
LastSwiftMigration = 1130;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
buildConfigurationList = 4B08E1F123E73380003504E1 /* Build configuration list for PBXProject "keychain-kit" */;
|
|
||||||
compatibilityVersion = "Xcode 9.3";
|
|
||||||
developmentRegion = en;
|
|
||||||
hasScannedForEncodings = 0;
|
|
||||||
knownRegions = (
|
|
||||||
en,
|
|
||||||
Base,
|
|
||||||
);
|
|
||||||
mainGroup = 4B08E1ED23E7337F003504E1;
|
|
||||||
productRefGroup = 4B08E1F823E73380003504E1 /* Products */;
|
|
||||||
projectDirPath = "";
|
|
||||||
projectRoot = "";
|
|
||||||
targets = (
|
|
||||||
4B08E1F623E73380003504E1 /* keychain-kit */,
|
|
||||||
);
|
|
||||||
};
|
|
||||||
/* End PBXProject section */
|
|
||||||
|
|
||||||
/* Begin PBXResourcesBuildPhase section */
|
|
||||||
4B08E1F523E73380003504E1 /* Resources */ = {
|
|
||||||
isa = PBXResourcesBuildPhase;
|
|
||||||
buildActionMask = 2147483647;
|
|
||||||
files = (
|
|
||||||
);
|
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
|
||||||
};
|
|
||||||
/* End PBXResourcesBuildPhase section */
|
|
||||||
|
|
||||||
/* Begin PBXSourcesBuildPhase section */
|
|
||||||
4B08E1F323E73380003504E1 /* Sources */ = {
|
|
||||||
isa = PBXSourcesBuildPhase;
|
|
||||||
buildActionMask = 2147483647;
|
|
||||||
files = (
|
|
||||||
4B08E20823E73CAF003504E1 /* Keychain.swift in Sources */,
|
|
||||||
);
|
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
|
||||||
};
|
|
||||||
/* End PBXSourcesBuildPhase section */
|
|
||||||
|
|
||||||
/* Begin XCBuildConfiguration section */
|
|
||||||
4B08E1FD23E73380003504E1 /* Debug */ = {
|
|
||||||
isa = XCBuildConfiguration;
|
|
||||||
baseConfigurationReference = 4B08E20423E733F3003504E1 /* Debug.xcconfig */;
|
|
||||||
buildSettings = {
|
|
||||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
|
||||||
CLANG_ANALYZER_NONNULL = YES;
|
|
||||||
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
|
|
||||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
|
|
||||||
CLANG_CXX_LIBRARY = "libc++";
|
|
||||||
CLANG_ENABLE_MODULES = YES;
|
|
||||||
CLANG_ENABLE_OBJC_ARC = YES;
|
|
||||||
CLANG_ENABLE_OBJC_WEAK = YES;
|
|
||||||
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
|
|
||||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
|
||||||
CLANG_WARN_COMMA = YES;
|
|
||||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
|
||||||
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
|
|
||||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
|
||||||
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
|
|
||||||
CLANG_WARN_EMPTY_BODY = YES;
|
|
||||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
|
||||||
CLANG_WARN_INFINITE_RECURSION = YES;
|
|
||||||
CLANG_WARN_INT_CONVERSION = YES;
|
|
||||||
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
|
|
||||||
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
|
|
||||||
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
|
|
||||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
|
||||||
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
|
|
||||||
CLANG_WARN_STRICT_PROTOTYPES = YES;
|
|
||||||
CLANG_WARN_SUSPICIOUS_MOVE = YES;
|
|
||||||
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
|
|
||||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
|
||||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
|
||||||
COPY_PHASE_STRIP = NO;
|
|
||||||
CURRENT_PROJECT_VERSION = 1;
|
|
||||||
DEBUG_INFORMATION_FORMAT = dwarf;
|
|
||||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
|
||||||
ENABLE_TESTABILITY = YES;
|
|
||||||
GCC_C_LANGUAGE_STANDARD = gnu11;
|
|
||||||
GCC_DYNAMIC_NO_PIC = NO;
|
|
||||||
GCC_NO_COMMON_BLOCKS = YES;
|
|
||||||
GCC_OPTIMIZATION_LEVEL = 0;
|
|
||||||
GCC_PREPROCESSOR_DEFINITIONS = (
|
|
||||||
"DEBUG=1",
|
|
||||||
"$(inherited)",
|
|
||||||
);
|
|
||||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
|
||||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
|
||||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
|
||||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
|
||||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
|
||||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
|
||||||
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
|
|
||||||
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
|
|
||||||
MTL_FAST_MATH = YES;
|
|
||||||
ONLY_ACTIVE_ARCH = YES;
|
|
||||||
SDKROOT = iphoneos;
|
|
||||||
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
|
|
||||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
|
||||||
VERSIONING_SYSTEM = "apple-generic";
|
|
||||||
VERSION_INFO_PREFIX = "";
|
|
||||||
};
|
|
||||||
name = Debug;
|
|
||||||
};
|
|
||||||
4B08E1FE23E73380003504E1 /* Release */ = {
|
|
||||||
isa = XCBuildConfiguration;
|
|
||||||
baseConfigurationReference = 4B08E20523E733F9003504E1 /* Release.xcconfig */;
|
|
||||||
buildSettings = {
|
|
||||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
|
||||||
CLANG_ANALYZER_NONNULL = YES;
|
|
||||||
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
|
|
||||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
|
|
||||||
CLANG_CXX_LIBRARY = "libc++";
|
|
||||||
CLANG_ENABLE_MODULES = YES;
|
|
||||||
CLANG_ENABLE_OBJC_ARC = YES;
|
|
||||||
CLANG_ENABLE_OBJC_WEAK = YES;
|
|
||||||
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
|
|
||||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
|
||||||
CLANG_WARN_COMMA = YES;
|
|
||||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
|
||||||
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
|
|
||||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
|
||||||
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
|
|
||||||
CLANG_WARN_EMPTY_BODY = YES;
|
|
||||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
|
||||||
CLANG_WARN_INFINITE_RECURSION = YES;
|
|
||||||
CLANG_WARN_INT_CONVERSION = YES;
|
|
||||||
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
|
|
||||||
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
|
|
||||||
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
|
|
||||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
|
||||||
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
|
|
||||||
CLANG_WARN_STRICT_PROTOTYPES = YES;
|
|
||||||
CLANG_WARN_SUSPICIOUS_MOVE = YES;
|
|
||||||
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
|
|
||||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
|
||||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
|
||||||
COPY_PHASE_STRIP = NO;
|
|
||||||
CURRENT_PROJECT_VERSION = 1;
|
|
||||||
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
|
|
||||||
ENABLE_NS_ASSERTIONS = NO;
|
|
||||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
|
||||||
GCC_C_LANGUAGE_STANDARD = gnu11;
|
|
||||||
GCC_NO_COMMON_BLOCKS = YES;
|
|
||||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
|
||||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
|
||||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
|
||||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
|
||||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
|
||||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
|
||||||
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
|
|
||||||
MTL_ENABLE_DEBUG_INFO = NO;
|
|
||||||
MTL_FAST_MATH = YES;
|
|
||||||
SDKROOT = iphoneos;
|
|
||||||
SWIFT_COMPILATION_MODE = wholemodule;
|
|
||||||
SWIFT_OPTIMIZATION_LEVEL = "-O";
|
|
||||||
VALIDATE_PRODUCT = YES;
|
|
||||||
VERSIONING_SYSTEM = "apple-generic";
|
|
||||||
VERSION_INFO_PREFIX = "";
|
|
||||||
};
|
|
||||||
name = Release;
|
|
||||||
};
|
|
||||||
4B08E20023E73380003504E1 /* Debug */ = {
|
|
||||||
isa = XCBuildConfiguration;
|
|
||||||
buildSettings = {
|
|
||||||
CLANG_ENABLE_MODULES = YES;
|
|
||||||
CODE_SIGN_STYLE = Automatic;
|
|
||||||
DEFINES_MODULE = YES;
|
|
||||||
DEVELOPMENT_TEAM = 84Z2AMFMF3;
|
|
||||||
DYLIB_COMPATIBILITY_VERSION = 1;
|
|
||||||
DYLIB_CURRENT_VERSION = 1;
|
|
||||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
|
||||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
|
||||||
LD_RUNPATH_SEARCH_PATHS = (
|
|
||||||
"$(inherited)",
|
|
||||||
"@executable_path/Frameworks",
|
|
||||||
"@loader_path/Frameworks",
|
|
||||||
);
|
|
||||||
MARKETING_VERSION = 1.0.0;
|
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mr-noone.keychain-kit";
|
|
||||||
SKIP_INSTALL = YES;
|
|
||||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
|
||||||
SWIFT_VERSION = 5.0;
|
|
||||||
TARGETED_DEVICE_FAMILY = "1,2";
|
|
||||||
};
|
|
||||||
name = Debug;
|
|
||||||
};
|
|
||||||
4B08E20123E73380003504E1 /* Release */ = {
|
|
||||||
isa = XCBuildConfiguration;
|
|
||||||
buildSettings = {
|
|
||||||
CLANG_ENABLE_MODULES = YES;
|
|
||||||
CODE_SIGN_STYLE = Automatic;
|
|
||||||
DEFINES_MODULE = YES;
|
|
||||||
DEVELOPMENT_TEAM = 84Z2AMFMF3;
|
|
||||||
DYLIB_COMPATIBILITY_VERSION = 1;
|
|
||||||
DYLIB_CURRENT_VERSION = 1;
|
|
||||||
DYLIB_INSTALL_NAME_BASE = "@rpath";
|
|
||||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
|
||||||
LD_RUNPATH_SEARCH_PATHS = (
|
|
||||||
"$(inherited)",
|
|
||||||
"@executable_path/Frameworks",
|
|
||||||
"@loader_path/Frameworks",
|
|
||||||
);
|
|
||||||
MARKETING_VERSION = 1.0.0;
|
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mr-noone.keychain-kit";
|
|
||||||
SKIP_INSTALL = YES;
|
|
||||||
SWIFT_VERSION = 5.0;
|
|
||||||
TARGETED_DEVICE_FAMILY = "1,2";
|
|
||||||
};
|
|
||||||
name = Release;
|
|
||||||
};
|
|
||||||
/* End XCBuildConfiguration section */
|
|
||||||
|
|
||||||
/* Begin XCConfigurationList section */
|
|
||||||
4B08E1F123E73380003504E1 /* Build configuration list for PBXProject "keychain-kit" */ = {
|
|
||||||
isa = XCConfigurationList;
|
|
||||||
buildConfigurations = (
|
|
||||||
4B08E1FD23E73380003504E1 /* Debug */,
|
|
||||||
4B08E1FE23E73380003504E1 /* Release */,
|
|
||||||
);
|
|
||||||
defaultConfigurationIsVisible = 0;
|
|
||||||
defaultConfigurationName = Release;
|
|
||||||
};
|
|
||||||
4B08E1FF23E73380003504E1 /* Build configuration list for PBXNativeTarget "keychain-kit" */ = {
|
|
||||||
isa = XCConfigurationList;
|
|
||||||
buildConfigurations = (
|
|
||||||
4B08E20023E73380003504E1 /* Debug */,
|
|
||||||
4B08E20123E73380003504E1 /* Release */,
|
|
||||||
);
|
|
||||||
defaultConfigurationIsVisible = 0;
|
|
||||||
defaultConfigurationName = Release;
|
|
||||||
};
|
|
||||||
/* End XCConfigurationList section */
|
|
||||||
};
|
|
||||||
rootObject = 4B08E1EE23E73380003504E1 /* Project object */;
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<Workspace
|
|
||||||
version = "1.0">
|
|
||||||
<FileRef
|
|
||||||
location = "self:keychain-kit.xcodeproj">
|
|
||||||
</FileRef>
|
|
||||||
</Workspace>
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
<?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>
|
|
||||||
<key>IDEDidComputeMac32BitWarning</key>
|
|
||||||
<true/>
|
|
||||||
</dict>
|
|
||||||
</plist>
|
|
||||||
@@ -1,88 +0,0 @@
|
|||||||
//
|
|
||||||
// Keychain.swift
|
|
||||||
// keychain-kit
|
|
||||||
//
|
|
||||||
// Created by Aleksey Zgurskiy on 02.02.2020.
|
|
||||||
// Copyright © 2020 mr.noone. All rights reserved.
|
|
||||||
//
|
|
||||||
|
|
||||||
import Foundation
|
|
||||||
|
|
||||||
public struct Keychain {
|
|
||||||
public enum Error: Swift.Error {
|
|
||||||
case noData
|
|
||||||
case unexpectedData
|
|
||||||
case unexpected(code: OSStatus)
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - Inits
|
|
||||||
|
|
||||||
public init() {}
|
|
||||||
|
|
||||||
// MARK: - Public methods
|
|
||||||
|
|
||||||
public func get(_ key: String) throws -> Data {
|
|
||||||
let query: [CFString : AnyObject] = [
|
|
||||||
kSecClass : kSecClassGenericPassword,
|
|
||||||
kSecAttrAccount : key as AnyObject,
|
|
||||||
kSecMatchLimit : kSecMatchLimitOne,
|
|
||||||
kSecReturnData : kCFBooleanTrue
|
|
||||||
]
|
|
||||||
|
|
||||||
var queryResult: AnyObject?
|
|
||||||
let status = withUnsafeMutablePointer(to: &queryResult) {
|
|
||||||
SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0))
|
|
||||||
}
|
|
||||||
|
|
||||||
guard status != errSecItemNotFound else { throw Error.noData }
|
|
||||||
guard status == noErr else { throw Error.unexpected(code: status) }
|
|
||||||
|
|
||||||
guard
|
|
||||||
let item = queryResult as? [CFString : AnyObject],
|
|
||||||
let data = item[kSecValueData] as? Data
|
|
||||||
else { throw Error.noData }
|
|
||||||
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
public func get(_ key: String) throws -> String {
|
|
||||||
return String(data: try get(key), encoding: .utf8)!
|
|
||||||
}
|
|
||||||
|
|
||||||
public func get<T>(_ key: String, decoder: JSONDecoder = JSONDecoder()) throws -> T where T: Decodable {
|
|
||||||
return try decoder.decode(T.self, from: get(key))
|
|
||||||
}
|
|
||||||
|
|
||||||
public func set(_ data: Data, for key: String) throws {
|
|
||||||
try delete(key)
|
|
||||||
|
|
||||||
let query: [CFString : AnyObject] = [
|
|
||||||
kSecClass : kSecClassGenericPassword,
|
|
||||||
kSecAttrAccount : key as AnyObject,
|
|
||||||
kSecValueData : data as AnyObject
|
|
||||||
]
|
|
||||||
|
|
||||||
let status = SecItemAdd(query as CFDictionary, nil)
|
|
||||||
guard status == noErr else { throw Error.unexpected(code: status) }
|
|
||||||
}
|
|
||||||
|
|
||||||
public func set(_ value: String, for key: String) throws {
|
|
||||||
try set(value.data(using: .utf8)!, for: key)
|
|
||||||
}
|
|
||||||
|
|
||||||
public func set<T>(_ value: T, for key: String, encoder: JSONEncoder = JSONEncoder()) throws where T: Encodable {
|
|
||||||
try set(encoder.encode(value), for: key)
|
|
||||||
}
|
|
||||||
|
|
||||||
public func delete(_ key: String) throws {
|
|
||||||
let query: [CFString : AnyObject] = [
|
|
||||||
kSecClass : kSecClassGenericPassword,
|
|
||||||
kSecAttrAccount : key as AnyObject
|
|
||||||
]
|
|
||||||
|
|
||||||
let status = SecItemDelete(query as CFDictionary)
|
|
||||||
guard status == noErr || status == errSecItemNotFound else {
|
|
||||||
throw Error.unexpected(code: status)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
//
|
|
||||||
// Config.xcconfig
|
|
||||||
// keychain-kit
|
|
||||||
//
|
|
||||||
// Created by Aleksey Zgurskiy on 02.02.2020.
|
|
||||||
// Copyright © 2020 mr.noone. All rights reserved.
|
|
||||||
//
|
|
||||||
|
|
||||||
// Configuration settings file format documentation can be found at:
|
|
||||||
// https://help.apple.com/xcode/#/dev745c5c974
|
|
||||||
|
|
||||||
INFOPLIST_FILE = keychain-kit/Support Files/Info.plist
|
|
||||||
PRODUCT_NAME = KeychainKit // $(TARGET_NAME:c99extidentifier)
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
//
|
|
||||||
// Debug.xcconfig
|
|
||||||
// keychain-kit
|
|
||||||
//
|
|
||||||
// Created by Aleksey Zgurskiy on 02.02.2020.
|
|
||||||
// Copyright © 2020 mr.noone. All rights reserved.
|
|
||||||
//
|
|
||||||
|
|
||||||
// Configuration settings file format documentation can be found at:
|
|
||||||
// https://help.apple.com/xcode/#/dev745c5c974
|
|
||||||
|
|
||||||
#include "Config.xcconfig"
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
<?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>
|
|
||||||
<key>CFBundleDevelopmentRegion</key>
|
|
||||||
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
|
||||||
<key>CFBundleExecutable</key>
|
|
||||||
<string>$(EXECUTABLE_NAME)</string>
|
|
||||||
<key>CFBundleIdentifier</key>
|
|
||||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
|
||||||
<key>CFBundleInfoDictionaryVersion</key>
|
|
||||||
<string>6.0</string>
|
|
||||||
<key>CFBundleName</key>
|
|
||||||
<string>$(PRODUCT_NAME)</string>
|
|
||||||
<key>CFBundlePackageType</key>
|
|
||||||
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
|
|
||||||
<key>CFBundleShortVersionString</key>
|
|
||||||
<string>$(MARKETING_VERSION)</string>
|
|
||||||
<key>CFBundleVersion</key>
|
|
||||||
<string>$(CURRENT_PROJECT_VERSION)</string>
|
|
||||||
</dict>
|
|
||||||
</plist>
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
//
|
|
||||||
// Release.xcconfig
|
|
||||||
// keychain-kit
|
|
||||||
//
|
|
||||||
// Created by Aleksey Zgurskiy on 02.02.2020.
|
|
||||||
// Copyright © 2020 mr.noone. All rights reserved.
|
|
||||||
//
|
|
||||||
|
|
||||||
// Configuration settings file format documentation can be found at:
|
|
||||||
// https://help.apple.com/xcode/#/dev745c5c974
|
|
||||||
|
|
||||||
#include "Config.xcconfig"
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
//
|
|
||||||
// keychain_kit.h
|
|
||||||
// keychain-kit
|
|
||||||
//
|
|
||||||
// Created by Aleksey Zgurskiy on 02.02.2020.
|
|
||||||
// Copyright © 2020 mr.noone. All rights reserved.
|
|
||||||
//
|
|
||||||
|
|
||||||
#import <Foundation/Foundation.h>
|
|
||||||
|
|
||||||
//! Project version number for keychain_kit.
|
|
||||||
FOUNDATION_EXPORT double keychain_kitVersionNumber;
|
|
||||||
|
|
||||||
//! Project version string for keychain_kit.
|
|
||||||
FOUNDATION_EXPORT const unsigned char keychain_kitVersionString[];
|
|
||||||
|
|
||||||
// In this header, you should import all the public headers of your framework using statements like #import <keychain_kit/PublicHeader.h>
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user