Skip to content

Commit b120f65

Browse files
Added UserAgentKeyMaterial Error type
1 parent f6e7d51 commit b120f65

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// Base64URLDecodingError.swift
3+
// swift-webpush
4+
//
5+
// Created by Dimitri Bouniol on 2024-12-13.
6+
// Copyright © 2024 Mochi Development, Inc. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/// An error encountered while decoding Base64 data.
12+
public struct Base64URLDecodingError: LocalizedError, Hashable {
13+
public init() {}
14+
15+
public var errorDescription: String? {
16+
"The Base64 data could not be decoded."
17+
}
18+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// UserAgentKeyMaterialError.swift
3+
// swift-webpush
4+
//
5+
// Created by Dimitri Bouniol on 2024-12-13.
6+
// Copyright © 2024 Mochi Development, Inc. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
/// An error encountered during ``VAPID/Configuration`` initialization or decoding.
12+
public struct UserAgentKeyMaterialError: LocalizedError {
13+
enum Kind {
14+
case invalidPublicKey
15+
case invalidAuthenticationSecret
16+
}
17+
18+
var kind: Kind
19+
var underlyingError: any Error
20+
21+
/// The public key was invalid.
22+
public static func invalidPublicKey(underlyingError: Error) -> Self {
23+
Self(kind: .invalidPublicKey, underlyingError: underlyingError)
24+
}
25+
26+
/// The authentication secret was invalid.
27+
public static func invalidAuthenticationSecret(underlyingError: Error) -> Self {
28+
Self(kind: .invalidAuthenticationSecret, underlyingError: underlyingError)
29+
}
30+
31+
public var errorDescription: String? {
32+
switch kind {
33+
case .invalidPublicKey:
34+
"Subscriber Public Key (`\(UserAgentKeyMaterial.CodingKeys.publicKey)`) was invalid: \(underlyingError.localizedDescription)."
35+
case .invalidAuthenticationSecret:
36+
"Subscriber Authentication Secret (`\(UserAgentKeyMaterial.CodingKeys.authenticationSecret)`) was invalid: \(underlyingError.localizedDescription)."
37+
}
38+
}
39+
}

Sources/WebPush/Subscriber.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ public struct UserAgentKeyMaterial: Sendable {
6767
public init(
6868
publicKey: String,
6969
authenticationSecret: String
70-
) throws {
70+
) throws(UserAgentKeyMaterialError) {
7171
guard let publicKeyData = Data(base64URLEncoded: publicKey)
72-
else { throw CancellationError() } // invalid public key error (underlying error = URLDecoding error)
72+
else { throw .invalidPublicKey(underlyingError: Base64URLDecodingError()) }
7373
do {
7474
self.publicKey = try P256.KeyAgreement.PublicKey(x963Representation: publicKeyData)
75-
} catch { throw CancellationError() } // invalid public key error (underlying error = error)
75+
} catch { throw .invalidPublicKey(underlyingError: error) }
7676

7777
guard let authenticationSecretData = Data(base64URLEncoded: authenticationSecret)
78-
else { throw CancellationError() } // invalid authentication secret error (underlying error = URLDecoding error)
78+
else { throw .invalidAuthenticationSecret(underlyingError: Base64URLDecodingError()) }
7979

8080
self.authenticationSecret = authenticationSecretData
8181
}
@@ -190,5 +190,6 @@ public struct Subscriber: SubscriberProtocol, Codable, Hashable, Sendable {
190190
}
191191

192192
extension Subscriber: Identifiable {
193+
/// A safe identifier to use for the subscriber without exposing key material.
193194
public var id: String { endpoint.absoluteString }
194195
}

0 commit comments

Comments
 (0)