Skip to content

More Tests #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Please check the [releases](https://github.com/mochidev/swift-webpush/releases)
dependencies: [
.package(
url: "https://github.com/mochidev/swift-webPush.git",
.upToNextMinor(from: "0.1.3")
.upToNextMinor(from: "0.1.4")
),
],
...
Expand Down Expand Up @@ -105,7 +105,13 @@ Before integrating WebPush into your server, you must generate one time VAPID ke

To uninstall the generator:
```zsh
% package experimental-uninstall vapid-key-generator
% swift package experimental-uninstall vapid-key-generator
```

To update the generator, uninstall it and re-install it after pulling from main:
```zsh
% swift package experimental-uninstall vapid-key-generator
% swift package experimental-install
```

Once installed, a new configuration can be generated as needed:
Expand Down
9 changes: 4 additions & 5 deletions Sources/WebPush/Helpers/DataProtocol+Base64URLCoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation

extension DataProtocol {
@_disfavoredOverload
@usableFromInline
func base64URLEncodedString() -> String {
Data(self)
.base64EncodedString()
Expand All @@ -20,18 +21,16 @@ extension DataProtocol {
}

extension ContiguousBytes {
@usableFromInline
func base64URLEncodedString() -> String {
withUnsafeBytes { bytes in
Data(bytes)
.base64EncodedString()
.replacingOccurrences(of: "+", with: "-")
.replacingOccurrences(of: "/", with: "_")
.replacingOccurrences(of: "=", with: "")
(bytes as any DataProtocol).base64URLEncodedString()
}
}
}

extension DataProtocol where Self: RangeReplaceableCollection {
@usableFromInline
init?(base64URLEncoded string: some StringProtocol) {
var base64String = string.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/")
while base64String.count % 4 != 0 {
Expand Down
13 changes: 13 additions & 0 deletions Sources/WebPush/VAPID/VAPIDKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,26 @@ extension VoluntaryApplicationServerIdentification {
public struct Key: Sendable {
private var privateKey: P256.Signing.PrivateKey

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

/// Initialize a key from a P256 SIgning Private Key.
///
/// - Warning: Do not re-use this key for any other purpose other than VAPID authorization!
public init(privateKey: P256.Signing.PrivateKey) {
self.privateKey = privateKey
}

/// Decode a key directly from a Base 64 (URL) encoded string, or throw an error if decoding failed.
public init(base64URLEncoded: String) throws {
guard let data = Data(base64URLEncoded: base64URLEncoded)
else { throw Base64URLDecodingError() }
privateKey = try P256.Signing.PrivateKey(rawRepresentation: data)
}
}
}

Expand Down
32 changes: 21 additions & 11 deletions Tests/WebPushTests/Base64URLCodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,26 @@ import Foundation
import Testing
@testable import WebPush

@Test func base64URLDecoding() async throws {
let string = ">>> Hello, swift-webpush world??? 🎉"
let base64Encoded = "Pj4+IEhlbGxvLCBzd2lmdC13ZWJwdXNoIHdvcmxkPz8/IPCfjok="
let base64URLEncoded = "Pj4-IEhlbGxvLCBzd2lmdC13ZWJwdXNoIHdvcmxkPz8_IPCfjok"
#expect(String(decoding: Data(base64URLEncoded: base64Encoded)!, as: UTF8.self) == string)
#expect(String(decoding: Data(base64URLEncoded: base64URLEncoded)!, as: UTF8.self) == string)
}
@Suite("Base 64 URL Coding")
struct Base64URLCoding {
@Test func base64URLDecoding() async throws {
let string = ">>> Hello, swift-webpush world??? 🎉"
let base64Encoded = "Pj4+IEhlbGxvLCBzd2lmdC13ZWJwdXNoIHdvcmxkPz8/IPCfjok="
let base64URLEncoded = "Pj4-IEhlbGxvLCBzd2lmdC13ZWJwdXNoIHdvcmxkPz8_IPCfjok"
#expect(String(decoding: Data(base64URLEncoded: base64Encoded)!, as: UTF8.self) == string)
#expect(String(decoding: Data(base64URLEncoded: base64URLEncoded)!, as: UTF8.self) == string)
#expect(String(decoding: [UInt8](base64URLEncoded: base64Encoded)!, as: UTF8.self) == string)
#expect(String(decoding: [UInt8](base64URLEncoded: base64URLEncoded)!, as: UTF8.self) == string)
}

@Test func invalidBase64URLDecoding() async throws {
#expect(Data(base64URLEncoded: " ") == nil)
}

@Test func base64URLEncoding() async throws {
let string = ">>> Hello, swift-webpush world??? 🎉"
let base64URLEncoded = "Pj4-IEhlbGxvLCBzd2lmdC13ZWJwdXNoIHdvcmxkPz8_IPCfjok"
#expect(Array(string.utf8).base64URLEncodedString() == base64URLEncoded)
@Test func base64URLEncoding() async throws {
let string = ">>> Hello, swift-webpush world??? 🎉"
let base64URLEncoded = "Pj4-IEhlbGxvLCBzd2lmdC13ZWJwdXNoIHdvcmxkPz8_IPCfjok"
#expect([UInt8](string.utf8).base64URLEncodedString() == base64URLEncoded)
#expect(Data(string.utf8).base64URLEncodedString() == base64URLEncoded)
}
}
Loading
Loading