Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
dm-zharov committed Apr 29, 2024
1 parent eb36bb9 commit fd7e8ef
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ let password: String? = try keychain.retrieve(
For example, if you need to store distinct ports credentials for the same user working on the same server, you might further characterize the query by specifying protection space.

```swift
let space1 = WebProtectionSpace(host: "https://example.com", port: 443)
let space1 = WebProtectionSpace(host: "example.com", port: 443)
try keychain.store(password1, query: .credential(for: user, space: space1))

let space2 = WebProtectionSpace(host: "https://example.com", port: 8443)
let space2 = WebProtectionSpace(host: "example.com", port: 8443)
try keychain.store(password2, query: .credential(for: user, space: space2))
```

#### Get Attribute
#### Get Attributes

```swift
if let info = try keychain.info(for: .credential(for: "OpenAI")) {
Expand Down Expand Up @@ -156,6 +156,30 @@ SecItemQuery<SecCertificate> // kSecClassSecCertificate
SecItemQuery<SecIdentity> // kSecClassSecIdentity
```

#### CryptoKit

```swift
// Store private key
let privateKey = P256.KeyAgreement.PrivateKey()
try Keychain.default.store(privateKey, query: .privateKey(tag: "Alice"))

// Retrieve private key (+ public key)
let privateKey: P256.KeyAgreement.PrivateKey? = try? Keychain.default.retrieve(.privateKey(tag: "Alice"))
let publicKey = privateKey.publicKey
```

#### Get Data & Persistent Reference

```swift
let value = try keychain.retrieve([.data, .persistentReference], query: .credential(for: "OpenAI"))
if case let .dictionary(info) = value {
// Data
info.data
// Persistent Reference
info.persistentReference
}
```

#### Debug

```swift
Expand Down Expand Up @@ -256,10 +280,8 @@ if success {
> [SharedWebCredentials API](https://developer.apple.com/documentation/security/shared_web_credentials) makes it possible to share credentials with the website counterpart. For example, a user may log in to a website in Safari and save credentials to the iCloud Keychain. Later, the user may run an app from the same developer, and instead of asking the user to reenter a username and password, it could access the existing credentials. The user can create new accounts, update passwords, or delete account from within the app. These changes should be saved from the app to be used by Safari.
```swift
let credential = SharedWebCredential("https://example.com", account: "username")

// Store
credential.store(password) { result in
/ Store
SharedWebCredential.store("https://example.com", account: "username", password: "secret") { result in
switch result {
case .failure(let error):
// Handle error
Expand All @@ -269,14 +291,14 @@ credential.store(password) { result in
}

// Remove
credential.remove(completion: { result in
SharedWebCredential.remove("https://example.com", account: "username") { result in
switch result {
case .failure(let error):
// Handle error
case .success:
// Handle success
}
})
}

// Retrieve
// - Use `ASAuthorizationController` to make an `ASAuthorizationPasswordRequest`.
Expand Down Expand Up @@ -305,7 +327,7 @@ To add support for custom types, you can extend them by conforming to the follow
// Store as Data (GenericPassword, InternetPassword)
extension CustomType: SecDataConvertible {}

// Store as Key (SecKey)
// Store as x8.Key (SecKey)
extension CustomType: SecKeyConvertible {}

// Store as Certificate (X.509)
Expand Down

0 comments on commit fd7e8ef

Please sign in to comment.