Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NTEX migration start #3076

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
914 changes: 386 additions & 528 deletions Cargo.lock

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions apps/labrinth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ name = "labrinth"
path = "src/main.rs"

[dependencies]
actix-web = "4.4.1"
actix-rt = "2.9.0"
actix-multipart = "0.6.1"
actix-cors = "0.7.0"
actix-ws = "0.3.0"
actix-files = "0.6.5"
actix-web-prom = { version = "0.8.0", features = ["process"] }
governor = "0.6.3"
ntex = { version = "2.0", features = ["tokio", "compress"] }
# actix-rt = "2.9.0"
ntex-multipart = "2.0.0"
ntex-cors = "2.0.0"
# actix-ws = "0.3.0"
ntex-files = "2.0.0"
# actix-web-prom = { version = "0.8.0", features = ["process"] }
governor = "0.8.0"

tokio = { version = "1.35.1", features = ["sync"] }
tokio-stream = "0.1.14"
Expand Down Expand Up @@ -97,15 +97,15 @@ maxminddb = "0.24.0"
flate2 = "1.0.25"
tar = "0.4.38"

sentry = { version = "0.34.0", default-features = false, features = [
"backtrace",
"contexts",
"debug-images",
"panic",
"rustls",
"reqwest",
] }
sentry-actix = "0.34.0"
#sentry = { version = "0.34.0", default-features = false, features = [
# "backtrace",
# "contexts",
# "debug-images",
# "panic",
# "rustls",
# "reqwest",
#] }
#sentry-actix = "0.34.0"

image = "0.24.6"
color-thief = "0.2.2"
Expand Down
10 changes: 5 additions & 5 deletions apps/labrinth/src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub use validate::{check_is_moderator_from_headers, get_user_from_headers};

use crate::file_hosting::FileHostingError;
use crate::models::error::ApiError;
use actix_web::http::StatusCode;
use actix_web::HttpResponse;
use ntex::http::StatusCode;
use ntex::web::{HttpRequest, HttpResponse};
use thiserror::Error;

#[derive(Error, Debug)]
Expand Down Expand Up @@ -51,7 +51,7 @@ pub enum AuthenticationError {
Url,
}

impl actix_web::ResponseError for AuthenticationError {
impl ntex::web::WebResponseError for AuthenticationError {
fn status_code(&self) -> StatusCode {
match self {
AuthenticationError::Env(..) => StatusCode::INTERNAL_SERVER_ERROR,
Expand All @@ -77,8 +77,8 @@ impl actix_web::ResponseError for AuthenticationError {
}
}

fn error_response(&self) -> HttpResponse {
HttpResponse::build(self.status_code()).json(ApiError {
fn error_response(&self, _req: &HttpRequest) -> HttpResponse {
HttpResponse::build(self.status_code()).json(&ApiError {
error: self.error_name(),
description: self.to_string(),
})
Expand Down
12 changes: 6 additions & 6 deletions apps/labrinth/src/auth/oauth/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use super::ValidatedRedirectUri;
use crate::auth::AuthenticationError;
use crate::models::error::ApiError;
use crate::models::ids::DecodingError;
use actix_web::http::{header::LOCATION, StatusCode};
use actix_web::HttpResponse;
use ntex::http::{header::LOCATION, StatusCode};
use ntex::web::{HttpRequest, HttpResponse};

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

impl actix_web::ResponseError for OAuthError {
impl ntex::web::WebResponseError for OAuthError {
fn status_code(&self) -> StatusCode {
match self.error_type {
OAuthErrorType::AuthenticationError(_)
Expand Down Expand Up @@ -83,7 +83,7 @@ impl actix_web::ResponseError for OAuthError {
}
}

fn error_response(&self) -> HttpResponse {
fn error_response(&self, _req: &HttpRequest) -> HttpResponse {
if let Some(ValidatedRedirectUri(mut redirect_uri)) =
self.valid_redirect_uri.clone()
{
Expand All @@ -99,10 +99,10 @@ impl actix_web::ResponseError for OAuthError {
}

HttpResponse::Ok()
.append_header((LOCATION, redirect_uri.clone()))
.header(LOCATION, redirect_uri.clone())
.body(redirect_uri)
} else {
HttpResponse::build(self.status_code()).json(ApiError {
HttpResponse::build(self.status_code()).json(&ApiError {
error: &self.error_type.error_name(),
description: self.error_type.to_string(),
})
Expand Down
66 changes: 33 additions & 33 deletions apps/labrinth/src/auth/oauth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ use crate::models;
use crate::models::ids::OAuthClientId;
use crate::models::pats::Scopes;
use crate::queue::session::AuthQueue;
use actix_web::http::header::LOCATION;
use actix_web::web::{Data, Query, ServiceConfig};
use actix_web::{get, post, web, HttpRequest, HttpResponse};
use chrono::Duration;
use ntex::http::header::LOCATION;
use ntex::http::header::{CACHE_CONTROL, PRAGMA};
use ntex::web::ServiceConfig;
use ntex::web::{self, get, post, HttpRequest, HttpResponse};
use rand::distributions::Alphanumeric;
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha20Rng;
use reqwest::header::{CACHE_CONTROL, PRAGMA};
use serde::{Deserialize, Serialize};
use sqlx::postgres::PgPool;

Expand Down Expand Up @@ -59,14 +59,14 @@ pub struct OAuthClientAccessRequest {
#[get("authorize")]
pub async fn init_oauth(
req: HttpRequest,
Query(oauth_info): Query<OAuthInit>,
pool: Data<PgPool>,
redis: Data<RedisPool>,
session_queue: Data<AuthQueue>,
web::types::Query(oauth_info): web::types::Query<OAuthInit>,
pool: web::types::State<PgPool>,
redis: web::types::State<RedisPool>,
session_queue: web::types::State<AuthQueue>,
) -> Result<HttpResponse, OAuthError> {
let user = get_user_from_headers(
&req,
&**pool,
&*pool,
&redis,
&session_queue,
Some(&[Scopes::USER_AUTH_WRITE]),
Expand All @@ -75,7 +75,7 @@ pub async fn init_oauth(
.1;

let client_id = oauth_info.client_id.into();
let client = DBOAuthClient::get(client_id, &**pool).await?;
let client = DBOAuthClient::get(client_id, &*pool).await?;

if let Some(client) = client {
let redirect_uri = ValidatedRedirectUri::validate(
Expand Down Expand Up @@ -107,7 +107,7 @@ pub async fn init_oauth(
}

let existing_authorization =
OAuthClientAuthorization::get(client.id, user.id.into(), &**pool)
OAuthClientAuthorization::get(client.id, user.id.into(), &*pool)
.await
.map_err(|e| {
OAuthError::redirect(e, &oauth_info.state, &redirect_uri)
Expand Down Expand Up @@ -154,7 +154,7 @@ pub async fn init_oauth(
flow_id,
requested_scopes,
};
Ok(HttpResponse::Ok().json(access_request))
Ok(HttpResponse::Ok().json(&access_request))
}
}
} else {
Expand All @@ -172,10 +172,10 @@ pub struct RespondToOAuthClientScopes {
#[post("accept")]
pub async fn accept_client_scopes(
req: HttpRequest,
accept_body: web::Json<RespondToOAuthClientScopes>,
pool: Data<PgPool>,
redis: Data<RedisPool>,
session_queue: Data<AuthQueue>,
accept_body: web::types::Json<RespondToOAuthClientScopes>,
pool: web::types::State<PgPool>,
redis: web::types::State<RedisPool>,
session_queue: web::types::State<AuthQueue>,
) -> Result<HttpResponse, OAuthError> {
accept_or_reject_client_scopes(
true,
Expand All @@ -191,10 +191,10 @@ pub async fn accept_client_scopes(
#[post("reject")]
pub async fn reject_client_scopes(
req: HttpRequest,
body: web::Json<RespondToOAuthClientScopes>,
pool: Data<PgPool>,
redis: Data<RedisPool>,
session_queue: Data<AuthQueue>,
body: web::types::Json<RespondToOAuthClientScopes>,
pool: web::types::State<PgPool>,
redis: web::types::State<RedisPool>,
session_queue: web::types::State<AuthQueue>,
) -> Result<HttpResponse, OAuthError> {
accept_or_reject_client_scopes(false, req, body, pool, redis, session_queue)
.await
Expand All @@ -221,12 +221,12 @@ pub struct TokenResponse {
/// Per IETF RFC6749 Section 4.1.3 (https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.3)
pub async fn request_token(
req: HttpRequest,
req_params: web::Form<TokenRequest>,
pool: Data<PgPool>,
redis: Data<RedisPool>,
req_params: web::types::Form<TokenRequest>,
pool: web::types::State<PgPool>,
redis: web::types::State<RedisPool>,
) -> Result<HttpResponse, OAuthError> {
let req_client_id = req_params.client_id;
let client = DBOAuthClient::get(req_client_id.into(), &**pool).await?;
let client = DBOAuthClient::get(req_client_id.into(), &*pool).await?;
if let Some(client) = client {
authenticate_client_token_request(&req, &client)?;

Expand Down Expand Up @@ -294,9 +294,9 @@ pub async fn request_token(

// IETF RFC6749 Section 5.1 (https://datatracker.ietf.org/doc/html/rfc6749#section-5.1)
Ok(HttpResponse::Ok()
.append_header((CACHE_CONTROL, "no-store"))
.append_header((PRAGMA, "no-cache"))
.json(TokenResponse {
.header(CACHE_CONTROL, "no-store")
.header(PRAGMA, "no-cache")
.json(&TokenResponse {
access_token: token,
token_type: "Bearer".to_string(),
expires_in: time_until_expiration.num_seconds(),
Expand All @@ -314,14 +314,14 @@ pub async fn request_token(
pub async fn accept_or_reject_client_scopes(
accept: bool,
req: HttpRequest,
body: web::Json<RespondToOAuthClientScopes>,
pool: Data<PgPool>,
redis: Data<RedisPool>,
session_queue: Data<AuthQueue>,
body: web::types::Json<RespondToOAuthClientScopes>,
pool: web::types::State<PgPool>,
redis: web::types::State<RedisPool>,
session_queue: web::types::State<AuthQueue>,
) -> Result<HttpResponse, OAuthError> {
let current_user = get_user_from_headers(
&req,
&**pool,
&*pool,
&redis,
&session_queue,
Some(&[Scopes::SESSION_ACCESS]),
Expand Down Expand Up @@ -449,7 +449,7 @@ async fn init_oauth_code_flow(

// IETF RFC 6749 Section 4.1.2 (https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2)
Ok(HttpResponse::Ok()
.append_header((LOCATION, redirect_uri.clone()))
.header(LOCATION, redirect_uri.clone())
.body(redirect_uri))
}

Expand Down
12 changes: 6 additions & 6 deletions apps/labrinth/src/auth/templates/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::auth::AuthenticationError;
use actix_web::http::StatusCode;
use actix_web::{HttpResponse, ResponseError};
use ntex::http::StatusCode;
use ntex::web::{HttpRequest, HttpResponse, WebResponseError};
use std::fmt::{Debug, Display, Formatter};

pub struct Success<'a> {
Expand All @@ -13,7 +13,7 @@ impl Success<'_> {
let html = include_str!("success.html");

HttpResponse::Ok()
.append_header(("Content-Type", "text/html; charset=utf-8"))
.header("Content-Type", "text/html; charset=utf-8")
.body(
html.replace("{{ icon }}", self.icon)
.replace("{{ name }}", self.name),
Expand Down Expand Up @@ -41,17 +41,17 @@ impl Display for ErrorPage {
impl ErrorPage {
pub fn render(&self) -> HttpResponse {
HttpResponse::Ok()
.append_header(("Content-Type", "text/html; charset=utf-8"))
.header("Content-Type", "text/html; charset=utf-8")
.body(self.to_string())
}
}

impl actix_web::ResponseError for ErrorPage {
impl WebResponseError for ErrorPage {
fn status_code(&self) -> StatusCode {
self.code
}

fn error_response(&self) -> HttpResponse {
fn error_response(&self, _req: &HttpRequest) -> HttpResponse {
self.render()
}
}
Expand Down
10 changes: 5 additions & 5 deletions apps/labrinth/src/auth/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use crate::models::pats::Scopes;
use crate::models::users::User;
use crate::queue::session::AuthQueue;
use crate::routes::internal::session::get_session_metadata;
use actix_web::http::header::{HeaderValue, AUTHORIZATION};
use actix_web::HttpRequest;
use chrono::Utc;
use ntex::http::header::{HeaderValue, AUTHORIZATION};
use ntex::web::HttpRequest;

pub async fn get_user_from_headers<'a, E>(
req: &HttpRequest,
Expand All @@ -18,7 +18,7 @@ pub async fn get_user_from_headers<'a, E>(
required_scopes: Option<&[Scopes]>,
) -> Result<(Scopes, User), AuthenticationError>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy,
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy + Send + Sync,
{
// Fetch DB user record and minos user from headers
let (scopes, db_user) = get_user_record_from_bearer_token(
Expand Down Expand Up @@ -52,7 +52,7 @@ pub async fn get_user_record_from_bearer_token<'a, 'b, E>(
session_queue: &AuthQueue,
) -> Result<Option<(Scopes, user_item::User)>, AuthenticationError>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy,
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy + Send + Sync,
{
let token = if let Some(token) = token {
token
Expand Down Expand Up @@ -174,7 +174,7 @@ pub async fn check_is_moderator_from_headers<'a, 'b, E>(
required_scopes: Option<&[Scopes]>,
) -> Result<User, AuthenticationError>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy,
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy + Send + Sync,
{
let user = get_user_from_headers(
req,
Expand Down
Loading
Loading