Skip to content

Commit

Permalink
fix: always send www-authenticate header with 401
Browse files Browse the repository at this point in the history
Signed-off-by: Thorsten Hans <[email protected]>
  • Loading branch information
ThorstenHans committed Jan 15, 2025
1 parent 7acc462 commit fa1880c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
3 changes: 1 addition & 2 deletions src/bartholomew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ fn has_credentials() -> bool {
pub fn render(req: Request) -> Result<Response> {
if has_credentials() {
match is_authenticated(&req) {
Ok(false) => return response::send_unauthorized(false),
Ok(true) => (),
_ => return response::send_unauthorized(true),
_ => return response::send_unauthorized(),
}
}
// Preview mode lets you see content marked as unpublished.
Expand Down
20 changes: 7 additions & 13 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,13 @@ pub fn send_redirect(route: String, location: String, status: u16) -> Result<Res
Ok(bldr.body(None)?)
}

pub fn send_unauthorized(with_challenge: bool) -> Result<Response> {
if with_challenge {
let realm =
std::env::var(BASIC_AUTH_REALM).unwrap_or(FALLBACK_BASIC_AUTH_REALM.to_string());
let response_builder = Builder::new().status(401).header(
http::header::WWW_AUTHENTICATE,
format!("Basic realm=\"{}\"", realm),
);

return Ok(response_builder.body(None)?);
}

Ok(Builder::new().status(401).body(None)?)
pub fn send_unauthorized() -> Result<Response> {
let realm = std::env::var(BASIC_AUTH_REALM).unwrap_or(FALLBACK_BASIC_AUTH_REALM.to_string());
let response_builder = Builder::new().status(401).header(
http::header::WWW_AUTHENTICATE,
format!("Basic realm=\"{}\"", realm),
);
Ok(response_builder.body(None)?)
}
/// Based on the Accept-Encoding header, return the best Content-Encoding.
fn parse_encoding(enc: Option<&HeaderValue>) -> Result<ContentEncoding> {
Expand Down

0 comments on commit fa1880c

Please sign in to comment.