Skip to content

Commit

Permalink
feat: add better error logging to debug/server-info endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed May 25, 2024
1 parent 764f18f commit 278f0fd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-backend.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ jobs:
git
key: cache-${{ hashFiles('backend/Dockerfile') }}

# TODO: CI: Cache Injection Doesn't Work on Docker Backend Build For Some Reason.
- name: Inject Container Build Cache
uses: reproducible-containers/[email protected]
with:
Expand Down
1 change: 1 addition & 0 deletions backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0.86"
async-trait = "0.1.80"
axum = "0.7.5"
axum-extra = { version = "0.9.3", features = ["cookie"] }
Expand Down
51 changes: 46 additions & 5 deletions backend/src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Context;
use axum::{
extract::{FromRequest, Request},
BoxError,
Expand Down Expand Up @@ -26,25 +27,33 @@ where
.headers(req.headers().clone())
.send()
.await?;
let session_info = session_info.json::<RauthySessionInfo>().await?;
let session_info = session_info
.json::<RauthyResponse<RauthySessionInfo>>()
.await
.context("Parsing session info")?
.result()?;
let user_info = CLIENT
.get(
ARGS.rauthy_url
.join(&format!("/auth/v1/users/{}", session_info.user_id))
.join(&format!("/auth/v1/users/t{}", session_info.user_id))
.unwrap(),
)
.headers(req.headers().clone())
.send()
.await?;
let user_info = user_info.json::<RauthyUserInfo>().await?;
Ok::<_, reqwest::Error>(Some(RauthySession {
let user_info = user_info
.json::<RauthyResponse<RauthyUserInfo>>()
.await
.context("Parsing user info")?
.result()?;
Ok::<_, anyhow::Error>(Some(RauthySession {
info: session_info,
user: user_info,
}))
}
.await;
if let Err(e) = &session {
tracing::warn!("{e:#?}");
tracing::warn!("{e:?}");
}

Ok(AuthCtx {
Expand Down Expand Up @@ -115,6 +124,38 @@ pub struct RauthyUserInfoUserValues {
pub country: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum RauthyResponse<T> {
Ok(T),
Err(RauthyError),
}

impl<T> RauthyResponse<T> {
pub fn result(self) -> Result<T, RauthyError> {
match self {
RauthyResponse::Ok(t) => Ok(t),
RauthyResponse::Err(e) => Err(e),
}
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RauthyError {
error: String,
message: String,
timestamp: u64,
}
impl std::fmt::Display for RauthyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"Error[{}]: {}: {}",
self.timestamp, self.error, self.message
))
}
}
impl std::error::Error for RauthyError {}

#[derive(Debug, Clone)]
pub struct AuthenticationError;
impl std::fmt::Display for AuthenticationError {
Expand Down

0 comments on commit 278f0fd

Please sign in to comment.