use serde::{Deserialize, Serialize}; use crate::error::SfAuthError; const VALIDATION_URL: &str = "https://snazzyfellas.com/api/redirect/validate"; /// Request payload for validation API #[derive(Debug, Serialize)] struct ValidationRequest { user_id: String, key: String, } /// Response from validation API #[derive(Debug, Deserialize)] struct ValidationResponse { valid: bool, user_id: String, } /// Validates user credentials with the SF authentication server. /// /// Makes a POST request to the validation endpoint with the user_id and key. /// Returns `Ok(user_id)` if validation succeeds, or an error otherwise. /// /// # Arguments /// /// * `user_id` - The user ID to validate /// * `key` - The authentication key to validate /// /// # Errors /// /// Returns an error if: /// - The HTTP request fails /// - The validation response indicates invalid credentials /// - The returned user_id doesn't match the requested user_id pub(crate) async fn validate_user(user_id: String, key: String) -> Result { let client = reqwest::Client::new(); let request_payload = ValidationRequest { user_id: user_id.clone(), key, }; let response = client .post(VALIDATION_URL) .json(&request_payload) .send() .await?; let validation_response: ValidationResponse = response.json().await?; // Check if validation succeeded if !validation_response.valid { return Err(SfAuthError::ValidationFailed); } // Verify that the returned user_id matches what we sent if validation_response.user_id != user_id { return Err(SfAuthError::UserIdMismatch); } Ok(validation_response.user_id) }