• Stars
    star
    7,532
  • Rank 5,112 (Top 0.2 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple Swift wrapper for Keychain that works on iOS, watchOS, tvOS and macOS.

KeychainAccess

Build Status Carthage compatible SPM supported Version Platform

KeychainAccess is a simple Swift wrapper for Keychain that works on iOS and macOS. Makes using Keychain APIs extremely easy and much more palatable to use in Swift.

๐Ÿ’ก Features

๐Ÿ“– Usage

๐Ÿ‘€ See also:

๐Ÿ”‘ Basics

Saving Application Password

let keychain = Keychain(service: "com.example.github-token")
keychain["kishikawakatsumi"] = "01234567-89ab-cdef-0123-456789abcdef"

Saving Internet Password

let keychain = Keychain(server: "https://github.com", protocolType: .https)
keychain["kishikawakatsumi"] = "01234567-89ab-cdef-0123-456789abcdef"

๐Ÿ”‘ Instantiation

Create Keychain for Application Password

let keychain = Keychain(service: "com.example.github-token")
let keychain = Keychain(service: "com.example.github-token", accessGroup: "12ABCD3E4F.shared")

Create Keychain for Internet Password

let keychain = Keychain(server: "https://github.com", protocolType: .https)
let keychain = Keychain(server: "https://github.com", protocolType: .https, authenticationType: .htmlForm)

๐Ÿ”‘ Adding an item

subscripting

for String
keychain["kishikawakatsumi"] = "01234567-89ab-cdef-0123-456789abcdef"
keychain[string: "kishikawakatsumi"] = "01234567-89ab-cdef-0123-456789abcdef"
for NSData
keychain[data: "secret"] = NSData(contentsOfFile: "secret.bin")

set method

keychain.set("01234567-89ab-cdef-0123-456789abcdef", key: "kishikawakatsumi")

error handling

do {
    try keychain.set("01234567-89ab-cdef-0123-456789abcdef", key: "kishikawakatsumi")
}
catch let error {
    print(error)
}

๐Ÿ”‘ Obtaining an item

subscripting

for String (If the value is NSData, attempt to convert to String)
let token = keychain["kishikawakatsumi"]
let token = keychain[string: "kishikawakatsumi"]
for NSData
let secretData = keychain[data: "secret"]

get methods

as String
let token = try? keychain.get("kishikawakatsumi")
let token = try? keychain.getString("kishikawakatsumi")
as NSData
let data = try? keychain.getData("kishikawakatsumi")

๐Ÿ”‘ Removing an item

subscripting

keychain["kishikawakatsumi"] = nil

remove method

do {
    try keychain.remove("kishikawakatsumi")
} catch let error {
    print("error: \(error)")
}

๐Ÿ”‘ Set Label and Comment

let keychain = Keychain(server: "https://github.com", protocolType: .https)
do {
    try keychain
        .label("github.com (kishikawakatsumi)")
        .comment("github access token")
        .set("01234567-89ab-cdef-0123-456789abcdef", key: "kishikawakatsumi")
} catch let error {
    print("error: \(error)")
}

๐Ÿ”‘ Obtaining Other Attributes

PersistentRef

let keychain = Keychain()
let persistentRef = keychain[attributes: "kishikawakatsumi"]?.persistentRef
...

Creation Date

let keychain = Keychain()
let creationDate = keychain[attributes: "kishikawakatsumi"]?.creationDate
...

All Attributes

let keychain = Keychain()
do {
    let attributes = try keychain.get("kishikawakatsumi") { $0 }
    print(attributes?.comment)
    print(attributes?.label)
    print(attributes?.creator)
    ...
} catch let error {
    print("error: \(error)")
}
subscripting
let keychain = Keychain()
if let attributes = keychain[attributes: "kishikawakatsumi"] {
    print(attributes.comment)
    print(attributes.label)
    print(attributes.creator)
}

๐Ÿ”‘ Configuration (Accessibility, Sharing, iCloud Sync)

Provides fluent interfaces

let keychain = Keychain(service: "com.example.github-token")
    .label("github.com (kishikawakatsumi)")
    .synchronizable(true)
    .accessibility(.afterFirstUnlock)
Default accessibility matches background application (=kSecAttrAccessibleAfterFirstUnlock)
let keychain = Keychain(service: "com.example.github-token")
For background application
Creating instance
let keychain = Keychain(service: "com.example.github-token")
    .accessibility(.afterFirstUnlock)

keychain["kishikawakatsumi"] = "01234567-89ab-cdef-0123-456789abcdef"
One-shot
let keychain = Keychain(service: "com.example.github-token")

do {
    try keychain
        .accessibility(.afterFirstUnlock)
        .set("01234567-89ab-cdef-0123-456789abcdef", key: "kishikawakatsumi")
} catch let error {
    print("error: \(error)")
}
For foreground application
Creating instance
let keychain = Keychain(service: "com.example.github-token")
    .accessibility(.whenUnlocked)

keychain["kishikawakatsumi"] = "01234567-89ab-cdef-0123-456789abcdef"
One-shot
let keychain = Keychain(service: "com.example.github-token")

do {
    try keychain
        .accessibility(.whenUnlocked)
        .set("01234567-89ab-cdef-0123-456789abcdef", key: "kishikawakatsumi")
} catch let error {
    print("error: \(error)")
}

๐Ÿ‘ซ Sharing Keychain items

let keychain = Keychain(service: "com.example.github-token", accessGroup: "12ABCD3E4F.shared")
Creating instance
let keychain = Keychain(service: "com.example.github-token")
    .synchronizable(true)

keychain["kishikawakatsumi"] = "01234567-89ab-cdef-0123-456789abcdef"
One-shot
let keychain = Keychain(service: "com.example.github-token")

do {
    try keychain
        .synchronizable(true)
        .set("01234567-89ab-cdef-0123-456789abcdef", key: "kishikawakatsumi")
} catch let error {
    print("error: \(error)")
}

Any Operation that require authentication must be run in the background thread.
If you run in the main thread, UI thread will lock for the system to try to display the authentication dialog.

To use Face ID, add NSFaceIDUsageDescription key to your Info.plist

๐Ÿ” Adding a Touch ID (Face ID) protected item

If you want to store the Touch ID protected Keychain item, specify accessibility and authenticationPolicy attributes.

let keychain = Keychain(service: "com.example.github-token")

DispatchQueue.global().async {
    do {
        // Should be the secret invalidated when passcode is removed? If not then use `.WhenUnlocked`
        try keychain
            .accessibility(.whenPasscodeSetThisDeviceOnly, authenticationPolicy: [.biometryAny])
            .set("01234567-89ab-cdef-0123-456789abcdef", key: "kishikawakatsumi")
    } catch let error {
        // Error handling if needed...
    }
}

๐Ÿ” Updating a Touch ID (Face ID) protected item

The same way as when adding.

Do not run in the main thread if there is a possibility that the item you are trying to add already exists, and protected. Because updating protected items requires authentication.

Additionally, you want to show custom authentication prompt message when updating, specify an authenticationPrompt attribute. If the item not protected, the authenticationPrompt parameter just be ignored.

let keychain = Keychain(service: "com.example.github-token")

DispatchQueue.global().async {
    do {
        // Should be the secret invalidated when passcode is removed? If not then use `.WhenUnlocked`
        try keychain
            .accessibility(.whenPasscodeSetThisDeviceOnly, authenticationPolicy: [.biometryAny])
            .authenticationPrompt("Authenticate to update your access token")
            .set("01234567-89ab-cdef-0123-456789abcdef", key: "kishikawakatsumi")
    } catch let error {
        // Error handling if needed...
    }
}

๐Ÿ” Obtaining a Touch ID (Face ID) protected item

The same way as when you get a normal item. It will be displayed automatically Touch ID or passcode authentication If the item you try to get is protected.
If you want to show custom authentication prompt message, specify an authenticationPrompt attribute. If the item not protected, the authenticationPrompt parameter just be ignored.

let keychain = Keychain(service: "com.example.github-token")

DispatchQueue.global().async {
    do {
        let password = try keychain
            .authenticationPrompt("Authenticate to login to server")
            .get("kishikawakatsumi")

        print("password: \(password)")
    } catch let error {
        // Error handling if needed...
    }
}

๐Ÿ” Removing a Touch ID (Face ID) protected item

The same way as when you remove a normal item. There is no way to show Touch ID or passcode authentication when removing Keychain items.

let keychain = Keychain(service: "com.example.github-token")

do {
    try keychain.remove("kishikawakatsumi")
} catch let error {
    // Error handling if needed...
}

Shared web credentials is a programming interface that enables native iOS apps to share credentials with their website counterparts. For example, a user may log in to a website in Safari, entering a user name and password, and save those credentials using the iCloud Keychain. Later, the user may run a native app from the same developer, and instead of the app requiring the user to reenter a user name and password, shared web credentials gives it access to the credentials that were entered earlier in Safari. The user can also create new accounts, update passwords, or delete her account from within the app. These changes are then saved and used by Safari.
https://developer.apple.com/library/ios/documentation/Security/Reference/SharedWebCredentialsRef/

let keychain = Keychain(server: "https://www.kishikawakatsumi.com", protocolType: .HTTPS)

let username = "[email protected]"

// First, check the credential in the app's Keychain
if let password = try? keychain.get(username) {
    // If found password in the Keychain,
    // then log into the server
} else {
    // If not found password in the Keychain,
    // try to read from Shared Web Credentials
    keychain.getSharedPassword(username) { (password, error) -> () in
        if password != nil {
            // If found password in the Shared Web Credentials,
            // then log into the server
            // and save the password to the Keychain

            keychain[username] = password
        } else {
            // If not found password either in the Keychain also Shared Web Credentials,
            // prompt for username and password

            // Log into server

            // If the login is successful,
            // save the credentials to both the Keychain and the Shared Web Credentials.

            keychain[username] = inputPassword
            keychain.setSharedPassword(inputPassword, account: username)
        }
    }
}

Request all associated domain's credentials

Keychain.requestSharedWebCredential { (credentials, error) -> () in

}

Generate strong random password

Generate strong random password that is in the same format used by Safari autofill (xxx-xxx-xxx-xxx).

let password = Keychain.generatePassword() // => Nhu-GKm-s3n-pMx

How to set up Shared Web Credentials

  1. Add a com.apple.developer.associated-domains entitlement to your app. This entitlement must include all the domains with which you want to share credentials.

  2. Add an apple-app-site-association file to your website. This file must include application identifiers for all the apps with which the site wants to share credentials, and it must be properly signed.

  3. When the app is installed, the system downloads and verifies the site association file for each of its associated domains. If the verification is successful, the app is associated with the domain.

More details:
https://developer.apple.com/library/ios/documentation/Security/Reference/SharedWebCredentialsRef/

๐Ÿ” Debugging

Display all stored items if print keychain object

let keychain = Keychain(server: "https://github.com", protocolType: .https)
print("\(keychain)")
=>
[
  [authenticationType: default, key: kishikawakatsumi, server: github.com, class: internetPassword, protocol: https]
  [authenticationType: default, key: hirohamada, server: github.com, class: internetPassword, protocol: https]
  [authenticationType: default, key: honeylemon, server: github.com, class: internetPassword, protocol: https]
]

Obtaining all stored keys

let keychain = Keychain(server: "https://github.com", protocolType: .https)

let keys = keychain.allKeys()
for key in keys {
  print("key: \(key)")
}
=>
key: kishikawakatsumi
key: hirohamada
key: honeylemon

Obtaining all stored items

let keychain = Keychain(server: "https://github.com", protocolType: .https)

let items = keychain.allItems()
for item in items {
  print("item: \(item)")
}
=>
item: [authenticationType: Default, key: kishikawakatsumi, server: github.com, class: InternetPassword, protocol: https]
item: [authenticationType: Default, key: hirohamada, server: github.com, class: InternetPassword, protocol: https]
item: [authenticationType: Default, key: honeylemon, server: github.com, class: InternetPassword, protocol: https]

Keychain sharing capability

If you encounter the error below, you need to add an Keychain.entitlements.

OSStatus error:[-34018] Internal error when a required entitlement isn't present, client has neither application-identifier nor keychain-access-groups entitlements.

Screen Shot 2019-10-27 at 8 08 50

Requirements

OS Swift
v1.1.x iOS 7+, macOS 10.9+ 1.1
v1.2.x iOS 7+, macOS 10.9+ 1.2
v2.0.x iOS 7+, macOS 10.9+, watchOS 2+ 2.0
v2.1.x iOS 7+, macOS 10.9+, watchOS 2+ 2.0
v2.2.x iOS 8+, macOS 10.9+, watchOS 2+, tvOS 9+ 2.0, 2.1
v2.3.x iOS 8+, macOS 10.9+, watchOS 2+, tvOS 9+ 2.0, 2.1, 2.2
v2.4.x iOS 8+, macOS 10.9+, watchOS 2+, tvOS 9+ 2.2, 2.3
v3.0.x iOS 8+, macOS 10.9+, watchOS 2+, tvOS 9+ 3.x
v3.1.x iOS 8+, macOS 10.9+, watchOS 2+, tvOS 9+ 4.0, 4.1, 4.2
v3.2.x iOS 8+, macOS 10.9+, watchOS 2+, tvOS 9+ 4.0, 4.1, 4.2, 5.0
v4.0.x iOS 8+, macOS 10.9+, watchOS 2+, tvOS 9+ 4.0, 4.1, 4.2, 5.1
v4.1.x iOS 8+, macOS 10.9+, watchOS 3+, tvOS 9+, Mac Catalyst 13+ 4.0, 4.1, 4.2, 5.1

Installation

CocoaPods

KeychainAccess is available through CocoaPods. To install it, simply add the following lines to your Podfile:

use_frameworks!
pod 'KeychainAccess'

Carthage

KeychainAccess is available through Carthage. To install it, simply add the following line to your Cartfile:

github "kishikawakatsumi/KeychainAccess"

Swift Package Manager

KeychainAccess is also available through Swift Package Manager.

Xcode

Select File > Add Packages... > Add Package Dependency...,

CLI

First, create Package.swift that its package declaration includes:

// swift-tools-version:5.0
import PackageDescription

let package = Package(
    name: "MyLibrary",
    products: [
        .library(name: "MyLibrary", targets: ["MyLibrary"]),
    ],
    dependencies: [
        .package(url: "https://github.com/kishikawakatsumi/KeychainAccess.git", from: "3.0.0"),
    ],
    targets: [
        .target(name: "MyLibrary", dependencies: ["KeychainAccess"]),
    ]
)

Then, type

$ swift build

To manually add to your project

  1. Add Lib/KeychainAccess.xcodeproj to your project
  2. Link KeychainAccess.framework with your target
  3. Add Copy Files Build Phase to include the framework to your application bundle

See iOS Example Project as reference.

Author

kishikawa katsumi, [email protected]

License

KeychainAccess is available under the MIT license. See the LICENSE file for more info.

More Repositories

1

UICKeyChainStore

UICKeyChainStore is a simple wrapper for Keychain on iOS, watchOS, tvOS and macOS. Makes using Keychain APIs as easy as NSUserDefaults.
Objective-C
3,072
star
2

IBPCollectionViewCompositionalLayout

Backport of UICollectionViewCompositionalLayout to earlier iOS 12
Swift
1,467
star
3

PEPhotoCropEditor

Image cropping library for iOS.
Objective-C
1,087
star
4

SECoreTextView

SECoreTextView is multi style text view.
Objective-C
947
star
5

SourceKitForSafari

SourceKit for Safari is a Safari extension for GitHub, that enables Xcode features like go to definition, find references, or documentation on hover.
JavaScript
680
star
6

Kuery

A type-safe Core Data query API using Swift 4's Smart KeyPaths
Swift
621
star
7

ClassicMap

Google Map is back to iOS 6.
Objective-C
541
star
8

JavaScriptBridge

Write iOS apps in Javascript! JavaScriptBridge provides the way to write iOS apps with JavaScript. Powered by JavaScriptCore.framework.
Objective-C
523
star
9

UCZProgressView

UCZProgressView is a circular progress indicator for image loading.
Objective-C
481
star
10

swift-power-assert

Power Assert in Swift.
Swift
355
star
11

MapKit-Route-Directions

Extend MapKit to add route directions powered by Google Maps API.
Objective-C
350
star
12

ScreenRecorder

Capturing a screen as videos on iOS devices for user testing.
Objective-C
315
star
13

BookReader

Sample code for PDFKit on iOS 11, clone of iBooks.app built on top of PDFKit.
Swift
306
star
14

xcresulttool

A GitHub Action that generates a human-readable test report from the Xcode result bundle and shows it on GitHub Checks.
TypeScript
297
star
15

TextKitExamples

TextKit examples for try! Swift NYC 2016
Swift
294
star
16

AppStore-Clone-CollectionViewCompositionalLayouts

Sample project for implementing App Store.app UI with Collection View Compositional Layouts
Swift
280
star
17

swiftfmt

Format Swift code
Swift
215
star
18

SwiftPowerAssert

Power Assert in Swift. Provides descriptive assertion messages.
Swift
198
star
19

UltimateGuideToAnimations

Swift
184
star
20

JapaneseKeyboardKit

Sample implementation for iOS Custom Keyboard Extension with Mozc (Google Japanese Input)
Objective-C
148
star
21

RealmTypeSafeQuery

A type-safe Realm query extensions using Swift 4 Smart KeyPaths
Swift
131
star
22

swiftui-playground

SwiftUI Online Playground
JavaScript
119
star
23

xcjobs

Support the automation of release process of iOS/OSX apps with CI
Ruby
117
star
24

applelocalization-web

HTML
106
star
25

AutoLayoutManiacs

Swift
93
star
26

deliverbot

Go
81
star
27

WebTranslator

Safari web extension for DeepL translate.
CSS
79
star
28

FlipCardNavigationView

Objective-C
75
star
29

UUIDShortener

Convert UUID 32-character hex string into a Base32 short string and back.
Objective-C
70
star
30

BandwidthLimiter

Swift
69
star
31

YAMapKit

Yet Another MapKit.framework based on Google Maps Javascript API.
Objective-C
67
star
32

DescriptionBuilder

DescriptionBuilder (iPhone Utility Program) - Assists implementing description method.
Objective-C
67
star
33

Mozc-for-iOS

Mozc - Japanese Input Method for Chromium OS, Android, Windows, Mac and Linux
C
65
star
34

VoiceNavigation

UI Navigation by voice dictation on iOS 5.1
Objective-C
46
star
35

swift-ast-explorer-playground

Online playground for Swift AST Explorer
HTML
45
star
36

AUCapture

Swift
44
star
37

SymbolFontKit

Easy to use 'SymbolFont' as image in iOS 6.
Objective-C
38
star
38

TiledLayerView

CATiledLayer with UIScrollView Sample.
Objective-C
37
star
39

hatena-touch

Hatena touch / iPhone
Objective-C
36
star
40

CollectionUtils

Useful utilities for Objective-C collection classes.
Objective-C
36
star
41

ForceOrientationSample-iOS

Force view to enter landscape in iOS like YouTube app
Swift
36
star
42

ldr-touch

LDR touch / iPhone
Objective-C
35
star
43

swift-magic

A Swift wrapper for libmagic
Swift
33
star
44

SwiftAST

Experimental project for parsing an output that `swift -dump-ast` produces
Swift
28
star
45

CropImageSample

Objective-C
27
star
46

FTSKit

Full Text Search Library for iOS SDK.
C
27
star
47

PhotoFlipCardView

Flip card photo galally sample
Objective-C
26
star
48

VirtualCameraComposer-Example

Objective-C++
25
star
49

tv-listings

TV Listings / iPhone
Objective-C
23
star
50

SwiftSyntax

SwiftSyntax SwiftPM module
Swift
22
star
51

UIKeyInput-UITextInput-Sample

Objective-C
21
star
52

Doorlock

Swift
20
star
53

DownloadFont

Sample code for downloading additional font on iOS 6 or 7.
Objective-C
19
star
54

swift-compiler-discord-bot

JavaScript
19
star
55

WithCamera

Swift
16
star
56

KeyboardShortcuts

Sample program of handling keyboard shortcuts or any keyboard input events.
Objective-C
16
star
57

MacCatalystSlackApp

Swift
15
star
58

TiKeyChainStore

KeyChain module for Titanium Mobile iPhone
Objective-C
14
star
59

Realm-Hands-On

Swift
13
star
60

WWDCChecker-iPhone

WWDC 2012 Checker
Objective-C
13
star
61

applelocalization-data

Clojure
9
star
62

WWDCChecker-Mac

WWDC Site Update Checker With Push Notification Support (Via Parse.com)
Objective-C
9
star
63

iOSDC-2020-UICollectionViewCompositionalLayout

9
star
64

applelocalization-tools

Swift
8
star
65

ExplodeChristmas

Let's explode Christmas.
Objective-C
7
star
66

BuildNumber

Objective-C
7
star
67

bitrise-step-xcode-result-bundle-to-checks

A Bitrise Step that generates a human-readable test report from the Xcode result bundle and shows it on GitHub Checks.
JavaScript
7
star
68

attendancebot

Go
5
star
69

UICollectionViewCompositionalLayout-Workshop-Starter

Swift
5
star
70

LeapMotionSwipeLockScreen

Swipe gesture to lock the screen with Leap Motion.
Objective-C
5
star
71

GoogleAnalytics-for-WinJS

GoogleAnalytics for WinJS
JavaScript
4
star
72

TextViewLinks

Customize clickable links with UITextView.
Objective-C
4
star
73

TextViewCompatibility

Workaround for iOS 7 UITextView scrolling bugs.
Objective-C
4
star
74

webpdecoder

Swift package for libwebpdecoder
C
4
star
75

Realm-CoreData-Performance

Benchmark code for Realm and CoreData
Objective-C
4
star
76

swift-power-assert-playground

SwiftPowerAssert online live demo
HTML
4
star
77

TextChatTranslator

Swift
4
star
78

FittingLabel

Swift
3
star
79

coveralls-gcov

Upload coverage information generated by Gcov to coveralls.io.
Ruby
3
star
80

dotfiles

Shell
3
star
81

swift-online-playground-tutorial

HTML
3
star
82

downloadable-ios-apps

Objective-C
3
star
83

SwiftFiddleEditor

Swift
3
star
84

async-await-in-swift

HTML
2
star
85

xcresulttool-static

HTML
2
star
86

xcresulttool-example

Swift
2
star
87

swiftfmt-playground

Swiftfmt online playground
HTML
2
star
88

codespace-swift

Dockerfile
2
star
89

swift-package-libbsd

Simple wrapper around the C Library BSD for use with Swift Package Manager on Linux
Swift
2
star
90

make-check

Create GitHub Checks.
JavaScript
2
star
91

swift-developers-japan-discussion-archive

Swift Developers JapanใฎDiscordใ‚ตใƒผใƒใƒผใซๆŠ•็จฟใ•ใ‚ŒใŸ้ŽๅŽปใฎ่ญฐ่ซ–ใ‚’้–ฒ่ฆงใƒปๆคœ็ดขใงใใ‚‹Webใ‚ตใƒผใƒ“ใ‚นใฎใƒชใƒใ‚ธใƒˆใƒชใงใ™ใ€‚
2
star
92

among-us-server-status

Swift
2
star
93

Tree

Swift
2
star
94

WorkaroundPreprocessInfoPlist

Swift
2
star
95

XcodeCloudMac

Swift
1
star
96

prime-number

Swift
1
star
97

XcodeCloud

Swift
1
star
98

NICOLA-Note

1
star
99

StringWidth

Swift
1
star
100

anime-today

Ruby
1
star