Can close all user sessions when Matrix token is changed

This commit is contained in:
2025-02-06 22:39:53 +01:00
parent 4ff72e073e
commit 558d5cda3f
7 changed files with 172 additions and 111 deletions

View File

@ -1,5 +1,6 @@
use crate::app_config::AppConfig;
use crate::constants::{STATE_KEY, USER_SESSION_KEY};
use crate::server::api::ws::WsMessage;
use crate::server::{HttpFailure, HttpResult};
use crate::user::{APIClient, APIClientID, User, UserConfig, UserID};
use crate::utils;
@ -9,6 +10,7 @@ use askama::Template;
use ipnet::IpNet;
use light_openid::primitives::OpenIDConfig;
use std::str::FromStr;
use tokio::sync::broadcast;
/// Static assets
#[derive(rust_embed::Embed)]
@ -60,7 +62,11 @@ pub struct FormRequest {
}
/// Main route
pub async fn home(session: Session, form_req: Option<web::Form<FormRequest>>) -> HttpResult {
pub async fn home(
session: Session,
form_req: Option<web::Form<FormRequest>>,
tx: web::Data<broadcast::Sender<WsMessage>>,
) -> HttpResult {
// Get user information, requesting authentication if information is missing
let Some(user): Option<User> = session.get(USER_SESSION_KEY)? else {
// Generate auth state
@ -93,10 +99,14 @@ pub async fn home(session: Session, form_req: Option<web::Form<FormRequest>>) ->
if t.len() < 3 {
error_message = Some("Specified Matrix token is too short!".to_string());
} else {
// TODO : invalidate all existing connections
config.matrix_token = t;
config.save().await?;
success_message = Some("Matrix token was successfully updated!".to_string());
// Invalidate all Ws connections
if let Err(e) = tx.send(WsMessage::CloseAllUserSessions(user.id.clone())) {
log::error!("Failed to send CloseAllUserSessions: {}", e);
}
}
}