Jobley / app · jobley.ai / Sources / JobleyUI / Turnstile.swift
| 1 | import SwiftUI |
| 2 | import WebKit |
| 3 | import JobleyKit |
| 4 | |
| 5 | /// The result from jobley.ai/app/turnstile/ (PRD "Turnstile from the apps"). |
| 6 | public enum TurnstileResult: Equatable, Sendable { |
| 7 | case token(String) |
| 8 | case notNeeded |
| 9 | case expired |
| 10 | case error(String) |
| 11 | } |
| 12 | |
| 13 | /// Loads the site's small Turnstile page in a web view. The page posts to the `turnstile` script |
| 14 | /// message handler; tokens are single use, so each submit gets a fresh one. |
| 15 | public struct TurnstileView: View { |
| 16 | let page: URL |
| 17 | let onResult: (TurnstileResult) -> Void |
| 18 | |
| 19 | public init(page: URL, onResult: @escaping (TurnstileResult) -> Void) { |
| 20 | self.page = page; self.onResult = onResult |
| 21 | } |
| 22 | |
| 23 | public var body: some View { |
| 24 | TurnstileWebView(page: page, onResult: onResult) |
| 25 | .frame(height: 90) |
| 26 | .accessibilityLabel("Checking you're a person") |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | final class TurnstileBridge: NSObject, WKScriptMessageHandler { |
| 31 | var onResult: (TurnstileResult) -> Void |
| 32 | init(onResult: @escaping (TurnstileResult) -> Void) { self.onResult = onResult } |
| 33 | |
| 34 | func userContentController(_ controller: WKUserContentController, didReceive message: WKScriptMessage) { |
| 35 | guard let body = message.body as? [String: Any], let type = body["type"] as? String else { return } |
| 36 | let result: TurnstileResult |
| 37 | switch type { |
| 38 | case "token": result = .token(body["token"] as? String ?? "") |
| 39 | case "not_needed": result = .notNeeded |
| 40 | case "expired": result = .expired |
| 41 | default: result = .error(body["message"] as? String ?? "The check did not load.") |
| 42 | } |
| 43 | DispatchQueue.main.async { self.onResult(result) } |
| 44 | } |
| 45 | |
| 46 | static func makeWebView(page: URL, bridge: TurnstileBridge) -> WKWebView { |
| 47 | let configuration = WKWebViewConfiguration() |
| 48 | configuration.userContentController.add(bridge, name: "turnstile") |
| 49 | configuration.websiteDataStore = .nonPersistent() |
| 50 | let view = WKWebView(frame: .zero, configuration: configuration) |
| 51 | #if os(iOS) |
| 52 | view.isOpaque = false |
| 53 | view.backgroundColor = .clear |
| 54 | view.scrollView.isScrollEnabled = false |
| 55 | #else |
| 56 | view.setValue(false, forKey: "drawsBackground") |
| 57 | #endif |
| 58 | view.load(URLRequest(url: page)) |
| 59 | return view |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | #if os(iOS) |
| 64 | struct TurnstileWebView: UIViewRepresentable { |
| 65 | let page: URL |
| 66 | let onResult: (TurnstileResult) -> Void |
| 67 | func makeCoordinator() -> TurnstileBridge { TurnstileBridge(onResult: onResult) } |
| 68 | func makeUIView(context: Context) -> WKWebView { TurnstileBridge.makeWebView(page: page, bridge: context.coordinator) } |
| 69 | func updateUIView(_ view: WKWebView, context: Context) { context.coordinator.onResult = onResult } |
| 70 | } |
| 71 | #else |
| 72 | struct TurnstileWebView: NSViewRepresentable { |
| 73 | let page: URL |
| 74 | let onResult: (TurnstileResult) -> Void |
| 75 | func makeCoordinator() -> TurnstileBridge { TurnstileBridge(onResult: onResult) } |
| 76 | func makeNSView(context: Context) -> WKWebView { TurnstileBridge.makeWebView(page: page, bridge: context.coordinator) } |
| 77 | func updateNSView(_ view: WKWebView, context: Context) { context.coordinator.onResult = onResult } |
| 78 | } |
| 79 | #endif |
| 80 | |
| 81 | /// Holds the current Turnstile state for a form and hands out the token once. |
| 82 | @MainActor |
| 83 | public final class TurnstileGate: ObservableObject { |
| 84 | @Published public private(set) var state: TurnstileResult? |
| 85 | /// Changing this reloads the web view for a fresh, single-use token. |
| 86 | @Published public private(set) var generation = 0 |
| 87 | |
| 88 | public init() {} |
| 89 | |
| 90 | /// Local development servers run without Turnstile, like the site's dev server. |
| 91 | public static func isNeeded(for base: URL) -> Bool { |
| 92 | !["localhost", "127.0.0.1"].contains(base.host() ?? "") |
| 93 | } |
| 94 | |
| 95 | public func receive(_ result: TurnstileResult) { state = result } |
| 96 | |
| 97 | public var isReady: Bool { |
| 98 | switch state { case .token, .notNeeded: true; default: false } |
| 99 | } |
| 100 | |
| 101 | public var token: String? { if case .token(let t) = state { t } else { nil } } |
| 102 | |
| 103 | public var statusText: String { |
| 104 | switch state { |
| 105 | case nil: "Checking you're a person…" |
| 106 | case .token, .notNeeded: "Check passed." |
| 107 | case .expired: "The check expired. Reloading it…" |
| 108 | case .error(let message): message |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | public func reset() { state = nil; generation += 1 } |
| 113 | } |