solid-pod-rs-idp 0.4.0-alpha.12

Solid-OIDC identity provider (authorization-code + DPoP-bound tokens, JWKS, credentials, dynamic client registration) — Rust port of JavaScriptSolidServer/src/idp
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
//! NIP-07 Schnorr SSO (row 81 — Sprint 11: full handshake).
//!
//! This module exposes:
//!
//! 1. [`SchnorrSso`] — the stable trait the IdP invokes during the
//!    login flow. Implementations issue a challenge, then verify the
//!    client's signed response.
//! 2. [`Nip07SchnorrSso`] — the production implementation. Stores
//!    per-user challenges with a TTL and verifies BIP-340 Schnorr
//!    signatures via the core crate's
//!    [`solid_pod_rs::auth::nip98::verify_schnorr_signature`]
//!    helper (feature `nip98-schnorr`).
//! 3. [`SchnorrTodo`] — a `#[doc(hidden)]` fallback whose methods
//!    always return [`SchnorrError::Unimplemented`]. Useful for
//!    integrators wiring a provider before deciding whether to
//!    enable `schnorr-sso`.
//!
//! # Handshake
//!
//! 1. Client calls `issue_challenge(user_id)` — server mints 32
//!    random bytes, hex-encodes, persists `(token, timestamp)`.
//! 2. Client signs the message `SHA-256(token ‖ user_id ‖ pubkey)`
//!    (BIP-340 Schnorr) and POSTs `(pubkey, signature)` back.
//! 3. Server calls `verify_response(user_id, pubkey, sig)` — the
//!    challenge is looked up, TTL-checked, the digest is recomputed,
//!    the signature is verified. On success the challenge is
//!    **consumed** (removed from the map) so it cannot be replayed.
//!
//! # One-shot semantics
//!
//! Challenges are single-use. A successful verification removes the
//! challenge from the store. A failed verification also removes it
//! (we do not want to leak "which part of the token was wrong" by
//! letting the client retry arbitrarily; the client can request a
//! fresh challenge if they mis-signed).
//!
//! JSS parity: `src/idp/interactions.js:handleSchnorrLogin` +
//! `handleSchnorrComplete`.

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[cfg(feature = "schnorr-sso")]
use crate::user_store::UserStore;
#[cfg(feature = "schnorr-sso")]
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

#[cfg(feature = "schnorr-sso")]
use dashmap::DashMap;

/// Errors from a Schnorr SSO backend.
#[derive(Debug, Error)]
pub enum SchnorrError {
    /// Backend not wired up (the Todo fallback).
    #[error("schnorr SSO backend not implemented")]
    Unimplemented,
    /// Signature verification failed.
    #[error("invalid signature: {0}")]
    InvalidSignature(String),
    /// Challenge not found / expired.
    #[error("challenge: {0}")]
    Challenge(String),
    /// Profile lookup (npub ↔ WebID mapping) failed.
    #[error("no account for npub: {0}")]
    UnknownNpub(String),
    /// CSPRNG failure (effectively impossible, but surfaced rather
    /// than panic).
    #[error("rng: {0}")]
    Rng(String),
    /// Input parse error (bad hex, wrong length, etc.).
    #[error("parse: {0}")]
    Parse(String),
}

/// A freshly-issued challenge the client must sign.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchnorrChallenge {
    /// The authenticating user's stable id. Binds the challenge to
    /// a specific account so a challenge issued for Alice cannot be
    /// used to log in as Bob.
    pub user_id: String,
    /// Opaque challenge token (hex-encoded 32 random bytes).
    pub token: String,
    /// Unix seconds the challenge was issued at.
    pub created_at: u64,
}

/// Backward-compatible alias — Sprint 10 called this `Challenge`.
pub type Challenge = SchnorrChallenge;

/// Result of a successful Schnorr verification.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchnorrAssertion {
    /// The account id whose challenge was verified.
    pub user_id: String,
    /// The Schnorr pubkey (hex) that produced the signature.
    pub pubkey: String,
    /// Unix seconds at which the server accepted the proof.
    pub verified_at: u64,
}

/// The client's signed response to a [`SchnorrChallenge`].
///
/// Retained as a data carrier for transports that prefer a
/// struct-shaped request body; `Nip07SchnorrSso::verify_response`
/// accepts the individual fields directly.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignedChallenge {
    /// The challenge token being answered.
    pub token: String,
    /// Schnorr public key (x-only, hex).
    pub pubkey: String,
    /// BIP-340 Schnorr signature, hex-encoded.
    pub signature: String,
}

/// NIP-07 Schnorr SSO contract.
///
/// Implementations MUST be `Send + Sync` so the provider can hold
/// them in an `Arc`.
#[async_trait]
pub trait SchnorrSso: Send + Sync + 'static {
    /// Mint a fresh challenge bound to `user_id`. The challenge is
    /// stored server-side so `verify_response` can look it up.
    async fn issue_challenge(&self, user_id: &str) -> Result<SchnorrChallenge, SchnorrError>;

    /// Verify a client-supplied Schnorr signature against the most
    /// recently issued challenge for `user_id`. Consumes the
    /// challenge on return (success or failure).
    async fn verify_response(
        &self,
        user_id: &str,
        pubkey_hex: &str,
        signature_hex: &str,
    ) -> Result<SchnorrAssertion, SchnorrError>;
}

/// Legacy alias — Sprint 10 called this `SchnorrBackend`.
pub trait SchnorrBackend: SchnorrSso {}
impl<T: SchnorrSso> SchnorrBackend for T {}

/// Test-only fallback. Every call returns
/// [`SchnorrError::Unimplemented`].
#[doc(hidden)]
pub struct SchnorrTodo;

/// Backward-compatible alias for the Sprint-10 name.
#[doc(hidden)]
pub type NullSchnorrBackend = SchnorrTodo;

#[async_trait]
impl SchnorrSso for SchnorrTodo {
    async fn issue_challenge(&self, _user_id: &str) -> Result<SchnorrChallenge, SchnorrError> {
        Err(SchnorrError::Unimplemented)
    }

    async fn verify_response(
        &self,
        _user_id: &str,
        _pubkey_hex: &str,
        _signature_hex: &str,
    ) -> Result<SchnorrAssertion, SchnorrError> {
        Err(SchnorrError::Unimplemented)
    }
}

// ---------------------------------------------------------------
// Real impl — `Nip07SchnorrSso` backed by core nip98 Schnorr.
// ---------------------------------------------------------------

/// Production implementation of [`SchnorrSso`] for NIP-07 style
/// (Solid-over-Nostr) sign-in.
///
/// Stores per-user challenges in an in-memory
/// [`dashmap::DashMap`]; the map grows on issue and shrinks on
/// verify. The TTL defaults to 5 minutes, matching the WebAuthn
/// recommendation.
#[cfg(feature = "schnorr-sso")]
pub struct Nip07SchnorrSso {
    challenges: DashMap<String, (SchnorrChallenge, Instant)>,
    ttl: Duration,
}

#[cfg(feature = "schnorr-sso")]
impl Default for Nip07SchnorrSso {
    fn default() -> Self {
        Self::new(Duration::from_secs(5 * 60))
    }
}

#[cfg(feature = "schnorr-sso")]
impl Nip07SchnorrSso {
    /// Build a new SSO backend with the given challenge TTL.
    pub fn new(ttl: Duration) -> Self {
        Self {
            challenges: DashMap::new(),
            ttl,
        }
    }

    /// Hash the canonical authentication message.
    ///
    /// Returns `SHA-256(token ‖ user_id ‖ pubkey)` — 32 bytes.
    pub fn canonical_digest(token: &str, user_id: &str, pubkey_hex: &str) -> [u8; 32] {
        use sha2::{Digest, Sha256};
        let mut h = Sha256::new();
        h.update(token.as_bytes());
        h.update(user_id.as_bytes());
        h.update(pubkey_hex.as_bytes());
        h.finalize().into()
    }

    /// Typed-username VM fallback: verify a Schnorr response with a
    /// fallback that resolves the pubkey from the user's profile when
    /// `did:nostr` resolution fails.
    ///
    /// Flow:
    /// 1. Try the normal `verify_response` path (caller-supplied pubkey).
    /// 2. If that fails and `username` is `Some`, look up the account by
    ///    username, fetch `nostr_pubkey` from the profile's
    ///    `verificationMethod`, and re-verify the signature against that
    ///    key.
    ///
    /// This prevents the typed-username fallback from being used as a
    /// pubkey oracle: the challenge is consumed on the first attempt, so
    /// the fallback operates on the same one-shot challenge.
    pub async fn verify_response_with_username_fallback(
        &self,
        user_id: &str,
        pubkey_hex: &str,
        signature_hex: &str,
        username: Option<&str>,
        user_store: &dyn UserStore,
    ) -> Result<SchnorrAssertion, SchnorrError> {
        use k256::schnorr::{signature::Verifier, Signature, VerifyingKey};

        // 1. Look up & remove challenge — one-shot semantics.
        let (_, (challenge, issued_at)) = self
            .challenges
            .remove(user_id)
            .ok_or_else(|| SchnorrError::Challenge("no active challenge for user".into()))?;

        if issued_at.elapsed() > self.ttl {
            return Err(SchnorrError::Challenge("expired".into()));
        }

        // Parse signature (shared across both paths).
        let sig_bytes = hex::decode(signature_hex)
            .map_err(|e| SchnorrError::Parse(format!("signature: {e}")))?;
        if sig_bytes.len() != 64 {
            return Err(SchnorrError::Parse(format!(
                "signature must be 64 bytes, got {}",
                sig_bytes.len()
            )));
        }
        let sig = Signature::try_from(sig_bytes.as_slice())
            .map_err(|e| SchnorrError::Parse(format!("signature parse: {e}")))?;

        // 2. Try caller-supplied pubkey first.
        let pub_bytes =
            hex::decode(pubkey_hex).map_err(|e| SchnorrError::Parse(format!("pubkey: {e}")))?;
        if pub_bytes.len() == 32 {
            if let Ok(vk) = VerifyingKey::from_bytes(&pub_bytes) {
                let digest = Self::canonical_digest(&challenge.token, user_id, pubkey_hex);
                if vk.verify(&digest, &sig).is_ok() {
                    let verified_at = SystemTime::now()
                        .duration_since(UNIX_EPOCH)
                        .map(|d| d.as_secs())
                        .unwrap_or(0);
                    return Ok(SchnorrAssertion {
                        user_id: user_id.to_string(),
                        pubkey: pubkey_hex.to_string(),
                        verified_at,
                    });
                }
            }
        }

        // 3. Fallback: look up by username, get profile verificationMethod key.
        let username = username.ok_or_else(|| {
            SchnorrError::InvalidSignature(
                "caller-supplied pubkey verification failed and no username provided for fallback"
                    .into(),
            )
        })?;

        let user = user_store
            .find_by_username(username)
            .await
            .map_err(|e| SchnorrError::UnknownNpub(format!("user store error: {e}")))?
            .ok_or_else(|| {
                SchnorrError::UnknownNpub(format!("no account for username: {username}"))
            })?;

        let profile_pubkey_hex = user.nostr_pubkey.ok_or_else(|| {
            SchnorrError::UnknownNpub(format!(
                "account '{}' has no nostr_pubkey verificationMethod",
                username
            ))
        })?;

        // 4. Verify the signature against the profile's verificationMethod key.
        let profile_pub_bytes = hex::decode(&profile_pubkey_hex)
            .map_err(|e| SchnorrError::Parse(format!("profile pubkey: {e}")))?;
        if profile_pub_bytes.len() != 32 {
            return Err(SchnorrError::Parse(format!(
                "profile pubkey must be 32 bytes, got {}",
                profile_pub_bytes.len()
            )));
        }
        let profile_vk = VerifyingKey::from_bytes(&profile_pub_bytes)
            .map_err(|e| SchnorrError::Parse(format!("profile pubkey parse: {e}")))?;

        // The digest uses the *profile* pubkey, not the caller-supplied one,
        // because the client signed against the key they hold (which should
        // match the profile's verificationMethod).
        let digest = Self::canonical_digest(&challenge.token, user_id, &profile_pubkey_hex);
        profile_vk
            .verify(&digest, &sig)
            .map_err(|e| SchnorrError::InvalidSignature(e.to_string()))?;

        let verified_at = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        Ok(SchnorrAssertion {
            user_id: user_id.to_string(),
            pubkey: profile_pubkey_hex,
            verified_at,
        })
    }
}

#[cfg(feature = "schnorr-sso")]
#[async_trait]
impl SchnorrSso for Nip07SchnorrSso {
    async fn issue_challenge(&self, user_id: &str) -> Result<SchnorrChallenge, SchnorrError> {
        use rand::RngCore;
        let mut buf = [0u8; 32];
        rand::thread_rng()
            .try_fill_bytes(&mut buf)
            .map_err(|e| SchnorrError::Rng(e.to_string()))?;
        let token = hex::encode(buf);
        let created_at = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        let challenge = SchnorrChallenge {
            user_id: user_id.to_string(),
            token,
            created_at,
        };
        self.challenges
            .insert(user_id.to_string(), (challenge.clone(), Instant::now()));
        Ok(challenge)
    }

    async fn verify_response(
        &self,
        user_id: &str,
        pubkey_hex: &str,
        signature_hex: &str,
    ) -> Result<SchnorrAssertion, SchnorrError> {
        use k256::schnorr::{signature::Verifier, Signature, VerifyingKey};

        // 1. Look up & remove — one-shot semantics. Any outcome
        //    below consumes the challenge.
        let (_, (challenge, issued_at)) = self
            .challenges
            .remove(user_id)
            .ok_or_else(|| SchnorrError::Challenge("no active challenge for user".into()))?;

        // 2. TTL check.
        if issued_at.elapsed() > self.ttl {
            return Err(SchnorrError::Challenge("expired".into()));
        }

        // 3. Parse pubkey + signature.
        let pub_bytes =
            hex::decode(pubkey_hex).map_err(|e| SchnorrError::Parse(format!("pubkey: {e}")))?;
        if pub_bytes.len() != 32 {
            return Err(SchnorrError::Parse(format!(
                "pubkey must be 32 bytes, got {}",
                pub_bytes.len()
            )));
        }
        let sig_bytes = hex::decode(signature_hex)
            .map_err(|e| SchnorrError::Parse(format!("signature: {e}")))?;
        if sig_bytes.len() != 64 {
            return Err(SchnorrError::Parse(format!(
                "signature must be 64 bytes, got {}",
                sig_bytes.len()
            )));
        }

        // 4. Verify BIP-340 Schnorr signature of the canonical
        //    digest. We use k256 directly here to keep the public
        //    API of `solid_pod_rs::auth::nip98::verify_schnorr_signature`
        //    focused on NIP-98 events (kind 27235). The underlying
        //    cryptography is identical to what that helper enforces
        //    and is exercised by the core crate's tests under the
        //    `nip98-schnorr` feature.
        let vk = VerifyingKey::from_bytes(&pub_bytes)
            .map_err(|e| SchnorrError::Parse(format!("pubkey parse: {e}")))?;
        let sig = Signature::try_from(sig_bytes.as_slice())
            .map_err(|e| SchnorrError::Parse(format!("signature parse: {e}")))?;
        let digest = Self::canonical_digest(&challenge.token, user_id, pubkey_hex);
        vk.verify(&digest, &sig)
            .map_err(|e| SchnorrError::InvalidSignature(e.to_string()))?;

        let verified_at = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        Ok(SchnorrAssertion {
            user_id: user_id.to_string(),
            pubkey: pubkey_hex.to_string(),
            verified_at,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn todo_backend_is_callable_and_returns_unimplemented() {
        let backend = SchnorrTodo;
        assert!(matches!(
            backend.issue_challenge("alice").await.unwrap_err(),
            SchnorrError::Unimplemented
        ));
        assert!(matches!(
            backend
                .verify_response("alice", "pub", "sig")
                .await
                .unwrap_err(),
            SchnorrError::Unimplemented
        ));
    }

    #[cfg(feature = "schnorr-sso")]
    mod username_fallback {
        use super::*;
        use crate::user_store::InMemoryUserStore;
        use k256::schnorr::{signature::Signer, SigningKey};
        use std::time::Duration;

        fn make_sso() -> Nip07SchnorrSso {
            Nip07SchnorrSso::new(Duration::from_secs(300))
        }

        fn make_signing_key() -> SigningKey {
            SigningKey::from_bytes(&[0x42; 32]).unwrap()
        }

        fn pubkey_hex(sk: &SigningKey) -> String {
            hex::encode(sk.verifying_key().to_bytes())
        }

        #[tokio::test]
        async fn fallback_resolves_pubkey_from_username() {
            let sso = make_sso();
            let sk = make_signing_key();
            let pk_hex = pubkey_hex(&sk);

            // Store a user with username and nostr_pubkey.
            let store = InMemoryUserStore::new();
            store
                .insert_user_with_nostr(
                    "u-1",
                    "alice@example.com",
                    "https://alice.example/profile#me",
                    Some("Alice".into()),
                    "password123",
                    Some("alice".into()),
                    Some(pk_hex.clone()),
                )
                .unwrap();

            // Issue challenge.
            let challenge = sso.issue_challenge("u-1").await.unwrap();

            // Client signs with the profile key.
            let digest = Nip07SchnorrSso::canonical_digest(&challenge.token, "u-1", &pk_hex);
            let sig: k256::schnorr::Signature = sk.sign(&digest);
            let sig_hex = hex::encode(sig.to_bytes());

            // Verify with a WRONG caller-supplied pubkey but correct username.
            let wrong_pk = "a".repeat(64);
            let result = sso
                .verify_response_with_username_fallback(
                    "u-1",
                    &wrong_pk,
                    &sig_hex,
                    Some("alice"),
                    &store,
                )
                .await
                .unwrap();

            assert_eq!(result.user_id, "u-1");
            assert_eq!(result.pubkey, pk_hex);
        }

        #[tokio::test]
        async fn fallback_succeeds_with_direct_pubkey() {
            let sso = make_sso();
            let sk = make_signing_key();
            let pk_hex = pubkey_hex(&sk);

            let store = InMemoryUserStore::new();

            let challenge = sso.issue_challenge("u-2").await.unwrap();
            let digest = Nip07SchnorrSso::canonical_digest(&challenge.token, "u-2", &pk_hex);
            let sig: k256::schnorr::Signature = sk.sign(&digest);
            let sig_hex = hex::encode(sig.to_bytes());

            // Direct pubkey verification succeeds without needing fallback.
            let result = sso
                .verify_response_with_username_fallback("u-2", &pk_hex, &sig_hex, None, &store)
                .await
                .unwrap();

            assert_eq!(result.user_id, "u-2");
            assert_eq!(result.pubkey, pk_hex);
        }

        #[tokio::test]
        async fn fallback_fails_when_no_username_and_bad_pubkey() {
            let sso = make_sso();
            let sk = make_signing_key();
            let pk_hex = pubkey_hex(&sk);

            let store = InMemoryUserStore::new();

            let challenge = sso.issue_challenge("u-3").await.unwrap();
            let digest = Nip07SchnorrSso::canonical_digest(&challenge.token, "u-3", &pk_hex);
            let sig: k256::schnorr::Signature = sk.sign(&digest);
            let sig_hex = hex::encode(sig.to_bytes());

            let wrong_pk = "b".repeat(64);
            let err = sso
                .verify_response_with_username_fallback(
                    "u-3", &wrong_pk, &sig_hex, None, // no username
                    &store,
                )
                .await
                .unwrap_err();

            assert!(matches!(err, SchnorrError::InvalidSignature(_)));
        }

        #[tokio::test]
        async fn fallback_fails_when_username_not_found() {
            let sso = make_sso();
            let sk = make_signing_key();
            let pk_hex = pubkey_hex(&sk);

            let store = InMemoryUserStore::new();

            let challenge = sso.issue_challenge("u-4").await.unwrap();
            let digest = Nip07SchnorrSso::canonical_digest(&challenge.token, "u-4", &pk_hex);
            let sig: k256::schnorr::Signature = sk.sign(&digest);
            let sig_hex = hex::encode(sig.to_bytes());

            let wrong_pk = "c".repeat(64);
            let err = sso
                .verify_response_with_username_fallback(
                    "u-4",
                    &wrong_pk,
                    &sig_hex,
                    Some("nonexistent"),
                    &store,
                )
                .await
                .unwrap_err();

            assert!(matches!(err, SchnorrError::UnknownNpub(_)));
        }

        #[tokio::test]
        async fn fallback_fails_when_user_has_no_nostr_pubkey() {
            let sso = make_sso();
            let sk = make_signing_key();
            let pk_hex = pubkey_hex(&sk);

            let store = InMemoryUserStore::new();
            // User exists but has no nostr_pubkey.
            store
                .insert_user_with_nostr(
                    "u-5",
                    "bob@example.com",
                    "https://bob.example/profile#me",
                    None,
                    "password123",
                    Some("bob".into()),
                    None, // no nostr_pubkey
                )
                .unwrap();

            let challenge = sso.issue_challenge("u-5").await.unwrap();
            let digest = Nip07SchnorrSso::canonical_digest(&challenge.token, "u-5", &pk_hex);
            let sig: k256::schnorr::Signature = sk.sign(&digest);
            let sig_hex = hex::encode(sig.to_bytes());

            let wrong_pk = "d".repeat(64);
            let err = sso
                .verify_response_with_username_fallback(
                    "u-5",
                    &wrong_pk,
                    &sig_hex,
                    Some("bob"),
                    &store,
                )
                .await
                .unwrap_err();

            assert!(matches!(err, SchnorrError::UnknownNpub(_)));
        }

        #[tokio::test]
        async fn challenge_consumed_even_on_fallback_failure() {
            let sso = make_sso();
            let store = InMemoryUserStore::new();

            let _challenge = sso.issue_challenge("u-6").await.unwrap();

            // First attempt fails (bad sig, no username).
            let _ = sso
                .verify_response_with_username_fallback(
                    "u-6",
                    &"e".repeat(64),
                    &"f".repeat(128),
                    None,
                    &store,
                )
                .await;

            // Second attempt must fail with "no active challenge" since
            // the challenge was consumed on the first attempt.
            let err = sso
                .verify_response_with_username_fallback(
                    "u-6",
                    &"e".repeat(64),
                    &"f".repeat(128),
                    None,
                    &store,
                )
                .await
                .unwrap_err();

            assert!(matches!(err, SchnorrError::Challenge(_)));
        }

        #[tokio::test]
        async fn fallback_case_insensitive_username() {
            let sso = make_sso();
            let sk = make_signing_key();
            let pk_hex = pubkey_hex(&sk);

            let store = InMemoryUserStore::new();
            store
                .insert_user_with_nostr(
                    "u-7",
                    "carol@example.com",
                    "https://carol.example/profile#me",
                    None,
                    "password123",
                    Some("Carol".into()),
                    Some(pk_hex.clone()),
                )
                .unwrap();

            let challenge = sso.issue_challenge("u-7").await.unwrap();
            let digest = Nip07SchnorrSso::canonical_digest(&challenge.token, "u-7", &pk_hex);
            let sig: k256::schnorr::Signature = sk.sign(&digest);
            let sig_hex = hex::encode(sig.to_bytes());

            // Use different casing for username.
            let wrong_pk = "a".repeat(64);
            let result = sso
                .verify_response_with_username_fallback(
                    "u-7",
                    &wrong_pk,
                    &sig_hex,
                    Some("CAROL"),
                    &store,
                )
                .await
                .unwrap();

            assert_eq!(result.user_id, "u-7");
            assert_eq!(result.pubkey, pk_hex);
        }
    }
}