From b2f0cec6bcf25b3aa7ebfe4153e5155571f8009c Mon Sep 17 00:00:00 2001 From: BillionClaw Date: Fri, 17 Apr 2026 18:10:17 +0000 Subject: [PATCH] fix(auth): handle email-style usernames in OIDC JIT provisioning --- .../services/auth_application_service.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/application/services/auth_application_service.rs b/src/application/services/auth_application_service.rs index 0b82bb72..2ad22cfb 100644 --- a/src/application/services/auth_application_service.rs +++ b/src/application/services/auth_application_service.rs @@ -1144,8 +1144,22 @@ impl AuthApplicationService { let quota = self.capped_quota(&role); - // Sanitize username (max 32 chars, ensure uniqueness) - let mut username = oidc_username.chars().take(32).collect::(); + // Sanitize username: if it looks like an email, extract the local part + // (some OIDC providers like Keycloak use email as the preferred username) + let base_username = if oidc_username.contains('@') { + oidc_username.split('@').next().unwrap_or(&oidc_username) + } else { + &oidc_username + }; + + // Filter to valid username characters only, then truncate to 32 chars + let mut username = base_username + .chars() + .filter(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_' || *c == '.') + .take(32) + .collect::(); + + // Ensure minimum length if username.len() < 3 { username = format!("user_{}", &claims.sub[..8.min(claims.sub.len())]); }