|
| 1 | +use actix_web::{HttpRequest, delete, web}; |
| 2 | +use eyre::eyre; |
| 3 | +use xredis::RedisPool; |
| 4 | + |
| 5 | +use crate::auth::get_user_from_headers; |
| 6 | +use crate::database::PgPool; |
| 7 | +use crate::database::models::DBUser; |
| 8 | +use crate::database::models::pat_item::DBPersonalAccessToken; |
| 9 | +use crate::database::models::session_item::DBSession; |
| 10 | +use crate::models::pats::Scopes; |
| 11 | +use crate::queue::session::AuthQueue; |
| 12 | +use crate::routes::ApiError; |
| 13 | +use crate::util::error::Context as _; |
| 14 | + |
| 15 | +pub fn config(cfg: &mut web::ServiceConfig) { |
| 16 | + cfg.service(revoke_user_sessions); |
| 17 | +} |
| 18 | + |
| 19 | +/// Revokes all sessions and personal access tokens of a user, signing them out everywhere. |
| 20 | +#[utoipa::path( |
| 21 | + context_path = "/admin/user", |
| 22 | + tag = "moderation", |
| 23 | + responses((status = NO_CONTENT)) |
| 24 | +)] |
| 25 | +#[delete("/{id}/sessions")] |
| 26 | +pub async fn revoke_user_sessions( |
| 27 | + req: HttpRequest, |
| 28 | + path: web::Path<(String,)>, |
| 29 | + pool: web::Data<PgPool>, |
| 30 | + redis: web::Data<RedisPool>, |
| 31 | + session_queue: web::Data<AuthQueue>, |
| 32 | +) -> Result<(), ApiError> { |
| 33 | + let user = get_user_from_headers( |
| 34 | + &req, |
| 35 | + &**pool, |
| 36 | + &redis, |
| 37 | + &session_queue, |
| 38 | + Scopes::SESSION_ACCESS, |
| 39 | + ) |
| 40 | + .await |
| 41 | + .wrap_auth_err("authenticating API request")? |
| 42 | + .1; |
| 43 | + |
| 44 | + if !user.role.is_admin() { |
| 45 | + return Err(ApiError::Auth(eyre!( |
| 46 | + "only admins can revoke user sessions" |
| 47 | + ))); |
| 48 | + } |
| 49 | + |
| 50 | + let target = DBUser::get(&path.into_inner().0, &**pool, &redis) |
| 51 | + .await |
| 52 | + .wrap_internal_err("fetching user from database")? |
| 53 | + .wrap_not_found_err("user not found")?; |
| 54 | + |
| 55 | + let mut transaction = pool |
| 56 | + .begin() |
| 57 | + .await |
| 58 | + .wrap_internal_err("starting database transaction")?; |
| 59 | + |
| 60 | + let sessions = DBSession::remove_all_for_user(target.id, &mut transaction) |
| 61 | + .await |
| 62 | + .wrap_internal_err("revoking user sessions")?; |
| 63 | + |
| 64 | + let pats = |
| 65 | + DBPersonalAccessToken::remove_all_for_user(target.id, &mut transaction) |
| 66 | + .await |
| 67 | + .wrap_internal_err("revoking user personal access tokens")?; |
| 68 | + |
| 69 | + transaction |
| 70 | + .commit() |
| 71 | + .await |
| 72 | + .wrap_internal_err("committing database transaction")?; |
| 73 | + |
| 74 | + DBSession::clear_user_sessions_cache(target.id, sessions, &redis) |
| 75 | + .await |
| 76 | + .wrap_internal_err("clearing session cache")?; |
| 77 | + |
| 78 | + DBPersonalAccessToken::clear_user_pats_cache(target.id, pats, &redis) |
| 79 | + .await |
| 80 | + .wrap_internal_err("clearing personal access token cache")?; |
| 81 | + |
| 82 | + Ok(()) |
| 83 | +} |
0 commit comments