Skip to content

Commit

Permalink
Finish most
Browse files Browse the repository at this point in the history
  • Loading branch information
Geometrically committed Jan 16, 2024
1 parent a449f7d commit cd6c7b9
Show file tree
Hide file tree
Showing 65 changed files with 2,848 additions and 2,915 deletions.
39 changes: 39 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ path = "src/main.rs"
# actix-ws = "0.2.5"
# actix-files = "0.6.5"
# actix-web-prom = "0.7.0"
axum = { version = "0.7.4", features = ["json", "form", "multipart", "query", "ws"] }
axum = { version = "0.7.4", features = ["json", "form", "multipart", "query", "ws", "macros"] }
axum-prometheus = "0.5.0"

# middleware + utils
tower-http = { version = "0.5.0", features = ["cors", "compression-br", "sensitive-headers"] }
sentry = { version = "0.32.1", features = ["tracing"] }
sentry-tower = "0.32.1"
tower-http = { version = "0.5.0", features = ["cors", "compression-br", "sensitive-headers", "fs"] }
tower = "0.4.13"

# tracing
tracing = "0.1.40"
Expand Down Expand Up @@ -104,9 +107,6 @@ maxminddb = "0.24.0"
flate2 = "1.0.25"
tar = "0.4.38"

sentry = { version = "0.32.1" }
# sentry-actix = "0.32.1"

image = "0.24.6"
color-thief = "0.2.2"

Expand Down
6 changes: 3 additions & 3 deletions src/auth/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub async fn filter_enlisted_projects_ids(
.collect::<Vec<_>>(),
user_id as database::models::ids::UserId,
)
.fetch_many(&***pool)
.fetch_many(pool)
.try_for_each(|e| {
if let Some(row) = e.right() {
for x in projects.iter() {
Expand Down Expand Up @@ -225,7 +225,7 @@ pub async fn filter_visible_version_ids(

// Get visible projects- ones we are allowed to see public versions for.
let visible_project_ids = filter_visible_project_ids(
Project::get_many_ids(&project_ids, &***pool, redis)
Project::get_many_ids(&project_ids, pool, redis)
.await?
.iter()
.map(|x| &x.inner)
Expand Down Expand Up @@ -274,7 +274,7 @@ pub async fn filter_enlisted_version_ids(

// Get enlisted projects- ones we are allowed to see hidden versions for.
let authorized_project_ids = filter_enlisted_projects_ids(
Project::get_many_ids(&project_ids, &***pool, redis)
Project::get_many_ids(&project_ids, pool, redis)
.await?
.iter()
.map(|x| &x.inner)
Expand Down
27 changes: 14 additions & 13 deletions src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub mod templates;
pub mod validate;

use axum::http::StatusCode;
use axum::Json;
use axum::response::{IntoResponse, Response};
use axum::Json;
pub use checks::{
filter_enlisted_projects_ids, filter_enlisted_version_ids, filter_visible_collections,
filter_visible_project_ids, filter_visible_projects,
Expand Down Expand Up @@ -51,10 +51,20 @@ pub enum AuthenticationError {
Url,
}


impl IntoResponse for AuthenticationError {
fn into_response(self) -> Response {
let status_code = match &self {
let error_message = ApiError {
error: self.error_name(),
description: &*self.to_string(),
};

(self.status_code(), Json(error_message)).into_response()
}
}

impl AuthenticationError {
pub fn status_code(&self) -> StatusCode {
match self {
AuthenticationError::Env(..) => StatusCode::INTERNAL_SERVER_ERROR,
AuthenticationError::Sqlx(..) => StatusCode::INTERNAL_SERVER_ERROR,
AuthenticationError::Database(..) => StatusCode::INTERNAL_SERVER_ERROR,
Expand All @@ -69,18 +79,9 @@ impl IntoResponse for AuthenticationError {
AuthenticationError::FileHosting(..) => StatusCode::INTERNAL_SERVER_ERROR,
AuthenticationError::DuplicateUser => StatusCode::BAD_REQUEST,
AuthenticationError::SocketError => StatusCode::BAD_REQUEST,
};

let error_message = ApiError {
error: self.error_name(),
description: &*self.to_string(),
};

(status_code, Json(error_message)).into_response()
}
}
}

impl AuthenticationError {
pub fn error_name(&self) -> &'static str {
match self {
AuthenticationError::Env(..) => "environment_error",
Expand Down
77 changes: 43 additions & 34 deletions src/auth/oauth/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use super::ValidatedRedirectUri;
use crate::auth::AuthenticationError;
use crate::models::error::ApiError;
use crate::models::ids::DecodingError;
use axum::http::header::LOCATION;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::Json;

#[derive(thiserror::Error, Debug)]
#[error("{}", .error_type)]
Expand Down Expand Up @@ -53,33 +57,8 @@ impl OAuthError {
}
}

impl actix_web::ResponseError for OAuthError {
fn status_code(&self) -> StatusCode {
match self.error_type {
OAuthErrorType::AuthenticationError(_)
| OAuthErrorType::FailedScopeParse(_)
| OAuthErrorType::ScopesTooBroad
| OAuthErrorType::AccessDenied => {
if self.valid_redirect_uri.is_some() {
StatusCode::OK
} else {
StatusCode::INTERNAL_SERVER_ERROR
}
}
OAuthErrorType::RedirectUriNotConfigured(_)
| OAuthErrorType::ClientMissingRedirectURI { client_id: _ }
| OAuthErrorType::InvalidAcceptFlowId
| OAuthErrorType::MalformedId(_)
| OAuthErrorType::InvalidClientId(_)
| OAuthErrorType::InvalidAuthCode
| OAuthErrorType::OnlySupportsAuthorizationCodeGrant(_)
| OAuthErrorType::RedirectUriChanged(_)
| OAuthErrorType::UnauthorizedClient => StatusCode::BAD_REQUEST,
OAuthErrorType::ClientAuthenticationFailed => StatusCode::UNAUTHORIZED,
}
}

fn error_response(&self) -> HttpResponse {
impl IntoResponse for OAuthError {
fn into_response(self) -> Response {
if let Some(ValidatedRedirectUri(mut redirect_uri)) = self.valid_redirect_uri.clone() {
redirect_uri = format!(
"{}?error={}&error_description={}",
Expand All @@ -92,14 +71,44 @@ impl actix_web::ResponseError for OAuthError {
redirect_uri = format!("{}&state={}", redirect_uri, state);
}

HttpResponse::Ok()
.append_header((LOCATION, redirect_uri.clone()))
.body(redirect_uri)
(
StatusCode::TEMPORARY_REDIRECT,
[(LOCATION, redirect_uri.clone())],
redirect_uri,
)
.into_response()
} else {
HttpResponse::build(self.status_code()).json(ApiError {
error: &self.error_type.error_name(),
description: &self.error_type.to_string(),
})
let status_code = match self.error_type {
OAuthErrorType::AuthenticationError(_)
| OAuthErrorType::FailedScopeParse(_)
| OAuthErrorType::ScopesTooBroad
| OAuthErrorType::AccessDenied => {
if self.valid_redirect_uri.is_some() {
StatusCode::OK
} else {
StatusCode::INTERNAL_SERVER_ERROR
}
}
OAuthErrorType::RedirectUriNotConfigured(_)
| OAuthErrorType::ClientMissingRedirectURI { client_id: _ }
| OAuthErrorType::InvalidAcceptFlowId
| OAuthErrorType::MalformedId(_)
| OAuthErrorType::InvalidClientId(_)
| OAuthErrorType::InvalidAuthCode
| OAuthErrorType::OnlySupportsAuthorizationCodeGrant(_)
| OAuthErrorType::RedirectUriChanged(_)
| OAuthErrorType::UnauthorizedClient => StatusCode::BAD_REQUEST,
OAuthErrorType::ClientAuthenticationFailed => StatusCode::UNAUTHORIZED,
};

(
status_code,
Json(ApiError {
error: &self.error_type.error_name(),
description: &self.error_type.to_string(),
}),
)
.into_response()
}
}
}
Expand Down
Loading

0 comments on commit cd6c7b9

Please sign in to comment.