Skip to main content

lichess_api/api/
oauth.rs

1//! Inspect and revoke OAuth2 tokens, and exchange an authorization code for
2//! an access token.
3//!
4//! This module covers the token-facing half of the OAuth2 PKCE flow. For
5//! generating the authorization URL and completing the flow end to end, see
6//! [`crate::model::oauth::authorize::AuthorizationUrl`] and
7//! [`crate::model::oauth::PendingAuthorization`], which both carry a full
8//! walkthrough.
9
10use crate::client::LichessApi;
11use crate::error::Result;
12use crate::model::oauth::*;
13
14impl LichessApi<reqwest::Client> {
15    pub async fn test_tokens(&self, request: impl Into<test::PostRequest>) -> Result<TestResults> {
16        self.get_single_model(request.into()).await
17    }
18
19    pub async fn revoke_token(&self) -> Result<()> {
20        self.get_empty(revoke::DeleteRequest::new()).await
21    }
22
23    /// Exchange an authorization code for an access token.
24    ///
25    /// This completes the flow started with
26    /// [`crate::model::oauth::authorize::AuthorizationUrl`]. It is the one
27    /// endpoint that takes no bearer token, since it is what produces one, so
28    /// the client may be built with `LichessApi::new(client, None)`.
29    pub async fn obtain_access_token(
30        &self,
31        request: impl Into<token::PostRequest>,
32    ) -> Result<AccessToken> {
33        self.get_single_model(request.into()).await
34    }
35}