day-part-auth 0.1.2

Sign-in for Day apps: OAuth 2.0 and OpenID Connect through the system's auth browser, Sign in with Apple, Google, and passkeys
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
// Copyright © The Daybrite Project
// SPDX-License-Identifier: MPL-2.0

//! The one daybridge declaration every foreign arm of this crate implements, and the arms
//! themselves (<https://daybrite.dev/contributing/backend-contracts/bridge>). day-build generates
//! one module per crate from the `bridge!` block, so every platform entry point this crate asks
//! a foreign language for is declared here once: Swift for AuthenticationServices on iOS and
//! macOS, Java for Custom Tabs and Credential Manager on Android, JavaScript for the popup and
//! `navigator.credentials` on the web. The Rust files around this one read the answers.
//!
//! # The wire
//!
//! An enum cannot cross the bridge, so answers are strings, bytes and small integers:
//!
//! - A web auth session completes with the callback URL, or fails with `cancelled` (the person
//!   dismissed the browser), `unsupported`, or the platform's message.
//! - Sign in with Apple completes with a JSON object (`user`, `identityToken`,
//!   `authorizationCode`, `email`, `fullName`, `realUserStatus`), or fails with `cancelled` or
//!   the platform's message.
//! - Google completes with a JSON object (`idToken`, `id`, `displayName`, `givenName`,
//!   `familyName`, `profilePictureUri`), or fails with `cancelled`, `no_credential` or the
//!   platform's message.
//! - Passkeys take WebAuthn JSON (a `PublicKeyCredentialCreationOptionsJSON` or
//!   `PublicKeyCredentialRequestOptionsJSON`) and complete with the credential's JSON
//!   (`PublicKeyCredential.toJSON()`), or fail with `cancelled`, `unsupported`,
//!   `invalid_state` (a credential already exists), `not_allowed`, or the platform's message.
//!
//! Every arm completes on its platform's own thread; the Rust side's futures bring the answer
//! back to the UI thread.

day_bridge::bridge! {
    #[day_bridge::declare]
    extern "day" {
        /// `n` random bytes from the browser (`crypto.getRandomValues`), as lowercase hex,
        /// completed before the call returns (a string crosses the JavaScript bridge through a
        /// completion only).
        fn random_hex_native(n: i32, done: day_bridge::Done<String>) -> Result<(), day_bridge::Error>;
        /// The page's own URL without its fragment, on the web: the redirect a popup flow returns
        /// to. Unsupported elsewhere.
        fn page_url_native(done: day_bridge::Done<String>) -> Result<(), day_bridge::Error>;
        /// Open the system's sign-in browser on `url` and complete with the URL the provider
        /// redirected to (one starting with `callback_prefix`, whose scheme is
        /// `callback_scheme`), with `cancelled`, or with `error:<message>`. Android completes
        /// with an empty string when the tab closes: the callback itself arrives as a deep link.
        fn open_session_native(url: &str, callback_scheme: &str, callback_prefix: &str, ephemeral: bool, done: day_bridge::Done<String>) -> Result<(), day_bridge::Error>;
        /// Dismiss the session on screen, if any.
        fn close_session_native() -> Result<(), day_bridge::Error>;
        /// Whether a platform authenticator can make and use passkeys now: 1 or 0.
        fn passkeys_available_native(done: day_bridge::Done<i32>) -> Result<(), day_bridge::Error>;
        /// Register a passkey from `PublicKeyCredentialCreationOptionsJSON`; completes with the
        /// credential JSON, or `cancelled`, `unsupported`, `invalid_state`, `not_allowed`,
        /// `error:<message>`.
        fn create_passkey_native(options_json: &str, done: day_bridge::Done<String>) -> Result<(), day_bridge::Error>;
        /// Sign in with a passkey from `PublicKeyCredentialRequestOptionsJSON`; completes as
        /// `create_passkey_native` does.
        fn get_passkey_native(options_json: &str, done: day_bridge::Done<String>) -> Result<(), day_bridge::Error>;
        /// Sign in with Apple: `scopes` is `fullName`/`email` joined by commas, `nonce` the hex
        /// SHA-256 Apple expects. Completes with the credential JSON (`user`, `identityToken`,
        /// `authorizationCode`, `email`, `givenName`, `familyName`, `realUserStatus`, `state`),
        /// `cancelled`, or `error:<message>`.
        fn apple_sign_in_native(scopes: &str, nonce: &str, state: &str, done: day_bridge::Done<String>) -> Result<(), day_bridge::Error>;
        /// Apple's credential state for a user identifier: 1 authorized, 2 revoked, 3 not found.
        fn apple_credential_state_native(user: &str, done: day_bridge::Done<i32>) -> Result<(), day_bridge::Error>;
        /// Google through Credential Manager. Completes with the credential JSON (`idToken`,
        /// `id`, `displayName`, `givenName`, `familyName`, `profilePictureUri`), `cancelled`,
        /// `no_credential`, or `error:<message>`.
        fn google_sign_in_native(server_client_id: &str, nonce: &str, filter_by_authorized: bool, auto_select: bool, done: day_bridge::Done<String>) -> Result<(), day_bridge::Error>;
    }

    // Apple: ASWebAuthenticationSession. A strong reference to the session and to its
    // presentation-context provider is kept for the session's life (the session holds the
    // provider weakly), anchored to the key window. An https callback uses the iOS 17.4 /
    // macOS 14.4 form, which needs an associated domain; a custom scheme works everywhere.
    #[day_bridge::impl(swift, platforms = [ios, macos])]
    swift!(
        prelude = r#"
            import AuthenticationServices
            #if os(macOS)
            import AppKit
            #else
            import UIKit
            #endif
        "#,
        body = r#"
            private final class DayAuthAnchor: NSObject, ASWebAuthenticationPresentationContextProviding {
                func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
                    #if os(macOS)
                    return NSApplication.shared.keyWindow ?? NSApplication.shared.mainWindow ?? ASPresentationAnchor()
                    #else
                    let scenes = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }
                    let windows = scenes.flatMap { $0.windows }
                    return windows.first { $0.isKeyWindow } ?? windows.first ?? ASPresentationAnchor()
                    #endif
                }
            }

            private let dayAuthAnchor = DayAuthAnchor()
            private var dayAuthSession: ASWebAuthenticationSession? = nil

            func open_session_native(url: String, callback_scheme callbackScheme: String, callback_prefix callbackPrefix: String,
                                     ephemeral: Bool, done: UInt64) throws {
                DispatchQueue.main.async {
                    guard let target = URL(string: url) else {
                        open_session_native_complete(done, "error:the sign-in URL is not a URL")
                        return
                    }
                    dayAuthSession?.cancel()
                    let handler: ASWebAuthenticationSession.CompletionHandler = { callback, error in
                        dayAuthSession = nil
                        if let callback = callback {
                            open_session_native_complete(done, callback.absoluteString)
                        } else if let error = error as? ASWebAuthenticationSessionError,
                                  error.code == .canceledLogin {
                            open_session_native_complete(done, "cancelled")
                        } else {
                            open_session_native_complete(done, "error:" + (error?.localizedDescription ?? "the session failed"))
                        }
                    }
                    let session: ASWebAuthenticationSession
                    if callbackPrefix.hasPrefix("https://"),
                       let callback = URL(string: callbackPrefix), let host = callback.host,
                       #available(iOS 17.4, macOS 14.4, *) {
                        session = ASWebAuthenticationSession(url: target,
                                                             callback: .https(host: host, path: callback.path),
                                                             completionHandler: handler)
                    } else {
                        session = ASWebAuthenticationSession(url: target, callbackURLScheme: callbackScheme,
                                                             completionHandler: handler)
                    }
                    session.presentationContextProvider = dayAuthAnchor
                    session.prefersEphemeralWebBrowserSession = ephemeral
                    dayAuthSession = session
                    if !session.start() {
                        dayAuthSession = nil
                        open_session_native_complete(done, "error:the session could not start")
                    }
                }
            }

            func close_session_native() throws {
                DispatchQueue.main.async {
                    dayAuthSession?.cancel()
                    dayAuthSession = nil
                }
            }

            // Passkeys: ASAuthorizationController over the platform credential provider (iOS 16,
            // macOS 13). The controller and its delegate are kept alive until it answers.

            private func dayB64u(_ data: Data) -> String {
                return data.base64EncodedString()
                    .replacingOccurrences(of: "+", with: "-")
                    .replacingOccurrences(of: "/", with: "_")
                    .replacingOccurrences(of: "=", with: "")
            }

            private func dayB64uDecode(_ text: String) -> Data? {
                var s = text.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/")
                while s.count % 4 != 0 { s += "=" }
                return Data(base64Encoded: s)
            }

            private func dayJson(_ object: Any) -> String {
                if let data = try? JSONSerialization.data(withJSONObject: object),
                   let text = String(data: data, encoding: .utf8) {
                    return text
                }
                return "error:could not serialize the credential"
            }

            @available(iOS 16.0, macOS 13.0, *)
            private final class DayPasskeyDelegate: NSObject, ASAuthorizationControllerDelegate,
                                                     ASAuthorizationControllerPresentationContextProviding {
                let done: UInt64
                let complete: (UInt64, String) -> Void
                var controller: ASAuthorizationController? = nil

                init(done: UInt64, complete: @escaping (UInt64, String) -> Void) {
                    self.done = done
                    self.complete = complete
                }

                func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
                    return dayAuthAnchor.presentationAnchor(for: ASWebAuthenticationSession(url: URL(string: "https://daybrite.dev")!, callbackURLScheme: nil) { _, _ in })
                }

                func authorizationController(controller: ASAuthorizationController,
                                             didCompleteWithAuthorization authorization: ASAuthorization) {
                    dayPasskeyDelegates.removeValue(forKey: done)
                    if let reg = authorization.credential as? ASAuthorizationPlatformPublicKeyCredentialRegistration {
                        let id = dayB64u(reg.credentialID)
                        complete(done, dayJson([
                            "id": id, "rawId": id, "type": "public-key",
                            "authenticatorAttachment": "platform", "clientExtensionResults": [:],
                            "response": [
                                "clientDataJSON": dayB64u(reg.rawClientDataJSON),
                                "attestationObject": dayB64u(reg.rawAttestationObject ?? Data()),
                                "transports": ["internal"],
                            ],
                        ]))
                    } else if let assertion = authorization.credential as? ASAuthorizationPlatformPublicKeyCredentialAssertion {
                        let id = dayB64u(assertion.credentialID)
                        complete(done, dayJson([
                            "id": id, "rawId": id, "type": "public-key",
                            "authenticatorAttachment": "platform", "clientExtensionResults": [:],
                            "response": [
                                "clientDataJSON": dayB64u(assertion.rawClientDataJSON),
                                "authenticatorData": dayB64u(assertion.rawAuthenticatorData),
                                "signature": dayB64u(assertion.signature),
                                "userHandle": dayB64u(assertion.userID),
                            ],
                        ]))
                    } else {
                        complete(done, "error:unexpected credential type")
                    }
                }

                func authorizationController(controller: ASAuthorizationController,
                                             didCompleteWithError error: Error) {
                    dayPasskeyDelegates.removeValue(forKey: done)
                    if let e = error as? ASAuthorizationError {
                        switch e.code {
                        case .canceled: complete(done, "cancelled")
                        case .notInteractive: complete(done, "not_allowed")
                        default: complete(done, "error:" + e.localizedDescription)
                        }
                    } else {
                        complete(done, "error:" + error.localizedDescription)
                    }
                }
            }

            private var dayPasskeyDelegates: [UInt64: AnyObject] = [:]

            private func dayUserVerification(_ text: String?) -> ASAuthorizationPublicKeyCredentialUserVerificationPreference {
                switch text {
                case "required": return .required
                case "discouraged": return .discouraged
                default: return .preferred
                }
            }

            func passkeys_available_native(done: UInt64) throws {
                if #available(iOS 16.0, macOS 13.0, *) {
                    passkeys_available_native_complete(done, 1)
                } else {
                    passkeys_available_native_complete(done, 0)
                }
            }

            func create_passkey_native(options_json optionsJson: String, done: UInt64) throws {
                DispatchQueue.main.async {
                    guard #available(iOS 16.0, macOS 13.0, *) else {
                        create_passkey_native_complete(done, "unsupported")
                        return
                    }
                    guard let data = optionsJson.data(using: .utf8),
                          let options = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any],
                          let rp = options["rp"] as? [String: Any], let rpId = rp["id"] as? String,
                          let user = options["user"] as? [String: Any], let userName = user["name"] as? String,
                          let userIdText = user["id"] as? String, let userId = dayB64uDecode(userIdText),
                          let challengeText = options["challenge"] as? String, let challenge = dayB64uDecode(challengeText) else {
                        create_passkey_native_complete(done, "error:the creation options are incomplete")
                        return
                    }
                    let provider = ASAuthorizationPlatformPublicKeyCredentialProvider(relyingPartyIdentifier: rpId)
                    let request = provider.createCredentialRegistrationRequest(challenge: challenge, name: userName, userID: userId)
                    if let selection = options["authenticatorSelection"] as? [String: Any] {
                        request.userVerificationPreference = dayUserVerification(selection["userVerification"] as? String)
                    }
                    if let exclude = options["excludeCredentials"] as? [[String: Any]], #available(iOS 17.4, macOS 14.4, *) {
                        request.excludedCredentials = exclude.compactMap { entry in
                            (entry["id"] as? String).flatMap(dayB64uDecode).map {
                                ASAuthorizationPlatformPublicKeyCredentialDescriptor(credentialID: $0)
                            }
                        }
                    }
                    let delegate = DayPasskeyDelegate(done: done, complete: create_passkey_native_complete)
                    let controller = ASAuthorizationController(authorizationRequests: [request])
                    controller.delegate = delegate
                    controller.presentationContextProvider = delegate
                    delegate.controller = controller
                    dayPasskeyDelegates[done] = delegate
                    controller.performRequests()
                }
            }

            func get_passkey_native(options_json optionsJson: String, done: UInt64) throws {
                DispatchQueue.main.async {
                    guard #available(iOS 16.0, macOS 13.0, *) else {
                        get_passkey_native_complete(done, "unsupported")
                        return
                    }
                    guard let data = optionsJson.data(using: .utf8),
                          let options = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any],
                          let rpId = options["rpId"] as? String,
                          let challengeText = options["challenge"] as? String, let challenge = dayB64uDecode(challengeText) else {
                        get_passkey_native_complete(done, "error:the request options are incomplete")
                        return
                    }
                    let provider = ASAuthorizationPlatformPublicKeyCredentialProvider(relyingPartyIdentifier: rpId)
                    let request = provider.createCredentialAssertionRequest(challenge: challenge)
                    request.userVerificationPreference = dayUserVerification(options["userVerification"] as? String)
                    if let allow = options["allowCredentials"] as? [[String: Any]] {
                        request.allowedCredentials = allow.compactMap { entry in
                            (entry["id"] as? String).flatMap(dayB64uDecode).map {
                                ASAuthorizationPlatformPublicKeyCredentialDescriptor(credentialID: $0)
                            }
                        }
                    }
                    let delegate = DayPasskeyDelegate(done: done, complete: get_passkey_native_complete)
                    let controller = ASAuthorizationController(authorizationRequests: [request])
                    controller.delegate = delegate
                    controller.presentationContextProvider = delegate
                    delegate.controller = controller
                    dayPasskeyDelegates[done] = delegate
                    controller.performRequests()
                }
            }

            // Sign in with Apple: the Apple ID provider through the same controller.

            private final class DayAppleDelegate: NSObject, ASAuthorizationControllerDelegate,
                                                   ASAuthorizationControllerPresentationContextProviding {
                let done: UInt64
                var controller: ASAuthorizationController? = nil

                init(done: UInt64) {
                    self.done = done
                }

                func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
                    return dayAuthAnchor.presentationAnchor(for: ASWebAuthenticationSession(url: URL(string: "https://daybrite.dev")!, callbackURLScheme: nil) { _, _ in })
                }

                func authorizationController(controller: ASAuthorizationController,
                                             didCompleteWithAuthorization authorization: ASAuthorization) {
                    dayPasskeyDelegates.removeValue(forKey: done)
                    guard let credential = authorization.credential as? ASAuthorizationAppleIDCredential else {
                        apple_sign_in_native_complete(done, "error:unexpected credential type")
                        return
                    }
                    var status = 0
                    if #available(iOS 14.0, macOS 11.0, *) {
                        switch credential.realUserStatus {
                        case .unknown: status = 1
                        case .likelyReal: status = 2
                        default: status = 0
                        }
                    }
                    apple_sign_in_native_complete(done, dayJson([
                        "user": credential.user,
                        "identityToken": credential.identityToken.flatMap { String(data: $0, encoding: .utf8) } ?? "",
                        "authorizationCode": credential.authorizationCode.flatMap { String(data: $0, encoding: .utf8) } ?? "",
                        "email": credential.email ?? "",
                        "givenName": credential.fullName?.givenName ?? "",
                        "familyName": credential.fullName?.familyName ?? "",
                        "realUserStatus": status,
                        "state": credential.state ?? "",
                    ]))
                }

                func authorizationController(controller: ASAuthorizationController,
                                             didCompleteWithError error: Error) {
                    dayPasskeyDelegates.removeValue(forKey: done)
                    if let e = error as? ASAuthorizationError, e.code == .canceled {
                        apple_sign_in_native_complete(done, "cancelled")
                    } else {
                        apple_sign_in_native_complete(done, "error:" + error.localizedDescription)
                    }
                }
            }

            func apple_sign_in_native(scopes: String, nonce: String, state: String, done: UInt64) throws {
                DispatchQueue.main.async {
                    let request = ASAuthorizationAppleIDProvider().createRequest()
                    var requested: [ASAuthorization.Scope] = []
                    for scope in scopes.split(separator: ",") {
                        if scope == "fullName" { requested.append(.fullName) }
                        if scope == "email" { requested.append(.email) }
                    }
                    request.requestedScopes = requested
                    request.nonce = nonce
                    if !state.isEmpty { request.state = state }
                    let delegate = DayAppleDelegate(done: done)
                    let controller = ASAuthorizationController(authorizationRequests: [request])
                    controller.delegate = delegate
                    controller.presentationContextProvider = delegate
                    delegate.controller = controller
                    dayPasskeyDelegates[done] = delegate
                    controller.performRequests()
                }
            }

            func apple_credential_state_native(user: String, done: UInt64) throws {
                ASAuthorizationAppleIDProvider().getCredentialState(forUserID: user) { state, _ in
                    switch state {
                    case .authorized: apple_credential_state_native_complete(done, 1)
                    case .revoked: apple_credential_state_native_complete(done, 2)
                    default: apple_credential_state_native_complete(done, 3)
                    }
                }
            }

            // The declarations the other arms own. Randomness comes from the Security framework
            // (Rust reads /dev/urandom here, so this is reached only by a caller that asks for it);
            // the page URL and Google's sheet do not exist on Apple platforms.

            func random_hex_native(n: Int32, done: UInt64) throws {
                var bytes = [UInt8](repeating: 0, count: Int(max(n, 0)))
                let status = SecRandomCopyBytes(kSecRandomDefault, bytes.count, &bytes)
                if status != errSecSuccess {
                    random_hex_native_complete(done, "error:SecRandomCopyBytes \(status)")
                    return
                }
                random_hex_native_complete(done, bytes.map { String(format: "%02x", $0) }.joined())
            }

            func page_url_native(done: UInt64) throws {
                page_url_native_complete(done, "unsupported")
            }

            func google_sign_in_native(server_client_id: String, nonce: String, filter_by_authorized: Bool,
                                       auto_select: Bool, done: UInt64) throws {
                google_sign_in_native_complete(done, "unsupported")
            }
        "#,
    );

    // Android: a Custom Tab from the activity. The provider's redirect to the app's scheme
    // relaunches the activity (singleTask) and reaches the deep-link rail; this arm reports the
    // activity resuming, which is the tab closing, so the Rust side can tell a dismissal from a
    // callback still on its way. Plain Java so it compiles in any Android project.
    #[day_bridge::impl(java, platforms = [android])]
    java!(
        prelude = r#"
            import android.app.Activity;
            import android.app.Application;
            import android.content.Context;
            import android.net.Uri;
            import android.os.Build;
            import android.os.Bundle;
            import android.os.CancellationSignal;
            import androidx.browser.customtabs.CustomTabsIntent;
            import androidx.credentials.CreateCredentialResponse;
            import androidx.credentials.CreatePublicKeyCredentialRequest;
            import androidx.credentials.CreatePublicKeyCredentialResponse;
            import androidx.credentials.Credential;
            import androidx.credentials.CredentialManager;
            import androidx.credentials.CredentialManagerCallback;
            import androidx.credentials.GetCredentialRequest;
            import androidx.credentials.GetCredentialResponse;
            import androidx.credentials.GetPublicKeyCredentialOption;
            import androidx.credentials.PublicKeyCredential;
            import androidx.credentials.exceptions.CreateCredentialCancellationException;
            import androidx.credentials.exceptions.CreateCredentialException;
            import androidx.credentials.exceptions.CreateCredentialProviderConfigurationException;
            import androidx.credentials.exceptions.GetCredentialCancellationException;
            import androidx.credentials.exceptions.GetCredentialException;
            import androidx.credentials.exceptions.GetCredentialProviderConfigurationException;
            import androidx.credentials.exceptions.NoCredentialException;
            import androidx.credentials.exceptions.publickeycredential.CreatePublicKeyCredentialDomException;
            import androidx.credentials.CustomCredential;
            import com.google.android.libraries.identity.googleid.GetGoogleIdOption;
            import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential;
            import dev.daybrite.day.bridge.DayRuntime;
            import org.json.JSONObject;
        "#,
        body = r#"
            private static long daySessionDone = 0;
            private static Application.ActivityLifecycleCallbacks dayResumeWatch = null;

            private static void daySessionSettle(String answer) {
                long done = daySessionDone;
                daySessionDone = 0;
                if (dayResumeWatch != null) {
                    Context ctx = DayRuntime.ctx;
                    if (ctx instanceof Activity) {
                        ((Activity) ctx).getApplication().unregisterActivityLifecycleCallbacks(dayResumeWatch);
                    }
                    dayResumeWatch = null;
                }
                if (done != 0) {
                    open_session_native_complete(done, answer);
                }
            }

            public static void open_session_native(final String url, String callbackScheme, String callbackPrefix,
                                                   boolean ephemeral, final long done) {
                final Context ctx = DayRuntime.ctx;
                if (!(ctx instanceof Activity)) {
                    open_session_native_complete(done, "error:no activity to open the browser from");
                    return;
                }
                final Activity activity = (Activity) ctx;
                DayRuntime.main.post(new Runnable() {
                    @Override
                    public void run() {
                        daySessionSettle("cancelled");
                        daySessionDone = done;
                        dayResumeWatch = new Application.ActivityLifecycleCallbacks() {
                            @Override public void onActivityCreated(Activity a, Bundle b) {}
                            @Override public void onActivityStarted(Activity a) {}
                            @Override public void onActivityResumed(Activity a) {
                                if (a == activity) {
                                    daySessionSettle("");
                                }
                            }
                            @Override public void onActivityPaused(Activity a) {}
                            @Override public void onActivityStopped(Activity a) {}
                            @Override public void onActivitySaveInstanceState(Activity a, Bundle b) {}
                            @Override public void onActivityDestroyed(Activity a) {}
                        };
                        activity.getApplication().registerActivityLifecycleCallbacks(dayResumeWatch);
                        try {
                            CustomTabsIntent tab = new CustomTabsIntent.Builder().build();
                            tab.launchUrl(activity, Uri.parse(url));
                        } catch (Throwable t) {
                            daySessionSettle("error:" + t);
                        }
                    }
                });
            }

            public static void close_session_native() {
                DayRuntime.main.post(new Runnable() {
                    @Override
                    public void run() {
                        daySessionSettle("cancelled");
                    }
                });
            }

            // Passkeys: Credential Manager, which takes and returns the WebAuthn JSON as is.

            private static String dayCredentialFailure(Throwable t) {
                String name = t.getClass().getSimpleName();
                if (t instanceof CreateCredentialCancellationException || t instanceof GetCredentialCancellationException) {
                    return "cancelled";
                }
                if (t instanceof NoCredentialException) {
                    return "not_allowed";
                }
                if (t instanceof CreateCredentialProviderConfigurationException
                    || t instanceof GetCredentialProviderConfigurationException) {
                    return "unsupported";
                }
                if (t instanceof CreatePublicKeyCredentialDomException) {
                    String type = ((CreatePublicKeyCredentialDomException) t).getDomError().getType();
                    if ("androidx.credentials.TYPE_INVALID_STATE_ERROR".equals(type)) {
                        return "invalid_state";
                    }
                    if ("androidx.credentials.TYPE_NOT_ALLOWED_ERROR".equals(type)) {
                        return "not_allowed";
                    }
                }
                return "error:" + name + ": " + t.getMessage();
            }

            public static void passkeys_available_native(long done) {
                passkeys_available_native_complete(done, Build.VERSION.SDK_INT >= 28 ? 1 : 0);
            }

            public static void create_passkey_native(final String optionsJson, final long done) {
                final Context ctx = DayRuntime.ctx;
                if (!(ctx instanceof Activity) || Build.VERSION.SDK_INT < 28) {
                    create_passkey_native_complete(done, "unsupported");
                    return;
                }
                final Activity activity = (Activity) ctx;
                DayRuntime.main.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            CredentialManager manager = CredentialManager.create(ctx);
                            CreatePublicKeyCredentialRequest request = new CreatePublicKeyCredentialRequest(optionsJson);
                            manager.createCredentialAsync(activity, request, new CancellationSignal(),
                                ctx.getMainExecutor(),
                                new CredentialManagerCallback<CreateCredentialResponse, CreateCredentialException>() {
                                    @Override
                                    public void onResult(CreateCredentialResponse response) {
                                        if (response instanceof CreatePublicKeyCredentialResponse) {
                                            create_passkey_native_complete(done,
                                                ((CreatePublicKeyCredentialResponse) response).getRegistrationResponseJson());
                                        } else {
                                            create_passkey_native_complete(done, "error:unexpected credential type");
                                        }
                                    }

                                    @Override
                                    public void onError(CreateCredentialException e) {
                                        create_passkey_native_complete(done, dayCredentialFailure(e));
                                    }
                                });
                        } catch (Throwable t) {
                            create_passkey_native_complete(done, dayCredentialFailure(t));
                        }
                    }
                });
            }

            public static void google_sign_in_native(final String serverClientId, final String nonce,
                                                     final boolean filterByAuthorized, final boolean autoSelect,
                                                     final long done) {
                final Context ctx = DayRuntime.ctx;
                if (!(ctx instanceof Activity) || Build.VERSION.SDK_INT < 28) {
                    google_sign_in_native_complete(done, "unsupported");
                    return;
                }
                final Activity activity = (Activity) ctx;
                DayRuntime.main.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            GetGoogleIdOption option = new GetGoogleIdOption.Builder()
                                .setServerClientId(serverClientId)
                                .setFilterByAuthorizedAccounts(filterByAuthorized)
                                .setAutoSelectEnabled(autoSelect)
                                .setNonce(nonce)
                                .build();
                            GetCredentialRequest request = new GetCredentialRequest.Builder()
                                .addCredentialOption(option)
                                .build();
                            CredentialManager.create(ctx).getCredentialAsync(activity, request, new CancellationSignal(),
                                ctx.getMainExecutor(),
                                new CredentialManagerCallback<GetCredentialResponse, GetCredentialException>() {
                                    @Override
                                    public void onResult(GetCredentialResponse response) {
                                        try {
                                            Credential credential = response.getCredential();
                                            if (!(credential instanceof CustomCredential)
                                                || !GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL.equals(credential.getType())) {
                                                google_sign_in_native_complete(done, "error:unexpected credential type");
                                                return;
                                            }
                                            GoogleIdTokenCredential google = GoogleIdTokenCredential.createFrom(credential.getData());
                                            JSONObject out = new JSONObject();
                                            out.put("idToken", google.getIdToken());
                                            out.put("id", google.getId());
                                            out.put("displayName", google.getDisplayName() == null ? "" : google.getDisplayName());
                                            out.put("givenName", google.getGivenName() == null ? "" : google.getGivenName());
                                            out.put("familyName", google.getFamilyName() == null ? "" : google.getFamilyName());
                                            out.put("profilePictureUri", google.getProfilePictureUri() == null ? "" : google.getProfilePictureUri().toString());
                                            google_sign_in_native_complete(done, out.toString());
                                        } catch (Throwable t) {
                                            google_sign_in_native_complete(done, "error:" + t);
                                        }
                                    }

                                    @Override
                                    public void onError(GetCredentialException e) {
                                        if (e instanceof NoCredentialException) {
                                            google_sign_in_native_complete(done, "no_credential");
                                        } else {
                                            google_sign_in_native_complete(done, dayCredentialFailure(e));
                                        }
                                    }
                                });
                        } catch (Throwable t) {
                            google_sign_in_native_complete(done, "error:" + t);
                        }
                    }
                });
            }

            // The declarations the other arms own: Apple's sheet and the page URL do not exist on
            // Android. Randomness comes from SecureRandom (Rust reads /dev/urandom here).

            public static void random_hex_native(int n, long done) {
                byte[] bytes = new byte[Math.max(n, 0)];
                new java.security.SecureRandom().nextBytes(bytes);
                StringBuilder hex = new StringBuilder(bytes.length * 2);
                for (byte b : bytes) {
                    hex.append(String.format("%02x", b));
                }
                random_hex_native_complete(done, hex.toString());
            }

            public static void page_url_native(long done) {
                page_url_native_complete(done, "unsupported");
            }

            public static void apple_sign_in_native(String scopes, String nonce, String state, long done) {
                apple_sign_in_native_complete(done, "unsupported");
            }

            public static void apple_credential_state_native(String user, long done) {
                apple_credential_state_native_complete(done, 3);
            }

            public static void get_passkey_native(final String optionsJson, final long done) {
                final Context ctx = DayRuntime.ctx;
                if (!(ctx instanceof Activity) || Build.VERSION.SDK_INT < 28) {
                    get_passkey_native_complete(done, "unsupported");
                    return;
                }
                final Activity activity = (Activity) ctx;
                DayRuntime.main.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            CredentialManager manager = CredentialManager.create(ctx);
                            GetCredentialRequest request = new GetCredentialRequest.Builder()
                                .addCredentialOption(new GetPublicKeyCredentialOption(optionsJson))
                                .build();
                            manager.getCredentialAsync(activity, request, new CancellationSignal(),
                                ctx.getMainExecutor(),
                                new CredentialManagerCallback<GetCredentialResponse, GetCredentialException>() {
                                    @Override
                                    public void onResult(GetCredentialResponse response) {
                                        Credential credential = response.getCredential();
                                        if (credential instanceof PublicKeyCredential) {
                                            get_passkey_native_complete(done,
                                                ((PublicKeyCredential) credential).getAuthenticationResponseJson());
                                        } else {
                                            get_passkey_native_complete(done, "error:unexpected credential type");
                                        }
                                    }

                                    @Override
                                    public void onError(GetCredentialException e) {
                                        get_passkey_native_complete(done, dayCredentialFailure(e));
                                    }
                                });
                        } catch (Throwable t) {
                            get_passkey_native_complete(done, dayCredentialFailure(t));
                        }
                    }
                });
            }
        "#,
    );

    // The web: a popup of the same origin. The redirect lands the app's own page in the popup;
    // this module, loaded there too, sees it is the popup and posts its URL to the opener, which
    // completes the session and closes it. A popup closed any other way is a dismissal.
    #[day_bridge::impl(js, platforms = [web])]
    js!(r#"
        const DAY_AUTH_POPUP = 'day-part-auth-callback';
        let dayAuthPopup = null;
        let dayAuthDone = 0;
        let dayAuthPrefix = '';
        let dayAuthPoll = 0;

        if (window.opener && window.name === DAY_AUTH_POPUP) {
            try {
                window.opener.postMessage({ dayPartAuth: window.location.href }, window.location.origin);
            } catch (e) {}
            window.close();
        }

        function dayAuthSettle(answer) {
            const done = dayAuthDone;
            dayAuthDone = 0;
            if (dayAuthPoll) {
                clearInterval(dayAuthPoll);
                dayAuthPoll = 0;
            }
            dayAuthPopup = null;
            if (done) {
                open_session_native_complete(done, answer);
            }
        }

        window.addEventListener('message', (event) => {
            if (event.origin !== window.location.origin || !event.data || typeof event.data.dayPartAuth !== 'string') {
                return;
            }
            const url = event.data.dayPartAuth;
            if (dayAuthDone && url.startsWith(dayAuthPrefix)) {
                dayAuthSettle(url);
            }
        });

        export function open_session_native(url, callbackScheme, callbackPrefix, ephemeral, done) {
            dayAuthSettle('cancelled');
            dayAuthDone = done;
            dayAuthPrefix = callbackPrefix;
            const popup = window.open(url, DAY_AUTH_POPUP, 'popup=yes,width=520,height=680');
            if (!popup) {
                dayAuthSettle('error:the browser blocked the sign-in window');
                return;
            }
            dayAuthPopup = popup;
            dayAuthPoll = setInterval(() => {
                if (dayAuthPopup && dayAuthPopup.closed) {
                    dayAuthSettle('cancelled');
                }
            }, 400);
        }

        export function close_session_native() {
            if (dayAuthPopup) {
                try { dayAuthPopup.close(); } catch (e) {}
            }
            dayAuthSettle('cancelled');
        }

        export function page_url_native(done) {
            page_url_native_complete(done, String(window.location.href).split('#')[0]);
        }

        export function random_hex_native(n, done) {
            const bytes = new Uint8Array(n);
            crypto.getRandomValues(bytes);
            random_hex_native_complete(done, Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join(''));
        }

        // Passkeys: navigator.credentials with the WebAuthn JSON forms. Browsers from 2023 on
        // parse and serialize the JSON themselves; the two helpers below are the fallback for
        // the ones that take and give ArrayBuffers.
        function dayB64uToBytes(text) {
            const b64 = text.replace(/-/g, '+').replace(/_/g, '/') + '==='.slice((text.length + 3) % 4);
            const bin = atob(b64);
            const out = new Uint8Array(bin.length);
            for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
            return out;
        }

        function dayBytesToB64u(buffer) {
            const bytes = new Uint8Array(buffer);
            let bin = '';
            for (const b of bytes) bin += String.fromCharCode(b);
            return btoa(bin).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
        }

        function dayCreationOptions(json) {
            if (typeof PublicKeyCredential.parseCreationOptionsFromJSON === 'function') {
                return PublicKeyCredential.parseCreationOptionsFromJSON(json);
            }
            const out = Object.assign({}, json);
            out.challenge = dayB64uToBytes(json.challenge);
            out.user = Object.assign({}, json.user, { id: dayB64uToBytes(json.user.id) });
            if (Array.isArray(json.excludeCredentials)) {
                out.excludeCredentials = json.excludeCredentials.map((c) => Object.assign({}, c, { id: dayB64uToBytes(c.id) }));
            }
            return out;
        }

        function dayRequestOptions(json) {
            if (typeof PublicKeyCredential.parseRequestOptionsFromJSON === 'function') {
                return PublicKeyCredential.parseRequestOptionsFromJSON(json);
            }
            const out = Object.assign({}, json);
            out.challenge = dayB64uToBytes(json.challenge);
            if (Array.isArray(json.allowCredentials)) {
                out.allowCredentials = json.allowCredentials.map((c) => Object.assign({}, c, { id: dayB64uToBytes(c.id) }));
            }
            return out;
        }

        function dayCredentialJson(cred) {
            if (typeof cred.toJSON === 'function') {
                return JSON.stringify(cred.toJSON());
            }
            const r = cred.response;
            const response = { clientDataJSON: dayBytesToB64u(r.clientDataJSON) };
            if (r.attestationObject) {
                response.attestationObject = dayBytesToB64u(r.attestationObject);
                if (typeof r.getTransports === 'function') response.transports = r.getTransports();
            } else {
                response.authenticatorData = dayBytesToB64u(r.authenticatorData);
                response.signature = dayBytesToB64u(r.signature);
                response.userHandle = r.userHandle ? dayBytesToB64u(r.userHandle) : null;
            }
            return JSON.stringify({
                id: cred.id,
                rawId: dayBytesToB64u(cred.rawId),
                type: cred.type,
                authenticatorAttachment: cred.authenticatorAttachment || null,
                clientExtensionResults: typeof cred.getClientExtensionResults === 'function' ? cred.getClientExtensionResults() : {},
                response,
            });
        }

        function dayPasskeyFailure(e) {
            const name = e && e.name ? e.name : '';
            if (name === 'NotAllowedError' || name === 'AbortError') return 'cancelled';
            if (name === 'InvalidStateError') return 'invalid_state';
            if (name === 'NotSupportedError') return 'unsupported';
            if (name === 'SecurityError') return 'not_allowed';
            return 'error:' + (e && e.message ? e.message : String(e));
        }

        // The declarations the other arms own: the system sheets do not exist in a browser.
        export function apple_sign_in_native(scopes, nonce, state, done) {
            apple_sign_in_native_complete(done, 'unsupported');
        }

        export function apple_credential_state_native(user, done) {
            apple_credential_state_native_complete(done, 3);
        }

        export function google_sign_in_native(serverClientId, nonce, filterByAuthorized, autoSelect, done) {
            google_sign_in_native_complete(done, 'unsupported');
        }

        export function passkeys_available_native(done) {
            if (typeof PublicKeyCredential === 'undefined') {
                passkeys_available_native_complete(done, 0);
                return;
            }
            PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable()
                .then((ok) => passkeys_available_native_complete(done, ok ? 1 : 0),
                      () => passkeys_available_native_complete(done, 0));
        }

        export function create_passkey_native(optionsJson, done) {
            if (typeof PublicKeyCredential === 'undefined' || !navigator.credentials) {
                create_passkey_native_complete(done, 'unsupported');
                return;
            }
            let options;
            try {
                options = dayCreationOptions(JSON.parse(optionsJson));
            } catch (e) {
                create_passkey_native_complete(done, 'error:' + e);
                return;
            }
            navigator.credentials.create({ publicKey: options })
                .then((cred) => create_passkey_native_complete(done, dayCredentialJson(cred)),
                      (e) => create_passkey_native_complete(done, dayPasskeyFailure(e)));
        }

        export function get_passkey_native(optionsJson, done) {
            if (typeof PublicKeyCredential === 'undefined' || !navigator.credentials) {
                get_passkey_native_complete(done, 'unsupported');
                return;
            }
            let options;
            try {
                options = dayRequestOptions(JSON.parse(optionsJson));
            } catch (e) {
                get_passkey_native_complete(done, 'error:' + e);
                return;
            }
            navigator.credentials.get({ publicKey: options })
                .then((cred) => get_passkey_native_complete(done, dayCredentialJson(cred)),
                      (e) => get_passkey_native_complete(done, dayPasskeyFailure(e)));
        }
    "#);

    #[day_bridge::impl(rust, platforms = [other])]
    fn random_hex_native(_n: i32, _done: day_bridge::Done<String>) -> Result<(), day_bridge::Error> {
        Err(day_bridge::Error::Unsupported)
    }

    #[day_bridge::impl(rust, platforms = [other])]
    fn page_url_native(_done: day_bridge::Done<String>) -> Result<(), day_bridge::Error> {
        Err(day_bridge::Error::Unsupported)
    }

    #[day_bridge::impl(rust, platforms = [other])]
    fn open_session_native(_url: &str, _callback_scheme: &str, _callback_prefix: &str, _ephemeral: bool, _done: day_bridge::Done<String>) -> Result<(), day_bridge::Error> {
        Err(day_bridge::Error::Unsupported)
    }

    #[day_bridge::impl(rust, platforms = [other])]
    fn close_session_native() -> Result<(), day_bridge::Error> {
        Err(day_bridge::Error::Unsupported)
    }

    #[day_bridge::impl(rust, platforms = [other])]
    fn passkeys_available_native(_done: day_bridge::Done<i32>) -> Result<(), day_bridge::Error> {
        Err(day_bridge::Error::Unsupported)
    }

    #[day_bridge::impl(rust, platforms = [other])]
    fn create_passkey_native(_options_json: &str, _done: day_bridge::Done<String>) -> Result<(), day_bridge::Error> {
        Err(day_bridge::Error::Unsupported)
    }

    #[day_bridge::impl(rust, platforms = [other])]
    fn get_passkey_native(_options_json: &str, _done: day_bridge::Done<String>) -> Result<(), day_bridge::Error> {
        Err(day_bridge::Error::Unsupported)
    }

    #[day_bridge::impl(rust, platforms = [other])]
    fn apple_sign_in_native(_scopes: &str, _nonce: &str, _state: &str, _done: day_bridge::Done<String>) -> Result<(), day_bridge::Error> {
        Err(day_bridge::Error::Unsupported)
    }

    #[day_bridge::impl(rust, platforms = [other])]
    fn apple_credential_state_native(_user: &str, _done: day_bridge::Done<i32>) -> Result<(), day_bridge::Error> {
        Err(day_bridge::Error::Unsupported)
    }

    #[day_bridge::impl(rust, platforms = [other])]
    fn google_sign_in_native(_server_client_id: &str, _nonce: &str, _filter_by_authorized: bool, _auto_select: bool, _done: day_bridge::Done<String>) -> Result<(), day_bridge::Error> {
        Err(day_bridge::Error::Unsupported)
    }
}

/// What a synchronous arm completes with before it returns.
fn settled<T: Send + 'static>(
    call: impl FnOnce(
        Box<dyn FnOnce(Result<T, day_bridge::Error>) + Send>,
    ) -> Result<(), day_bridge::Error>,
) -> Result<T, day_bridge::Error> {
    use std::sync::{Arc, Mutex};
    let slot: Arc<Mutex<Option<Result<T, day_bridge::Error>>>> = Arc::new(Mutex::new(None));
    let sink = Arc::clone(&slot);
    call(Box::new(move |answer| {
        if let Ok(mut s) = sink.lock() {
            *s = Some(answer);
        }
    }))?;
    let taken = slot.lock().ok().and_then(|mut s| s.take());
    taken.unwrap_or(Err(day_bridge::Error::Runtime))
}

/// `n` random bytes as hex, from the browser. Used by `crypto::random_bytes` on the web.
#[allow(dead_code)] // reached on wasm32 only
pub(crate) fn random_hex(n: i32) -> Result<String, day_bridge::Error> {
    settled(|cb| random_hex_native_async(n, cb).map(|_| ()))
}

/// The page's own URL without its fragment, on the web. Used by `session::page_url`.
#[allow(dead_code)] // reached on wasm32 only
pub(crate) fn page_url() -> Result<String, day_bridge::Error> {
    settled(|cb| page_url_native_async(cb).map(|_| ()))
}

/// Dismiss the session on screen (`close_session_native`), for `session.rs`.
#[allow(dead_code)] // reached on the platforms with a session arm
pub(crate) fn close_session() -> Result<(), day_bridge::Error> {
    close_session_native()
}