Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cli command to expire all sessions #96

Merged
merged 2 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions backend/rvoc-backend/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ enum Cli {
/// Apply pending database migrations.
ApplyMigrations,

/// Expire the passwords of all users.
/// Expire the passwords and sessions of all users.
/// This should always succeed, and users who update their passwords simultaneously should receive an error.
/// Note that this does not expire all sessions.
ExpireAllPasswords,

/// Expire all sessions of all users.
/// This should always succeed, and sessions that are updated simultaneously should be be logged out anyways.
ExpireAllSessions,

/// Set the password of a user.
/// If no password is given, then it is read from stdin.
SetPassword {
Expand Down Expand Up @@ -74,6 +77,7 @@ pub async fn run_cli_command(configuration: &Configuration) -> RVocResult<()> {
}
Cli::ApplyMigrations => apply_pending_database_migrations(configuration).await?,
Cli::ExpireAllPasswords => expire_all_passwords(configuration).await?,
Cli::ExpireAllSessions => expire_all_sessions(configuration).await?,
Cli::SetPassword { username, password } => {
set_password(username, password, configuration).await?
}
Expand Down Expand Up @@ -158,6 +162,36 @@ async fn expire_all_passwords(configuration: &Configuration) -> RVocResult<()> {
)
.await?;

expire_all_sessions(configuration).await?;

Ok(())
}

#[instrument(err, skip(configuration))]
async fn expire_all_sessions(configuration: &Configuration) -> RVocResult<()> {
let database_connection_pool = create_async_database_connection_pool(configuration).await?;

database_connection_pool
.execute_read_committed_transaction(
|database_connection| {
Box::pin(async {
use crate::database::schema::sessions::dsl::*;

diesel::delete(sessions)
.execute(database_connection)
.await
.map_err(|error| {
RVocError::ExpireAllSessions {
source: Box::new(error),
}
.into()
})
})
},
configuration.maximum_transaction_retry_count,
)
.await?;

Ok(())
}

Expand Down
3 changes: 3 additions & 0 deletions backend/rvoc-backend/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ pub enum RVocError {
#[error("error expiring all passwords: {source}")]
ExpireAllPasswords { source: BoxDynError },

#[error("error expiring all sessions: {source}")]
ExpireAllSessions { source: BoxDynError },

#[error("error reading password from stdin: {source}")]
ReadPasswordFromStdin { source: BoxDynError },

Expand Down
Loading