summaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2022-03-20 17:39:10 -0700
committerJesse Morgan <jesse@jesterpm.net>2022-03-20 17:39:10 -0700
commit237accf0a1313ff5d73fe18096dd7229e0f8ba62 (patch)
treea7b8660ab4555706cf0f0c42f758f7afc46524eb /src/error.rs
Initial commit
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
new file mode 100644
index 0000000..271dc91
--- /dev/null
+++ b/src/error.rs
@@ -0,0 +1,53 @@
+use actix_web::http::{header, StatusCode};
+use actix_web::{HttpResponse, ResponseError};
+use std::fmt::{Display, Formatter};
+
+#[derive(Debug, Copy, Clone)]
+pub enum Error {
+ MissingToken,
+ InvalidToken,
+ ConfigurationError,
+ IntrospectionServerError,
+ AccessDenied,
+}
+
+impl Display for Error {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ f.write_str(match self {
+ Error::AccessDenied => "Access denied",
+ Error::MissingToken => "Missing authorization token",
+ Error::InvalidToken => "Invalid access token",
+ Error::ConfigurationError => "OAuth2 client configuration error",
+ Error::IntrospectionServerError => "Introspection endpoint returned an error",
+ })
+ }
+}
+
+impl ResponseError for Error {
+ fn status_code(&self) -> StatusCode {
+ match self {
+ Error::AccessDenied => StatusCode::FORBIDDEN,
+ Error::MissingToken => StatusCode::UNAUTHORIZED,
+ Error::InvalidToken => StatusCode::UNAUTHORIZED,
+ Error::ConfigurationError => StatusCode::INTERNAL_SERVER_ERROR,
+ Error::IntrospectionServerError => StatusCode::SERVICE_UNAVAILABLE,
+ }
+ }
+
+ fn error_response(&self) -> HttpResponse {
+ let mut resp = HttpResponse::build(self.status_code());
+ match self {
+ Error::AccessDenied => {
+ resp.insert_header((header::WWW_AUTHENTICATE, "Bearer"));
+ resp.body("{\"error\": \"insufficient_scope\"}")
+ }
+ Error::MissingToken => resp.finish(),
+ Error::InvalidToken => {
+ resp.insert_header((header::WWW_AUTHENTICATE, "Bearer"));
+ resp.body("{\"error\": \"invalid_token\"}")
+ }
+ Error::ConfigurationError => resp.finish(),
+ Error::IntrospectionServerError => resp.finish(),
+ }
+ }
+}