Skip to content

Commit b7a7f47

Browse files
committed
Refactor auth service; add OIDC auto-provisioning
Split the monolithic unified auth module into submodules (unified::types, audit, bearer, password, mod). Introduces provider-aware bearer logic: deterministic provider username composition, provider user resolution/auto-provisioning, and provider-based user-id hashing. Add JWT claim mapping for preferred_username, and propagate a new config flag (auth.auto_create_users_from_provider) through jwt_config/init_auth_config with a default (false). Remove the old unified.rs and wire the new modules into authentication flow. Misc: add docs (security, datatypes), Keycloak realm file, and update docker-compose and server.toml and related config defaults/overrides to reflect the new settings.
1 parent a6c5021 commit b7a7f47

39 files changed

Lines changed: 2871 additions & 1542 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Notes.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,17 @@ come uop with a better and shorter one without causing issues in the future
12841284

12851285
188) Instead of notify all followers remember where the user is connected to through the livequerymanager and forward the notification to that exact node only
12861286

1287+
189) pub fn parse_basic_auth_header(auth_header: &str) -> AuthResult<(String, String)> should return UserId instead of String for the username
1288+
also: Credentials { username: String, password: String },
1289+
async fn authenticate_credentials(
1290+
username: &str,
1291+
1292+
1293+
190) when auto create user use the userid generation like the rest in the code
1294+
1295+
191) /// TODO: This will hurt the sharding distribution of provider users, i prefer adding the provider code as a prefix to the username instead of suffixing it, but we can revisit this if it becomes an issue.
1296+
pub(crate) fn compose_provider_username(issuer: &str, subject: &str) -> UserName
1297+
12871298

12881299

12891300

backend/crates/kalamdb-auth/src/providers/jwt_auth.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ pub struct JwtClaims {
4444
pub exp: usize,
4545
/// Issued at (Unix timestamp)
4646
pub iat: usize,
47-
/// Username (custom claim)
47+
/// Username (custom claim). Also maps provider claim `preferred_username`.
48+
#[serde(alias = "preferred_username")]
4849
pub username: Option<UserName>,
4950
/// Email (custom claim)
5051
pub email: Option<String>,

backend/crates/kalamdb-auth/src/providers/jwt_config.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use once_cell::sync::OnceCell;
1010
pub struct JwtConfig {
1111
pub secret: String,
1212
pub trusted_issuers: Vec<String>,
13+
pub auto_create_users_from_provider: bool,
1314
}
1415

1516
static JWT_CONFIG: OnceCell<JwtConfig> = OnceCell::new();
@@ -18,10 +19,15 @@ static JWT_CONFIG: OnceCell<JwtConfig> = OnceCell::new();
1819
///
1920
/// This should be called once at startup after loading server.toml and applying
2021
/// environment overrides. If not called, defaults are used.
21-
pub fn init_jwt_config(secret: &str, trusted_issuers: &str) {
22+
pub fn init_jwt_config(
23+
secret: &str,
24+
trusted_issuers: &str,
25+
auto_create_users_from_provider: bool,
26+
) {
2227
let config = JwtConfig {
2328
secret: secret.to_string(),
2429
trusted_issuers: parse_trusted_issuers(trusted_issuers),
30+
auto_create_users_from_provider,
2531
};
2632
let _ = JWT_CONFIG.set(config);
2733
}
@@ -32,6 +38,8 @@ pub fn get_jwt_config() -> &'static JwtConfig {
3238
trusted_issuers: parse_trusted_issuers(
3339
&kalamdb_configs::defaults::default_auth_jwt_trusted_issuers(),
3440
),
41+
auto_create_users_from_provider:
42+
kalamdb_configs::defaults::default_auth_auto_create_users_from_provider(),
3543
})
3644
}
3745

0 commit comments

Comments
 (0)