ferrispot 0.4.3

A wrapper for the Spotify Web API
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
//! Contains the [AuthorizationCodeUserClient](AuthorizationCodeUserClient) and its builder structs. The client
//! implements the authorization code flow with optional PKCE.
//!
//! [Spotify documentation on the authorization code flow.](https://developer.spotify.com/documentation/general/guides/authorization/code-flow/)
//!
//! # Usage
//!
//! A new [AuthorizationCodeUserClient] may be built with the
//! [`authorization_code_client`-function](crate::client::SpotifyClientWithSecret::authorization_code_client) in
//! [SpotifyClientWithSecret](crate::client::SpotifyClientWithSecret).
//!
//! ```no_run
//! # use ferrispot::client::SpotifyClientBuilder;
//! # use ferrispot::scope::Scope;
//! # async fn foo() {
//! // build a new Spotify client that has the application secret
//! let spotify_client = SpotifyClientBuilder::new("application client ID")
//!     .client_secret("application client secret")
//!     // a synchronous client may be built with .build_sync()
//!     .build_async()
//!     .await
//!     .expect("failed to build Spotify client");
//!
//! // begin building a new AuthorizationCodeUserClient
//! let incomplete_auth_code_client = spotify_client
//!     // the callback URL here should match one of the callback URLs
//!     // specified in your Spotify application
//!     .authorization_code_client("http://localhost/callback")
//!     // specify any (or none) of the scopes you require access to
//!     .scopes([Scope::UserReadPlaybackState])
//!     // in case the user has already approved the application, this may be
//!     // set to `true` to force the user approve the application again
//!     .show_dialog(true)
//!     .build();
//!
//! // at this point the client is configured but not yet ready for use; it is
//! // still missing the user authorization
//!
//! // generate an authorization URL for the user. this URL takes the user to a
//! // Spotify page where they are prompted to give the application access to
//! // their account and all the scopes you've specified earlier
//! let authorize_url = incomplete_auth_code_client.get_authorize_url();
//!
//! // the user should now be directed to this URL in some manner
//!
//! // when the user accepts, they are redirected to the previously specified
//! // callback URL, which will contain an authorization code (`code`) and a
//! // state code (`state`) in the query parameters. you should extract both of
//! // them from the URL in some manner
//! # let code = "";
//! # let state = "";
//!
//! // finalize the client with the authorization code and state. the client
//! // will use the authorization code to request an access token and a refresh
//! // token from Spotify, which it will use to access the API
//! let user_client = incomplete_auth_code_client
//!     .finalize(code, state)
//!     .await
//!     .expect("failed to finalize authorization code flow client");
//! # }
//! ```
//!
//! # Usage with PKCE
//!
//! In case the application's client secret cannot be safely stored in the environment, PKCE may still be used to
//! strongly authenticate the client with Spotify. A new [AuthorizationCodeUserClient] that uses PKCE may be built with
//! the [`authorization_code_client_with_pkce`-function](crate::client::SpotifyClient::authorization_code_client_with_pkce)
//! in [SpotifyClient](crate::client::SpotifyClient).
//!
//! ```no_run
//! # use ferrispot::client::SpotifyClientBuilder;
//! # use ferrispot::scope::Scope;
//! # async fn foo() {
//! // build a new Spotify client that doesn't have the application secret
//! let spotify_client = SpotifyClientBuilder::new("application client ID")
//!     .build_async();
//!
//! // begin building a new AuthorizationCodeUserClient that uses PKCE
//! let incomplete_auth_code_client = spotify_client
//!     // the callback URL here should match one of the callback URLs
//!     // specified in your Spotify application
//!     .authorization_code_client_with_pkce("http://localhost/callback")
//!     // specify any (or none) of the scopes you require access to
//!     .scopes([Scope::UserReadPlaybackState])
//!     // in case the user has already approved the application, this may be
//!     // set to `true` to force the user approve the application again
//!     .show_dialog(true)
//!     .build();
//!
//! // from here on out, the usage is identical as with the usual client. refer
//! // to the documentation above
//! # }

use std::sync::{Arc, RwLock};

use base64::Engine;
use log::debug;
use rand::{distributions::Alphanumeric, Rng};
use reqwest::{IntoUrl, Method, Url};
use serde::Deserialize;
use sha2::Digest;

use super::{
    private, ACCOUNTS_API_TOKEN_ENDPOINT, ACCOUNTS_AUTHORIZE_ENDPOINT, PKCE_VERIFIER_LENGTH, RANDOM_STATE_LENGTH,
};
#[cfg(feature = "async")]
use super::{private::AsyncClient, AccessTokenRefreshAsync};
#[cfg(feature = "sync")]
use super::{private::SyncClient, AccessTokenRefreshSync};
use crate::{
    error::{Error, Result},
    model::error::AuthenticationErrorKind,
    scope::ToScopesString,
};

/// Type alias for an asynchronous authorization code user client. See
/// [AuthorizationCodeUserClient](AuthorizationCodeUserClient).
#[cfg(feature = "async")]
pub type AsyncAuthorizationCodeUserClient = AuthorizationCodeUserClient<AsyncClient>;

/// Type alias for a synchronous authorization code user client. See
/// [AuthorizationCodeUserClient](AuthorizationCodeUserClient).
#[cfg(feature = "sync")]
pub type SyncAuthorizationCodeUserClient = AuthorizationCodeUserClient<SyncClient>;

/// Type alias for an incomplete asynchronous authorization code user client. See
/// [IncompleteAuthorizationCodeUserClient](IncompleteAuthorizationCodeUserClient).
#[cfg(feature = "async")]
pub type AsyncIncompleteAuthorizationCodeUserClient = IncompleteAuthorizationCodeUserClient<AsyncClient>;

/// Type alias for a incomplete synchronous authorization code user client. See
/// [IncompleteAuthorizationCodeUserClient](IncompleteAuthorizationCodeUserClient).
#[cfg(feature = "sync")]
pub type SyncIncompleteAuthorizationCodeUserClient = IncompleteAuthorizationCodeUserClient<SyncClient>;

/// Type alias for an asynchronous authorization code client user client builder. See
/// [AuthorizationCodeUserClientBuilder](AuthorizationCodeUserClientBuilder).
#[cfg(feature = "async")]
pub type AsyncAuthorizationCodeUserClientBuilder = AuthorizationCodeUserClientBuilder<AsyncClient>;

/// Type alias for a synchronous authorization code client user client builder. See
/// [AuthorizationCodeUserClientBuilder](AuthorizationCodeUserClientBuilder).
#[cfg(feature = "sync")]
pub type SyncAuthorizationCodeUserClientBuilder = AuthorizationCodeUserClientBuilder<SyncClient>;

/// A client that implements the authorization code flow to authenticate an user with Spotify. May optionally use PKCE
/// if the client secret is not available. See the [module-level documentation](self) for more information.
///
/// Implements all the [scoped](crate::client::ScopedClient) and [unscoped endpoints](crate::client::UnscopedClient).
///
/// This struct is generic over its internal asynchronous/synchronous HTTP client. You cannot refer to the internal
/// client types directly, hence there are type aliases for both kinds of clients: [AsyncAuthorizationCodeUserClient]
/// and [SyncAuthorizationCodeUserClient]. Likewise, both the builder struct and the incomplete client struct are
/// similarly generic, and have equivalent type aliases.
///
/// This client uses `Arc` and interior mutability internally, so you do not need to wrap it in an `Arc` in order to
/// reuse it; it is cheap to clone, and all clones refer to the same internal structures.
#[derive(Debug, Clone)]
pub struct AuthorizationCodeUserClient<C>
where
    C: private::HttpClient + Clone,
{
    inner: Arc<AuthorizationCodeUserClientRef>,
    http_client: C,
}

#[derive(Debug)]
struct AuthorizationCodeUserClientRef {
    access_token: RwLock<String>,
    refresh_token: RwLock<String>,
    client_id: Option<String>,
}

/// An incomplete authorization code user client.
///
/// The client has been configured, and it has to be [finalized](IncompleteAuthorizationCodeUserClient::finalize) by
/// directing the user to the [authorize URL](IncompleteAuthorizationCodeUserClient::get_authorize_url) and retrieving
/// an authorization code and a state parameter from the redirect callback URL.
#[derive(Debug)]
pub struct IncompleteAuthorizationCodeUserClient<C>
where
    C: private::HttpClient + Clone,
{
    client_id: String,
    redirect_uri: String,
    state: String,
    scopes: Option<String>,
    show_dialog: bool,
    pkce_verifier: Option<String>,

    http_client: C,
}

/// Builder for [AuthorizationCodeUserClient].
#[derive(Debug)]
pub struct AuthorizationCodeUserClientBuilder<C>
where
    C: private::HttpClient + Clone,
{
    client_id: String,
    redirect_uri: String,
    scopes: Option<String>,
    show_dialog: bool,
    pkce_verifier: Option<String>,

    http_client: C,
}

#[derive(Debug, Deserialize)]
struct AuthorizeUserTokenResponse {
    access_token: String,
    refresh_token: String,

    // these fields are in the response but the library doesn't need them. keep them here for logging purposes
    #[allow(dead_code)]
    scope: Option<String>,
    #[allow(dead_code)]
    expires_in: u32,
    #[allow(dead_code)]
    token_type: String,
}

#[derive(Debug, Deserialize)]
struct RefreshUserTokenResponse {
    access_token: String,
    refresh_token: Option<String>,

    // these fields are in the response but the library doesn't need them. keep them here for logging purposes
    #[allow(dead_code)]
    scope: Option<String>,
    #[allow(dead_code)]
    expires_in: u32,
    #[allow(dead_code)]
    token_type: String,
}

impl<C> AuthorizationCodeUserClient<C>
where
    C: private::HttpClient + Clone,
{
    fn new_from_refresh_token(
        token_response: RefreshUserTokenResponse,
        refresh_token: String,
        client_id: Option<String>,
        http_client: C,
    ) -> Self {
        debug!(
            "Got token response for refreshing authorization code flow tokens: {:?}",
            token_response
        );

        let refresh_token = token_response.refresh_token.unwrap_or(refresh_token);

        Self {
            inner: Arc::new(AuthorizationCodeUserClientRef {
                access_token: RwLock::new(token_response.access_token),
                refresh_token: RwLock::new(refresh_token),
                client_id,
            }),
            http_client,
        }
    }

    /// Returns the current refresh token.
    ///
    /// The refresh token may be saved and reused later when creating a new client with the
    /// [`authorization_code_client_with_refresh_token`-function](crate::client::SpotifyClientWithSecret::authorization_code_client_with_refresh_token)
    /// or the [`authorization_code_client_with_refresh_token_and_pkce`-function](crate::client::SpotifyClient::authorization_code_client_with_refresh_token_and_pkce).
    ///
    /// This function returns an owned String by cloning the internal refresh token.
    pub fn get_refresh_token(&self) -> String {
        self.inner
            .refresh_token
            .read()
            .expect("refresh token rwlock poisoned")
            .to_owned()
    }

    fn update_access_and_refresh_tokens(&self, token_response: RefreshUserTokenResponse) {
        debug!(
            "Got token response for refreshing authorization code flow tokens: {:?}",
            token_response
        );

        *self.inner.access_token.write().expect("access token rwlock poisoned") = token_response.access_token;

        if let Some(refresh_token) = token_response.refresh_token {
            *self.inner.refresh_token.write().expect("refresh token rwlock poisoned") = refresh_token;
        }
    }
}

#[cfg(feature = "async")]
impl AsyncAuthorizationCodeUserClient {
    pub(crate) async fn new_with_refresh_token(
        http_client: AsyncClient,
        refresh_token: String,
        client_id: Option<String>,
    ) -> Result<Self> {
        debug!(
            "Attempting to create new authorization code flow client with existng refresh token: {} and client ID \
             (for PKCE): {:?}",
            refresh_token, client_id
        );

        let response = http_client
            .post(ACCOUNTS_API_TOKEN_ENDPOINT)
            .form(&build_refresh_token_request_form(&refresh_token, client_id.as_deref()))
            .send()
            .await?;

        let response = super::extract_authentication_error_async(response)
            .await
            .map_err(map_refresh_token_error)?;

        let token_response = response.json().await?;

        Ok(Self::new_from_refresh_token(
            token_response,
            refresh_token,
            client_id,
            http_client,
        ))
    }
}

#[cfg(feature = "sync")]
impl SyncAuthorizationCodeUserClient {
    pub(crate) fn new_with_refresh_token(
        http_client: SyncClient,
        refresh_token: String,
        client_id: Option<String>,
    ) -> Result<Self> {
        debug!(
            "Attempting to create new authorization code flow client with existng refresh token: {} and client ID \
             (for PKCE): {:?}",
            refresh_token, client_id
        );

        let response = http_client
            .post(ACCOUNTS_API_TOKEN_ENDPOINT)
            .form(&build_refresh_token_request_form(&refresh_token, client_id.as_deref()))
            .send()?;

        let response = super::extract_authentication_error_sync(response).map_err(map_refresh_token_error)?;
        let token_response = response.json()?;

        Ok(Self::new_from_refresh_token(
            token_response,
            refresh_token,
            client_id,
            http_client,
        ))
    }
}

impl<C> IncompleteAuthorizationCodeUserClient<C>
where
    C: private::HttpClient + Clone,
{
    /// Returns an authorization URL the user should be directed to in some manner.
    ///
    /// Once the user approves the application, they are redirected back to the application's callback URL. The URL
    /// query in the callback will contain a `code` parameter and a `state` parameter, which should be passed to the
    /// [`finalize`-function](IncompleteAuthorizationCodeUserClient::finalize) in order to complete the client and get
    /// an [AuthorizationCodeUserClient].
    pub fn get_authorize_url(&self) -> String {
        let mut query_params = vec![
            ("response_type", "code"),
            ("redirect_uri", self.redirect_uri.as_str()),
            ("client_id", self.client_id.as_str()),
            ("state", self.state.as_str()),
            ("show_dialog", if self.show_dialog { "true" } else { "false" }),
        ];

        if let Some(scopes) = &self.scopes {
            query_params.push(("scope", scopes.as_str()));
        }

        let authorize_url = if let Some(pkce_verifier) = self.pkce_verifier.as_deref() {
            let mut hasher = sha2::Sha256::new();
            hasher.update(pkce_verifier);
            let pkce_challenge = hasher.finalize();
            let pkce_challenge = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(pkce_challenge);

            debug!(
                "Using PKCE extension with verifier: {} and challenge: {}",
                pkce_verifier, pkce_challenge
            );

            query_params.extend([("code_challenge_method", "S256"), ("code_challenge", &pkce_challenge)]);

            // parsing the URL fails only if the base URL is invalid, not the parameters. if this method fails, there's
            // a bug in the library

            // while both these branches end the same way, this one borrows the pkce_challenge string in query_params so
            // the URL must be built before the string falls out of scope
            Url::parse_with_params(ACCOUNTS_AUTHORIZE_ENDPOINT, &query_params)
                .expect("failed to build authorize URL: invalid base URL (this is likely a bug)")
        } else {
            Url::parse_with_params(ACCOUNTS_AUTHORIZE_ENDPOINT, &query_params)
                .expect("failed to build authorize URL: invalid base URL (this is likely a bug)")
        };

        authorize_url.into()
    }

    fn build_authorization_code_token_request_form<'a>(
        &'a self,
        code: &'a str,
        state: &str,
    ) -> Result<Vec<(&'a str, &'a str)>> {
        debug!(
            "Attempting to finalize authorization code flow user client with code: {} and state: {}",
            code, state
        );

        if state != self.state {
            return Err(Error::AuthorizationCodeStateMismatch);
        }

        let mut token_request_form = vec![
            ("grant_type", "authorization_code"),
            ("code", code),
            ("redirect_uri", self.redirect_uri.as_str()),
        ];

        if let Some(pkce_verifier) = self.pkce_verifier.as_deref() {
            debug!("Requesting access and refresh tokens for authorization code flow with PKCE");
            token_request_form.extend([("client_id", self.client_id.as_str()), ("code_verifier", pkce_verifier)]);
        } else {
            debug!("Requesting access and refresh tokens for authorization code flow");
        }

        Ok(token_request_form)
    }

    fn build_client(self, token_response: AuthorizeUserTokenResponse) -> AuthorizationCodeUserClient<C> {
        debug!("Got token response for authorization code flow: {:?}", token_response);

        AuthorizationCodeUserClient {
            http_client: self.http_client,
            // from here on out, using PKCE only requires us supplying our client ID when refreshing the access
            // token. if the PKCE verifier is used, include the client ID
            inner: Arc::new(AuthorizationCodeUserClientRef {
                access_token: RwLock::new(token_response.access_token),
                refresh_token: RwLock::new(token_response.refresh_token),
                client_id: self.pkce_verifier.and(Some(self.client_id)),
            }),
        }
    }
}

#[cfg(feature = "async")]
impl AsyncIncompleteAuthorizationCodeUserClient {
    /// Finalize this client with a code and a state from the callback URL query the user was redirected to after they
    /// approved the application and return an usable [AuthorizationCodeUserClient].
    ///
    /// This function will use the authorization code to request an access and a refresh token from Spotify. If the
    /// originally generated state does not match the `state` parameter, the function will return an
    /// [AuthorizationCodeStateMismatch-error](Error::AuthorizationCodeStateMismatch).
    pub async fn finalize(self, code: &str, state: &str) -> Result<AsyncAuthorizationCodeUserClient> {
        let token_request_form = self.build_authorization_code_token_request_form(code, state)?;
        let response = self
            .http_client
            .post(ACCOUNTS_API_TOKEN_ENDPOINT)
            .form(&token_request_form)
            .send()
            .await?;

        let response = super::extract_authentication_error_async(response)
            .await
            .map_err(map_authentication_error)?;

        let token_response = response.json().await?;

        Ok(self.build_client(token_response))
    }
}

#[cfg(feature = "sync")]
impl SyncIncompleteAuthorizationCodeUserClient {
    /// Finalize this client with a code and a state from the callback URL query the user was redirected to after they
    /// approved the application and return an usable [AuthorizationCodeUserClient].
    ///
    /// This function will use the authorization code to request an access and a refresh token from Spotify. If the
    /// originally generated state does not match the `state` parameter, the function will return an
    /// [AuthorizationCodeStateMismatch-error](Error::AuthorizationCodeStateMismatch).
    pub fn finalize(self, code: &str, state: &str) -> Result<SyncAuthorizationCodeUserClient> {
        let token_request_form = self.build_authorization_code_token_request_form(code, state)?;
        let response = self
            .http_client
            .post(ACCOUNTS_API_TOKEN_ENDPOINT)
            .form(&token_request_form)
            .send()?;

        let response = super::extract_authentication_error_sync(response).map_err(map_authentication_error)?;
        let token_response = response.json()?;

        Ok(self.build_client(token_response))
    }
}

#[cfg(feature = "async")]
impl AsyncAuthorizationCodeUserClientBuilder {
    pub(super) fn new(redirect_uri: String, client_id: String, http_client: AsyncClient) -> Self {
        Self {
            client_id,
            redirect_uri,
            scopes: None,
            show_dialog: false,
            pkce_verifier: None,

            http_client,
        }
    }
}

#[cfg(feature = "sync")]
impl SyncAuthorizationCodeUserClientBuilder {
    pub(super) fn new(redirect_uri: String, client_id: String, http_client: SyncClient) -> Self {
        Self {
            client_id,
            redirect_uri,
            scopes: None,
            show_dialog: false,
            pkce_verifier: None,

            http_client,
        }
    }
}

impl<C> AuthorizationCodeUserClientBuilder<C>
where
    C: private::HttpClient + Clone,
{
    /// Generates a PKCE code verifier to be used in the authentication process.
    pub(super) fn with_pkce(self) -> Self {
        let code_verifier = rand::thread_rng()
            .sample_iter(&Alphanumeric)
            .take(PKCE_VERIFIER_LENGTH)
            .map(char::from)
            .collect();

        Self {
            pkce_verifier: Some(code_verifier),
            ..self
        }
    }

    /// Specify the [OAuth authorization scopes](crate::scope::Scope) that the user is asked to grant for the
    /// application.
    ///
    /// Note that if you change the scopes you request, the new scopes will not be granted for users with existing
    /// approvals for the application. You will have to force users to reapprove the application by setting
    /// [`show_dialog`](AuthorizationCodeUserClientBuilder::show_dialog) to true and directing users through the
    /// authorization flow instead of using an existing refresh token.
    pub fn scopes<T>(self, scopes: T) -> Self
    where
        T: ToScopesString,
    {
        Self {
            scopes: Some(scopes.to_scopes_string()),
            ..self
        }
    }

    // TODO: is it possible for us to detect when the scopes have changed compared to a token and then automatically
    // force user reapproval?
    /// Set whether or not to force the user to approve the application again, if they've already done so.
    ///
    /// If false (default), a user who has already approved the application is automatically redirected to the specified
    /// redirect URL. If true, the user will not be automatically redirected and will have to approve the application
    /// again.
    ///
    /// Note that if you change the [scopes you request](AuthorizationCodeUserClientBuilder::scopes), the new scopes
    /// will not be granted for users with existing approvals for the application. You will have to force users to
    /// reapprove the application by setting this parameter to true and directing users through the authorization
    /// flow instead of using an existing refresh token.
    pub fn show_dialog(self, show_dialog: bool) -> Self {
        Self { show_dialog, ..self }
    }

    /// Finalize the builder and return an [IncompleteAuthorizationCodeUserClient].
    pub fn build(self) -> IncompleteAuthorizationCodeUserClient<C> {
        let state = rand::thread_rng()
            .sample_iter(&Alphanumeric)
            .take(RANDOM_STATE_LENGTH)
            .map(char::from)
            .collect();

        IncompleteAuthorizationCodeUserClient {
            redirect_uri: self.redirect_uri,
            state,
            scopes: self.scopes,
            show_dialog: self.show_dialog,
            client_id: self.client_id,
            pkce_verifier: self.pkce_verifier,

            http_client: self.http_client,
        }
    }
}

impl<C> crate::private::Sealed for AuthorizationCodeUserClient<C> where C: private::HttpClient + Clone {}

#[cfg(feature = "async")]
impl private::BuildHttpRequestAsync for AsyncAuthorizationCodeUserClient {
    fn build_http_request<U>(&self, method: Method, url: U) -> reqwest::RequestBuilder
    where
        U: IntoUrl,
    {
        let access_token = self.inner.access_token.read().expect("access token rwlock poisoned");
        self.http_client.request(method, url).bearer_auth(access_token.as_str())
    }
}

#[cfg(feature = "sync")]
impl private::BuildHttpRequestSync for SyncAuthorizationCodeUserClient {
    fn build_http_request<U>(&self, method: Method, url: U) -> reqwest::blocking::RequestBuilder
    where
        U: IntoUrl,
    {
        let access_token = self.inner.access_token.read().expect("access token rwlock poisoned");
        self.http_client.request(method, url).bearer_auth(access_token.as_str())
    }
}

#[cfg(feature = "async")]
impl super::ScopedClient for AsyncAuthorizationCodeUserClient {}

#[cfg(feature = "sync")]
impl super::ScopedClient for SyncAuthorizationCodeUserClient {}

#[cfg(feature = "async")]
impl super::UnscopedClient for AsyncAuthorizationCodeUserClient {}

#[cfg(feature = "sync")]
impl super::UnscopedClient for SyncAuthorizationCodeUserClient {}

#[cfg(feature = "async")]
#[async_trait::async_trait]
impl super::AccessTokenRefreshAsync for AsyncAuthorizationCodeUserClient {
    async fn refresh_access_token(&self) -> Result<()> {
        // build and send the request this way to not hold the non-async RwLockReadGuard across await points
        let response = {
            let refresh_token = self.inner.refresh_token.read().expect("refresh token rwlock poisoned");
            debug!(
                "Attempting to refresh authorization code flow access token with refresh token: {}",
                refresh_token
            );

            // build the HTTP request straight from the client so it'll use the client credentials authorization header
            // instead of the access token
            let request = self
                .http_client
                .post(ACCOUNTS_API_TOKEN_ENDPOINT)
                .form(&build_refresh_token_request_form(
                    &refresh_token,
                    self.inner.client_id.as_deref(),
                ))
                .send();

            // for some reason if I just let the refresh token read guard drop by its own at the end of this scope, it
            // doesn't actually drop by the end and is kept across the await, causing issues
            drop(refresh_token);
            request
        }
        .await?;

        let response = super::extract_authentication_error_async(response)
            .await
            .map_err(map_refresh_token_error)?;

        let token_response = response.json().await?;
        self.update_access_and_refresh_tokens(token_response);

        Ok(())
    }
}

#[cfg(feature = "sync")]
impl super::AccessTokenRefreshSync for SyncAuthorizationCodeUserClient {
    fn refresh_access_token(&self) -> Result<()> {
        let refresh_token = self.inner.refresh_token.read().expect("refresh token rwlock poisoned");
        debug!(
            "Attempting to refresh authorization code flow access token with refresh token: {}",
            refresh_token
        );

        // build the HTTP request straight from the client so it'll use the client credentials authorization header
        // instead of the access token
        let response = self
            .http_client
            .post(ACCOUNTS_API_TOKEN_ENDPOINT)
            .form(&build_refresh_token_request_form(
                &refresh_token,
                self.inner.client_id.as_deref(),
            ))
            .send()?;

        // the refresh token may later be written to, drop our read guard
        drop(refresh_token);

        let response = super::extract_authentication_error_sync(response).map_err(map_refresh_token_error)?;
        let token_response = response.json()?;
        self.update_access_and_refresh_tokens(token_response);

        Ok(())
    }
}

#[cfg(feature = "async")]
#[async_trait::async_trait]
impl private::AccessTokenExpiryAsync for AsyncAuthorizationCodeUserClient {
    async fn handle_access_token_expired(&self) -> Result<private::AccessTokenExpiryResult> {
        self.refresh_access_token().await?;
        Ok(private::AccessTokenExpiryResult::Ok)
    }
}

#[cfg(feature = "sync")]
impl private::AccessTokenExpirySync for SyncAuthorizationCodeUserClient {
    fn handle_access_token_expired(&self) -> Result<private::AccessTokenExpiryResult> {
        self.refresh_access_token()?;
        Ok(private::AccessTokenExpiryResult::Ok)
    }
}

fn build_refresh_token_request_form<'a>(refresh_token: &'a str, client_id: Option<&'a str>) -> Vec<(&'a str, &'a str)> {
    let mut token_request_form = vec![("grant_type", "refresh_token"), ("refresh_token", refresh_token)];

    if let Some(client_id) = client_id {
        token_request_form.push(("client_id", client_id));
    }

    token_request_form
}

fn map_authentication_error(err: Error) -> Error {
    if let Error::UnhandledAuthenticationError(AuthenticationErrorKind::InvalidGrant, _) = err {
        Error::InvalidAuthorizationCode
    } else {
        err
    }
}

fn map_refresh_token_error(err: Error) -> Error {
    if let Error::UnhandledAuthenticationError(AuthenticationErrorKind::InvalidGrant, description) = err {
        Error::InvalidRefreshToken(description)
    } else {
        err
    }
}