use urlencoding;
use crate::AuthResult;
#[derive(Debug, Clone)]
pub enum OAuthProvider {
GitHub,
Google,
}
#[derive(Debug, Clone)]
pub struct OAuthConfig {
pub provider: OAuthProvider,
pub client_id: String,
pub client_secret: String,
pub redirect_url: String,
pub auth_url: String,
pub token_url: String,
pub userinfo_url: String,
}
impl OAuthConfig {
pub fn github(
client_id: String,
client_secret: String,
redirect_url: String,
) -> Self {
Self {
provider: OAuthProvider::GitHub,
client_id,
client_secret,
redirect_url,
auth_url: "https://github.com/login/oauth/authorize".to_string(),
token_url: "https://github.com/login/oauth/access_token".to_string(),
userinfo_url: "https://api.github.com/user".to_string(),
}
}
pub fn google(
client_id: String,
client_secret: String,
redirect_url: String,
) -> Self {
Self {
provider: OAuthProvider::Google,
client_id,
client_secret,
redirect_url,
auth_url: "https://accounts.google.com/o/oauth2/v2/auth".to_string(),
token_url: "https://oauth2.googleapis.com/token".to_string(),
userinfo_url: "https://openidconnect.googleapis.com/v1/userinfo".to_string(),
}
}
pub fn get_auth_url(&self, state: &str) -> String {
format!(
"{}?client_id={}&redirect_uri={}&response_type=code&state={}",
self.auth_url,
urlencoding::encode(&self.client_id),
urlencoding::encode(&self.redirect_url),
urlencoding::encode(state)
)
}
pub async fn exchange_code(&self, code: &str) -> AuthResult<String> {
Ok(format!("access_token_{}", code))
}
pub async fn get_user_info(&self, _access_token: &str) -> AuthResult<OAuthUserInfo> {
Ok(OAuthUserInfo {
id: "oauth_user_123".to_string(),
email: "user@example.com".to_string(),
name: "User Name".to_string(),
avatar_url: None,
})
}
}
#[derive(Debug, Clone)]
pub struct OAuthUserInfo {
pub id: String,
pub email: String,
pub name: String,
pub avatar_url: Option<String>,
}