Skip to content

Commit

Permalink
Added a way to check the status of a subscriber's key against a confi…
Browse files Browse the repository at this point in the history
…guration
  • Loading branch information
dimitribouniol committed Dec 14, 2024
1 parent 72b50d6 commit 91d0cd1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Sources/WebPush/VAPID/VAPIDConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,24 @@ extension VAPID.Configuration {
}
}

/// The satus of a key as it relates to a configuration.
///
/// - SeeAlso: ``WebPushManager/keyStatus(for:)``
public enum KeyStatus: Sendable, Hashable {
/// The key is valid and should continue to be used.
case valid

/// The key had been deprecated.
///
/// The user should be encouraged to re-register using a new key.
case deprecated

/// The key is unknown to the configuration.
///
/// The configuration should be investigated as all keys should be accounted for.
case unknown
}

public struct Duration: Hashable, Comparable, Codable, ExpressibleByIntegerLiteral, AdditiveArithmetic, Sendable {
public let seconds: Int

Expand Down
12 changes: 12 additions & 0 deletions Sources/WebPush/WebPushManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ public actor WebPushManager: Sendable {
vapidConfiguration.primaryKey?.id ?? vapidConfiguration.keys.randomElement()!.id
}

/// Check the status of a key against the current configuration.
public nonisolated func keyStatus(for keyID: VAPID.Key.ID) -> VAPID.Configuration.KeyStatus {
guard let key = vapidKeyLookup[keyID]
else { return .unknown }

if vapidConfiguration.deprecatedKeys?.contains(key) == true {
return .deprecated
}

return .valid
}

/// Send a push message as raw data.
///
/// The service worker you registered is expected to know how to decode the data you send.
Expand Down

0 comments on commit 91d0cd1

Please sign in to comment.