Skip to content

Commit 4a51c35

Browse files
Added complete documentation for VAPID Configurations
1 parent 91d0cd1 commit 4a51c35

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

Sources/WebPush/VAPID/VAPIDConfiguration.swift

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,62 @@
99
import Foundation
1010

1111
extension VoluntaryApplicationServerIdentification {
12+
/// A configuration object specifying the contact information along with the keys that your application server identifies itself with.
13+
///
14+
/// The ``primaryKey``, when priovided, will always be used for new subscriptions when ``WebPushManager/nextVAPIDKeyID`` is called. If omitted, one of the keys in ``keys`` will be randomely chosen instead.
15+
///
16+
/// ``deprecatedKeys`` that you must stull support for older subscribers, but don't wish to use when registering new subscribers, may also be specified.
17+
///
18+
/// To reduce implementation complexity, it is recommended to only use a single ``primaryKey``, though this key should be stored with subscribers as ``Subscriber`` encourages you to do so that you can deprecate it in the future should it ever leak.
19+
///
20+
/// ## Codable
21+
///
22+
/// VAPID configurations should ideally be generated a single time and shared across all instances of your application server, across runs. To facilitate this, you can encode and decode a configuration to load it at runtime rather than instanciate a new one every time:
23+
/// ```swift
24+
/// // TODO: Load this data from .env or from file system
25+
/// let configurationData = Data(#" {"contactInformation":"https://example.com","expirationDuration":79200,"primaryKey":"6PSSAJiMj7uOvtE4ymNo5GWcZbT226c5KlV6c+8fx5g=","validityDuration":72000} "#.utf8)
26+
/// let vapidConfiguration = try JSONDecoder().decode(VAPID.Configuration.self, from: configurationData)
27+
/// ```
28+
///
29+
/// - SeeAlso: [Generating Keys](https://github.com/mochidev/swift-webpush?tab=readme-ov-file#generating-keys): Keys can also be generated by our `vapid-key-generator` helper tool.
1230
public struct Configuration: Hashable, Sendable {
1331
/// The VAPID key that identifies the push service to subscribers.
1432
///
15-
/// This key should be shared by all instances of your push service, and should be kept secure. Rotating this key is not recommended as you'll lose access to subscribers that registered against it.
16-
///
17-
/// Some implementations will choose to use different keys per subscriber. In that case, choose to provide a set of keys instead.
33+
/// If not provided, a key from ``keys`` will be used instead.
34+
/// - SeeAlso: ``VoluntaryApplicationServerIdentification/Configuration``
1835
public private(set) var primaryKey: Key?
36+
37+
/// The set of valid keys to choose from when identifying the applications erver to new registrations.
1938
public private(set) var keys: Set<Key>
39+
40+
/// The set of deprecated keys to continue to support when signing push messages, but shouldn't be used for new registrations.
41+
///
42+
/// This set can be interogated via ``WebPushManager/`` to determine if a subscriber should be re-registered against a new key or not:
43+
/// ```swift
44+
/// webPushManager.keyStatus(for: subscriber.vapidKeyID) == .deprecated
45+
/// ```
2046
public private(set) var deprecatedKeys: Set<Key>?
47+
48+
/// The contact information an administrator of a push service may use to contact you in the case of an issue.
2149
public var contactInformation: ContactInformation
50+
51+
/// The number of seconds before a cached authentication header signed by this configuration fully expires.
52+
///
53+
/// This value must be 24 hours or less, and it conservatively set to 22 hours by default to account for clock drift between your applications erver and push services.
2254
public var expirationDuration: Duration
55+
56+
/// The number of seconds before a cached authentication header signed by this configuration is renewed.
57+
///
58+
/// This valus must be less than ``expirationDuration``, and is set to 20 hours by default as an adequate compromise between re-usability and key over-use.
2359
public var validityDuration: Duration
2460

61+
/// Initialize a configuration with a single primary key.
62+
/// - Parameters:
63+
/// - key: The primary key to use when introducing your application server during registration.
64+
/// - deprecatedKeys: Suppoted, but deprecated, keys to use during push delivery if a subscriber requires them.
65+
/// - contactInformation: The contact information an administrator of a push service may use to contact you in the case of an issue.
66+
/// - expirationDuration: The number of seconds before a cached authentication header signed by this configuration fully expires.
67+
/// - validityDuration: The number of seconds before a cached authentication header signed by this configuration is renewed.
2568
public init(
2669
key: Key,
2770
deprecatedKeys: Set<Key>? = nil,
@@ -39,6 +82,16 @@ extension VoluntaryApplicationServerIdentification {
3982
self.validityDuration = validityDuration
4083
}
4184

85+
/// Initialize a configuration with a multiple VAPID keys.
86+
///
87+
/// Use this initializer _only_ if you wish to implement more complicated key rotation if you believe keys may be leaked at a higher rate than usual. In all other cases, it is highly recommended to use ``init(key:deprecatedKeys:contactInformation:expirationDuration:validityDuration:)`` instead to supply a singly primary key and keep it secure.
88+
/// - Parameters:
89+
/// - primaryKey: The optional primary key to use when introducing your application server during registration.
90+
/// - keys: The set of valid keys to choose from when identifying the applications erver to new registrations.
91+
/// - deprecatedKeys: Suppoted, but deprecated, keys to use during push delivery if a subscriber requires them.
92+
/// - contactInformation: The contact information an administrator of a push service may use to contact you in the case of an issue.
93+
/// - expirationDuration: The number of seconds before a cached authentication header signed by this configuration fully expires.
94+
/// - validityDuration: The number of seconds before a cached authentication header signed by this configuration is renewed.
4295
public init(
4396
primaryKey: Key?,
4497
keys: Set<Key>,
@@ -64,7 +117,14 @@ extension VoluntaryApplicationServerIdentification {
64117
self.validityDuration = validityDuration
65118
}
66119

67-
mutating func updateKeys(
120+
/// Update the keys that this configuration represents.
121+
///
122+
/// At least one non-deprecated key must be specified, whether it is a primary key or specified in the list of keys, or this method will throw.
123+
/// - Parameters:
124+
/// - primaryKey: The primary key to use when registering a new subscriber.
125+
/// - keys: A list of valid, non deprecated keys to cycle through if a primary key is not specified.
126+
/// - deprecatedKeys: A list of deprecated keys to use for signing if a subscriber requires it, but won't be used for new registrations.
127+
public mutating func updateKeys(
68128
primaryKey: Key?,
69129
keys: Set<Key>,
70130
deprecatedKeys: Set<Key>? = nil

0 commit comments

Comments
 (0)