70 lines
2.0 KiB
Rust
70 lines
2.0 KiB
Rust
#[cfg(feature = "gitea")]
|
|
use serde::{Deserialize, Serialize};
|
|
use std::env;
|
|
|
|
#[cfg(feature = "gitea")]
|
|
use oauth2::{basic::BasicClient, AuthUrl, ClientId, ClientSecret, RedirectUrl, TokenUrl};
|
|
|
|
/// Gitea OAuth configuration
|
|
#[cfg(feature = "gitea")]
|
|
#[derive(Clone, Debug)]
|
|
pub struct GiteaOAuthConfig {
|
|
/// OAuth client
|
|
pub client: BasicClient,
|
|
/// Gitea instance URL
|
|
pub instance_url: String,
|
|
}
|
|
|
|
#[cfg(feature = "gitea")]
|
|
impl GiteaOAuthConfig {
|
|
/// Creates a new Gitea OAuth configuration
|
|
pub fn new() -> Self {
|
|
// Get configuration from environment variables
|
|
let client_id =
|
|
env::var("GITEA_CLIENT_ID").expect("Missing GITEA_CLIENT_ID environment variable");
|
|
let client_secret = env::var("GITEA_CLIENT_SECRET")
|
|
.expect("Missing GITEA_CLIENT_SECRET environment variable");
|
|
let instance_url = env::var("GITEA_INSTANCE_URL")
|
|
.expect("Missing GITEA_INSTANCE_URL environment variable");
|
|
|
|
// Create OAuth client
|
|
let auth_url = format!("{}/login/oauth/authorize", instance_url);
|
|
let token_url = format!("{}/login/oauth/access_token", instance_url);
|
|
|
|
let client = BasicClient::new(
|
|
ClientId::new(client_id),
|
|
Some(ClientSecret::new(client_secret)),
|
|
AuthUrl::new(auth_url).unwrap(),
|
|
Some(TokenUrl::new(token_url).unwrap()),
|
|
)
|
|
.set_redirect_uri(
|
|
RedirectUrl::new(format!(
|
|
"{}/auth/gitea/callback",
|
|
env::var("APP_URL").unwrap_or_else(|_| "http://localhost:9999".to_string())
|
|
))
|
|
.unwrap(),
|
|
);
|
|
|
|
Self {
|
|
client,
|
|
instance_url,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Gitea user information structure
|
|
#[cfg(feature = "gitea")]
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
pub struct GiteaUser {
|
|
/// User ID
|
|
pub id: i64,
|
|
/// Username
|
|
pub login: String,
|
|
/// Full name
|
|
pub full_name: String,
|
|
/// Email address
|
|
pub email: String,
|
|
/// Avatar URL
|
|
pub avatar_url: String,
|
|
}
|