-
Notifications
You must be signed in to change notification settings - Fork 285
/
KeychainManager.swift
86 lines (67 loc) · 2.06 KB
/
KeychainManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import Foundation
// MARK: - KeychainManager
//
enum KeychainManager {
/// Simplenote's Share Extension Token
///
@KeychainItemWrapper(service: SimplenoteKeychain.extensionService, account: SimplenoteKeychain.extensionAccount)
static var extensionToken: String?
/// Simplenote's PIN Lock Keychain Item
///
@KeychainItemWrapper(service: SimplenoteKeychain.pinlockService, account: SimplenoteKeychain.pinlockAccount)
static var pinlock: String?
/// Simplenote's Timestamp Keychain Item
///
@KeychainItemWrapper(service: SimplenoteKeychain.timestampService, account: SimplenoteKeychain.timestampAccount)
static var timestamp: String?
}
// MARK: - KeychainItemWrapper
//
@propertyWrapper
struct KeychainItemWrapper {
let item: KeychainPasswordItem
/// Designated Initializer
///
init(service: String, account: String) {
item = KeychainPasswordItem(service: service, account: account)
}
var wrappedValue: String? {
mutating get {
do {
return try item.readPassword()
} catch KeychainError.noPassword {
return nil
} catch {
NSLog("Error Reading Keychain Item \(item.service).\(item.account): \(error)")
return nil
}
}
set {
do {
if let value = newValue {
try item.savePassword(value)
} else {
try item.deleteItem()
}
} catch {
NSLog("Error Setting Keychain Item \(item.service).\(item.account)")
}
}
}
}
// MARK: - Keychain Constants
//
enum SimplenoteKeychain {
/// Extension Token
///
static let extensionAccount = "Main"
static let extensionService = "SimplenoteShare"
/// Pinlock
///
static let pinlockAccount = "SimplenotePin"
static let pinlockService = pinlockAccount
/// Timestamp
///
static let timestampAccount = "Main"
static let timestampService = "simplenote-passcode"
}