gritea/oauth/
mod.rs

1pub mod dto;
2
3pub use dto::*;
4
5use http::Method;
6use reqwest::Client;
7use url::Url;
8
9use crate::{client::resp_json, Result};
10
11pub fn oauth2_url(
12    base_url: &str,
13    client_id: &str,
14    redirect_uri: &str,
15    response_type: &str,
16    state: &str,
17) -> Result<Url> {
18    let mut url = Url::parse(base_url)?.join("login/oauth/authorize")?;
19    url.query_pairs_mut()
20        .append_pair("client_id", client_id)
21        .append_pair("redirect_uri", redirect_uri)
22        .append_pair("response_type", response_type)
23        .append_pair("state", state);
24
25    Ok(url)
26}
27
28pub async fn access_token(
29    base_url: &str,
30    ac_form: AccessTokenForm,
31    http_cli: Client,
32) -> Result<AccessToken> {
33    let url = Url::parse(base_url)?.join("login/oauth/access_token")?;
34
35    let resp = http_cli
36        .request(Method::POST, url)
37        .json(&ac_form)
38        .send()
39        .await?;
40
41    resp_json(resp, "get access_token failed").await
42}