Skip to main content

keystate_core/model/
kind.rs

1//! The vocabulary of canonical entity kinds.
2
3use serde::{Deserialize, Serialize};
4
5/// The kinds of entity the canonical model can represent.
6///
7/// This is the fixed vocabulary the completeness manifests and the volatile
8/// section are keyed on.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
10#[serde(rename_all = "snake_case")]
11pub enum EntityKind {
12    /// A realm: the top-level configuration container.
13    Realm,
14    /// An OAuth/OIDC or SAML client (or "application").
15    Client,
16    /// A reusable bundle of provider configuration (Keycloak client scopes).
17    ClientScope,
18    /// A group.
19    Group,
20    /// A role.
21    Role,
22    /// A user account.
23    User,
24    /// A credential record owned by a user.
25    Credential,
26    /// A protocol mapper.
27    ProtocolMapper,
28    /// An authentication flow (Keycloak: a flow with execution steps).
29    AuthFlow,
30    /// An external identity provider.
31    IdentityProvider,
32}
33
34impl EntityKind {
35    /// The common field that holds this entity's row identity key.
36    ///
37    /// Used by the completeness verifier to key volatile records. `Realm` is
38    /// identified by its `name`; every other entity by its `id`.
39    pub const fn id_field(self) -> &'static str {
40        match self {
41            EntityKind::Realm => "name",
42            _ => "id",
43        }
44    }
45
46    /// The human-facing identity key this entity carries in common.
47    pub const fn identity_key(self) -> &'static str {
48        match self {
49            EntityKind::Realm => "name",
50            EntityKind::Client => "client_id",
51            EntityKind::ClientScope => "name",
52            EntityKind::Group => "name",
53            EntityKind::Role => "id",
54            EntityKind::User => "username",
55            EntityKind::Credential => "type",
56            EntityKind::ProtocolMapper => "name",
57            EntityKind::AuthFlow => "alias",
58            EntityKind::IdentityProvider => "alias",
59        }
60    }
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66
67    #[test]
68    fn each_entity_carries_its_identity_key() {
69        assert_eq!(EntityKind::Client.identity_key(), "client_id");
70        assert_eq!(EntityKind::User.identity_key(), "username");
71        assert_eq!(EntityKind::AuthFlow.identity_key(), "alias");
72        assert_eq!(EntityKind::Realm.identity_key(), "name");
73    }
74
75    #[test]
76    fn id_field_falls_back_to_name_for_realm() {
77        assert_eq!(EntityKind::Client.id_field(), "id");
78        assert_eq!(EntityKind::Realm.id_field(), "name");
79    }
80}