Skip to content
Merged
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
33 changes: 24 additions & 9 deletions Cargo.lock

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

10 changes: 10 additions & 0 deletions crates/enigma-auth/src/jwt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ pub fn create_jwt(
permissions: Vec<String>,
secret: &str,
) -> Result<String, AuthError> {
if secret.len() < 32 {
return Err(AuthError::InvalidInput(
"JWT secret must be at least 32 bytes".to_string(),
));
}
let now = chrono::Utc::now().timestamp() as usize;
let claims = AuthClaims {
sub: user_id.to_string(),
Expand All @@ -41,6 +46,11 @@ pub fn create_jwt(
}

pub fn verify_jwt(token: &str, secret: &str) -> Result<AuthClaims, AuthError> {
if secret.len() < 32 {
return Err(AuthError::InvalidInput(
"JWT secret must be at least 32 bytes".to_string(),
));
}
let mut validation = Validation::default();
validation.set_issuer(&["enigma"]);
let data = decode::<AuthClaims>(
Expand Down
4 changes: 3 additions & 1 deletion crates/enigma-auth/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ where
let store_clone = auth_state.auth_store.clone();
let tid = api_token.id.clone();
tokio::spawn(async move {
let _ = store_clone.touch_token(&tid).await;
if let Err(e) = store_clone.touch_token(&tid).await {
tracing::warn!("Failed to update token last_used_at: {e}");
}
});
let permissions = store.get_user_permissions(&user.id).await?;
let groups: Vec<String> = store
Expand Down
18 changes: 10 additions & 8 deletions crates/enigma-auth/src/store/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,12 @@ impl AuthStore for PostgresAuthStore {
.fetch_one(&self.pool)
.await
.map_err(|e| {
if e.to_string().contains("duplicate") || e.to_string().contains("unique") {
AuthError::Duplicate(format!("user '{username}' already exists"))
} else {
AuthError::Database(e.to_string())
if let sqlx::Error::Database(ref db_err) = e {
if db_err.code().map_or(false, |c| c == "23505") {
return AuthError::Duplicate(format!("user '{username}' already exists"));
}
}
AuthError::Database(e.to_string())
})?;
Ok(User {
id: row.0,
Expand Down Expand Up @@ -279,11 +280,12 @@ impl AuthStore for PostgresAuthStore {
.fetch_one(&self.pool)
.await
.map_err(|e| {
if e.to_string().contains("duplicate") || e.to_string().contains("unique") {
AuthError::Duplicate(format!("group '{name}' already exists"))
} else {
AuthError::Database(e.to_string())
if let sqlx::Error::Database(ref db_err) = e {
if db_err.code().map_or(false, |c| c == "23505") {
return AuthError::Duplicate(format!("group '{name}' already exists"));
}
}
AuthError::Database(e.to_string())
})?;
Ok(Group {
id: row.0,
Expand Down
Loading
Loading