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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import ATProtoKit
import Darwin
import Foundation
import OSLog
import Security
import UIKit
extension ATProtoBlueskyService {
func currentSessionID() throws -> String? {
if !usesSharedSession { return defaults.string(forKey: sessionDefaultsKey) }
guard let data = try keychainRead(account: activeSessionAccount) else { return nil }
guard let value = String(data: data, encoding: .utf8), UUID(uuidString: value) != nil else {
throw ServiceError.invalidResponse
}
return value
}
func setCurrentSessionID(_ sessionID: String) throws {
if usesSharedSession {
try keychainWrite(Data(sessionID.utf8), account: activeSessionAccount)
} else {
defaults.set(sessionID, forKey: sessionDefaultsKey)
}
}
func clearCurrentSessionID() throws {
if usesSharedSession {
try keychainDelete(account: activeSessionAccount)
} else {
defaults.removeObject(forKey: sessionDefaultsKey)
}
}
func clearLegacySession() {
guard let account = defaults.string(forKey: sessionDefaultsKey) else { return }
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: keychainService, kSecAttrAccount as String: account
]
SecItemDelete(query as CFDictionary)
defaults.removeObject(forKey: sessionDefaultsKey)
}
func migrateLegacySession(account: String) throws {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: keychainService, kSecAttrAccount as String: account,
kSecReturnData as String: true, kSecReturnAttributes as String: true,
kSecMatchLimit as String: kSecMatchLimitOne
]
var result: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &result)
if status == errSecItemNotFound {
defaults.removeObject(forKey: sessionDefaultsKey)
return
}
guard status == errSecSuccess, let item = result as? [String: Any],
let data = item[kSecValueData as String] as? Data,
let group = item[kSecAttrAccessGroup as String] as? String
else { throw ServiceError.keychain(status) }
try keychainWrite(data, account: account)
try setCurrentSessionID(account)
defaults.removeObject(forKey: sessionDefaultsKey)
if group != sharedAccessGroup {
var legacy: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: keychainService, kSecAttrAccount as String: account
]
legacy[kSecAttrAccessGroup as String] = group
let deletion = SecItemDelete(legacy as CFDictionary)
guard deletion == errSecSuccess || deletion == errSecItemNotFound else {
throw ServiceError.keychain(deletion)
}
}
}
func keychainQuery(account: String) -> [String: Any] {
var query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: keychainService, kSecAttrAccount as String: account
]
if usesSharedSession { query[kSecAttrAccessGroup as String] = sharedAccessGroup }
return query
}
func keychainRead(account: String) throws -> Data? {
var query = keychainQuery(account: account)
query[kSecReturnData as String] = true
query[kSecMatchLimit as String] = kSecMatchLimitOne
var result: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &result)
if status == errSecItemNotFound { return nil }
guard status == errSecSuccess, let data = result as? Data else { throw ServiceError.keychain(status) }
return data
}
func keychainWrite(_ data: Data, account: String) throws {
let query = keychainQuery(account: account)
let attributes: [String: Any] = [
kSecValueData as String: data,
kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
]
let status = SecItemUpdate(query as CFDictionary, attributes as CFDictionary)
if status == errSecSuccess { return }
guard status == errSecItemNotFound else { throw ServiceError.keychain(status) }
var insert = query
attributes.forEach { insert[$0.key] = $0.value }
let insertStatus = SecItemAdd(insert as CFDictionary, nil)
guard insertStatus == errSecSuccess else { throw ServiceError.keychain(insertStatus) }
}
func keychainDelete(account: String) throws {
let query = keychainQuery(account: account)
let status = SecItemDelete(query as CFDictionary)
guard status == errSecSuccess || status == errSecItemNotFound else { throw ServiceError.keychain(status) }
}
}