Skip to content

Commit 76d1aa6

Browse files
committed
web/error: add AxumNope::Unauthorized to subsume all auth failures
1 parent 148d80d commit 76d1aa6

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/web/builds.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,13 @@ pub(crate) async fn build_trigger_rebuild_handler(
169169
))))?;
170170

171171
// (Future: would it be better to have standard middleware handle auth?)
172-
let TypedHeader(auth_header) =
173-
opt_auth_header.ok_or(JsonAxumNope(AxumNope::MissingAuthenticationToken))?;
172+
let TypedHeader(auth_header) = opt_auth_header.ok_or(JsonAxumNope(AxumNope::Unauthorized(
173+
"Missing authentication token",
174+
)))?;
174175
if auth_header.token() != expected_token {
175-
return Err(JsonAxumNope(AxumNope::InvalidAuthenticationToken));
176+
return Err(JsonAxumNope(AxumNope::Unauthorized(
177+
"The token used for authentication is not valid",
178+
)));
176179
}
177180

178181
build_trigger_check(conn, &name, &version, &build_queue)
@@ -377,7 +380,6 @@ mod tests {
377380
let response = env.frontend().post("/crate/regex/1.3.1/rebuild").send()?;
378381
assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
379382
let text = response.text()?;
380-
assert!(text.contains("access token `trigger_rebuild_token` is not configured"));
381383
let json: serde_json::Value = serde_json::from_str(&text)?;
382384
assert_eq!(
383385
json,
@@ -408,8 +410,8 @@ mod tests {
408410
assert_eq!(
409411
json,
410412
serde_json::json!({
411-
"title": "Missing authentication token",
412-
"message": "The token used for authentication is missing"
413+
"title": "Unauthorized",
414+
"message": "Missing authentication token"
413415
})
414416
);
415417
}
@@ -426,7 +428,7 @@ mod tests {
426428
assert_eq!(
427429
json,
428430
serde_json::json!({
429-
"title": "Invalid authentication token",
431+
"title": "Unauthorized",
430432
"message": "The token used for authentication is not valid"
431433
})
432434
);

src/web/error.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ pub enum AxumNope {
2828
VersionNotFound,
2929
#[error("Search yielded no results")]
3030
NoResults,
31-
#[error("Missing authentication token")]
32-
MissingAuthenticationToken,
33-
#[error("Invalid authentication token")]
34-
InvalidAuthenticationToken,
31+
#[error("Unauthorized: {0}")]
32+
Unauthorized(&'static str),
3533
#[error("internal error")]
3634
InternalError(anyhow::Error),
3735
#[error("bad request")]
@@ -97,14 +95,9 @@ impl AxumNope {
9795
message: Cow::Owned(source.to_string()),
9896
status: StatusCode::BAD_REQUEST,
9997
}),
100-
AxumNope::MissingAuthenticationToken => ErrorResponse::ErrorInfo(ErrorInfo {
101-
title: "Missing authentication token",
102-
message: "The token used for authentication is missing".into(),
103-
status: StatusCode::UNAUTHORIZED,
104-
}),
105-
AxumNope::InvalidAuthenticationToken => ErrorResponse::ErrorInfo(ErrorInfo {
106-
title: "Invalid authentication token",
107-
message: "The token used for authentication is not valid".into(),
98+
AxumNope::Unauthorized(what) => ErrorResponse::ErrorInfo(ErrorInfo {
99+
title: "Unauthorized",
100+
message: what.into(),
108101
status: StatusCode::UNAUTHORIZED,
109102
}),
110103
AxumNope::InternalError(source) => {

0 commit comments

Comments
 (0)