Skip to content

Commit 48bc25c

Browse files
Added more docs to internal types
1 parent 412fa22 commit 48bc25c

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Please check the [releases](https://github.com/mochidev/swift-webpush/releases)
2828
```swift
2929
dependencies: [
3030
.package(
31-
url: "https://github.com/mochidev/swift-webPush.git",
31+
url: "https://github.com/mochidev/swift-webpush.git",
3232
.upToNextMinor(from: "0.2.1")
3333
),
3434
],
@@ -180,11 +180,15 @@ The `WebPushTesting` module can be used to obtain a mocked `WebPushManager` inst
180180

181181
## Specifications
182182

183+
- [RFC 7515 JSON Web Signature (JWS)](https://datatracker.ietf.org/doc/html/rfc7515)
184+
- [RFC 7519 JSON Web Token (JWT)](https://datatracker.ietf.org/doc/html/rfc7519)
183185
- [RFC 8030 Generic Event Delivery Using HTTP Push](https://datatracker.ietf.org/doc/html/rfc8030)
184186
- [RFC 8188 Encrypted Content-Encoding for HTTP](https://datatracker.ietf.org/doc/html/rfc8188)
185187
- [RFC 8291 Message Encryption for Web Push](https://datatracker.ietf.org/doc/html/rfc8291)
186188
- [RFC 8292 Voluntary Application Server Identification (VAPID) for Web Push](https://datatracker.ietf.org/doc/html/rfc8292)
187189

190+
- [Push API Working Draft](https://www.w3.org/TR/push-api/)
191+
188192
## Contributing
189193

190194
Contribution is welcome! Please take a look at the issues already available, or start a new discussion to propose a new feature. Although guarantees can't be made regarding feature requests, PRs that fit within the goals of the project and that have been discussed beforehand are more than welcome!

Sources/WebPush/VAPID/VAPIDKey.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extension VAPID.Key: Identifiable {
6666
///
6767
/// This value can be shared as is with a subscription registration as the `applicationServerKey` key in JavaScript.
6868
///
69-
/// - SeeAlso: [Push API Working Draft §7.2. PushSubscriptionOptions Interface](https://www.w3.org/TR/push-api/#pushsubscriptionoptions-interface)
69+
/// - SeeAlso: [Push API Working Draft §7.2. `PushSubscriptionOptions` Interface](https://www.w3.org/TR/push-api/#pushsubscriptionoptions-interface)
7070
public struct ID: Hashable, Comparable, Codable, Sendable, CustomStringConvertible {
7171
private var rawValue: String
7272

@@ -93,6 +93,10 @@ extension VAPID.Key: Identifiable {
9393
}
9494
}
9595

96+
/// The public key component in a format suitable for user agents to consume.
97+
///
98+
/// - SeeAlso: [Push API Working Draft §7.2. `PushSubscriptionOptions` Interface](https://www.w3.org/TR/push-api/#dom-pushsubscriptionoptions-applicationserverkey)
99+
/// - SeeAlso: [RFC 8292 Voluntary Application Server Identification (VAPID) for Web Push §3.2. Public Key Parameter ("k")](https://datatracker.ietf.org/doc/html/rfc8292#section-3.2)
96100
public var id: ID {
97101
ID(privateKey.publicKey.x963Representation.base64URLEncodedString())
98102
}

Sources/WebPush/VAPID/VAPIDToken.swift

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,62 @@ extension VAPID {
1313
/// An internal representation the token and authorization headers used self-identification.
1414
///
1515
/// - SeeAlso: [RFC 8292 Voluntary Application Server Identification (VAPID) for Web Push §2. Application Server Self-Identification](https://datatracker.ietf.org/doc/html/rfc8292#section-2)
16+
/// - SeeAlso: [RFC 7515 JSON Web Signature (JWS)](https://datatracker.ietf.org/doc/html/rfc7515)
17+
///- SeeAlso: [RFC 7519 JSON Web Token (JWT)](https://datatracker.ietf.org/doc/html/rfc7519)
1618
struct Token: Hashable, Codable, Sendable {
1719
enum CodingKeys: String, CodingKey {
1820
case audience = "aud"
1921
case subject = "sub"
2022
case expiration = "exp"
2123
}
2224

25+
/// The audience claim, which encodes the origin of the ``Subscriber/endpoint``
26+
///
27+
/// - SeeAlso: ``/Foundation/URL/origin``
28+
/// - SeeAlso: [RFC 7519 JSON Web Token (JWT) §4.1.3. "aud" (Audience) Claim](https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.3)
29+
/// - SeeAlso: [RFC 8292 Voluntary Application Server Identification (VAPID) for Web Push §2. Application Server Self-Identification](https://datatracker.ietf.org/doc/html/rfc8292#section-2)
2330
var audience: String
24-
var subject: VAPID.Configuration.ContactInformation
31+
32+
/// The subject claim, which encodes contact information for the application server.
33+
///
34+
/// - SeeAlso: [RFC 7519 JSON Web Token (JWT) §4.1.2. "sub" (Subject) Claim](https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.2)
35+
/// - SeeAlso: [RFC 8292 Voluntary Application Server Identification (VAPID) for Web Push §2.1. Application Server Contact Information](https://datatracker.ietf.org/doc/html/rfc8292#section-2.1)
36+
var subject: Configuration.ContactInformation
37+
38+
/// The expiry claim, which encodes the number of seconds after 1970/01/01 when the token expires.
39+
///
40+
/// - SeeAlso: [RFC 7519 JSON Web Token (JWT) §4.1.4. "exp" (Expiration Time) Claim](https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4)
41+
/// - SeeAlso: [RFC 8292 Voluntary Application Server Identification (VAPID) for Web Push §2. Application Server Self-Identification](https://datatracker.ietf.org/doc/html/rfc8292#section-2)
2542
var expiration: Int
2643

44+
/// The standard header including the type and algorithm.
45+
///
46+
/// - SeeAlso: [RFC 8292 Voluntary Application Server Identification (VAPID) for Web Push §2. Application Server Self-Identification](https://datatracker.ietf.org/doc/html/rfc8292#section-2)
2747
static let jwtHeader = Array(#"{"typ":"JWT","alg":"ES256"}"#.utf8).base64URLEncodedString()
2848

49+
/// Initialize a token with the specified claims.
2950
init(
3051
origin: String,
31-
contactInformation: VAPID.Configuration.ContactInformation,
52+
contactInformation: Configuration.ContactInformation,
3253
expiration: Date
3354
) {
3455
self.audience = origin
3556
self.subject = contactInformation
3657
self.expiration = Int(expiration.timeIntervalSince1970)
3758
}
3859

60+
/// Initialize a token with the specified claims.
3961
init(
4062
origin: String,
41-
contactInformation: VAPID.Configuration.ContactInformation,
42-
expiresIn: VAPID.Configuration.Duration
63+
contactInformation: Configuration.ContactInformation,
64+
expiresIn: Configuration.Duration
4365
) {
4466
audience = origin
4567
subject = contactInformation
4668
expiration = Int(Date.now.timeIntervalSince1970) + expiresIn.seconds
4769
}
4870

71+
/// Initialize a token from a VAPID `Authorization` header's values.
4972
init?(token: String, key: String) {
5073
let components = token.split(separator: ".")
5174

@@ -69,6 +92,7 @@ extension VAPID {
6992
self = token
7093
}
7194

95+
/// - SeeAlso: [RFC 7515 JSON Web Signature (JWS) §3. JSON Web Signature (JWS) Overview](https://datatracker.ietf.org/doc/html/rfc7515#section-3)
7296
func generateJWT(signedBy signingKey: some VAPIDKeyProtocol) throws -> String {
7397
let header = Self.jwtHeader
7498

@@ -81,6 +105,9 @@ extension VAPID {
81105
return "\(message).\(signature)"
82106
}
83107

108+
/// Generate an `Authorization` header.
109+
///
110+
/// - SeeAlso: [RFC 8292 Voluntary Application Server Identification (VAPID) for Web Push §3. VAPID Authentication Scheme](https://datatracker.ietf.org/doc/html/rfc8292#section-3)
84111
func generateAuthorization(signedBy signingKey: some VAPIDKeyProtocol) throws -> String {
85112
let token = try generateJWT(signedBy: signingKey)
86113
let key = signingKey.id
@@ -93,5 +120,7 @@ extension VAPID {
93120
protocol VAPIDKeyProtocol: Identifiable, Sendable {
94121
associatedtype Signature: ContiguousBytes
95122

123+
/// Returns a JWS signature for the message.
124+
/// - SeeAlso: [RFC 7515 JSON Web Signature (JWS)](https://datatracker.ietf.org/doc/html/rfc7515)
96125
func signature(for message: some DataProtocol) throws -> Signature
97126
}

0 commit comments

Comments
 (0)