|
| 1 | +// |
| 2 | +// VAPIDKeyGenerator.swift |
| 3 | +// swift-webpush |
| 4 | +// |
| 5 | +// Created by Dimitri Bouniol on 2024-12-14. |
| 6 | +// Copyright © 2024 Mochi Development, Inc. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import ArgumentParser |
| 10 | +import Foundation |
| 11 | +import WebPush |
| 12 | + |
| 13 | +@main |
| 14 | +struct MyCoolerTool: ParsableCommand { |
| 15 | + static let configuration = CommandConfiguration( |
| 16 | + abstract: "Generate VAPID Keys.", |
| 17 | + usage: """ |
| 18 | + vapid-key-generator <support-url> |
| 19 | + vapid-key-generator --email <email> |
| 20 | + vapid-key-generator --key-only |
| 21 | + """, |
| 22 | + discussion: """ |
| 23 | + Generates VAPID keys and configurations suitable for use on your server. Keys should generally only be generated once and kept secure. |
| 24 | + """ |
| 25 | + ) |
| 26 | + |
| 27 | + @Flag(name: [.long, .customShort("k")], help: "Only generate a VAPID key.") |
| 28 | + var keyOnly = false |
| 29 | + |
| 30 | + @Flag(name: [.long, .customShort("s")], help: "Output raw JSON only so this tool can be piped with others in scripts.") |
| 31 | + var silent = false |
| 32 | + |
| 33 | + @Flag(name: [.long, .customShort("p")], help: "Output JSON with spacing. Has no effect when generating keys only.") |
| 34 | + var pretty = false |
| 35 | + |
| 36 | + @Option(name: [.long], help: "Parse the input as an email address.") |
| 37 | + var email: String? |
| 38 | + |
| 39 | + @Argument(help: "The fully-qualified HTTPS support URL administrators of push services may contact you at: https://example.com/support") var supportURL: URL? |
| 40 | + |
| 41 | + mutating func run() throws { |
| 42 | + let key = VAPID.Key() |
| 43 | + let encoder = JSONEncoder() |
| 44 | + if pretty { |
| 45 | + encoder.outputFormatting = [.withoutEscapingSlashes, .sortedKeys, .prettyPrinted] |
| 46 | + } else { |
| 47 | + encoder.outputFormatting = [.withoutEscapingSlashes, .sortedKeys] |
| 48 | + } |
| 49 | + |
| 50 | + if keyOnly, supportURL == nil, email == nil { |
| 51 | + let json = String(decoding: try encoder.encode(key), as: UTF8.self) |
| 52 | + |
| 53 | + if silent { |
| 54 | + print("\(json)") |
| 55 | + } else { |
| 56 | + print("VAPID.Key (with quotes): \(json)\n\n") |
| 57 | + print("Example Usage:") |
| 58 | + 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)") |
| 61 | + } |
| 62 | + } else if !keyOnly { |
| 63 | + let contactInformation = if let supportURL, email == nil { |
| 64 | + VAPID.Configuration.ContactInformation.url(supportURL) |
| 65 | + } else if supportURL == nil, let email { |
| 66 | + VAPID.Configuration.ContactInformation.email(email) |
| 67 | + } else if supportURL != nil, email != nil { |
| 68 | + throw UnknownError(reason: "Only one of an email or a support-url may be specified.") |
| 69 | + } else { |
| 70 | + throw UnknownError(reason: "A valid support-url must be specified.") |
| 71 | + } |
| 72 | + if let supportURL { |
| 73 | + guard let scheme = supportURL.scheme?.lowercased(), scheme == "http" || scheme == "https" |
| 74 | + else { throw UnknownError(reason: "support-url must be an HTTP or HTTPS.") } |
| 75 | + } |
| 76 | + |
| 77 | + let configuration = VAPID.Configuration(key: key, contactInformation: contactInformation) |
| 78 | + |
| 79 | + let json = String(decoding: try encoder.encode(configuration), as: UTF8.self) |
| 80 | + |
| 81 | + if silent { |
| 82 | + print("\(json)") |
| 83 | + } else { |
| 84 | + var exampleJSON = "" |
| 85 | + if pretty { |
| 86 | + print("VAPID.Configuration:\n\(json)\n\n") |
| 87 | + exampleJSON = json |
| 88 | + exampleJSON.replace("\n", with: "\n ") |
| 89 | + exampleJSON = "#\"\"\"\n \(exampleJSON)\n \"\"\"#" |
| 90 | + } else { |
| 91 | + print("VAPID.Configuration: \(json)\n\n") |
| 92 | + exampleJSON = "#\" \(json) \"#" |
| 93 | + } |
| 94 | + print("Example Usage:") |
| 95 | + print(" // TODO: Load this data from .env or from file system") |
| 96 | + print(" let configurationData = Data(\(exampleJSON).utf8)") |
| 97 | + print(" let vapidConfiguration = try JSONDecoder().decode(VAPID.Configuration.self, from: configurationData)") |
| 98 | + } |
| 99 | + } else { |
| 100 | + if email != nil { |
| 101 | + throw UnknownError(reason: "An email cannot be specified if only keys are being generated.") |
| 102 | + } else { |
| 103 | + throw UnknownError(reason: "A support-url cannot be specified if only keys are being generated.") |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +struct UnknownError: LocalizedError { |
| 110 | + var reason: String |
| 111 | + |
| 112 | + var errorDescription: String? { reason } |
| 113 | +} |
| 114 | + |
| 115 | +extension URL: @retroactive ExpressibleByArgument { |
| 116 | + public init?(argument: String) { |
| 117 | + self.init(string: argument) |
| 118 | + } |
| 119 | +} |
0 commit comments