rustauth-scim 0.3.0

SCIM support for RustAuth.
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
430
431
432
433
434
435
436
437
//! SCIM plugin configuration.

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use http::StatusCode;
use rustauth_core::db::User;
use rustauth_core::error::RustAuthError;

use crate::store::ScimProviderRecord;

/// Boxed future returned by SCIM hooks.
pub type ScimHookFuture = Pin<Box<dyn Future<Output = Result<(), ScimHookError>> + Send>>;

/// Boxed future returned by custom token storage callbacks.
pub type ScimTokenStorageFuture =
    Pin<Box<dyn Future<Output = Result<String, RustAuthError>> + Send>>;

/// Custom token transformation callback.
pub type ScimTokenTransform = Arc<dyn Fn(String) -> ScimTokenStorageFuture + Send + Sync>;

/// Hook invoked before a SCIM token provider is persisted.
pub type BeforeScimTokenGeneratedHook =
    Arc<dyn Fn(BeforeScimTokenGeneratedInput) -> ScimHookFuture + Send + Sync>;

/// Hook invoked after a SCIM token provider is persisted.
pub type AfterScimTokenGeneratedHook =
    Arc<dyn Fn(AfterScimTokenGeneratedInput) -> ScimHookFuture + Send + Sync>;

/// How `POST /scim/v2/Bulk` applies database changes.
///
/// Better Auth **1.6.9** does not implement Bulk (`bulk.supported: false`). RustAuth
/// implements RFC 7644 bulk with two modes:
///
/// - [`ScimBulkMode::Independent`] (default): each operation commits on its own,
///   matching typical SCIM deployments and `failOnErrors` stop semantics.
/// - [`ScimBulkMode::Atomic`]: all mutating operations run in one adapter
///   transaction; the first error rolls back earlier mutations in the same request.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ScimBulkMode {
    /// Sequential operations; each mutation uses its own transaction (default).
    #[default]
    Independent,
    /// One transaction for the whole bulk request; rollback on first failing op.
    Atomic,
}

/// How `DELETE /scim/v2/Users/:id` (and bulk user delete) deprovisions users.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ScimDeprovisionMode {
    /// Delete the RustAuth user when they have no linked accounts besides the
    /// current SCIM provider; otherwise unlink (see [`ScimDeprovisionMode::UnlinkAccount`]).
    DeleteUser,
    /// Remove only the current provider account link and SCIM profile.
    #[default]
    UnlinkAccount,
}

/// Severity level for SCIM audit events.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScimAuditSeverity {
    /// Informational audit event.
    Info,
    /// Warning-level audit event.
    Warn,
    /// Error-level audit event.
    Error,
}

/// SCIM audit event kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScimAuditEventKind {
    /// Management token generated or rotated.
    TokenGenerated,
    /// User created or linked via SCIM.
    UserProvisioned,
    /// User deleted or unlinked via SCIM.
    UserDeprovisioned,
    /// A bulk operation returned an error status.
    BulkFailed,
    /// An atomic bulk request rolled back prior operations.
    BulkRolledBack,
}

/// Audit event emitted by the SCIM plugin.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScimAuditEvent {
    /// Event kind.
    pub kind: ScimAuditEventKind,
    /// Severity.
    pub severity: ScimAuditSeverity,
    /// SCIM provider id when known.
    pub provider_id: Option<String>,
    /// RustAuth user id when known.
    pub user_id: Option<String>,
    /// Organization id when the provider is org-scoped.
    pub organization_id: Option<String>,
    /// Optional detail (error message, rollback reason, etc.).
    pub reason: Option<String>,
}

impl ScimAuditEvent {
    /// Create an audit event with no optional context.
    pub fn new(kind: ScimAuditEventKind, severity: ScimAuditSeverity) -> Self {
        Self {
            kind,
            severity,
            provider_id: None,
            user_id: None,
            organization_id: None,
            reason: None,
        }
    }

    #[must_use]
    pub fn with_provider_id(mut self, provider_id: impl Into<String>) -> Self {
        self.provider_id = Some(provider_id.into());
        self
    }

    #[must_use]
    pub fn with_user_id(mut self, user_id: impl Into<String>) -> Self {
        self.user_id = Some(user_id.into());
        self
    }

    #[must_use]
    pub fn with_organization_id(mut self, organization_id: impl Into<String>) -> Self {
        self.organization_id = Some(organization_id.into());
        self
    }

    #[must_use]
    pub fn with_reason(mut self, reason: impl Into<String>) -> Self {
        self.reason = Some(reason.into());
        self
    }
}

/// SCIM plugin options.
///
/// ```
/// use rustauth_scim::{ScimOptions, ScimTokenStorage};
///
/// let options = ScimOptions::default();
/// assert!(matches!(options.token_storage, ScimTokenStorage::Hashed));
/// ```
#[derive(Clone)]
pub struct ScimOptions {
    /// Whether provider connections are tied to the user who generated them.
    pub provider_ownership: ProviderOwnershipOptions,
    /// Organization roles allowed to manage org-scoped SCIM providers.
    pub required_role: Option<Vec<String>>,
    /// Static SCIM providers checked before database-backed providers.
    pub default_scim: Vec<DefaultScimProvider>,
    /// How generated SCIM tokens are stored.
    pub token_storage: ScimTokenStorage,
    /// Callback invoked after built-in authorization and before persistence.
    pub before_token_generated: Option<BeforeScimTokenGeneratedHook>,
    /// Callback invoked after a provider has been persisted.
    pub after_token_generated: Option<AfterScimTokenGeneratedHook>,
    /// Bulk commit strategy ([`ScimBulkMode::Independent`] by default).
    pub bulk_mode: ScimBulkMode,
    /// User delete semantics for SCIM deprovision.
    pub deprovision_mode: ScimDeprovisionMode,
    /// Optional async audit sink (also logged through `AuthContext::logger`).
    pub audit_event: Option<crate::audit::ScimAuditEventResolver>,
}

impl Default for ScimOptions {
    fn default() -> Self {
        Self {
            provider_ownership: ProviderOwnershipOptions::default(),
            required_role: None,
            default_scim: Vec::new(),
            token_storage: ScimTokenStorage::Hashed,
            before_token_generated: None,
            after_token_generated: None,
            bulk_mode: ScimBulkMode::default(),
            deprovision_mode: ScimDeprovisionMode::default(),
            audit_event: None,
        }
    }
}

impl ScimOptions {
    /// Create default SCIM plugin options.
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    /// Configure provider ownership rules.
    pub fn provider_ownership(mut self, ownership: ProviderOwnershipOptions) -> Self {
        self.provider_ownership = ownership;
        self
    }

    #[must_use]
    /// Set organization roles allowed to manage org-scoped SCIM providers.
    pub fn required_role(mut self, roles: Vec<String>) -> Self {
        self.required_role = Some(roles);
        self
    }

    #[must_use]
    /// Add statically configured SCIM providers.
    pub fn default_scim(mut self, providers: Vec<DefaultScimProvider>) -> Self {
        self.default_scim = providers;
        self
    }

    #[must_use]
    /// Set how generated SCIM tokens are stored.
    pub fn token_storage(mut self, storage: ScimTokenStorage) -> Self {
        self.token_storage = storage;
        self
    }

    #[must_use]
    /// Set the hook invoked before a SCIM token is persisted.
    pub fn before_token_generated(mut self, hook: BeforeScimTokenGeneratedHook) -> Self {
        self.before_token_generated = Some(hook);
        self
    }

    #[must_use]
    /// Set the hook invoked after a SCIM token is persisted.
    pub fn after_token_generated(mut self, hook: AfterScimTokenGeneratedHook) -> Self {
        self.after_token_generated = Some(hook);
        self
    }

    #[must_use]
    /// Set bulk commit strategy.
    pub fn bulk_mode(mut self, mode: ScimBulkMode) -> Self {
        self.bulk_mode = mode;
        self
    }

    #[must_use]
    /// Set user delete semantics for SCIM deprovision.
    pub fn deprovision_mode(mut self, mode: ScimDeprovisionMode) -> Self {
        self.deprovision_mode = mode;
        self
    }

    #[must_use]
    /// Set an async audit event sink.
    pub fn audit_event(mut self, resolver: crate::audit::ScimAuditEventResolver) -> Self {
        self.audit_event = Some(resolver);
        self
    }
}

impl std::fmt::Debug for ScimOptions {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("ScimOptions")
            .field("provider_ownership", &self.provider_ownership)
            .field("required_role", &self.required_role)
            .field("default_scim", &self.default_scim)
            .field("token_storage", &self.token_storage)
            .field(
                "before_token_generated",
                &self.before_token_generated.as_ref().map(|_| "<hook>"),
            )
            .field(
                "after_token_generated",
                &self.after_token_generated.as_ref().map(|_| "<hook>"),
            )
            .field("bulk_mode", &self.bulk_mode)
            .field("deprovision_mode", &self.deprovision_mode)
            .field(
                "audit_event",
                &self.audit_event.as_ref().map(|_| "<resolver>"),
            )
            .finish()
    }
}

/// Organization member details passed to SCIM hooks.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScimOrganizationMember {
    /// Organization identifier.
    pub organization_id: String,
    /// User identifier.
    pub user_id: String,
    /// Persisted organization role string.
    pub role: String,
}

/// Payload for `before_token_generated`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BeforeScimTokenGeneratedInput {
    /// Authenticated user generating the token.
    pub user: User,
    /// Organization member row when the token is org-scoped.
    pub member: Option<ScimOrganizationMember>,
    /// Returned bearer token.
    pub scim_token: String,
}

/// Payload for `after_token_generated`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AfterScimTokenGeneratedInput {
    /// Authenticated user generating the token.
    pub user: User,
    /// Organization member row when the token is org-scoped.
    pub member: Option<ScimOrganizationMember>,
    /// Returned bearer token.
    pub scim_token: String,
    /// Persisted provider record.
    pub provider: ScimProviderRecord,
}

/// Error returned by SCIM hooks to abort a management request.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScimHookError {
    /// HTTP status returned to the caller.
    pub status: StatusCode,
    /// Stable API error code.
    pub code: String,
    /// Human-readable error message.
    pub message: String,
}

impl ScimHookError {
    /// Create a hook error with an explicit HTTP status and API code.
    pub fn new(status: StatusCode, code: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            status,
            code: code.into(),
            message: message.into(),
        }
    }

    /// Create a forbidden hook error.
    pub fn forbidden(message: impl Into<String>) -> Self {
        Self::new(StatusCode::FORBIDDEN, "FORBIDDEN", message)
    }
}

/// Provider ownership configuration.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ProviderOwnershipOptions {
    /// Enable user ownership for global SCIM provider connections.
    ///
    /// When disabled, management routes reject requests without `organizationId`.
    /// Organization-scoped providers still use [`ScimOptions::required_role`].
    pub enabled: bool,
}

/// A statically configured SCIM provider.
///
/// `provider_id` is globally unique in storage, like Better Auth: it names one SCIM
/// connection, not one organization. Pair it with `organization_id` when the token
/// should only provision members of that organization.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DefaultScimProvider {
    /// Stable provider identifier (one persisted SCIM connection per value).
    pub provider_id: String,
    /// Plain base token for the provider.
    pub scim_token: String,
    /// Optional organization scope.
    pub organization_id: Option<String>,
}

/// Built-in SCIM token storage modes.
#[derive(Clone)]
pub enum ScimTokenStorage {
    /// Store the base token directly.
    Plain,
    /// Store a SHA-256 digest of the base token.
    Hashed,
    /// Store the base token encrypted with RustAuth secret material.
    Encrypted,
    /// Store a custom hash of the base token.
    CustomHash { hash: ScimTokenTransform },
    /// Store a custom encrypted token and decrypt it during verification.
    CustomEncryption {
        encrypt: ScimTokenTransform,
        decrypt: ScimTokenTransform,
    },
}

impl ScimTokenStorage {
    /// Create a custom hash token storage mode.
    ///
    /// ```
    /// use rustauth_scim::ScimTokenStorage;
    ///
    /// let storage = ScimTokenStorage::custom_hash(|token| {
    ///     Box::pin(async move { Ok(format!("{token}:hashed")) })
    /// });
    /// ```
    pub fn custom_hash(
        hash: impl Fn(String) -> ScimTokenStorageFuture + Send + Sync + 'static,
    ) -> Self {
        Self::CustomHash {
            hash: Arc::new(hash),
        }
    }

    /// Create a custom encrypt/decrypt token storage mode.
    ///
    /// ```
    /// use rustauth_scim::ScimTokenStorage;
    ///
    /// let storage = ScimTokenStorage::custom_encryption(
    ///     |token| Box::pin(async move { Ok(token.chars().rev().collect()) }),
    ///     |token| Box::pin(async move { Ok(token.chars().rev().collect()) }),
    /// );
    /// ```
    pub fn custom_encryption(
        encrypt: impl Fn(String) -> ScimTokenStorageFuture + Send + Sync + 'static,
        decrypt: impl Fn(String) -> ScimTokenStorageFuture + Send + Sync + 'static,
    ) -> Self {
        Self::CustomEncryption {
            encrypt: Arc::new(encrypt),
            decrypt: Arc::new(decrypt),
        }
    }
}

impl std::fmt::Debug for ScimTokenStorage {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Plain => formatter.write_str("Plain"),
            Self::Hashed => formatter.write_str("Hashed"),
            Self::Encrypted => formatter.write_str("Encrypted"),
            Self::CustomHash { .. } => formatter.write_str("CustomHash"),
            Self::CustomEncryption { .. } => formatter.write_str("CustomEncryption"),
        }
    }
}