diff --git a/src/auth/oauth/mod.rs b/src/auth/oauth/mod.rs index 4b989458..d5978186 100644 --- a/src/auth/oauth/mod.rs +++ b/src/auth/oauth/mod.rs @@ -364,7 +364,8 @@ fn authenticate_client_token_request( req: &HttpRequest, client: &DBOAuthClient, ) -> Result<(), OAuthError> { - let client_secret = extract_authorization_header(req)?; + let header = extract_authorization_header(req)?; + let client_secret = header.as_str(); let hashed_client_secret = DBOAuthClient::hash_secret(client_secret); if client.secret_hash != hashed_client_secret { Err(OAuthError::error( diff --git a/src/auth/validate.rs b/src/auth/validate.rs index 4bfd404b..24d16feb 100644 --- a/src/auth/validate.rs +++ b/src/auth/validate.rs @@ -7,6 +7,7 @@ use crate::models::users::{Role, User, UserId, UserPayoutData}; use crate::queue::session::AuthQueue; use crate::routes::internal::session::get_session_metadata; use actix_web::HttpRequest; +use base64::Engine; use chrono::Utc; use reqwest::header::{HeaderValue, AUTHORIZATION}; @@ -92,11 +93,12 @@ pub async fn get_user_record_from_bearer_token<'a, 'b, E>( where E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy, { - let token = if let Some(token) = token { - token - } else { - extract_authorization_header(req)? - }; + // This is silly, but the compiler kept complaining and this is the only way this would work + let mut temp: String = String::new(); + if token.is_none() { + temp = extract_authorization_header(req)?; + } + let token = token.unwrap_or(&temp); let possible_user = match token.split_once('_') { Some(("mrp", _)) => { @@ -178,13 +180,33 @@ where Ok(possible_user) } -pub fn extract_authorization_header(req: &HttpRequest) -> Result<&str, AuthenticationError> { +pub fn extract_authorization_header(req: &HttpRequest) -> Result { let headers = req.headers(); let token_val: Option<&HeaderValue> = headers.get(AUTHORIZATION); - token_val + let val = token_val .ok_or_else(|| AuthenticationError::InvalidAuthMethod)? .to_str() - .map_err(|_| AuthenticationError::InvalidCredentials) + .map_err(|_| AuthenticationError::InvalidCredentials)?; + + return match val.split_once(' ') { + Some(("Bearer", token)) => Ok(token.trim().to_string()), + Some(("Basic", token)) => { + let decoded = base64::engine::general_purpose::STANDARD + .decode(token.trim()) + .map_err(|_| AuthenticationError::InvalidCredentials)?; + + let credentials: String = + String::from_utf8(decoded).map_err(|_| AuthenticationError::InvalidCredentials)?; + + Ok(credentials + .split_once(':') + .ok_or_else(|| AuthenticationError::InvalidCredentials)? + .1 + .trim() + .to_string()) + } + _ => Ok(val.trim().to_string()), + }; } pub async fn check_is_moderator_from_headers<'a, 'b, E>( diff --git a/src/routes/v3/mod.rs b/src/routes/v3/mod.rs index a5165fec..ae1bb6b5 100644 --- a/src/routes/v3/mod.rs +++ b/src/routes/v3/mod.rs @@ -1,7 +1,6 @@ pub use super::ApiError; use crate::util::cors::default_cors; -use actix_web::{web, HttpResponse}; -use serde_json::json; +use actix_web::web; pub mod analytics_get; pub mod collections; @@ -47,9 +46,3 @@ pub fn config(cfg: &mut web::ServiceConfig) { .configure(versions::config), ); } - -pub async fn hello_world() -> Result { - Ok(HttpResponse::Ok().json(json!({ - "hello": "world", - }))) -} diff --git a/src/search/indexing/mod.rs b/src/search/indexing/mod.rs index 05919a1b..35b7f72c 100644 --- a/src/search/indexing/mod.rs +++ b/src/search/indexing/mod.rs @@ -115,14 +115,19 @@ pub async fn get_indexes_for_indexing( let client = config.make_client(); let project_name = config.get_index_name("projects", next); let project_filtered_name = config.get_index_name("projects_filtered", next); - let projects_index = create_or_update_index(&client, &project_name, Some(&[ - "words", - "typo", - "proximity", - "attribute", - "exactness", - "sort", - ]),).await?; + let projects_index = create_or_update_index( + &client, + &project_name, + Some(&[ + "words", + "typo", + "proximity", + "attribute", + "exactness", + "sort", + ]), + ) + .await?; let projects_filtered_index = create_or_update_index( &client, &project_filtered_name,