cedros-login-server 0.0.1

Authentication server for cedros-login with email/password, Google OAuth, and Solana wallet sign-in
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Custom role repository for per-org role definitions

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use tokio::sync::RwLock;
use uuid::Uuid;

use crate::errors::AppError;
use crate::repositories::pagination::{cap_limit, cap_offset};
use crate::services::Permission;

/// Custom role entity
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomRole {
    pub id: Uuid,
    pub org_id: Uuid,
    /// Display name for the role
    pub name: String,
    /// Optional description
    pub description: Option<String>,
    /// Set of permissions granted by this role
    pub permissions: HashSet<String>,
    /// Whether this is a default role assigned to new members
    pub is_default: bool,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

impl CustomRole {
    /// Create a new custom role
    pub fn new(org_id: Uuid, name: impl Into<String>, permissions: HashSet<String>) -> Self {
        let now = Utc::now();
        Self {
            id: Uuid::new_v4(),
            org_id,
            name: name.into(),
            description: None,
            permissions,
            is_default: false,
            created_at: now,
            updated_at: now,
        }
    }

    /// Check if this role has a specific permission
    pub fn has_permission(&self, permission: &str) -> bool {
        self.permissions.contains(permission)
    }

    /// Check if this role has the given Permission enum
    pub fn has_permission_enum(&self, permission: Permission) -> bool {
        self.permissions.contains(permission.as_str())
    }
}

/// Custom role repository trait
#[async_trait]
pub trait CustomRoleRepository: Send + Sync {
    /// Create a new custom role
    async fn create(&self, role: CustomRole) -> Result<CustomRole, AppError>;

    /// Find a role by ID
    async fn find_by_id(&self, id: Uuid) -> Result<Option<CustomRole>, AppError>;

    /// Find all roles for an organization
    async fn find_by_org(&self, org_id: Uuid) -> Result<Vec<CustomRole>, AppError>;

    /// Find roles for an organization with pagination
    async fn find_by_org_paged(
        &self,
        org_id: Uuid,
        limit: u32,
        offset: u32,
    ) -> Result<Vec<CustomRole>, AppError>;

    /// Count roles for an organization
    async fn count_by_org(&self, org_id: Uuid) -> Result<u64, AppError>;

    /// Find a role by name within an organization
    async fn find_by_org_and_name(
        &self,
        org_id: Uuid,
        name: &str,
    ) -> Result<Option<CustomRole>, AppError>;

    /// Update a custom role
    async fn update(&self, role: CustomRole) -> Result<CustomRole, AppError>;

    /// Delete a custom role
    async fn delete(&self, id: Uuid) -> Result<bool, AppError>;

    /// Delete all custom roles for an organization
    async fn delete_by_org(&self, org_id: Uuid) -> Result<u64, AppError>;

    /// Get the default role for an organization (if any)
    async fn get_default_role(&self, org_id: Uuid) -> Result<Option<CustomRole>, AppError>;

    /// Set a role as the default for an organization
    async fn set_default_role(&self, org_id: Uuid, role_id: Uuid) -> Result<(), AppError>;
}

/// In-memory custom role repository
pub struct InMemoryCustomRoleRepository {
    roles: RwLock<HashMap<Uuid, CustomRole>>,
}

impl InMemoryCustomRoleRepository {
    pub fn new() -> Self {
        Self {
            roles: RwLock::new(HashMap::new()),
        }
    }
}

impl Default for InMemoryCustomRoleRepository {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl CustomRoleRepository for InMemoryCustomRoleRepository {
    async fn create(&self, role: CustomRole) -> Result<CustomRole, AppError> {
        let mut roles = self.roles.write().await;

        // Check for duplicate name within org
        for existing in roles.values() {
            if existing.org_id == role.org_id
                && existing.name.to_lowercase() == role.name.to_lowercase()
            {
                return Err(AppError::Validation(format!(
                    "Role '{}' already exists in this organization",
                    role.name
                )));
            }
        }

        roles.insert(role.id, role.clone());
        Ok(role)
    }

    async fn find_by_id(&self, id: Uuid) -> Result<Option<CustomRole>, AppError> {
        let roles = self.roles.read().await;

        Ok(roles.get(&id).cloned())
    }

    async fn find_by_org(&self, org_id: Uuid) -> Result<Vec<CustomRole>, AppError> {
        let roles = self.roles.read().await;

        Ok(roles
            .values()
            .filter(|r| r.org_id == org_id)
            .cloned()
            .collect())
    }

    async fn find_by_org_paged(
        &self,
        org_id: Uuid,
        limit: u32,
        offset: u32,
    ) -> Result<Vec<CustomRole>, AppError> {
        let roles = self.roles.read().await;
        let mut results: Vec<_> = roles
            .values()
            .filter(|r| r.org_id == org_id)
            .cloned()
            .collect();
        results.sort_by(|a, b| a.created_at.cmp(&b.created_at));
        let capped_limit = cap_limit(limit);
        let capped_offset = cap_offset(offset);
        let start = capped_offset as usize;
        let end = start.saturating_add(capped_limit as usize);
        Ok(results.into_iter().skip(start).take(end - start).collect())
    }

    async fn count_by_org(&self, org_id: Uuid) -> Result<u64, AppError> {
        let roles = self.roles.read().await;
        Ok(roles.values().filter(|r| r.org_id == org_id).count() as u64)
    }

    async fn find_by_org_and_name(
        &self,
        org_id: Uuid,
        name: &str,
    ) -> Result<Option<CustomRole>, AppError> {
        let roles = self.roles.read().await;

        Ok(roles
            .values()
            .find(|r| r.org_id == org_id && r.name.to_lowercase() == name.to_lowercase())
            .cloned())
    }

    async fn update(&self, role: CustomRole) -> Result<CustomRole, AppError> {
        let mut roles = self.roles.write().await;

        if !roles.contains_key(&role.id) {
            return Err(AppError::NotFound("Role not found".into()));
        }

        // Check for duplicate name within org (excluding self)
        for existing in roles.values() {
            if existing.id != role.id
                && existing.org_id == role.org_id
                && existing.name.to_lowercase() == role.name.to_lowercase()
            {
                return Err(AppError::Validation(format!(
                    "Role '{}' already exists in this organization",
                    role.name
                )));
            }
        }

        let mut updated = role;
        updated.updated_at = Utc::now();
        roles.insert(updated.id, updated.clone());
        Ok(updated)
    }

    async fn delete(&self, id: Uuid) -> Result<bool, AppError> {
        let mut roles = self.roles.write().await;

        Ok(roles.remove(&id).is_some())
    }

    async fn delete_by_org(&self, org_id: Uuid) -> Result<u64, AppError> {
        let mut roles = self.roles.write().await;

        let before = roles.len();
        roles.retain(|_, role| role.org_id != org_id);
        Ok((before - roles.len()) as u64)
    }

    async fn get_default_role(&self, org_id: Uuid) -> Result<Option<CustomRole>, AppError> {
        let roles = self.roles.read().await;

        Ok(roles
            .values()
            .find(|r| r.org_id == org_id && r.is_default)
            .cloned())
    }

    async fn set_default_role(&self, org_id: Uuid, role_id: Uuid) -> Result<(), AppError> {
        let mut roles = self.roles.write().await;

        // First, verify the role exists and belongs to the org
        let role = roles
            .get(&role_id)
            .ok_or(AppError::NotFound("Role not found".into()))?;
        if role.org_id != org_id {
            return Err(AppError::Forbidden(
                "Role does not belong to this org".into(),
            ));
        }

        // Clear existing default
        for r in roles.values_mut() {
            if r.org_id == org_id {
                r.is_default = false;
            }
        }

        // Set new default
        if let Some(r) = roles.get_mut(&role_id) {
            r.is_default = true;
            r.updated_at = Utc::now();
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_role(org_id: Uuid, name: &str) -> CustomRole {
        let mut permissions = HashSet::new();
        permissions.insert("member.read".to_string());
        permissions.insert("org.read".to_string());
        CustomRole::new(org_id, name, permissions)
    }

    #[tokio::test]
    async fn test_create_and_find_role() {
        let repo = InMemoryCustomRoleRepository::new();
        let org_id = Uuid::new_v4();
        let role = create_test_role(org_id, "Developer");

        let created = repo.create(role).await.unwrap();
        assert_eq!(created.name, "Developer");
        assert_eq!(created.org_id, org_id);

        let found = repo.find_by_id(created.id).await.unwrap();
        assert!(found.is_some());
        assert_eq!(found.unwrap().name, "Developer");
    }

    #[tokio::test]
    async fn test_find_by_org() {
        let repo = InMemoryCustomRoleRepository::new();
        let org_id = Uuid::new_v4();
        let other_org_id = Uuid::new_v4();

        repo.create(create_test_role(org_id, "Developer"))
            .await
            .unwrap();
        repo.create(create_test_role(org_id, "Designer"))
            .await
            .unwrap();
        repo.create(create_test_role(other_org_id, "Manager"))
            .await
            .unwrap();

        let roles = repo.find_by_org(org_id).await.unwrap();
        assert_eq!(roles.len(), 2);
    }

    #[tokio::test]
    async fn test_find_by_org_paged_and_count() {
        let repo = InMemoryCustomRoleRepository::new();
        let org_id = Uuid::new_v4();

        repo.create(create_test_role(org_id, "Developer"))
            .await
            .unwrap();
        repo.create(create_test_role(org_id, "Designer"))
            .await
            .unwrap();

        let total = repo.count_by_org(org_id).await.unwrap();
        assert_eq!(total, 2);

        let roles = repo.find_by_org_paged(org_id, 1, 1).await.unwrap();
        assert_eq!(roles.len(), 1);
    }

    #[tokio::test]
    async fn test_duplicate_name_rejected() {
        let repo = InMemoryCustomRoleRepository::new();
        let org_id = Uuid::new_v4();

        repo.create(create_test_role(org_id, "Developer"))
            .await
            .unwrap();
        let result = repo.create(create_test_role(org_id, "developer")).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_same_name_different_org_allowed() {
        let repo = InMemoryCustomRoleRepository::new();
        let org1 = Uuid::new_v4();
        let org2 = Uuid::new_v4();

        repo.create(create_test_role(org1, "Developer"))
            .await
            .unwrap();
        let result = repo.create(create_test_role(org2, "Developer")).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_update_role() {
        let repo = InMemoryCustomRoleRepository::new();
        let org_id = Uuid::new_v4();

        let role = repo
            .create(create_test_role(org_id, "Developer"))
            .await
            .unwrap();

        let mut updated = role.clone();
        updated.name = "Senior Developer".to_string();
        updated.description = Some("Senior engineering role".to_string());

        let result = repo.update(updated).await.unwrap();
        assert_eq!(result.name, "Senior Developer");
        assert_eq!(
            result.description,
            Some("Senior engineering role".to_string())
        );
    }

    #[tokio::test]
    async fn test_set_default_role() {
        let repo = InMemoryCustomRoleRepository::new();
        let org_id = Uuid::new_v4();

        let role1 = repo
            .create(create_test_role(org_id, "Member"))
            .await
            .unwrap();
        let role2 = repo
            .create(create_test_role(org_id, "Viewer"))
            .await
            .unwrap();

        // Set first as default
        repo.set_default_role(org_id, role1.id).await.unwrap();
        let default = repo.get_default_role(org_id).await.unwrap();
        assert!(default.is_some());
        assert_eq!(default.unwrap().id, role1.id);

        // Set second as default
        repo.set_default_role(org_id, role2.id).await.unwrap();
        let default = repo.get_default_role(org_id).await.unwrap();
        assert!(default.is_some());
        assert_eq!(default.unwrap().id, role2.id);

        // Verify first is no longer default
        let role1_updated = repo.find_by_id(role1.id).await.unwrap().unwrap();
        assert!(!role1_updated.is_default);
    }

    #[tokio::test]
    async fn test_has_permission() {
        let org_id = Uuid::new_v4();
        let role = create_test_role(org_id, "Developer");

        assert!(role.has_permission("member.read"));
        assert!(role.has_permission("org.read"));
        assert!(!role.has_permission("org.delete"));
    }
}