oauth2-passkey 0.6.1

OAuth2 and Passkey authentication library for Rust web applications
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use std::collections::HashMap;

use crate::oauth2::{AccountSearchField, OAuth2Store, ProviderUserId};
use crate::passkey::{CredentialId, CredentialSearchField, PasskeyStore};
use crate::userdb::{User, UserStore};

use super::errors::CoordinationError;
use crate::session::{
    SessionId, User as SessionUser, UserId, cleanup_stale_sessions,
    delete_session_from_store_by_session_id, get_user_from_session,
};

/// Retrieves a list of all users in the system.
///
/// This admin-level function fetches all user accounts from the database.
/// It provides a comprehensive view of all registered users and their details.
/// Requires administrative privileges.
///
/// # Arguments
///
/// * `session_id` - The session ID of the administrator performing the action
///
/// # Returns
///
/// * `Ok(Vec<User>)` - A vector containing all user accounts
/// * `Err(CoordinationError::Unauthorized)` - If the user doesn't have admin privileges
/// * `Err(CoordinationError)` - If a database error occurs
///
/// # Examples
///
/// ```no_run
/// use oauth2_passkey::{get_all_users, SessionId};
///
/// async fn list_all_users(session_id: &str) -> Vec<String> {
///     let session_id = SessionId::new(session_id.to_string()).expect("Valid session ID");
///     match get_all_users(session_id).await {
///         Ok(users) => users.iter().map(|user| user.account.clone()).collect(),
///         Err(_) => Vec::new()
///     }
/// }
/// ```
pub async fn get_all_users(session_id: SessionId) -> Result<Vec<User>, CoordinationError> {
    // Validate admin session with fresh database lookup
    let _admin_user = validate_admin_session(session_id).await?;

    UserStore::get_all_users()
        .await
        .map_err(|e| CoordinationError::Database(e.to_string()))
}

/// Retrieves a specific user by their ID.
///
/// This function fetches a user's account information from the database using their
/// unique identifier. It's used for user profile viewing, account management,
/// and administrative tasks. Requires administrative privileges.
///
/// # Arguments
///
/// * `session_id` - The session ID of the administrator performing the action
/// * `user_id` - The unique identifier of the user to retrieve
///
/// # Returns
///
/// * `Ok(Some(User))` - The user's account information if found
/// * `Ok(None)` - If no user exists with the provided ID
/// * `Err(CoordinationError::Unauthorized)` - If the user doesn't have admin privileges
/// * `Err(CoordinationError)` - If a database error occurs
///
/// # Examples
///
/// ```no_run
/// use oauth2_passkey::{get_user, SessionId, UserId};
///
/// async fn fetch_user_profile(session_id: &str, id: &str) -> Option<String> {
///     let session_id = SessionId::new(session_id.to_string()).expect("Valid session ID");
///     let user_id = UserId::new(id.to_string()).expect("Valid user ID");
///     match get_user(session_id, user_id).await {
///         Ok(Some(user)) => Some(user.account),
///         _ => None
///     }
/// }
/// ```
pub async fn get_user(
    session_id: SessionId,
    user_id: UserId,
) -> Result<Option<User>, CoordinationError> {
    // Validate admin session with fresh database lookup
    let _admin_user = validate_admin_session(session_id).await?;

    UserStore::get_user(user_id)
        .await
        .map_err(|e| CoordinationError::Database(e.to_string()))
}

/// Deletes a passkey credential as an administrator.
///
/// This administrative function allows a system administrator to delete any user's
/// passkey credential. It requires the calling user to have administrative privileges.
/// This is useful for managing compromised credentials or helping users who are
/// locked out of their accounts.
///
/// # Arguments
///
/// * `session_id` - The session ID of the administrator performing the action
/// * `credential_id` - The ID of the passkey credential to delete
///
/// # Returns
///
/// * `Ok(())` - If the credential was successfully deleted
/// * `Err(CoordinationError::Unauthorized)` - If the user doesn't have admin privileges
/// * `Err(CoordinationError)` - If another error occurs during deletion
///
/// # Examples
///
/// ```no_run
/// use oauth2_passkey::{delete_passkey_credential_admin, SessionId, CredentialId};
///
/// async fn remove_credential(session_id: &str, credential_id: &str) -> bool {
///     let session_id = SessionId::new(session_id.to_string()).expect("Valid session ID");
///     let credential_id = CredentialId::new(credential_id.to_string()).expect("Valid credential ID");
///     delete_passkey_credential_admin(session_id, credential_id).await.is_ok()
/// }
/// ```
pub async fn delete_passkey_credential_admin(
    session_id: SessionId,
    credential_id: CredentialId,
) -> Result<(), CoordinationError> {
    // Validate admin session with fresh database lookup
    let admin_user = validate_admin_session(session_id).await?;

    tracing::debug!(
        "Admin user: {} is deleting credential with ID: {}",
        admin_user.id,
        credential_id.as_str()
    );

    let credential = PasskeyStore::get_credentials_by(CredentialSearchField::CredentialId(
        credential_id.clone(),
    ))
    .await?
    .into_iter()
    .next()
    .ok_or_else(|| {
        CoordinationError::ResourceNotFound {
            resource_type: "Passkey".to_string(),
            resource_id: credential_id.as_str().to_string(),
        }
        .log()
    })?;

    // Should we verify a context token here?

    // Delete the credential using the raw credential ID format from the database
    let credential_id = CredentialId::new(credential.credential_id.clone())
        .map_err(|e| CoordinationError::Validation(format!("Invalid credential ID: {e}")))?;
    PasskeyStore::delete_credential_by(CredentialSearchField::CredentialId(credential_id)).await?;

    tracing::debug!("Successfully deleted credential");

    Ok(())
}

/// Deletes an OAuth2 account as an administrator.
///
/// This administrative function allows a system administrator to delete any user's
/// OAuth2 account. It requires the calling user to have administrative privileges.
/// This is useful for managing compromised accounts or removing unauthorized
/// OAuth2 connections.
///
/// # Arguments
///
/// * `session_id` - The session ID of the administrator performing the action
/// * `provider_user_id` - The unique provider-specific user ID of the OAuth2 account to delete
///
/// # Returns
///
/// * `Ok(())` - If the OAuth2 account was successfully deleted
/// * `Err(CoordinationError::Unauthorized)` - If the user doesn't have admin privileges
/// * `Err(CoordinationError)` - If another error occurs during deletion
///
/// # Examples
///
/// ```no_run
/// use oauth2_passkey::{delete_oauth2_account_admin, SessionId, ProviderUserId};
///
/// async fn remove_oauth2_account(session_id: &str, provider_id: &str) -> bool {
///     let session_id = SessionId::new(session_id.to_string()).expect("Valid session ID");
///     let provider_id = ProviderUserId::new(provider_id.to_string()).expect("Valid provider ID");
///     delete_oauth2_account_admin(session_id, provider_id).await.is_ok()
/// }
/// ```
pub async fn delete_oauth2_account_admin(
    session_id: SessionId,
    provider_user_id: ProviderUserId,
) -> Result<(), CoordinationError> {
    // Validate admin session with fresh database lookup
    let admin_user = validate_admin_session(session_id).await?;

    tracing::debug!(
        "Admin user: {} is deleting OAuth2 account with ID: {}",
        admin_user.id,
        provider_user_id.as_str()
    );

    // Delete the OAuth2 account
    OAuth2Store::delete_oauth2_accounts_by(AccountSearchField::ProviderUserId(
        provider_user_id.clone(),
    ))
    .await?;

    tracing::info!(
        "Successfully deleted OAuth2 account {} for user {}",
        provider_user_id.as_str(),
        admin_user.id
    );
    Ok(())
}

/// Completely deletes a user account as an administrator.
///
/// This administrative function permanently removes a user account and all associated
/// data (including OAuth2 accounts and passkey credentials). This is a destructive
/// operation that cannot be undone. Requires administrative privileges.
///
/// # Arguments
///
/// * `session_id` - The session ID of the administrator performing the action
/// * `user_id` - The unique identifier of the user account to delete
///
/// # Returns
///
/// * `Ok(())` - If the user account was successfully deleted
/// * `Err(CoordinationError::Unauthorized)` - If the user doesn't have admin privileges
/// * `Err(CoordinationError::ResourceNotFound)` - If the user doesn't exist
/// * `Err(CoordinationError::Conflict)` - If trying to delete the last admin user
/// * `Err(CoordinationError)` - If another error occurs during deletion
///
/// # Examples
///
/// ```no_run
/// use oauth2_passkey::{delete_user_account_admin, SessionId, UserId};
///
/// async fn purge_account(session_id: &str, user_id: &str) -> Result<(), String> {
///     let session_id = SessionId::new(session_id.to_string()).expect("Valid session ID");
///     let user_id = UserId::new(user_id.to_string()).expect("Valid user ID");
///     delete_user_account_admin(session_id, user_id).await.map_err(|e| e.to_string())
/// }
/// ```
pub async fn delete_user_account_admin(
    session_id: SessionId,
    user_id: UserId,
) -> Result<(), CoordinationError> {
    // Validate admin session with fresh database lookup
    let _admin_user = validate_admin_session(session_id).await?;
    // Check if the user exists
    let user = UserStore::get_user(user_id.clone()).await?.ok_or_else(|| {
        CoordinationError::ResourceNotFound {
            resource_type: "User".to_string(),
            resource_id: user_id.as_str().to_string(),
        }
        .log()
    })?;

    tracing::debug!("Deleting user account: {:#?}", user);

    // Delete the user account. Related OAuth2 accounts and Passkey credentials
    // are automatically removed via ON DELETE CASCADE foreign key constraints.
    if user.has_admin_privileges() {
        // Atomic: only deletes if other admins exist (prevents last-admin deletion race)
        let deleted = UserStore::delete_user_if_not_last_admin(user_id)
            .await
            .map_err(|e| CoordinationError::Database(e.to_string()))?;
        if !deleted {
            return Err(CoordinationError::Conflict(
                "Cannot delete the last admin user".to_string(),
            )
            .log());
        }
    } else {
        UserStore::delete_user(user_id).await?;
    }

    Ok(())
}

/// Updates a user's administrative status.
///
/// This function allows an administrator to grant or revoke administrative privileges
/// for another user. For security reasons:
/// - The first user (sequence_number=1) cannot be demoted (unconditional protection)
/// - The last admin user in the system cannot be demoted (to prevent admin lockout)
///
/// # Arguments
///
/// * `session_id` - The session ID of the administrator performing the action
/// * `user_id` - The ID of the user whose admin status will be changed
/// * `is_admin` - The new admin status (`true` = admin, `false` = regular user)
///
/// # Returns
///
/// * `Ok(User)` - The updated user account information
/// * `Err(CoordinationError::Unauthorized)` - If the caller doesn't have admin privileges
/// * `Err(CoordinationError::ResourceNotFound)` - If the target user doesn't exist
/// * `Err(CoordinationError::Conflict)` - If trying to demote the first user or last admin
///
/// # Examples
///
/// ```no_run
/// use oauth2_passkey::{update_user_admin_status, SessionId, UserId};
///
/// async fn make_user_admin(session_id: &str, user_id: &str) -> bool {
///     let session_id = SessionId::new(session_id.to_string()).expect("Valid session ID");
///     let user_id = UserId::new(user_id.to_string()).expect("Valid user ID");
///     update_user_admin_status(session_id, user_id, true).await.is_ok()
/// }
/// ```
pub async fn update_user_admin_status(
    session_id: SessionId,
    user_id: UserId,
    is_admin: bool,
) -> Result<User, CoordinationError> {
    // Validate admin session with fresh database lookup
    let _admin_user = validate_admin_session(session_id).await?;

    // Get the current user
    let user = UserStore::get_user(user_id.clone()).await?.ok_or_else(|| {
        CoordinationError::ResourceNotFound {
            resource_type: "User".to_string(),
            resource_id: user_id.as_str().to_string(),
        }
        .log()
    })?;

    // First user (sequence_number=1) cannot be demoted unconditionally
    if !is_admin && user.sequence_number == Some(1) {
        return Err(CoordinationError::Conflict("Cannot demote the first user".to_string()).log());
    }

    // Update the user with the new admin status
    if !is_admin && user.has_admin_privileges() {
        // Atomic: only demotes if other admins exist (prevents last-admin demotion race)
        let demoted = UserStore::demote_user_if_not_last_admin(user_id.clone())
            .await
            .map_err(|e| CoordinationError::Database(e.to_string()))?;
        if !demoted {
            return Err(CoordinationError::Conflict(
                "Cannot demote the last admin user".to_string(),
            )
            .log());
        }
        // Fetch updated user to return
        let user = UserStore::get_user(user_id).await?.ok_or_else(|| {
            CoordinationError::Internal("User disappeared after demotion".to_string())
        })?;
        Ok(user)
    } else {
        let updated_user = User { is_admin, ..user };
        let user = UserStore::upsert_user(updated_user).await?;
        Ok(user)
    }
}

/// Gets active session counts for all users.
///
/// This function returns a map of user IDs to their active session counts.
///
/// # Arguments
///
/// * `session_id` - The session ID of the administrator performing the action
///
/// # Returns
///
/// * `Ok(HashMap<String, usize>)` - A map of user IDs to their active session counts
/// * `Err(CoordinationError::Unauthorized)` - If the caller doesn't have admin privileges
/// * `Err(CoordinationError)` - If an error occurs during the operation
pub async fn get_all_active_sessions(
    session_id: SessionId,
) -> Result<HashMap<String, usize>, CoordinationError> {
    // Validate admin session
    let _admin_user = validate_admin_session(session_id).await?;

    // Get all users
    let users = UserStore::get_all_users()
        .await
        .map_err(|e| CoordinationError::Database(e.to_string()))?;

    let mut result = HashMap::new();

    for user in users {
        let session_ids = cleanup_stale_sessions(&user.id).await?;
        result.insert(user.id, session_ids.len());
    }

    Ok(result)
}

/// Forces logout of a user by deleting all their active sessions.
///
/// This administrative function invalidates all active sessions for a specific user,
/// effectively logging them out from all devices. This is useful for security incidents,
/// account compromises, or when a user requests to be logged out remotely.
///
/// # Arguments
///
/// * `session_id` - The session ID of the administrator performing the action
/// * `user_id` - The unique identifier of the user to force logout
///
/// # Returns
///
/// * `Ok(usize)` - The number of sessions that were terminated
/// * `Err(CoordinationError::Unauthorized)` - If the caller doesn't have admin privileges
/// * `Err(CoordinationError)` - If an error occurs during the operation
pub async fn force_logout_user(
    session_id: SessionId,
    user_id: UserId,
) -> Result<usize, CoordinationError> {
    // Validate admin session
    let admin_user = validate_admin_session(session_id).await?;

    tracing::info!(
        admin_id = %admin_user.id,
        target_user_id = %user_id.as_str(),
        "Admin forcing logout for user"
    );

    // Get all active sessions for the user
    let session_ids = cleanup_stale_sessions(user_id.as_str()).await?;
    let session_count = session_ids.len();

    // Delete each session
    for sid in session_ids {
        let session_id = SessionId::new(sid.clone())
            .map_err(|e| CoordinationError::Validation(format!("Invalid session ID: {e}")))?;
        if let Err(e) = delete_session_from_store_by_session_id(session_id).await {
            tracing::warn!(session_id = %sid, error = %e, "Failed to delete session");
            // Continue with other sessions even if one fails
        }
    }

    tracing::info!(
        admin_id = %admin_user.id,
        target_user_id = %user_id.as_str(),
        sessions_terminated = session_count,
        "User sessions terminated successfully"
    );

    Ok(session_count)
}

/// Validates that a session belongs to an admin user.
///
/// This is a helper function used by admin and login_history modules.
/// It validates session data using get_user_from_session which already
/// performs fresh database lookup to ensure current user state.
pub(super) async fn validate_admin_session(
    session_id: SessionId,
) -> Result<SessionUser, CoordinationError> {
    // Get user from session (this already does fresh database validation)
    let session_cookie = crate::SessionCookie::new(session_id.as_str().to_string())
        .map_err(|_| CoordinationError::Unauthorized.log())?;
    let session_user = get_user_from_session(&session_cookie)
        .await
        .map_err(|_| CoordinationError::Unauthorized.log())?;

    // Check if user has admin privileges (session_user already has fresh database data)
    if !session_user.has_admin_privileges() {
        tracing::debug!(user_id = %session_user.id, "User is not authorized (not an admin)");
        return Err(CoordinationError::Unauthorized.log());
    }

    tracing::debug!(user_id = %session_user.id, "Admin session validated successfully");

    Ok(session_user)
}

#[cfg(test)]
mod tests;