summaryrefslogtreecommitdiffstats
path: root/crates/auth/src/client/discord.rs
blob: 921768441aa02baea797664b180a9f7e2aeff363 (plain)
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
use crate::{client::OauthClient, cnfg::OauthCredentials, error::AppError};
use anyhow::Context;
use oauth2::{AuthUrl, ClientId, ClientSecret, RedirectUrl, TokenUrl, basic::BasicClient};

pub fn discord_client(config: &OauthCredentials) -> Result<OauthClient, AppError> {
    let auth_url = config.auth_url.clone().unwrap_or_else(|| {
        "https://discord.com/api/oauth2/authorize?response_type=code".to_string()
    });

    let token_url = config
        .token_url
        .clone()
        .unwrap_or_else(|| "https://discord.com/api/oauth2/token".to_string());

    let c = BasicClient::new(ClientId::new(config.client_id.to_owned()))
        .set_client_secret(ClientSecret::new(config.client_secret.to_owned()))
        .set_auth_uri(
            AuthUrl::new(auth_url).context("failed to create new auth server url [discord]")?,
        )
        .set_redirect_uri(
            RedirectUrl::new(config.redirect_url.to_owned())
                .context("failed to create new redirect URL [discord]")?,
        )
        .set_token_uri(
            TokenUrl::new(token_url)
                .context("failed to create new token endpoint URL [discord]")?,
        );

    Ok(c)
}