Skip to main content

lichess_api/model/oauth/
token.rs

1use crate::model::{Body, Request};
2use serde::Serialize;
3
4/// Form used to exchange an authorization code for an access token.
5///
6/// The `code` comes from the redirect back to your `redirect_uri`, and
7/// `code_verifier` must be the value the `code_challenge` was derived from.
8/// Both `redirect_uri` and `client_id` must match those used to request the
9/// authorization code.
10#[derive(Clone, Debug, Serialize)]
11pub struct TokenExchangeForm {
12    grant_type: &'static str,
13    pub code: String,
14    pub code_verifier: String,
15    pub redirect_uri: String,
16    pub client_id: String,
17}
18
19impl TokenExchangeForm {
20    pub fn new(
21        code: impl Into<String>,
22        code_verifier: impl Into<String>,
23        redirect_uri: impl Into<String>,
24        client_id: impl Into<String>,
25    ) -> Self {
26        Self {
27            grant_type: "authorization_code",
28            code: code.into(),
29            code_verifier: code_verifier.into(),
30            redirect_uri: redirect_uri.into(),
31            client_id: client_id.into(),
32        }
33    }
34}
35
36#[derive(Default, Clone, Debug, Serialize)]
37pub struct PostQuery;
38
39pub type PostRequest = Request<PostQuery, TokenExchangeForm>;
40
41impl PostRequest {
42    pub fn new(form: TokenExchangeForm) -> Self {
43        Self::post("/api/token", None, Body::Form(form), None)
44    }
45}
46
47impl From<TokenExchangeForm> for PostRequest {
48    fn from(form: TokenExchangeForm) -> Self {
49        Self::new(form)
50    }
51}