Sources/OrgApp/LicenseKey.swift
79 lines · 3277 bytes
1import CryptoKit
2import Foundation
3
4/// A Mac license key: `ORGSTAR1-<payload>.<signature>`, both base64url. The payload is JSON the
5/// license issuer signs with Ed25519; the app checks the signature offline against `publicKey`.
6public struct LicenseKey: Codable, Sendable, Equatable {
7 /// The email the license was sold to.
8 public let email: String
9 /// The payment provider's transaction ID.
10 public let transaction: String
11 /// When the key was issued, as Unix seconds.
12 public let issued: Int
13
14 enum CodingKeys: String, CodingKey {
15 case email = "e", transaction = "t", issued = "i"
16 }
17
18 public enum Failure: Error, Equatable {
19 case malformed, badSignature
20 }
21
22 static let prefix = "ORGSTAR1-"
23
24 /// The issuer's Ed25519 public key (raw, 32 bytes), base64. `packaging/license-issuer/keygen.sh`
25 /// writes it here; until then it is a placeholder whose private half was discarded, and
26 /// `scripts/release-mac.sh` refuses to build.
27 public static let publicKey = "bsxWsmNYsFJiN6e/UaBgRzAmHBry9+sjI8BFFbf21aM="
28
29 public init(email: String, transaction: String, issued: Int) {
30 self.email = email
31 self.transaction = transaction
32 self.issued = issued
33 }
34
35 /// Checks `text` (whitespace and line breaks from pasting are ignored) and returns the license.
36 public static func verify(_ text: String, publicKey: String = publicKey) throws(Failure) -> LicenseKey {
37 let key = text.filter { !$0.isWhitespace }
38 guard key.hasPrefix(prefix) else { throw .malformed }
39 let parts = key.dropFirst(prefix.count).split(separator: ".", omittingEmptySubsequences: false)
40 guard parts.count == 2,
41 let payload = Data(base64URL: String(parts[0])),
42 let signature = Data(base64URL: String(parts[1])),
43 let raw = Data(base64Encoded: publicKey),
44 let signer = try? Curve25519.Signing.PublicKey(rawRepresentation: raw)
45 else { throw .malformed }
46 guard signer.isValidSignature(signature, for: payload) else { throw .badSignature }
47 guard let license = try? JSONDecoder().decode(LicenseKey.self, from: payload) else { throw .malformed }
48 return license
49 }
50}
51
52/// The Mac app's stored license.
53public enum LicenseStore {
54 static let key = "pro.licenseKey"
55
56 /// The stored key's license, if it still verifies.
57 public static func current(defaults: UserDefaults = .standard) -> LicenseKey? {
58 defaults.string(forKey: key).flatMap { try? LicenseKey.verify($0) }
59 }
60
61 /// Verifies and stores `text`.
62 public static func activate(_ text: String, defaults: UserDefaults = .standard) throws(LicenseKey.Failure) -> LicenseKey {
63 let license = try LicenseKey.verify(text)
64 defaults.set(text.filter { !$0.isWhitespace }, forKey: key)
65 return license
66 }
67
68 public static func remove(defaults: UserDefaults = .standard) {
69 defaults.removeObject(forKey: key)
70 }
71}
72
73extension Data {
74 init?(base64URL: String) {
75 var text = base64URL.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/")
76 text += String(repeating: "=", count: (4 - text.count % 4) % 4)
77 self.init(base64Encoded: text)
78 }
79}