mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 07:02:24 +00:00
frontend: Add redirect to login page for unauthorized requests
This commit is contained in:
@@ -3,9 +3,11 @@ use actix_session::{
|
|||||||
};
|
};
|
||||||
use actix_web::{
|
use actix_web::{
|
||||||
cookie::{Key, SameSite},
|
cookie::{Key, SameSite},
|
||||||
http::Method,
|
dev::ServiceResponse,
|
||||||
web::{self, Data, Path},
|
http::{Method, StatusCode},
|
||||||
Responder,
|
middleware::{ErrorHandlerResponse, ErrorHandlers},
|
||||||
|
web::{self, Data, Path, Redirect},
|
||||||
|
HttpResponse, Responder,
|
||||||
};
|
};
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
use assets::{Assets, EmbedService};
|
use assets::{Assets, EmbedService};
|
||||||
@@ -60,6 +62,31 @@ async fn route_calendar<C: CalendarStore + ?Sized>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn unauthorized_handler<B>(res: ServiceResponse<B>) -> actix_web::Result<ErrorHandlerResponse<B>> {
|
||||||
|
let (req, _) = res.into_parts();
|
||||||
|
let login_url = req.url_for_static("frontend_login").unwrap().to_string();
|
||||||
|
|
||||||
|
// let response = Redirect::to(login_url).respond_to(&req);
|
||||||
|
let response = HttpResponse::Unauthorized().body(format!(
|
||||||
|
r#"<!Doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="refresh" content="2; url={login_url}" />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
Unauthorized, redirecting to <a href="{login_url}">login page</a>
|
||||||
|
</body>
|
||||||
|
<html>
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
let res = ServiceResponse::new(req, response)
|
||||||
|
.map_into_boxed_body()
|
||||||
|
.map_into_right_body();
|
||||||
|
|
||||||
|
Ok(ErrorHandlerResponse::Response(res))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn configure_frontend<AP: AuthenticationProvider, C: CalendarStore + ?Sized>(
|
pub fn configure_frontend<AP: AuthenticationProvider, C: CalendarStore + ?Sized>(
|
||||||
cfg: &mut web::ServiceConfig,
|
cfg: &mut web::ServiceConfig,
|
||||||
auth_provider: Arc<AP>,
|
auth_provider: Arc<AP>,
|
||||||
@@ -68,6 +95,7 @@ pub fn configure_frontend<AP: AuthenticationProvider, C: CalendarStore + ?Sized>
|
|||||||
) {
|
) {
|
||||||
cfg.service(
|
cfg.service(
|
||||||
web::scope("")
|
web::scope("")
|
||||||
|
.wrap(ErrorHandlers::new().handler(StatusCode::UNAUTHORIZED, unauthorized_handler))
|
||||||
.wrap(AuthenticationMiddleware::new(auth_provider.clone()))
|
.wrap(AuthenticationMiddleware::new(auth_provider.clone()))
|
||||||
.wrap(
|
.wrap(
|
||||||
SessionMiddleware::builder(
|
SessionMiddleware::builder(
|
||||||
@@ -91,6 +119,7 @@ pub fn configure_frontend<AP: AuthenticationProvider, C: CalendarStore + ?Sized>
|
|||||||
)
|
)
|
||||||
.service(
|
.service(
|
||||||
web::resource("/login")
|
web::resource("/login")
|
||||||
|
.name("frontend_login")
|
||||||
.route(web::method(Method::GET).to(route_get_login))
|
.route(web::method(Method::GET).to(route_get_login))
|
||||||
.route(web::method(Method::POST).to(route_post_login::<AP>)),
|
.route(web::method(Method::POST).to(route_post_login::<AP>)),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
use actix_web::{
|
use actix_web::{
|
||||||
|
body::BoxBody,
|
||||||
http::{header, StatusCode},
|
http::{header, StatusCode},
|
||||||
FromRequest, HttpMessage, HttpResponse, ResponseError,
|
FromRequest, HttpMessage, HttpResponse, ResponseError,
|
||||||
};
|
};
|
||||||
use derive_more::Display;
|
use derive_more::{derive::Deref, Display};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::future::{ready, Ready};
|
use std::future::{ready, Ready};
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ impl ResponseError for UnauthorizedError {
|
|||||||
fn status_code(&self) -> actix_web::http::StatusCode {
|
fn status_code(&self) -> actix_web::http::StatusCode {
|
||||||
StatusCode::UNAUTHORIZED
|
StatusCode::UNAUTHORIZED
|
||||||
}
|
}
|
||||||
fn error_response(&self) -> actix_web::HttpResponse<actix_web::body::BoxBody> {
|
fn error_response(&self) -> HttpResponse<BoxBody> {
|
||||||
HttpResponse::build(StatusCode::UNAUTHORIZED)
|
HttpResponse::build(StatusCode::UNAUTHORIZED)
|
||||||
.insert_header((
|
.insert_header((
|
||||||
header::WWW_AUTHENTICATE,
|
header::WWW_AUTHENTICATE,
|
||||||
@@ -35,7 +36,6 @@ impl ResponseError for UnauthorizedError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl FromRequest for User {
|
impl FromRequest for User {
|
||||||
// type Error = actix_web::Error;
|
|
||||||
type Error = UnauthorizedError;
|
type Error = UnauthorizedError;
|
||||||
type Future = Ready<Result<Self, Self::Error>>;
|
type Future = Ready<Result<Self, Self::Error>>;
|
||||||
|
|
||||||
@@ -47,7 +47,6 @@ impl FromRequest for User {
|
|||||||
req.extensions()
|
req.extensions()
|
||||||
.get::<Self>()
|
.get::<Self>()
|
||||||
.cloned()
|
.cloned()
|
||||||
// .ok_or(ErrorUnauthorized("Not authenticated")),
|
|
||||||
.ok_or(UnauthorizedError),
|
.ok_or(UnauthorizedError),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user