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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import Foundation
@preconcurrency import KeychainAccess
/// A signed-in account the app can switch to.
struct StoredAccount: Codable, Identifiable, Hashable {
let did: String
var handle: String?
var avatar: String?
var id: String {
did
}
}
/// Where an account signs in and where its repo lives: what a refresh, a
/// revocation and every signed-in call need besides the tokens.
struct AccountServer: Codable, Sendable, Equatable {
/// The account's PDS, where signed-in calls go.
let pds: URL
/// The authorization server that issued the tokens.
let issuer: String
let tokenEndpoint: URL
let revocationEndpoint: URL?
}
/// Secure storage for OAuth tokens using Keychain.
///
/// Every credential is namespaced by DID so several accounts can stay signed in
/// at once. Reads and writes take the DID explicitly — a token refresh that
/// lands after the user switched accounts must not write over the account they
/// switched to. `activeDID` records which one the UI is currently showing.
enum TokenStorage {
/// Rebuilt per access so a task-local override applies. `Keychain` is a
/// value wrapper over query attributes, so this costs nothing.
private static var keychain: Keychain {
Keychain(service: StorageEnvironment.credentialService)
}
// MARK: - Account list
private static let accountsKey = "accounts"
private static let activeDIDKey = "active_did"
/// Every account with credentials on this device, in the order they were added.
static var accounts: [StoredAccount] {
get {
guard let data = try? keychain.getData(accountsKey),
let decoded = try? JSONDecoder().decode([StoredAccount].self, from: data)
else { return [] }
return decoded
}
set {
guard let data = try? JSONEncoder().encode(newValue) else { return }
try? keychain.set(data, key: accountsKey)
}
}
/// The DID the app is currently acting as.
static var activeDID: String? {
get { try? keychain.get(activeDIDKey) }
set {
if let newValue {
try? keychain.set(newValue, key: activeDIDKey)
} else {
try? keychain.remove(activeDIDKey)
}
}
}
/// Alias for `activeDID`, kept for call sites that just want "who am I".
static var userDID: String? {
get { activeDID }
set { activeDID = newValue }
}
/// Add or update an account in the list, preserving its position.
static func upsertAccount(_ account: StoredAccount) {
var list = accounts
if let index = list.firstIndex(where: { $0.did == account.did }) {
// Don't let a nil handle/avatar from a fresh token response wipe
// details a previous profile fetch resolved.
list[index].handle = account.handle ?? list[index].handle
list[index].avatar = account.avatar ?? list[index].avatar
} else {
list.append(account)
}
accounts = list
}
// MARK: - Per-account credentials
private static func read(_ name: String, _ did: String) -> String? {
try? keychain.get("\(name)::\(did)")
}
private static func write(_ name: String, _ did: String, _ value: String?) {
let key = "\(name)::\(did)"
if let value {
try? keychain.set(value, key: key)
} else {
try? keychain.remove(key)
}
}
static func accessToken(for did: String) -> String? {
read("access_token", did)
}
static func refreshToken(for did: String) -> String? {
read("refresh_token", did)
}
static func handle(for did: String) -> String? {
read("user_handle", did)
}
static func avatar(for did: String) -> String? {
read("user_avatar", did)
}
static func tokenExpiresAt(for did: String) -> Date? {
guard let raw = read("token_expires_at", did), let interval = Double(raw) else { return nil }
return Date(timeIntervalSince1970: interval)
}
/// Space-separated OAuth scope string from the token response. Used at
/// launch to detect tokens minted before newer scopes were added so the
/// app can force a fresh sign-in. `nil` for sessions created before this
/// field was introduced — treat as a full scope mismatch.
static func grantedScope(for did: String) -> String? {
read("token_scope", did)
}
static func server(for did: String) -> AccountServer? {
guard let raw = read("oauth_server", did) else { return nil }
return try? JSONDecoder().decode(AccountServer.self, from: Data(raw.utf8))
}
static func storeServer(_ server: AccountServer, for did: String) {
guard let data = try? JSONEncoder().encode(server) else { return }
write("oauth_server", did, String(data: data, encoding: .utf8))
}
static func setAvatar(_ avatar: String?, for did: String) {
write("user_avatar", did, avatar)
}
/// Persist a token response for one account. `scope` is only overwritten
/// when the response carries one — refresh responses often omit it.
static func storeTokens(
did: String,
accessToken: String,
refreshToken: String?,
handle: String?,
expiresAt: Date,
scope: String?
) {
write("access_token", did, accessToken)
write("refresh_token", did, refreshToken)
if let handle {
write("user_handle", did, handle)
}
write("token_expires_at", did, String(expiresAt.timeIntervalSince1970))
if let scope {
write("token_scope", did, scope)
}
}
/// Whether `did` has enough stored state to resume without a fresh sign-in.
static func hasCredentials(for did: String) -> Bool {
accessToken(for: did) != nil && refreshToken(for: did) != nil
}
/// Whether `did` can make signed-in calls: tokens, and the PDS and
/// authorization server they belong to.
static func hasSession(for did: String) -> Bool {
hasCredentials(for: did) && server(for: did) != nil
}
/// Forget one account: wipe its credentials and drop it from the list.
/// Clears `activeDID` if it was the active one — the caller decides which
/// account, if any, takes over.
static func removeAccount(_ did: String) {
for name in ["access_token", "refresh_token", "user_handle", "user_avatar", "token_expires_at", "token_scope", "oauth_server"] {
write(name, did, nil)
}
accounts = accounts.filter { $0.did != did }
if activeDID == did {
activeDID = nil
}
}
// MARK: - Migration
/// Pre-multi-account installs stored a single unsuffixed credential set.
/// Move it under its DID and seed the account list. Idempotent: it only
/// fires while a legacy `user_did` entry is still present.
/// Returns the migrated DID so the caller can move the DPoP key with it.
@discardableResult
static func migrateLegacyAccountIfNeeded() -> String? {
guard let did = try? keychain.get("user_did"), !did.isEmpty else { return nil }
let legacyHandle = try? keychain.get("user_handle")
let legacyAvatar = try? keychain.get("user_avatar")
for name in ["access_token", "refresh_token", "user_handle", "user_avatar", "token_expires_at", "token_scope"] {
write(name, did, try? keychain.get(name))
}
upsertAccount(StoredAccount(did: did, handle: legacyHandle, avatar: legacyAvatar))
activeDID = did
for legacy in ["access_token", "refresh_token", "user_handle", "user_avatar", "token_expires_at", "token_scope", "user_did"] {
try? keychain.remove(legacy)
}
return did
}
}