Skip to content

Commit

Permalink
refactor: error page is now generic
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Engelhardt <[email protected]>
  • Loading branch information
antonengelhardt committed Sep 5, 2024
1 parent f1afb82 commit bfe5295
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
23 changes: 4 additions & 19 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,7 @@ impl HttpContext for ConfiguredOidc {
Ok(_) => return Action::Pause,
Err(e) => {
warn!("token exchange failed: {}", e);
self.send_http_response(
503,
vec![("Cache-Control", "no-cache"),
("Content-Type", "text/html")],
Some(b"<div style=\"text-align: center; margin-top: 20%; font-family: Arial, sans-serif;\">
<h1>503</h1>
<h2>Token exchange failed</h2>
<p>Please try again, delete your cookies or contact your system administrator.</p>
</div>"),
);
self.show_error_page(503, "Token exchange failed", "Please try again, delete your cookies or contact your system administrator.");
}
}
return Action::Pause;
Expand Down Expand Up @@ -226,16 +217,10 @@ impl Context for ConfiguredOidc {
Err(e) => {
warn!("storing token in cookie failed: {}", e);
// Send a 503 if storing the token in the cookie failed
self.send_http_response(
self.show_error_page(
503,
vec![("Cache-Control", "no-cache"),
("Content-Type", "text/html")],
Some(b"<div style=\"text-align: center; margin-top: 20%; font-family: Arial, sans-serif;\">
<h1>503</h1>
<h2>Storing Token in Cookie failed</h2>
<p>Please try again, delete your cookies or contact your system administrator.</p>
</div>",
),
"Storing Token in Cookie failed",
"Please try again, delete your cookies or contact your system administrator.",
);
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// proxy-wasm
use proxy_wasm::traits::HttpContext;

// thiserror
use thiserror::Error;

// crate
use crate::auth::ConfiguredOidc;

/// Error type for the plugin
#[derive(Error, Debug)]
#[allow(clippy::enum_variant_names)]
Expand Down Expand Up @@ -59,3 +65,27 @@ pub enum PluginError {
#[error("state does not match")]
StateMismatchError,
}

impl ConfiguredOidc {
pub fn show_error_page(&self, status_code: u32, title: &str, message: &str) {
let mut headers = vec![];
headers.push(("cache-control", "no-cache"));
headers.push(("content-type", "text/html"));

self.send_http_response(
status_code,
headers,
Some(
format!(
"<div style=\"text-align: center; margin-top: 20%; font-family: Arial, sans-serif;\">
<h1>{}</h1>
<h2>{}</h2>
<p>{}</p>
</div>",
status_code, title, message
)
.as_bytes(),
),
);
}
}

0 comments on commit bfe5295

Please sign in to comment.