You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
‼️IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
use tokio::time::{interval, Duration};
use std::sync::Arc;
use tokio::sync::RwLock;
use std::collections::HashMap;
use chrono::Utc;
// Assume necessary imports and other parts of the module are present.
pub struct OAuthState {
pub csrf_token: String,
pub expires_at: chrono::DateTime<chrono::Utc>,
// other fields...
}
pub struct GithubProvider {
auth_url: String,
token_url: String,
user_info_url: String,
state_store: Arc<RwLock<HashMap<String, OAuthState>>>,
// other fields...
}
impl GithubProvider {
pub async fn new(db: Arc<dyn DBConnection + Send + Sync>) -> Result<Self, Error> {
let provider = Self {
auth_url: "https://example.com/auth".to_string(), // placeholder
token_url: "https://example.com/token".to_string(), // placeholder
user_info_url: "https://example.com/user".to_string(), // placeholder
state_store: Arc::new(RwLock::new(HashMap::new())),
};
// Start cleanup task
let state_store = provider.state_store.clone();
tokio::spawn(async move {
let mut interval = interval(Duration::from_secs(3600)); // Run every hour
loop {
interval.tick().await;
cleanup_expired_states(&state_store).await;
}
});
Ok(provider)
}
pub async fn store_state(&self, csrf_token: &str, state: OAuthState) {
self.state_store
.write()
.await
.insert(csrf_token.to_string(), state);
}
pub async fn validate_state(&self, state: &OAuthState) -> bool {
if let Some(stored_state) = self.state_store.read().await.get(&state.csrf_token) {
// Validate both the CSRF token and the client_id match
stored_state == state
} else {
false
}
}
}
async fn cleanup_expired_states(state_store: &Arc<RwLock<HashMap<String, OAuthState>>>) {
let now = Utc::now();
state_store.write().await.retain(|_, state| state.expires_at > now);
}
Implement state store cleanup.
The in-memory state store could grow indefinitely as there's no cleanup of old states. This could lead to memory leaks.
Add a cleanup mechanism:
📝 Committable suggestion
Originally posted by @coderabbitai[bot] in #3 (comment)
The text was updated successfully, but these errors were encountered: