Skip to content

Commit

Permalink
Return HTTP 401 when token is missing
Browse files Browse the repository at this point in the history
When the repository returns an HTTP 401 error, flatpak is supposed to
request a token (if it hasn't already) using the authenticator. However,
we currently return a 403, which causes the transaction to fail instead.
  • Loading branch information
jameswestman committed Apr 20, 2022
1 parent 7bfca06 commit b32a657
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ pub enum ApiError {

#[fail(display = "NotEnoughPermissions")]
NotEnoughPermissions(String),

#[fail(display = "TokenRequired")]
TokenRequired,
}

impl From<DieselError> for ApiError {
Expand Down Expand Up @@ -182,6 +185,11 @@ impl ApiError {
"error-type": "token-insufficient",
"message": format!("Not enough permissions: {}", message),
}),
ApiError::TokenRequired => json!({
"status": 401,
"error-type": "token-required",
"message": "Token required"
}),
}
}

Expand All @@ -196,6 +204,7 @@ impl ApiError {
ApiError::WrongPublishedState(_, _, _) => StatusCode::BAD_REQUEST,
ApiError::InvalidToken(_) => StatusCode::UNAUTHORIZED,
ApiError::NotEnoughPermissions(ref _message) => StatusCode::FORBIDDEN,
ApiError::TokenRequired => StatusCode::UNAUTHORIZED,
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ impl ClaimsValidator for HttpRequest {
if let Some(claims) = self.extensions().get::<Claims>() {
func(claims)
} else {
Err(ApiError::NotEnoughPermissions(
"No token specified".to_string(),
))
Err(ApiError::TokenRequired)
}
}

Expand Down

0 comments on commit b32a657

Please sign in to comment.