anytype 0.5.0

An ergonomic Anytype API client in rust
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
//! Anytype client authentication
//!
//! Performs interactive authentication, and transfers keys to and from the key store.
//!
//! # Authentication Flow methods
//!
//! - [`authenticate_interactive`](AnytypeClient::authenticate_interactive) - all-in-one authenticate with desktop app (combines `create_auth_challenge` and `create_api_key`)
//! - [`create_auth_challenge`](AnytypeClient::create_auth_challenge) - auth flow part 1
//! - [`create_api_key`](AnytypeClient::create_api_key) - auth flow part 2
//! - [`auth_status`](AnytypeClient::auth_status) - check current HTTP/gRPC auth state
//! - [`logout`](AnytypeClient::logout) - discard api key
//!
//! # `KeyStore` methods
//!
//! - [`clear_api_key`](AnytypeClient::clear_api_key)
//! - [`set_api_key`](AnytypeClient::set_api_key)
//! - [`get_key_store`](AnytypeClient::get_key_store)
//!

use std::path::PathBuf;

use serde::{Deserialize, Serialize};
use tracing::debug;

use crate::{Result, prelude::*};

/// Request to create an authentication challenge
#[derive(Debug, Serialize)]
struct CreateChallengeRequest {
    /// The name of the application requesting the challenge
    pub app_name: String,
}

/// Response containing challenge information
#[derive(Debug, Deserialize)]
struct CreateChallengeResponse {
    /// The unique identifier for the challenge
    pub challenge_id: String,
}

/// Request to create an API key using challenge response
#[derive(Debug, Serialize)]
struct CreateApiKeyRequest {
    /// The unique identifier for the challenge, returned from the challenge creation
    pub challenge_id: String,
    /// The 4-digit code provided by the user from the Anytype application in response to the challenge
    pub code: String,
}

/// Response from `create_api_key`
/// Example: `zhSG/zQRmgADyilWPtgdnfo1qD60oK02/SVgi1GaFt6=`
#[derive(Debug, Deserialize)]
struct CreateApiKeyResponse {
    /// API key that can be used in the Authorization header for subsequent requests
    pub api_key: String,
}

/// Status response from auth_status()
/// Contents subject to change
#[doc(hidden)]
#[derive(Clone, Debug, Serialize)]
pub struct AuthStatus {
    pub keystore: KeyStoreStatus,
    pub http: HttpStatus,
    pub grpc: GrpcStatus,
}

/// Http auth status
/// Contents subject to change
#[doc(hidden)]
#[derive(Clone, Debug, Serialize)]
pub struct HttpStatus {
    pub url: String,
    pub has_token: bool,
}

impl HttpStatus {
    /// Returns true if the http client has an auth token
    /// To check whether the credentials are valid, use `client.ping_http()`
    #[must_use]
    pub fn is_authenticated(&self) -> bool {
        self.has_token
    }
}

/// gRPC auth status
/// Contents subject to change
#[doc(hidden)]
#[derive(Clone, Debug, Serialize)]
pub struct GrpcStatus {
    pub endpoint: Option<String>,
    pub has_account_key: bool,
    pub has_session_token: bool,
}

impl GrpcStatus {
    /// Returns true if the grpc client has either an account key or session token
    /// To check whether the credentials are valid, use `client.ping_grpc()`
    #[must_use]
    pub fn is_authenticated(&self) -> bool {
        self.has_account_key || self.has_session_token
    }
}

#[derive(Clone, Debug, Serialize)]
pub struct KeyStoreStatus {
    pub id: String,
    pub service: String,
    /// path to file, if db-keystore (sqlite backend) is used
    pub path: Option<std::path::PathBuf>,
}

impl AnytypeClient {
    /// Generates a one-time authentication challenge for granting API
    /// access to the user's vault.
    ///
    /// Uses `ClientConfig.app_name` to identify the app, and causes the
    /// Anytype Desktop app to display a 4-digit code.
    /// After you receive the `challenge_id` from this method, and the code,
    /// call `create_api_key`
    ///
    /// Note: this is a low-level method: use `authenticate_interactive` for
    /// an all-in-one authentication.
    ///
    /// # Errors
    ///
    /// `AnytypeError::Http` for communication error
    /// `AnytypeError::ApiError` for malformed api request
    ///
    pub async fn create_auth_challenge(&self) -> Result<String> {
        let request = CreateChallengeRequest {
            app_name: self.config.app_name.clone(),
        };
        debug!("creating auth challenge ...");
        let response: CreateChallengeResponse = self
            .client
            .post_unauthenticated("/v1/auth/challenges", &request)
            .await?;
        debug!("challenge received: {}", &response.challenge_id);
        Ok(response.challenge_id)
    }

    /// Exchanges the challenge response for an API key.
    ///
    /// Invoke with the `challenge_id` returned by `create_auth_challenge`,
    /// and the 4-digit code from the user
    /// (displayed by the desktop app). If the challenge solution is correct,
    /// this method generates the api key.
    ///
    /// Your app should set this as the client api key with
    /// `set_api_key` and save it to the keystore with
    /// `get_key_store().update_http_credentials(key)`
    ///
    /// Note: this is a low-level method: use `authenticate_interactive` for
    /// an all-in-one authentication.
    ///
    /// # Parameters:
    ///   `challenge_id`: challenge id, example "67647f5ecda913e9a2e11b26"
    ///   `code`: 4-digit code from the desktop app, example `1234`
    ///
    /// # Returns:
    ///   `HttpCredentials`
    ///
    /// # Errors
    ///  `AnytypeError::Http` for communication error
    ///  `AnytypeError::ApiError` for malformed api request
    ///
    pub async fn create_api_key(
        &self,
        challenge_id: &str,
        code: impl Into<String>,
    ) -> Result<HttpCredentials> {
        let request = CreateApiKeyRequest {
            challenge_id: challenge_id.to_string(),
            code: code.into(),
        };
        let response: CreateApiKeyResponse = self
            .client
            .post_unauthenticated("/v1/auth/api_keys", &request)
            .await?;
        Ok(HttpCredentials::new(response.api_key))
    }

    /// Performs interactive authentication with Anytype app.
    ///
    /// This is a convenience method that:
    /// 1. Creates a challenge
    /// 2. Calls the provided closure to prompt the user for a code
    /// 3. Exchanges the code for an API key
    /// 4. Saves the `api_key` for this client
    /// 5. If `KeyStore` is configured, saves the key in the keystore
    ///
    /// # Arguments
    /// * `get_code` - Callback to obtain the 4-digit code from the user
    /// * `force_reauth` - ignore any existing keys, in client or keystore, and execute the interactive flow
    ///   to generate a new key.
    ///
    /// # Example
    /// ```no_run
    ///
    /// # use anytype::prelude::*;
    /// # async fn example() -> anytype::Result<()> {
    /// let mut config = ClientConfig::default().app_name("my-app");
    /// config.keystore = Some("file".to_string());
    /// let client = AnytypeClient::with_config(config)?;
    ///
    /// client
    ///     .authenticate_interactive(
    ///         |challenge_id| {
    ///             use std::io::{self, Write};
    ///             println!("Challenge ID: {}", challenge_id);
    ///             print!("Enter 4-digit code displayed by app: ");
    ///             io::stdout().flush().map_err(|e| AnytypeError::Auth {
    ///                 message: e.to_string(),
    ///             })?;
    ///             let mut code = String::new();
    ///             io::stdin().read_line(&mut code).map_err(|e| AnytypeError::Auth {
    ///                 message: e.to_string(),
    ///             })?;
    ///             Ok(code.trim().to_string())
    ///         },
    ///         false,
    ///     )
    ///     .await?;
    ///
    /// // Client is now authenticated
    /// # Ok(())
    /// # }
    /// ```
    pub async fn authenticate_interactive<F>(&self, get_code: F, force_reauth: bool) -> Result<()>
    where
        F: FnOnce(&str) -> Result<String>,
    {
        // the common code path is force_reauth==false: use key if we have one
        if !force_reauth {
            // if client has key already, no need to re-authenticate
            if self.client.has_key() {
                debug!("client already has key - no need to re-authenticate");
                return Ok(());
            }
            let creds = self.keystore.get_http_credentials()?;
            if creds.has_creds() {
                self.client.set_api_key(creds);
                return Ok(());
            }
        }
        debug!("beginning interactive authentication");

        // Create challenge
        // App displays 4-digit code
        let challenge_id: String = self.create_auth_challenge().await?;

        // Prompt user for code
        let code = get_code(&challenge_id)?;

        // Create API key
        let api_key = self.create_api_key(&challenge_id, code).await?;

        // save to keystore
        self.keystore.update_http_credentials(&api_key)?;

        // save to client
        self.set_api_key(api_key);

        Ok(())
    }

    /// Returns the configured keystore.
    ///
    /// # Example
    /// ```rust,no_run
    /// use anytype::prelude::*;
    /// # fn example() -> Result<(), AnytypeError> {
    /// let mut config = ClientConfig::default().app_name("my-app");
    /// config.keystore = Some("file".to_string());
    /// let client = AnytypeClient::with_config(config)?;
    /// let keystore = client.get_key_store();
    /// println!("keystore id: {}", keystore.id());
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn get_key_store(&self) -> &KeyStore {
        &self.keystore
    }

    /// Clears the client's API key.
    /// If the current key has become invalid and you need to re-authenticate,
    /// use `authenticate_interactive`, setting force=true
    /// To clear the client's key and remove key from keystore, use `logout`.
    ///
    /// # Example
    /// ```rust,no_run
    /// use anytype::prelude::*;
    /// # fn example() -> Result<(), AnytypeError> {
    /// let client = AnytypeClient::new("my-app")?;
    /// client.clear_api_key();
    /// # Ok(())
    /// # }
    /// ```
    pub fn clear_api_key(&self) {
        self.client.clear_api_key();
    }

    /// Sets the client's API key in memory for authenticated requests.
    ///
    /// # Example
    /// ```rust,no_run
    /// use anytype::prelude::*;
    /// # fn example() -> Result<(), AnytypeError> {
    /// let client = AnytypeClient::new("my-app")?;
    /// let api_key = HttpCredentials::new("api_key_value");
    /// client.set_api_key(api_key);
    /// # Ok(())
    /// # }
    /// ```
    pub fn set_api_key(&self, key: HttpCredentials) {
        self.client.set_api_key(key);
    }

    /// Clears client api key and removes key from configured key storage.
    /// Equivalent to calling `clear_api_key()` followed by `get_key_store().clear_http_credentials()`
    ///
    /// # Example
    /// ```rust,no_run
    /// use anytype::prelude::*;
    /// # fn example() -> Result<(), AnytypeError> {
    /// let mut config = ClientConfig::default().app_name("my-app");
    /// config.keystore = Some("file".to_string());
    /// let client = AnytypeClient::with_config(config)?;
    /// client.logout()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn logout(&self) -> Result<()> {
        self.clear_api_key();
        self.keystore.clear_all_credentials()?;
        Ok(())
    }

    /// Returns information about connection configuration and keystore status
    pub fn auth_status(&self) -> Result<AuthStatus, AnytypeError> {
        let keystore = self.get_key_store();
        let http_creds = keystore.get_http_credentials()?;
        let grpc_creds = keystore.get_grpc_credentials()?;
        let path = keystore
            .store()
            .as_any()
            .downcast_ref::<db_keystore::DbKeyStore>()
            .map(|store| PathBuf::from(&store.path()));

        Ok(AuthStatus {
            keystore: KeyStoreStatus {
                id: keystore.id(),
                service: keystore.service().to_string(),
                path,
            },
            http: HttpStatus {
                url: self.get_http_endpoint().to_string(),
                has_token: http_creds.has_creds(),
            },
            grpc: GrpcStatus {
                endpoint: self.get_grpc_endpoint(),
                has_account_key: grpc_creds.has_account_key(),
                has_session_token: grpc_creds.has_session_token(),
            },
        })
    }
}

#[cfg(test)]
mod tests {
    use std::{
        io::ErrorKind,
        ops::Deref,
        path::{Path, PathBuf},
        sync::atomic::{AtomicU64, Ordering},
    };

    use reqwest::StatusCode;

    use super::*;
    use crate::{
        client::ClientConfig,
        error::AnytypeError,
        keystore::{GrpcCredentials, HttpCredentials},
        test_util::scripted_http::{
            ScriptedHttpContentType, ScriptedHttpFixture, ScriptedHttpRequest, ScriptedHttpResponse,
        },
    };

    static NEXT_SCRIPT_ID: AtomicU64 = AtomicU64::new(1);

    struct ScriptedClient {
        client: Option<AnytypeClient>,
        key_path: PathBuf,
    }

    impl Deref for ScriptedClient {
        type Target = AnytypeClient;

        fn deref(&self) -> &Self::Target {
            self.client
                .as_ref()
                .expect("scripted auth client remains available until cleanup")
        }
    }

    impl ScriptedClient {
        fn cleanup(mut self) -> Result<(), String> {
            let clear_result = self
                .client
                .as_ref()
                .ok_or_else(|| "scripted auth client was already cleaned".to_owned())?
                .get_key_store()
                .clear_all_credentials()
                .map_err(|error| format!("clear scripted auth credentials: {error}"));
            drop(self.client.take());
            let file_result = remove_scripted_keystore(&self.key_path);
            clear_result.and(file_result)
        }

        fn cleanup_after_keystore_failure(mut self) -> Result<(), String> {
            drop(self.client.take());
            remove_scripted_keystore(&self.key_path)
        }
    }

    impl Drop for ScriptedClient {
        fn drop(&mut self) {
            if let Some(client) = self.client.as_ref() {
                let _ = client.get_key_store().clear_all_credentials();
            }
            drop(self.client.take());
            let _ = remove_scripted_keystore(&self.key_path);
        }
    }

    fn remove_scripted_keystore(path: &Path) -> Result<(), String> {
        [
            path.to_path_buf(),
            PathBuf::from(format!("{}-shm", path.display())),
            PathBuf::from(format!("{}-wal", path.display())),
        ]
        .into_iter()
        .try_for_each(|path| {
            // The directory case is detected from metadata rather than the
            // unlink errno: macOS reports EPERM (PermissionDenied), not
            // EISDIR, when unlinking a directory.
            if std::fs::symlink_metadata(&path).is_ok_and(|metadata| metadata.is_dir()) {
                return std::fs::remove_dir(&path).map_err(|error| {
                    format!(
                        "remove scripted auth keystore directory {}: {error}",
                        path.display()
                    )
                });
            }
            match std::fs::remove_file(&path) {
                Ok(()) => Ok(()),
                Err(error) if error.kind() == ErrorKind::NotFound => Ok(()),
                Err(error) => Err(format!(
                    "remove scripted auth keystore {}: {error}",
                    path.display()
                )),
            }
        })
    }

    fn scripted_client(fixture: &ScriptedHttpFixture) -> ScriptedClient {
        let id = NEXT_SCRIPT_ID.fetch_add(1, Ordering::Relaxed);
        let key_path = std::env::temp_dir().join(format!(
            "anytype-auth-scripted-{}-{id}.db",
            std::process::id()
        ));
        let mut config = ClientConfig::default().app_name("auth-scripted");
        config.base_url = Some(format!("http://{}", fixture.address()));
        config.keystore = Some(format!("file:path={}", key_path.display()));
        config.keystore_service = Some(format!("auth-scripted-{id}"));
        ScriptedClient {
            client: Some(AnytypeClient::with_config(config).expect("create scripted auth client")),
            key_path,
        }
    }

    fn response(status: StatusCode, body: &str) -> ScriptedHttpResponse {
        ScriptedHttpResponse::new(
            status,
            ScriptedHttpContentType::Json,
            body.as_bytes().to_vec(),
        )
    }

    fn request_json(request: &ScriptedHttpRequest) -> serde_json::Value {
        serde_json::from_slice(request.body()).expect("scripted auth request body is JSON")
    }

    fn assert_challenge_request(request: &ScriptedHttpRequest) {
        let body = request_json(request);
        assert_eq!(request.method(), "POST", "auth challenge uses POST");
        assert_eq!(
            request.path(),
            "/v1/auth/challenges",
            "auth challenge uses its documented path"
        );
        assert_eq!(
            body.as_object().map(serde_json::Map::len),
            Some(1),
            "auth challenge body has one field"
        );
        assert!(
            body.get("app_name").and_then(serde_json::Value::as_str) == Some("auth-scripted"),
            "auth challenge body identifies the configured application"
        );
    }

    fn assert_api_key_request(request: &ScriptedHttpRequest, challenge_id: &str, code: &str) {
        let body = request_json(request);
        assert_eq!(request.method(), "POST", "API key exchange uses POST");
        assert_eq!(
            request.path(),
            "/v1/auth/api_keys",
            "API key exchange uses its documented path"
        );
        assert_eq!(
            body.as_object().map(serde_json::Map::len),
            Some(2),
            "API key exchange body has two fields"
        );
        assert!(
            body.get("challenge_id").and_then(serde_json::Value::as_str) == Some(challenge_id),
            "API key exchange preserves the generated challenge ID"
        );
        assert!(
            body.get("code").and_then(serde_json::Value::as_str) == Some(code),
            "API key exchange forwards the callback code"
        );
    }

    fn has_http_token(client: &AnytypeClient, expected: &str) -> bool {
        client
            .get_key_store()
            .get_http_credentials()
            .map(|credentials| credentials.token().is_some_and(|token| token == expected))
            .unwrap_or(false)
    }

    fn has_in_memory_http_token(client: &AnytypeClient, expected: &str) -> bool {
        client
            .client
            .get_api_key()
            .token()
            .is_some_and(|token| token == expected)
    }

    #[tokio::test]
    async fn auth_endpoints_send_exact_unauthed_wire_requests() {
        let fixture = ScriptedHttpFixture::start(vec![
            response(StatusCode::OK, r#"{"challenge_id":"challenge-1"}"#),
            response(StatusCode::OK, r#"{"api_key":"scripted-api-key"}"#),
        ])
        .await
        .expect("start auth script");
        let client = scripted_client(&fixture);

        let challenge = client
            .create_auth_challenge()
            .await
            .expect("create scripted challenge");
        let api_key = client
            .create_api_key(&challenge, "4938")
            .await
            .expect("create scripted API key");

        assert!(api_key.has_creds());
        let requests = fixture.finish().await.expect("finish auth script");
        assert_eq!(requests.len(), 2);
        assert_challenge_request(&requests[0]);
        assert_api_key_request(&requests[1], "challenge-1", "4938");
        client.cleanup().expect("remove scripted auth keystore");
    }

    #[tokio::test]
    async fn auth_endpoint_failures_preserve_api_error_mapping() {
        let fixture =
            ScriptedHttpFixture::start(vec![response(StatusCode::BAD_REQUEST, "invalid")])
                .await
                .expect("start auth failure script");
        let client = scripted_client(&fixture);

        let error = client
            .create_auth_challenge()
            .await
            .expect_err("bad challenge response fails");

        assert!(matches!(error, AnytypeError::ApiError { code: 400, .. }));
        let requests = fixture.finish().await.expect("finish auth failure script");
        assert_eq!(requests.len(), 1);
        assert!(requests[0].path() == "/v1/auth/challenges");
        client.cleanup().expect("remove scripted auth keystore");
    }

    #[tokio::test]
    async fn interactive_auth_uses_existing_client_or_keystore_credentials_without_callback() {
        let fixture = ScriptedHttpFixture::start(vec![response(StatusCode::OK, "{}")])
            .await
            .expect("start unused auth script");
        let client = scripted_client(&fixture);
        client.set_api_key(HttpCredentials::new("existing-client-key"));

        client
            .authenticate_interactive(
                |_| {
                    Err(AnytypeError::Auth {
                        message: "callback must not run".to_owned(),
                    })
                },
                false,
            )
            .await
            .expect("existing client credentials bypass authentication");
        assert!(has_in_memory_http_token(&client, "existing-client-key"));

        client.clear_api_key();
        client
            .get_key_store()
            .update_http_credentials(&HttpCredentials::new("stored-key"))
            .expect("store fixture credentials");
        client
            .authenticate_interactive(
                |_| {
                    Err(AnytypeError::Auth {
                        message: "callback must not run".to_owned(),
                    })
                },
                false,
            )
            .await
            .expect("stored credentials bypass authentication");
        assert!(has_in_memory_http_token(&client, "stored-key"));
        client.cleanup().expect("remove scripted auth keystore");
        drop(fixture);
    }

    #[tokio::test]
    async fn forced_interactive_auth_replaces_and_persists_credentials() {
        let fixture = ScriptedHttpFixture::start(vec![
            response(StatusCode::OK, r#"{"challenge_id":"challenge-2"}"#),
            response(StatusCode::OK, r#"{"api_key":"fresh-key"}"#),
        ])
        .await
        .expect("start forced auth script");
        let client = scripted_client(&fixture);
        client.set_api_key(HttpCredentials::new("old-key"));
        client
            .get_key_store()
            .update_http_credentials(&HttpCredentials::new("old-key"))
            .expect("store old credentials");

        client
            .authenticate_interactive(|_| Ok("8402".to_owned()), true)
            .await
            .expect("forced authentication succeeds");

        assert!(has_in_memory_http_token(&client, "fresh-key"));
        assert!(has_http_token(&client, "fresh-key"));
        let requests = fixture.finish().await.expect("finish forced auth script");
        assert_eq!(requests.len(), 2);
        client.cleanup().expect("remove scripted auth keystore");
    }

    #[tokio::test]
    async fn interactive_auth_callback_failure_keeps_existing_credentials() {
        let fixture = ScriptedHttpFixture::start(vec![response(
            StatusCode::OK,
            r#"{"challenge_id":"challenge-3"}"#,
        )])
        .await
        .expect("start callback failure script");
        let client = scripted_client(&fixture);
        client.set_api_key(HttpCredentials::new("old-key"));
        client
            .get_key_store()
            .update_http_credentials(&HttpCredentials::new("old-key"))
            .expect("store old credentials");

        let error = client
            .authenticate_interactive(
                |_| {
                    Err(AnytypeError::Auth {
                        message: "callback failed".to_owned(),
                    })
                },
                true,
            )
            .await
            .expect_err("callback failure stops authentication");

        assert!(matches!(error, AnytypeError::Auth { .. }));
        assert!(has_in_memory_http_token(&client, "old-key"));
        assert!(has_http_token(&client, "old-key"));
        let requests = fixture
            .finish()
            .await
            .expect("finish callback failure script");
        assert_eq!(requests.len(), 1);
        client.cleanup().expect("remove scripted auth keystore");
    }

    #[tokio::test]
    async fn interactive_auth_api_key_failure_keeps_existing_credentials() {
        let fixture = ScriptedHttpFixture::start(vec![
            response(StatusCode::OK, r#"{"challenge_id":"challenge-4"}"#),
            response(StatusCode::BAD_REQUEST, "invalid code"),
        ])
        .await
        .expect("start API key failure script");
        let client = scripted_client(&fixture);
        client.set_api_key(HttpCredentials::new("old-key"));
        client
            .get_key_store()
            .update_http_credentials(&HttpCredentials::new("old-key"))
            .expect("store old credentials");

        let error = client
            .authenticate_interactive(|_| Ok("0000".to_owned()), true)
            .await
            .expect_err("API key failure stops authentication");

        assert!(matches!(error, AnytypeError::ApiError { code: 400, .. }));
        assert!(has_in_memory_http_token(&client, "old-key"));
        assert!(has_http_token(&client, "old-key"));
        let requests = fixture
            .finish()
            .await
            .expect("finish API key failure script");
        assert_eq!(requests.len(), 2);
        client.cleanup().expect("remove scripted auth keystore");
    }

    #[tokio::test]
    async fn interactive_auth_persistence_failure_keeps_existing_memory_credentials() {
        let fixture = ScriptedHttpFixture::start(vec![
            response(StatusCode::OK, r#"{"challenge_id":"challenge-5"}"#),
            response(StatusCode::OK, r#"{"api_key":"unpersisted-key"}"#),
        ])
        .await
        .expect("start persistence failure script");
        let client = scripted_client(&fixture);
        client.set_api_key(HttpCredentials::new("old-key"));
        std::fs::remove_file(&client.key_path).expect("replace scripted keystore with a directory");
        std::fs::create_dir(&client.key_path).expect("block scripted keystore writes");

        let error = client
            .authenticate_interactive(|_| Ok("1138".to_owned()), true)
            .await
            .expect_err("keystore write failure stops authentication");

        assert!(matches!(error, AnytypeError::KeyStore { .. }));
        assert!(has_in_memory_http_token(&client, "old-key"));
        let requests = fixture
            .finish()
            .await
            .expect("finish persistence failure script");
        assert_eq!(requests.len(), 2);
        client
            .cleanup_after_keystore_failure()
            .expect("remove failed scripted auth keystore");
    }

    #[tokio::test]
    async fn auth_status_and_logout_report_keystore_transitions() {
        let fixture = ScriptedHttpFixture::start(vec![response(StatusCode::OK, "{}")])
            .await
            .expect("start unused status script");
        let client = scripted_client(&fixture);

        let initial = client.auth_status().expect("read initial auth status");
        assert!(!initial.http.is_authenticated());
        assert!(!initial.grpc.is_authenticated());
        assert!(initial.keystore.path.is_some());

        client.set_api_key(HttpCredentials::new("status-key"));
        client
            .get_key_store()
            .update_http_credentials(&HttpCredentials::new("status-key"))
            .expect("store status credentials");
        client
            .get_key_store()
            .update_grpc_credentials(&GrpcCredentials::from_token("status-grpc-token"))
            .expect("store gRPC status credentials");
        assert!(
            client
                .auth_status()
                .expect("read stored status")
                .http
                .is_authenticated()
        );
        assert!(
            client
                .auth_status()
                .expect("read stored gRPC status")
                .grpc
                .is_authenticated()
        );

        client.logout().expect("logout clears credentials");
        let logged_out = client.auth_status().expect("read logged out status");
        assert!(!client.deref().client.get_api_key().has_creds());
        assert!(!logged_out.http.is_authenticated());
        assert!(!logged_out.grpc.is_authenticated());
        client.cleanup().expect("remove scripted auth keystore");
        drop(fixture);
    }

    #[tokio::test]
    async fn ping_failures_are_classified_as_authentication_failures() {
        let fixture =
            ScriptedHttpFixture::start(vec![response(StatusCode::UNAUTHORIZED, "denied")])
                .await
                .expect("start ping failure script");
        let client = scripted_client(&fixture);
        client.set_api_key(HttpCredentials::new("ping-key"));

        let http_error = client.ping_http().await.expect_err("HTTP ping is denied");
        assert!(matches!(http_error, AnytypeError::Unauthorized));
        assert!(http_error.is_authentication());
        let requests = fixture.finish().await.expect("finish ping failure script");
        assert_eq!(requests.len(), 1);
        assert!(requests[0].method() == "GET");
        assert!(requests[0].path() == "/v1/spaces?limit=1");
        client.cleanup().expect("remove scripted auth keystore");

        let grpc_fixture = ScriptedHttpFixture::start(vec![response(StatusCode::OK, "{}")])
            .await
            .expect("start unused gRPC script");
        let grpc_client = scripted_client(&grpc_fixture);
        let grpc_error = grpc_client
            .ping_grpc()
            .await
            .expect_err("missing gRPC credentials reject ping");
        assert!(matches!(grpc_error, AnytypeError::GrpcUnavailable { .. }));
        assert!(grpc_error.is_authentication());
        grpc_client
            .cleanup()
            .expect("remove scripted gRPC auth keystore");
        drop(grpc_fixture);
    }
}