Skip to content

Commit 91d0cd1

Browse files
Added a way to check the status of a subscriber's key against a configuration
1 parent 72b50d6 commit 91d0cd1

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Sources/WebPush/VAPID/VAPIDConfiguration.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,24 @@ extension VAPID.Configuration {
181181
}
182182
}
183183

184+
/// The satus of a key as it relates to a configuration.
185+
///
186+
/// - SeeAlso: ``WebPushManager/keyStatus(for:)``
187+
public enum KeyStatus: Sendable, Hashable {
188+
/// The key is valid and should continue to be used.
189+
case valid
190+
191+
/// The key had been deprecated.
192+
///
193+
/// The user should be encouraged to re-register using a new key.
194+
case deprecated
195+
196+
/// The key is unknown to the configuration.
197+
///
198+
/// The configuration should be investigated as all keys should be accounted for.
199+
case unknown
200+
}
201+
184202
public struct Duration: Hashable, Comparable, Codable, ExpressibleByIntegerLiteral, AdditiveArithmetic, Sendable {
185203
public let seconds: Int
186204

Sources/WebPush/WebPushManager.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ public actor WebPushManager: Sendable {
175175
vapidConfiguration.primaryKey?.id ?? vapidConfiguration.keys.randomElement()!.id
176176
}
177177

178+
/// Check the status of a key against the current configuration.
179+
public nonisolated func keyStatus(for keyID: VAPID.Key.ID) -> VAPID.Configuration.KeyStatus {
180+
guard let key = vapidKeyLookup[keyID]
181+
else { return .unknown }
182+
183+
if vapidConfiguration.deprecatedKeys?.contains(key) == true {
184+
return .deprecated
185+
}
186+
187+
return .valid
188+
}
189+
178190
/// Send a push message as raw data.
179191
///
180192
/// The service worker you registered is expected to know how to decode the data you send.

0 commit comments

Comments
 (0)