Skip to content

Commit d7fbf4f

Browse files
Merge branch 'master' into KM-16629-storekit2
2 parents a9ec5bb + ae9450a commit d7fbf4f

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

LocalPackages/PIAKPI/Sources/PIAKPI/Internal/KPIEventUtils.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ struct KPIEventUtils {
3232

3333
if let createdAt = Self.makeISO8601Formatter().date(from: identifier.createdAt) {
3434
let now = clock()
35-
let elapsedDays = now.timeIntervalSince(createdAt) / (24 * 60 * 60)
36-
if elapsedDays > 1.0 {
35+
if !Self.utcCalendar.isDate(now, inSameDayAs: createdAt) {
3736
identifier = newAggregatedIdentifier()
3837
kpiPersistency.clearAll()
3938
}
@@ -56,4 +55,10 @@ struct KPIEventUtils {
5655
f.timeZone = TimeZone(identifier: "UTC")
5756
return f
5857
}
58+
59+
private static let utcCalendar: Calendar = {
60+
var calendar = Calendar(identifier: .gregorian)
61+
calendar.timeZone = TimeZone(identifier: "UTC")!
62+
return calendar
63+
}()
5964
}

LocalPackages/PIALibrary/Sources/PIALibrary/Util/Keychain.swift

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,44 @@ public class Keychain {
8282

8383
/// :nodoc:
8484
public func set(password: String, for username: String) throws {
85-
removePassword(for: username)
85+
guard let data = password.data(using: .utf8) else {
86+
throw KeychainError.add
87+
}
8688

8789
var query = [String: Any]()
8890
setScope(query: &query)
8991
query[kSecClass as String] = kSecClassGenericPassword
9092
query[kSecAttrAccount as String] = username
91-
query[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlock
92-
query[kSecValueData as String] = password.data(using: .utf8)
9393

94-
let status = SecItemAdd(query as CFDictionary, nil)
95-
guard (status == errSecSuccess) else {
94+
let attributesToUpdate: [String: Any] = [
95+
kSecValueData as String: data,
96+
kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlock
97+
]
98+
let updateStatus = SecItemUpdate(query as CFDictionary, attributesToUpdate as CFDictionary)
99+
100+
switch updateStatus {
101+
case errSecSuccess:
102+
return
103+
case errSecItemNotFound:
104+
// No existing item — add a fresh one.
105+
query[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlock
106+
query[kSecValueData as String] = data
107+
let addStatus = SecItemAdd(query as CFDictionary, nil)
108+
switch addStatus {
109+
case errSecSuccess:
110+
return
111+
case errSecDuplicateItem:
112+
// Lost a first-write race — the item now exists, so update it in place.
113+
query.removeValue(forKey: kSecAttrAccessible as String)
114+
query.removeValue(forKey: kSecValueData as String)
115+
guard SecItemUpdate(query as CFDictionary, attributesToUpdate as CFDictionary) == errSecSuccess
116+
else {
117+
throw KeychainError.add
118+
}
119+
default:
120+
throw KeychainError.add
121+
}
122+
default:
96123
throw KeychainError.add
97124
}
98125
}

0 commit comments

Comments
 (0)