Skip to content

Commit ddd52fd

Browse files
committed
Refactor unauthorized response
Use ErrorWithStatus for unauthorized errors to set the WWW-Authenticate header.
1 parent b1184b9 commit ddd52fd

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

src/render.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
use crate::templates::SplitTemplate;
4545
use crate::webserver::http::RequestContext;
4646
use crate::webserver::response_writer::{AsyncResponseWriter, ResponseWriter};
47+
use crate::webserver::ErrorWithStatus;
4748
use crate::AppState;
4849
use actix_web::cookie::time::format_description::well_known::Rfc3339;
4950
use actix_web::cookie::time::OffsetDateTime;
50-
use actix_web::http::header::ContentType;
5151
use actix_web::http::{header, StatusCode};
5252
use actix_web::{HttpResponse, HttpResponseBuilder};
5353
use anyhow::{bail, format_err, Context as AnyhowContext};
@@ -323,15 +323,9 @@ impl HeaderContext {
323323
Redirecting to the login page...",
324324
)
325325
} else {
326-
let mut resp_builder = actix_web::HttpResponse::build(StatusCode::UNAUTHORIZED);
327-
resp_builder.content_type(ContentType::plaintext());
328-
resp_builder.insert_header((
329-
header::WWW_AUTHENTICATE,
330-
header::HeaderValue::from_static(
331-
"Basic realm=\"Authentication required\", charset=\"UTF-8\"",
332-
),
333-
));
334-
resp_builder.body("Sorry, but you are not authorized to access this page.")
326+
anyhow::bail!(ErrorWithStatus {
327+
status: StatusCode::UNAUTHORIZED
328+
})
335329
};
336330
self.has_status = true;
337331
Ok(PageContext::Close(http_response))

src/webserver/error.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use crate::webserver::ErrorWithStatus;
77
use crate::AppState;
88
use actix_web::error::UrlencodedError;
99
use actix_web::http::{header, StatusCode};
10-
use actix_web::{HttpRequest, HttpResponse};
1110
use actix_web::HttpResponseBuilder;
11+
use actix_web::{HttpRequest, HttpResponse};
1212
use handlebars::{Renderable, StringOutput};
1313
use serde_json::json;
1414

@@ -58,6 +58,12 @@ fn anyhow_err_to_actix_resp(e: &anyhow::Error, state: &AppState) -> HttpResponse
5858

5959
if let Some(&ErrorWithStatus { status }) = e.downcast_ref() {
6060
resp.status(status);
61+
if status == StatusCode::UNAUTHORIZED {
62+
resp.append_header((
63+
header::WWW_AUTHENTICATE,
64+
"Basic realm=\"Authentication required\", charset=\"UTF-8\"",
65+
));
66+
}
6167
} else if let Some(sqlx::Error::PoolTimedOut) = e.downcast_ref() {
6268
use rand::Rng;
6369
resp.status(StatusCode::TOO_MANY_REQUESTS).insert_header((

tests/errors/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use actix_web::{
44
};
55

66
use crate::common::req_path;
7+
mod basic_auth;
78

89
#[actix_web::test]
910
async fn test_privileged_paths_are_not_accessible() {

0 commit comments

Comments
 (0)