Skip to content

Commit 158cddb

Browse files
Added initializer to VAPID.Key for decoding Base64 strings directly.
1 parent 5194f38 commit 158cddb

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Please check the [releases](https://github.com/mochidev/swift-webpush/releases)
2929
dependencies: [
3030
.package(
3131
url: "https://github.com/mochidev/swift-webPush.git",
32-
.upToNextMinor(from: "0.1.3")
32+
.upToNextMinor(from: "0.1.4")
3333
),
3434
],
3535
...
@@ -105,7 +105,13 @@ Before integrating WebPush into your server, you must generate one time VAPID ke
105105

106106
To uninstall the generator:
107107
```zsh
108-
% package experimental-uninstall vapid-key-generator
108+
% swift package experimental-uninstall vapid-key-generator
109+
```
110+
111+
To update the generator, uninstall it and re-install it after pulling from main:
112+
```zsh
113+
% swift package experimental-uninstall vapid-key-generator
114+
% swift package experimental-install
109115
```
110116

111117
Once installed, a new configuration can be generated as needed:

Sources/WebPush/VAPID/VAPIDKey.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,26 @@ extension VoluntaryApplicationServerIdentification {
1616
public struct Key: Sendable {
1717
private var privateKey: P256.Signing.PrivateKey
1818

19+
/// Create a brand new VAPID signing key.
20+
///
21+
/// - Note: You must persist this key somehow if you are creating it yourself.
1922
public init() {
2023
privateKey = P256.Signing.PrivateKey(compactRepresentable: false)
2124
}
2225

26+
/// Initialize a key from a P256 SIgning Private Key.
27+
///
28+
/// - Warning: Do not re-use this key for any other purpose other than VAPID authorization!
2329
public init(privateKey: P256.Signing.PrivateKey) {
2430
self.privateKey = privateKey
2531
}
32+
33+
/// Decode a key directly from a Base 64 (URL) encoded string, or throw an error if decoding failed.
34+
public init(base64URLEncoded: String) throws {
35+
guard let data = Data(base64URLEncoded: base64URLEncoded)
36+
else { throw Base64URLDecodingError() }
37+
privateKey = try P256.Signing.PrivateKey(rawRepresentation: data)
38+
}
2639
}
2740
}
2841

vapid-key-generator/Sources/VAPIDKeyGenerator.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ struct MyCoolerTool: ParsableCommand {
5353
if silent {
5454
print("\(json)")
5555
} else {
56-
print("VAPID.Key (with quotes): \(json)\n\n")
56+
print("VAPID.Key: \(json)\n\n")
5757
print("Example Usage:")
5858
print(" // TODO: Load this data from .env or from file system")
59-
print(" let keyData = Data(#\" \(json) \"#.utf8)")
60-
print(" let vapidKey = try JSONDecoder().decode(VAPID.Key.self, from: keyData)")
59+
print(" let keyData = Data(\(json).utf8)")
60+
print(" let vapidKey = try VAPID.Key(base64URLEncoded: keyData)")
6161
}
6262
} else if !keyOnly {
6363
let contactInformation = if let supportURL, email == nil {

0 commit comments

Comments
 (0)