Compare commits
No commits in common. "eb7a3cf985fbcbbb962cd828df0d4f9b9e4d79ef" and "0d6810f3d52e5159c37b788ad0f1300701185ca9" have entirely different histories.
eb7a3cf985
...
0d6810f3d5
1
backend/Cargo.lock
generated
1
backend/Cargo.lock
generated
@ -1664,7 +1664,6 @@ dependencies = [
|
|||||||
"actix-multipart",
|
"actix-multipart",
|
||||||
"actix-web",
|
"actix-web",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"auth",
|
|
||||||
"futures",
|
"futures",
|
||||||
"log",
|
"log",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
pub mod auth_middleware;
|
|
||||||
pub mod auth_web_routes;
|
pub mod auth_web_routes;
|
||||||
|
|
||||||
mod constants;
|
mod constants;
|
||||||
|
@ -1,55 +0,0 @@
|
|||||||
use std::future::{self, Ready};
|
|
||||||
|
|
||||||
use actix_session::SessionExt;
|
|
||||||
use actix_web::{
|
|
||||||
Error, FromRequest, HttpMessage, HttpRequest,
|
|
||||||
body::MessageBody,
|
|
||||||
dev::{Payload, ServiceRequest, ServiceResponse},
|
|
||||||
error::ErrorUnauthorized,
|
|
||||||
middleware::Next,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::framework::web::constants::SESSION_KEY_USER_ID;
|
|
||||||
|
|
||||||
pub async fn auth_middleware(
|
|
||||||
req: ServiceRequest,
|
|
||||||
next: Next<impl MessageBody>,
|
|
||||||
) -> Result<ServiceResponse<impl MessageBody>, Error> {
|
|
||||||
let session = req.get_session();
|
|
||||||
let user_id = session.get::<i32>(SESSION_KEY_USER_ID);
|
|
||||||
|
|
||||||
if user_id.is_err() {
|
|
||||||
return next.call(req).await;
|
|
||||||
}
|
|
||||||
|
|
||||||
let user_id = user_id.unwrap();
|
|
||||||
if user_id.is_none() {
|
|
||||||
return next.call(req).await;
|
|
||||||
}
|
|
||||||
|
|
||||||
let user_id = user_id.unwrap();
|
|
||||||
req.extensions_mut().insert(user_id);
|
|
||||||
next.call(req).await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct UserId(i32);
|
|
||||||
|
|
||||||
impl UserId {
|
|
||||||
pub fn get(&self) -> i32 {
|
|
||||||
self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromRequest for UserId {
|
|
||||||
type Error = Error;
|
|
||||||
type Future = Ready<Result<Self, Self::Error>>;
|
|
||||||
|
|
||||||
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
|
||||||
let user_id = req.extensions().get::<i32>().cloned();
|
|
||||||
|
|
||||||
match user_id {
|
|
||||||
Some(id) => future::ready(Ok(UserId(id))),
|
|
||||||
None => future::ready(Err(ErrorUnauthorized(""))),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -6,9 +6,8 @@ use crate::{
|
|||||||
auth_controller::AuthController, oidc_callback_query_dto::OidcCallbackQueryDto,
|
auth_controller::AuthController, oidc_callback_query_dto::OidcCallbackQueryDto,
|
||||||
},
|
},
|
||||||
application::error::auth_error::AuthError,
|
application::error::auth_error::AuthError,
|
||||||
framework::web::{
|
framework::web::constants::{
|
||||||
auth_middleware::UserId,
|
SESSION_KEY_AUTH_NONCE, SESSION_KEY_AUTH_STATE, SESSION_KEY_USER_ID,
|
||||||
constants::{SESSION_KEY_AUTH_NONCE, SESSION_KEY_AUTH_STATE, SESSION_KEY_USER_ID},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -19,8 +18,6 @@ pub fn configure_auth_routes(cfg: &mut web::ServiceConfig) {
|
|||||||
.route("/callback", web::get().to(oidc_callback_handler))
|
.route("/callback", web::get().to(oidc_callback_handler))
|
||||||
.route("/logout", web::get().to(logout_handler)),
|
.route("/logout", web::get().to(logout_handler)),
|
||||||
);
|
);
|
||||||
|
|
||||||
cfg.service(web::resource("/me").route(web::get().to(get_logged_in_user_handler)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn oidc_login_handler(
|
async fn oidc_login_handler(
|
||||||
@ -95,12 +92,10 @@ async fn oidc_callback_handler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn logout_handler(session: Session) -> impl Responder {
|
async fn logout_handler(session: Session) -> impl Responder {
|
||||||
session.clear();
|
session.remove(SESSION_KEY_AUTH_STATE);
|
||||||
|
session.remove(SESSION_KEY_AUTH_NONCE);
|
||||||
|
session.remove(SESSION_KEY_USER_ID);
|
||||||
HttpResponse::Found()
|
HttpResponse::Found()
|
||||||
.append_header((header::LOCATION, "/"))
|
.append_header((header::LOCATION, "/"))
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_logged_in_user_handler(user_id: UserId) -> impl Responder {
|
|
||||||
HttpResponse::Ok().body(format!("Logged in user ID: {}", user_id.get()))
|
|
||||||
}
|
|
||||||
|
@ -11,5 +11,3 @@ futures.workspace = true
|
|||||||
log.workspace = true
|
log.workspace = true
|
||||||
serde.workspace = true
|
serde.workspace = true
|
||||||
sqlx.workspace = true
|
sqlx.workspace = true
|
||||||
|
|
||||||
auth.workspace = true
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use actix_multipart::Multipart;
|
use actix_multipart::Multipart;
|
||||||
use actix_web::{HttpResponse, Responder, web};
|
use actix_web::{HttpResponse, Responder, web};
|
||||||
use auth::framework::web::auth_middleware::UserId;
|
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -19,7 +18,6 @@ pub fn configure_image_routes(cfg: &mut web::ServiceConfig) {
|
|||||||
async fn upload_image_handler(
|
async fn upload_image_handler(
|
||||||
image_controller: web::Data<dyn ImageController>,
|
image_controller: web::Data<dyn ImageController>,
|
||||||
mut payload: Multipart,
|
mut payload: Multipart,
|
||||||
_: UserId,
|
|
||||||
) -> impl Responder {
|
) -> impl Responder {
|
||||||
let mut image_request_dto: Option<ImageRequestDto> = None;
|
let mut image_request_dto: Option<ImageRequestDto> = None;
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ impl SessionConfiguration {
|
|||||||
let session_key = Key::from(&session_key_bytes);
|
let session_key = Key::from(&session_key_bytes);
|
||||||
|
|
||||||
let redis_url =
|
let redis_url =
|
||||||
std::env::var("REDIS_URL").unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string());
|
std::env::var("REDIS_URL").unwrap_or_else(|_| "redis://127.0.1:6379".to_string());
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
session_key,
|
session_key,
|
||||||
|
@ -5,12 +5,9 @@ use actix_web::{
|
|||||||
App, Error, HttpServer,
|
App, Error, HttpServer,
|
||||||
body::MessageBody,
|
body::MessageBody,
|
||||||
dev::{ServiceFactory, ServiceRequest, ServiceResponse},
|
dev::{ServiceFactory, ServiceRequest, ServiceResponse},
|
||||||
middleware::from_fn,
|
|
||||||
web,
|
web,
|
||||||
};
|
};
|
||||||
use auth::framework::web::{
|
use auth::framework::web::auth_web_routes::configure_auth_routes;
|
||||||
auth_middleware::auth_middleware, auth_web_routes::configure_auth_routes,
|
|
||||||
};
|
|
||||||
use image::framework::web::image_web_routes::configure_image_routes;
|
use image::framework::web::image_web_routes::configure_image_routes;
|
||||||
use openidconnect::reqwest;
|
use openidconnect::reqwest;
|
||||||
use post::framework::web::post_web_routes::configure_post_routes;
|
use post::framework::web::post_web_routes::configure_post_routes;
|
||||||
@ -66,8 +63,6 @@ fn create_app(
|
|||||||
let container = Container::new(db_pool, http_client, configuration);
|
let container = Container::new(db_pool, http_client, configuration);
|
||||||
|
|
||||||
App::new()
|
App::new()
|
||||||
// The middlewares are executed in opposite order as registration.
|
|
||||||
.wrap(from_fn(auth_middleware))
|
|
||||||
.wrap(session_middleware_builder.build())
|
.wrap(session_middleware_builder.build())
|
||||||
.app_data(web::Data::from(container.auth_controller))
|
.app_data(web::Data::from(container.auth_controller))
|
||||||
.app_data(web::Data::from(container.image_controller))
|
.app_data(web::Data::from(container.image_controller))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user