1use crate::{be::Limits, prelude::*, value::Session};
7use kanidm_proto::internal::{ApiTokenPurpose, UatPurpose};
8use serde::{Deserialize, Serialize};
9use std::{collections::BTreeSet, hash::Hash, net::IpAddr, sync::Arc};
10use time::OffsetDateTime;
11
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub enum Source {
14 Internal,
15 Https(IpAddr),
16 Ldaps(IpAddr),
17}
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum AccessScope {
21 ReadOnly,
22 ReadWrite,
23 Synchronise,
24}
25
26impl std::fmt::Display for AccessScope {
27 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
28 match self {
29 AccessScope::ReadOnly => write!(f, "read only"),
30 AccessScope::ReadWrite => write!(f, "read write"),
31 AccessScope::Synchronise => write!(f, "synchronise"),
32 }
33 }
34}
35
36impl From<&ApiTokenPurpose> for AccessScope {
37 fn from(purpose: &ApiTokenPurpose) -> Self {
38 match purpose {
39 ApiTokenPurpose::ReadOnly => AccessScope::ReadOnly,
40 ApiTokenPurpose::ReadWrite => AccessScope::ReadWrite,
41 ApiTokenPurpose::Synchronise => AccessScope::Synchronise,
42 }
43 }
44}
45
46impl From<&UatPurpose> for AccessScope {
47 fn from(purpose: &UatPurpose) -> Self {
48 match purpose {
49 UatPurpose::ReadOnly => AccessScope::ReadOnly,
50 UatPurpose::ReadWrite { .. } => AccessScope::ReadWrite,
51 }
52 }
53}
54
55#[derive(Debug, Clone)]
56pub struct IdentUser {
58 pub entry: Arc<EntrySealedCommitted>,
59 }
62
63#[derive(Debug, Clone)]
64pub enum InternalRole {
66 System,
68 Migration,
70
71 AccountRequest,
74
75 MessageQueue,
77}
78
79impl std::fmt::Display for InternalRole {
80 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
81 match self {
82 Self::System => write!(f, "System"),
83 Self::Migration => write!(f, "Migration"),
84 Self::AccountRequest => write!(f, "AccountRequest"),
85 Self::MessageQueue => write!(f, "MessageQueue"),
86 }
87 }
88}
89
90impl InternalRole {
91 pub fn get_uuid(&self) -> Uuid {
92 match self {
93 Self::System => UUID_SYSTEM,
94 Self::Migration => UUID_INTERNAL_MIGRATION,
95 Self::AccountRequest => UUID_INTERNAL_ACCOUNT_REQUEST,
96 Self::MessageQueue => UUID_INTERNAL_MESSAGE_QUEUE,
97 }
98 }
99}
100
101#[derive(Debug, Clone)]
102pub enum IdentType {
104 User(IdentUser),
105 Synch(Uuid),
106 Internal(InternalRole),
107}
108
109#[derive(Debug, Clone, PartialEq, Hash, Ord, PartialOrd, Eq, Serialize, Deserialize)]
110pub enum IdentityId {
113 User(Uuid),
116 Synch(Uuid),
117 Internal(Uuid),
118}
119
120impl From<&IdentityId> for Uuid {
121 fn from(ident: &IdentityId) -> Uuid {
122 match ident {
123 IdentityId::User(uuid) | IdentityId::Synch(uuid) | IdentityId::Internal(uuid) => *uuid,
124 }
125 }
126}
127
128impl From<&IdentType> for IdentityId {
129 fn from(idt: &IdentType) -> Self {
130 match idt {
131 IdentType::Internal(role) => IdentityId::Internal(role.get_uuid()),
132 IdentType::User(u) => IdentityId::User(u.entry.get_uuid()),
133 IdentType::Synch(u) => IdentityId::Synch(*u),
134 }
135 }
136}
137
138#[derive(Debug, Clone)]
139pub struct Identity {
142 pub origin: IdentType,
143 #[allow(dead_code)]
144 source: Source,
145 pub(crate) session_id: Uuid,
146 pub(crate) scope: AccessScope,
147 limits: Limits,
148 last_verified_at: Option<OffsetDateTime>,
149}
150
151impl std::fmt::Display for Identity {
152 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
153 match &self.origin {
154 IdentType::Internal(u) => write!(f, "Internal ({}) ({})", u, self.scope),
155 IdentType::Synch(u) => write!(f, "Synchronise ({}) ({})", u, self.scope),
156 IdentType::User(u) => {
157 let nv = u.entry.get_uuid2spn();
158 write!(
159 f,
160 "User( {}, {} ) ({}, {})",
161 nv.to_proto_string_clone(),
162 u.entry.get_uuid().as_hyphenated(),
163 self.session_id,
164 self.scope
165 )
166 }
167 }
168 }
169}
170
171impl Identity {
172 pub(crate) fn new(
173 origin: IdentType,
174 source: Source,
175 session_id: Uuid,
176 scope: AccessScope,
177 limits: Limits,
178 last_verified_at: Option<OffsetDateTime>,
179 ) -> Self {
180 Self {
181 origin,
182 source,
183 session_id,
184 scope,
185 limits,
186 last_verified_at,
187 }
188 }
189
190 #[allow(dead_code)]
191 pub(crate) fn source(&self) -> &Source {
192 &self.source
193 }
194
195 pub(crate) fn limits(&self) -> &Limits {
196 &self.limits
197 }
198
199 #[cfg(test)]
200 pub(crate) fn limits_mut(&mut self) -> &mut Limits {
201 &mut self.limits
202 }
203
204 pub(crate) fn last_verified_at(&self) -> Option<OffsetDateTime> {
207 self.last_verified_at
208 }
209
210 pub(crate) fn migration() -> Self {
211 Identity {
212 origin: IdentType::Internal(InternalRole::Migration),
213 source: Source::Internal,
214 session_id: UUID_INTERNAL_SESSION_ID,
215 scope: AccessScope::ReadWrite,
216 limits: Limits::unlimited(),
217 last_verified_at: None,
218 }
219 }
220
221 pub(crate) fn account_request() -> Self {
222 Identity {
223 origin: IdentType::Internal(InternalRole::AccountRequest),
224 source: Source::Internal,
225 session_id: UUID_INTERNAL_SESSION_ID,
226 scope: AccessScope::ReadWrite,
227 limits: Limits::unlimited(),
228 last_verified_at: None,
229 }
230 }
231
232 pub(crate) fn message_queue() -> Self {
233 Identity {
234 origin: IdentType::Internal(InternalRole::MessageQueue),
235 source: Source::Internal,
236 session_id: UUID_INTERNAL_SESSION_ID,
237 scope: AccessScope::ReadWrite,
238 limits: Limits::unlimited(),
239 last_verified_at: None,
240 }
241 }
242
243 pub(crate) fn from_internal() -> Self {
244 Identity {
245 origin: IdentType::Internal(InternalRole::System),
246 source: Source::Internal,
247 session_id: UUID_INTERNAL_SESSION_ID,
248 scope: AccessScope::ReadWrite,
249 limits: Limits::unlimited(),
250 last_verified_at: None,
251 }
252 }
253
254 #[cfg(test)]
255 pub(crate) fn from_impersonate_entry_readonly(
256 entry: Arc<Entry<EntrySealed, EntryCommitted>>,
257 ) -> Self {
258 Identity {
259 origin: IdentType::User(IdentUser { entry }),
260 source: Source::Internal,
261 session_id: UUID_INTERNAL_SESSION_ID,
262 scope: AccessScope::ReadOnly,
263 limits: Limits::unlimited(),
264 last_verified_at: None,
265 }
266 }
267
268 pub fn from_impersonate_entry_readwrite(
269 entry: Arc<Entry<EntrySealed, EntryCommitted>>,
270 ) -> Self {
271 Identity {
272 origin: IdentType::User(IdentUser { entry }),
273 source: Source::Internal,
274 session_id: UUID_INTERNAL_SESSION_ID,
275 scope: AccessScope::ReadWrite,
276 limits: Limits::unlimited(),
277 last_verified_at: None,
278 }
279 }
280
281 pub fn access_scope(&self) -> AccessScope {
282 self.scope
283 }
284
285 pub fn project_with_scope(&self, scope: AccessScope) -> Self {
286 let mut new = self.clone();
287 new.scope = scope;
288 new
289 }
290
291 pub fn get_session_id(&self) -> Uuid {
292 self.session_id
293 }
294
295 pub fn get_session(&self) -> Option<&Session> {
296 match &self.origin {
297 IdentType::Internal(_) | IdentType::Synch(_) => None,
298 IdentType::User(u) => u
299 .entry
300 .get_ava_as_session_map(Attribute::UserAuthTokenSession)
301 .and_then(|sessions| sessions.get(&self.session_id)),
302 }
303 }
304
305 pub fn get_user_entry(&self) -> Option<Arc<EntrySealedCommitted>> {
306 match &self.origin {
307 IdentType::Internal(_) | IdentType::Synch(_) => None,
308 IdentType::User(u) => Some(u.entry.clone()),
309 }
310 }
311
312 pub fn from_impersonate(ident: &Self) -> Self {
313 ident.clone()
318 }
319
320 pub fn is_internal(&self) -> bool {
321 matches!(self.origin, IdentType::Internal(_))
322 }
323
324 pub fn get_uuid(&self) -> Uuid {
325 match &self.origin {
326 IdentType::Internal(role) => role.get_uuid(),
327 IdentType::User(u) => u.entry.get_uuid(),
328 IdentType::Synch(u) => *u,
329 }
330 }
331
332 pub fn can_logout(&self) -> bool {
336 match &self.origin {
337 IdentType::Internal(_) => false,
338 IdentType::User(u) => u.entry.get_uuid() != UUID_ANONYMOUS,
339 IdentType::Synch(_) => false,
340 }
341 }
342
343 pub fn get_event_origin_id(&self) -> IdentityId {
344 IdentityId::from(&self.origin)
345 }
346
347 #[cfg(test)]
348 pub fn has_claim(&self, claim: &str) -> bool {
349 match &self.origin {
350 IdentType::Internal(_) | IdentType::Synch(_) => false,
351 IdentType::User(u) => u
352 .entry
353 .attribute_equality(Attribute::Claim, &PartialValue::new_iutf8(claim)),
354 }
355 }
356
357 pub fn is_memberof(&self, group: Uuid) -> bool {
358 match &self.origin {
359 IdentType::Internal(_) | IdentType::Synch(_) => false,
360 IdentType::User(u) => u
361 .entry
362 .attribute_equality(Attribute::MemberOf, &PartialValue::Refer(group)),
363 }
364 }
365
366 pub fn get_memberof(&self) -> Option<&BTreeSet<Uuid>> {
367 match &self.origin {
368 IdentType::Internal(_) | IdentType::Synch(_) => None,
369 IdentType::User(u) => u.entry.get_ava_refer(Attribute::MemberOf),
370 }
371 }
372
373 pub fn get_oauth2_consent_scopes(&self, oauth2_rs: Uuid) -> Option<&BTreeSet<String>> {
374 match &self.origin {
375 IdentType::Internal(_) | IdentType::Synch(_) => None,
376 IdentType::User(u) => u
377 .entry
378 .get_ava_as_oauthscopemaps(Attribute::OAuth2ConsentScopeMap)
379 .and_then(|scope_map| scope_map.get(&oauth2_rs)),
380 }
381 }
382}