Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return HTTP 401 when token is missing #55

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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