mockforge_http/auth/
oauth2.rs

1//! OAuth2 utilities and client creation
2//!
3//! This module provides utilities for working with OAuth2 authentication,
4//! including client creation and configuration.
5
6use mockforge_core::{config::OAuth2Config, Error};
7
8/// Create OAuth2 client from configuration
9pub fn create_oauth2_client(config: &OAuth2Config) -> Result<oauth2::basic::BasicClient, Error> {
10    let client_id = oauth2::ClientId::new(config.client_id.clone());
11    let client_secret = oauth2::ClientSecret::new(config.client_secret.clone());
12
13    let auth_url = oauth2::AuthUrl::new(
14        config
15            .auth_url
16            .clone()
17            .unwrap_or_else(|| "https://example.com/auth".to_string()),
18    )
19    .map_err(|e| Error::generic(format!("Invalid auth URL: {}", e)))?;
20
21    let token_url = oauth2::TokenUrl::new(
22        config
23            .token_url
24            .clone()
25            .unwrap_or_else(|| "https://example.com/token".to_string()),
26    )
27    .map_err(|e| Error::generic(format!("Invalid token URL: {}", e)))?;
28
29    Ok(oauth2::basic::BasicClient::new(
30        client_id,
31        Some(client_secret),
32        auth_url,
33        Some(token_url),
34    ))
35}