authen 0.1.0

A robust, modular, high-level, and secure authentication crate for Rust.
Documentation
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! OAuth2 Manager Implementation
//!
//! This module provides the concrete implementation of the [`OAuth2Service`] trait
//! using the [`oauth2`] and [`reqwest`] crates for handling OAuth2 authentication flows.
//!
//! # Overview
//!
//! The [`OAuth2Manager`] struct manages OAuth2 authentication flows for multiple providers,
//! including Google, GitHub, Discord, and Microsoft. It handles generating authorization URLs,
//! exchanging authorization codes for tokens, refreshing tokens, and fetching user information.
//!
//! ## Supported Providers
//!
//! - Google
//! - GitHub
//! - Discord
//! - Microsoft
//!
//! ## Main Features
//!
//! - Provider configuration management
//! - HTTP requests for OAuth2 endpoints
//! - Parsing provider-specific user info responses
//! - Token exchange and refresh
//!
//! ## Example Usage
//!
//! ```rust
//! use crate::core::oauth::{OAuth2Manager, OAuth2Provider, OAuth2Config};
//! use std::collections::HashMap;
//!
//! let mut configs = HashMap::new();
//! configs.insert(OAuth2Provider::Google, OAuth2Config::default_google());
//! let manager = OAuth2Manager::new(configs);
//! ```

use async_trait::async_trait;
use oauth2::{
    AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, EmptyExtraTokenFields,
    EndpointSet, RedirectUrl, RefreshToken, Scope, TokenResponse, TokenUrl, basic::BasicClient,
    basic::BasicTokenType,
};
use reqwest::Client;
use serde_json::Value;
use std::collections::HashMap;

use super::OAuth2Service;
use super::store::{OAuth2Config, OAuth2Provider, OAuth2Token, OAuth2UserInfo};
use crate::AuthError;

use log::{debug, info};

/// Type alias for a fully configured OAuth2 BasicClient.
///
/// This alias simplifies the usage of the [`oauth2::Client`] type with all required generic parameters for standard OAuth2 flows.
/// It is used internally to manage provider-specific OAuth2 clients, ensuring type safety and consistency across supported providers.
type ConfiguredBasicClient = oauth2::Client<
    oauth2::StandardErrorResponse<oauth2::basic::BasicErrorResponseType>,
    oauth2::StandardTokenResponse<EmptyExtraTokenFields, BasicTokenType>,
    oauth2::basic::BasicTokenIntrospectionResponse,
    oauth2::StandardRevocableToken,
    oauth2::basic::BasicRevocationErrorResponse,
    EndpointSet,
    oauth2::EndpointNotSet,
    oauth2::EndpointNotSet,
    oauth2::EndpointNotSet,
    EndpointSet,
>;

/// Manages OAuth2 authentication flows for multiple providers.
///
/// The [`OAuth2Manager`] struct implements the [`OAuth2Service`] trait and provides methods for:
/// - Generating authorization URLs for OAuth2 login flows
/// - Exchanging authorization codes for access and refresh tokens
/// - Refreshing expired access tokens
/// - Fetching user information from provider-specific endpoints
/// - Managing provider configuration and HTTP clients
///
/// # Supported Providers
/// - Google
/// - GitHub
/// - Discord
/// - Microsoft
///
/// # Example
/// ```rust
/// use crate::core::oauth::{OAuth2Manager, OAuth2Provider, OAuth2Config};
/// use std::collections::HashMap;
/// let mut configs = HashMap::new();
/// configs.insert(OAuth2Provider::Google, OAuth2Config::default_google());
/// let manager = OAuth2Manager::new(configs);
/// ```
pub struct OAuth2Manager {
    /// Map of OAuth2 providers to their configuration.
    configs: HashMap<OAuth2Provider, OAuth2Config>,
}

impl OAuth2Manager {
    /// Creates a new [`OAuth2Manager`] with the provided provider configurations.
    ///
    /// # Arguments
    /// * `configs` - A map of [`OAuth2Provider`] to [`OAuth2Config`] containing all provider-specific settings.
    ///
    /// # Returns
    /// A new [`OAuth2Manager`] instance ready to manage OAuth2 flows for the configured providers.
    pub fn new(configs: HashMap<OAuth2Provider, OAuth2Config>) -> Self {
        info!(
            "Initializing OAuth2Manager with {} provider configs",
            configs.len()
        );

        Self { configs }
    }

    /// Returns a configured HTTP client for the given provider with the appropriate User-Agent.
    ///
    /// # Arguments
    /// * `provider` - The OAuth2 provider for which to create the HTTP client.
    ///
    /// # Errors
    /// Returns [`AuthError::ConfigError`] if the provider configuration is missing or the client cannot be built.
    ///
    /// # Example
    /// ```rust
    /// let client = manager.get_http_client(OAuth2Provider::Google)?;
    /// ```
    fn get_http_client(&self, provider: OAuth2Provider) -> Result<Client, AuthError> {
        let config = self.configs.get(&provider).ok_or_else(|| {
            AuthError::ConfigError(format!("No config found for provider: {provider:?}"))
        })?;

        Client::builder()
            .user_agent(&config.app_name)
            .build()
            .map_err(|e| AuthError::ConfigError(format!("Failed to create HTTP client: {e}")))
    }

    /// Returns a configured OAuth2 client for the given provider.
    ///
    /// # Arguments
    /// * `provider` - The OAuth2 provider for which to create the client.
    ///
    /// # Errors
    /// Returns [`AuthError::ConfigError`] if the provider configuration is missing or contains invalid URLs.
    ///
    /// # Example
    /// ```rust
    /// let client = manager.get_client(OAuth2Provider::GitHub)?;
    /// ```
    pub fn get_client(&self, provider: OAuth2Provider) -> Result<ConfiguredBasicClient, AuthError> {
        debug!("Getting OAuth2 client for provider: {provider:?}");
        let config = self.configs.get(&provider).ok_or_else(|| {
            debug!("No config found for provider: {provider:?}");
            AuthError::ConfigError(format!("No config found for provider: {provider:?}"))
        })?;

        let app_name = &config.app_name;

        let auth_url = AuthUrl::new(config.auth_url(provider).to_string()).map_err(|e| {
            debug!("Invalid auth URL for provider {provider:?}: {e}");
            AuthError::ConfigError(format!("Invalid auth URL: {e}"))
        })?;

        let token_url = TokenUrl::new(config.token_url(provider).to_string()).map_err(|e| {
            debug!("Invalid token URL for provider {provider:?}: {e}");
            AuthError::ConfigError(format!("Invalid token URL: {e}"))
        })?;

        let redirect_url = RedirectUrl::new(config.redirect_callback_uri.clone()).map_err(|e| {
            debug!("Invalid redirect URL for provider {provider:?}: {e}");
            AuthError::ConfigError(format!("Invalid redirect URL: {e}"))
        })?;

        debug!("OAuth2 client configured for provider: {provider:?}");
        debug!("App Name: {app_name}");
        debug!("Client ID: {}", config.client_id);
        debug!("Redirect URI: {}", config.redirect_callback_uri);
        debug!("Auth URL: {}", config.auth_url(provider));
        debug!("Token URL: {}", config.token_url(provider));
        let client = BasicClient::new(ClientId::new(config.client_id.clone()))
            .set_client_secret(ClientSecret::new(config.client_secret.clone()))
            .set_auth_uri(auth_url)
            .set_token_uri(token_url)
            .set_redirect_uri(redirect_url);

        Ok(client)
    }

    /// Parses user information from the provider's user info response.
    ///
    /// This method extracts and normalizes user profile data from the JSON response returned by the provider's user info endpoint.
    /// The parsing logic is provider-specific and handles differences in field names and formats.
    ///
    /// # Arguments
    /// * `provider` - The OAuth2 provider whose response is being parsed.
    /// * `response_body` - The JSON response body from the provider's user info endpoint.
    ///
    /// # Returns
    /// Returns [`OAuth2UserInfo`] on success, or [`AuthError`] if required fields are missing or the response is invalid.
    ///
    /// # Example
    /// ```rust
    /// let user_info = manager.parse_user_info(OAuth2Provider::Google, json_response).await?;
    /// ```
    pub async fn parse_user_info(
        &self,
        provider: OAuth2Provider,
        response_body: Value,
    ) -> Result<OAuth2UserInfo, AuthError> {
        let now = chrono::Utc::now().naive_utc();

        debug!("Parsing user info for provider: {provider:?}");

        match provider {
            OAuth2Provider::Google => {
                debug!("Google user info response: {response_body:?}");
                let email = response_body["email"].as_str().map(|s| s.to_string());
                let name = response_body["name"].as_str().map(|s| s.to_string());
                let avatar_url = response_body["picture"].as_str().map(|s| s.to_string());
                let verified_email = response_body["verified_email"].as_bool();
                let locale = response_body["locale"].as_str().map(|s| s.to_string());
                let provider_user_id = response_body["id"]
                    .as_str()
                    .ok_or_else(|| AuthError::OAuthInvalidResponse("Missing user ID".to_string()))?
                    .to_string();

                Ok(OAuth2UserInfo {
                    user_id: String::new(), // Will be set when linking to authen user
                    provider,
                    provider_user_id,
                    email,
                    name,
                    avatar_url,
                    verified_email,
                    locale,
                    updated_at: now,
                    raw_data: Some(response_body),
                })
            }
            OAuth2Provider::GitHub => {
                debug!("GitHub user info response: {response_body:?}");
                let name = response_body["name"].as_str().map(|s| s.to_string());
                let avatar_url = response_body["avatar_url"].as_str().map(|s| s.to_string());
                let provider_user_id = response_body["id"]
                    .as_u64()
                    .ok_or_else(|| AuthError::OAuthInvalidResponse("Missing user ID".to_string()))?
                    .to_string();

                // GitHub requires a separate API call for email
                let email = if let Some(email_str) = response_body["email"].as_str() {
                    if !email_str.is_empty() {
                        Some(email_str.to_string())
                    } else {
                        None
                    }
                } else {
                    None
                };

                Ok(OAuth2UserInfo {
                    user_id: String::new(), // Will be set when linking to authen user
                    provider,
                    provider_user_id,
                    email,
                    name,
                    avatar_url,
                    verified_email: None,
                    locale: None,
                    updated_at: now,
                    raw_data: Some(response_body),
                })
            }
            OAuth2Provider::Discord => {
                debug!("Discord user info response: {response_body:?}");
                let email = response_body["email"].as_str().map(|s| s.to_string());
                let name = response_body["username"].as_str().map(|s| s.to_string());
                let avatar = response_body["avatar"].as_str();
                let provider_user_id = response_body["id"]
                    .as_str()
                    .ok_or_else(|| AuthError::OAuthInvalidResponse("Missing user ID".to_string()))?
                    .to_string();

                let avatar_url = avatar.map(|avatar_hash| {
                    format!(
                        "https://cdn.discordapp.com/avatars/{provider_user_id}/{avatar_hash}.png"
                    )
                });

                let verified_email = response_body["verified"].as_bool();
                let locale = response_body["locale"].as_str().map(|s| s.to_string());

                Ok(OAuth2UserInfo {
                    user_id: String::new(), // Will be set when linking to authen user
                    provider,
                    provider_user_id,
                    email,
                    name,
                    avatar_url,
                    verified_email,
                    locale,
                    updated_at: now,
                    raw_data: Some(response_body),
                })
            }
            OAuth2Provider::Microsoft => {
                debug!("Microsoft user info response: {response_body:?}");
                let email = response_body["mail"]
                    .as_str()
                    .or_else(|| response_body["userPrincipalName"].as_str())
                    .map(|s| s.to_string());
                let name = response_body["displayName"].as_str().map(|s| s.to_string());
                let provider_user_id = response_body["id"]
                    .as_str()
                    .ok_or_else(|| AuthError::OAuthInvalidResponse("Missing user ID".to_string()))?
                    .to_string();

                Ok(OAuth2UserInfo {
                    user_id: String::new(), // Will be set when linking to authen user
                    provider,
                    provider_user_id,
                    email,
                    name,
                    avatar_url: None, // Microsoft Graph doesn't provide avatar URL directly
                    verified_email: None,
                    locale: None,
                    updated_at: now,
                    raw_data: Some(response_body),
                })
            }
        }
    }
}

#[async_trait]
impl OAuth2Service for OAuth2Manager {
    /// Generates the OAuth2 authorization URL for the specified provider.
    ///
    /// # Arguments
    ///
    /// * `provider` - The OAuth2 provider.
    /// * `state` - CSRF state parameter.
    /// * `scopes` - Optional additional scopes to request.
    ///
    /// # Returns
    ///
    /// Returns the authorization URL as a string, or [`AuthError`] on failure.
    async fn generate_auth_url(
        &self,
        provider: OAuth2Provider,
        state: &str,
        scopes: Option<Vec<String>>,
    ) -> Result<String, AuthError> {
        info!(
            "Generating auth URL for provider: {:?}, state: {}",
            provider, state
        );
        let client = self.get_client(provider)?;

        let config = self.configs.get(&provider).unwrap();
        // Dans ton code Rust, assure-toi de dédupliquer les scopes
        let mut all_scopes = provider
            .default_scopes()
            .into_iter()
            .map(|s| s.to_string())
            .collect::<Vec<_>>();

        if let Some(additional_scopes) = scopes {
            all_scopes.extend(additional_scopes);
        }
        all_scopes.extend(config.additional_scopes.clone());

        // Déduplication des scopes
        all_scopes.sort();
        all_scopes.dedup();

        debug!("Final scopes for auth URL: {all_scopes:?}");
        let mut auth_request = client.authorize_url(|| CsrfToken::new(state.to_string()));

        for scope in all_scopes {
            auth_request = auth_request.add_scope(Scope::new(scope));
        }

        let (auth_url, _csrf_token) = auth_request.url();
        info!("Generated auth URL: {}", auth_url);
        Ok(auth_url.to_string())
    }

    /// Exchanges an authorization code for an access token for the specified provider.
    ///
    /// # Arguments
    ///
    /// * `provider` - The OAuth2 provider.
    /// * `code` - The authorization code received from the provider.
    /// * `_state` - The CSRF state parameter (unused).
    ///
    /// # Returns
    ///
    /// Returns an [`OAuth2Token`] on success, or [`AuthError`] on failure.
    async fn exchange_code_for_token(
        &self,
        provider: OAuth2Provider,
        code: &str,
        _state: &str,
    ) -> Result<OAuth2Token, AuthError> {
        info!("Exchanging code for token for provider: {:?}", provider);
        debug!("Authorization code: {}", code);
        let client = self.get_client(provider)?;
        let http_client = self.get_http_client(provider)?;

        let token_result = client
            .exchange_code(AuthorizationCode::new(code.to_string()))
            .request_async(&http_client) // Utilise le client HTTP configuré avec le bon User-Agent
            .await
            .map_err(|e| {
                debug!("Token exchange failed for provider {provider:?}: {e}");
                debug!("Full error details: {e:?}");
                AuthError::OAuthTokenExchange(format!("Token exchange failed: {e}"))
            })?;

        let access_token = token_result.access_token().secret().clone();
        let refresh_token = token_result.refresh_token().map(|rt| rt.secret().clone());
        let expires_at = token_result.expires_in().map(|duration| {
            chrono::Utc::now().naive_utc()
                + chrono::Duration::from_std(duration).unwrap_or(chrono::Duration::seconds(0))
        });
        let token_type = token_result.token_type().as_ref().to_string();
        let scope = token_result.scopes().map(|scopes| {
            scopes
                .iter()
                .map(|s| s.to_string())
                .collect::<Vec<_>>()
                .join(" ")
        });

        info!("Token exchange successful for provider: {:?}", provider);
        debug!("Access token: {}", access_token);
        Ok(OAuth2Token {
            access_token,
            refresh_token,
            expires_at,
            token_type,
            scope,
            provider,
            created_at: chrono::Utc::now().naive_utc(),
        })
    }

    /// Fetches user information from the provider's user info endpoint using the access token.
    ///
    /// # Arguments
    ///
    /// * `token` - The [`OAuth2Token`] containing the access token and provider.
    ///
    /// # Returns
    ///
    /// Returns [`OAuth2UserInfo`] on success, or [`AuthError`] on failure.
    async fn fetch_user_info(&self, token: &OAuth2Token) -> Result<OAuth2UserInfo, AuthError> {
        info!("Fetching user info for provider: {:?}", token.provider);
        debug!("Access token: {}", token.access_token);
        let config = self.configs.get(&token.provider).ok_or_else(|| {
            debug!("No config found for provider: {:?}", token.provider);
            AuthError::ConfigError(format!(
                "No config found for provider: {:?}",
                token.provider
            ))
        })?;

        let user_info_url = config.user_info_url(token.provider);
        debug!("User info URL: {}", user_info_url);
        let http_client = self.get_http_client(token.provider)?;

        let response = http_client
            .get(user_info_url)
            .bearer_auth(&token.access_token)
            .header("Accept", "application/json")
            .send()
            .await
            .map_err(|e| {
                debug!(
                    "Failed to fetch user info for provider {:?}: {}",
                    token.provider, e
                );
                AuthError::OAuthNetwork(format!("Failed to fetch user info: {e}"))
            })?;

        if !response.status().is_success() {
            debug!(
                "User info request failed with status: {}",
                response.status()
            );
            return Err(AuthError::OAuthUserInfo(format!(
                "User info request failed with status: {}",
                response.status()
            )));
        }

        let response_body: Value = response.json().await.map_err(|e| {
            debug!(
                "Invalid JSON response for provider {:?}: {}",
                token.provider, e
            );
            AuthError::OAuthInvalidResponse(format!("Invalid JSON response: {e}"))
        })?;

        self.parse_user_info(token.provider, response_body).await
    }

    /// Refreshes the access token using the refresh token for the specified provider.
    ///
    /// # Arguments
    ///
    /// * `token` - The [`OAuth2Token`] containing the refresh token and provider.
    ///
    /// # Returns
    ///
    /// Returns a new [`OAuth2Token`] on success, or [`AuthError`] on failure.
    async fn refresh_token(&self, token: &OAuth2Token) -> Result<OAuth2Token, AuthError> {
        info!("Refreshing token for provider: {:?}", token.provider);
        debug!("Current refresh token: {:?}", token.refresh_token);
        let client = self.get_client(token.provider)?;
        let http_client = self.get_http_client(token.provider)?;

        let refresh_token = token.refresh_token.as_ref().ok_or_else(|| {
            debug!(
                "No refresh token available for provider: {:?}",
                token.provider
            );
            AuthError::OAuthTokenExchange("No refresh token available".to_string())
        })?;

        let token_result = client
            .exchange_refresh_token(&RefreshToken::new(refresh_token.clone()))
            .request_async(&http_client) // Utilise le client HTTP configuré avec le bon User-Agent
            .await
            .map_err(|e| {
                debug!(
                    "Token refresh failed for provider {:?}: {}",
                    token.provider, e
                );
                AuthError::OAuthTokenExchange(format!("Token refresh failed: {e}"))
            })?;

        let access_token = token_result.access_token().secret().clone();
        let new_refresh_token = token_result
            .refresh_token()
            .map(|rt| rt.secret().clone())
            .or_else(|| token.refresh_token.clone());
        let expires_at = token_result.expires_in().map(|duration| {
            chrono::Utc::now().naive_utc()
                + chrono::Duration::from_std(duration).unwrap_or(chrono::Duration::seconds(0))
        });
        let token_type = token_result.token_type().as_ref().to_string();
        let scope = token_result
            .scopes()
            .map(|scopes| {
                scopes
                    .iter()
                    .map(|s| s.to_string())
                    .collect::<Vec<_>>()
                    .join(" ")
            })
            .or_else(|| token.scope.clone());

        info!(
            "Token refresh successful for provider: {:?}",
            token.provider
        );
        debug!("New access token: {}", access_token);
        Ok(OAuth2Token {
            access_token,
            refresh_token: new_refresh_token,
            expires_at,
            token_type,
            scope,
            provider: token.provider,
            created_at: chrono::Utc::now().naive_utc(),
        })
    }

    async fn get_redirect_frontend_uri(
        &self,
        provider: OAuth2Provider,
    ) -> Result<String, AuthError> {
        self.get_redirect_frontend_uri(provider)
    }
}

impl OAuth2Manager {
    /// Gets the frontend redirect URI for the given provider.
    ///
    /// This URI is typically used to redirect users back to the frontend application after completing the OAuth2 flow.
    ///
    /// # Arguments
    /// * `provider` - The OAuth2 provider for which to retrieve the frontend redirect URI.
    ///
    /// # Returns
    /// Returns the frontend redirect URI as a string, or an [`AuthError`] if the provider configuration is missing.
    ///
    /// # Example
    /// ```rust
    /// let uri = manager.get_redirect_frontend_uri(OAuth2Provider::Discord)?;
    /// ```
    pub fn get_redirect_frontend_uri(&self, provider: OAuth2Provider) -> Result<String, AuthError> {
        let config = self.configs.get(&provider).ok_or_else(|| {
            AuthError::ConfigError(format!("No config found for provider: {provider:?}"))
        })?;
        Ok(config.redirect_frontend_uri.clone())
    }

    /// Sets the `user_id` field in [`OAuth2UserInfo`] to link it to a authen user.
    ///
    /// This is used to associate an external OAuth2 identity with an internal user account.
    ///
    /// # Arguments
    /// * `oauth_info` - The OAuth user info to update.
    /// * `user_id` - The authen user ID to link to.
    ///
    /// # Returns
    /// The updated [`OAuth2UserInfo`] with the `user_id` field set.
    ///
    /// # Example
    /// ```rust
    /// let linked_info = OAuth2Manager::link_to_user(oauth_info, user_id);
    /// ```
    pub fn link_to_user(mut oauth_info: OAuth2UserInfo, user_id: String) -> OAuth2UserInfo {
        info!("Linking OAuth2UserInfo to user_id: {user_id}");
        oauth_info.user_id = user_id;
        oauth_info
    }
}

/// Provides a default empty [`OAuth2Manager`] with no provider configurations.
///
/// This implementation is useful for testing or initializing the manager before loading provider configs.
///
/// # Example
/// ```rust
/// let manager = OAuth2Manager::default();
/// ```
impl Default for OAuth2Manager {
    fn default() -> Self {
        Self::new(HashMap::new())
    }
}