Compare commits

..
9 changed files with 59 additions and 19 deletions

No files matched your search

+1
View File
@@ -0,0 +1 @@
Added admin command to forcefully log out all of a user's existing sessions. Contributed by @nex.
+1 -1
View File
@@ -52,7 +52,7 @@ ENV BINSTALL_VERSION=1.16.6
# renovate: datasource=github-releases depName=psastras/sbom-rs
ENV CARGO_SBOM_VERSION=0.9.1
# renovate: datasource=crate depName=lddtree
ENV LDDTREE_VERSION=0.3.7
ENV LDDTREE_VERSION=0.4.0
# renovate: datasource=crate depName=timelord-cli
ENV TIMELORD_VERSION=3.0.1
+1 -1
View File
@@ -22,7 +22,7 @@ ENV BINSTALL_VERSION=1.16.6
# renovate: datasource=github-releases depName=psastras/sbom-rs
ENV CARGO_SBOM_VERSION=0.9.1
# renovate: datasource=crate depName=lddtree
ENV LDDTREE_VERSION=0.3.7
ENV LDDTREE_VERSION=0.4.0
# Install unpackaged tools
RUN <<EOF
+1 -1
View File
@@ -45,7 +45,7 @@ pub(super) async fn revoke_token(&self, token: String) -> Result {
let Some(token) = self
.services
.registration_tokens
.validate_token(token)
.validate_token(&token)
.await
else {
return Err!("This token does not exist or has already expired.");
+27
View File
@@ -1017,3 +1017,30 @@ pub(super) async fn unlock(&self, user_id: String) -> Result {
self.write_str(&format!("User {user_id} has been unlocked."))
.await
}
#[admin_command]
pub(super) async fn logout(&self, user_id: String) -> Result {
self.bail_restricted()?;
let user_id = parse_local_user_id(self.services, &user_id)?;
assert!(
self.services.globals.user_is_local(&user_id),
"Parsed user_id must be a local user"
);
if user_id == self.services.globals.server_user {
return Err!("Not allowed to log out the server service account.",);
}
if !self.services.users.exists(&user_id).await {
return Err!("User {user_id} does not exist.");
}
if self.services.users.is_admin(&user_id).await {
return Err!("You cannot forcefully log out admin users.");
}
self.services
.users
.all_device_ids(&user_id)
.for_each(|device_id| self.services.users.remove_device(&user_id, device_id))
.await;
self.write_str(&format!("User {user_id} has been logged out from all devices."))
.await
}
+12
View File
@@ -59,6 +59,18 @@ pub enum UserCommand {
force: bool,
},
/// - Forcefully log a user out of all of their devices.
///
/// This will invalidate all access tokens for the specified user,
/// effectively logging them out from all sessions.
/// Note that this is destructive and may result in data loss for the user,
/// such as encryption keys. Use with caution. Can only be used in the admin
/// room.
Logout {
/// Username of the user to log out
user_id: String,
},
/// - Suspend a user
///
/// Suspended users are able to log in, sync, and read messages, but are not
+1 -1
View File
@@ -880,7 +880,7 @@ pub(crate) async fn check_registration_token_validity(
let valid = services
.registration_tokens
.validate_token(body.token.clone())
.validate_token(&body.token)
.await
.is_some();
+14 -14
View File
@@ -23,18 +23,18 @@ struct Services {
/// A validated registration token which may be used to create an account.
#[derive(Debug)]
pub struct ValidToken {
pub token: String,
pub struct ValidToken<'token> {
pub token: &'token str,
pub source: ValidTokenSource,
}
impl std::fmt::Display for ValidToken {
impl std::fmt::Display for ValidToken<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "`{}` --- {}", self.token, &self.source)
}
}
impl PartialEq<str> for ValidToken {
impl PartialEq<str> for ValidToken<'_> {
fn eq(&self, other: &str) -> bool { self.token == other }
}
@@ -85,11 +85,11 @@ impl Service {
}
/// Get the registration token set in the config file, if it exists.
pub fn get_config_file_token(&self) -> Option<ValidToken> {
pub fn get_config_file_token(&self) -> Option<ValidToken<'_>> {
self.services
.config
.registration_token
.clone()
.as_deref()
.map(|token| ValidToken {
token,
source: ValidTokenSource::ConfigFile,
@@ -97,7 +97,7 @@ impl Service {
}
/// Validate a registration token.
pub async fn validate_token(&self, token: String) -> Option<ValidToken> {
pub async fn validate_token<'token>(&self, token: &'token str) -> Option<ValidToken<'token>> {
// Check the registration token in the config first
if self
.get_config_file_token()
@@ -110,7 +110,7 @@ impl Service {
}
// Now check the database
if let Some(token_info) = self.db.lookup_token_info(&token).await
if let Some(token_info) = self.db.lookup_token_info(token).await
&& token_info.is_valid()
{
return Some(ValidToken {
@@ -124,7 +124,7 @@ impl Service {
}
/// Mark a valid token as having been used to create a new account.
pub fn mark_token_as_used(&self, ValidToken { token, source }: ValidToken) {
pub fn mark_token_as_used(&self, ValidToken { token, source }: ValidToken<'_>) {
match source {
| ValidTokenSource::ConfigFile => {
// we don't track uses of the config file token, do nothing
@@ -132,7 +132,7 @@ impl Service {
| ValidTokenSource::Database(mut info) => {
info.uses = info.uses.saturating_add(1);
self.db.save_token(&token, &info);
self.db.save_token(token, &info);
},
}
}
@@ -141,7 +141,7 @@ impl Service {
///
/// Note that some tokens (like the one set in the config file) cannot be
/// revoked.
pub fn revoke_token(&self, ValidToken { token, source }: ValidToken) -> Result {
pub fn revoke_token(&self, ValidToken { token, source }: ValidToken<'_>) -> Result {
match source {
| ValidTokenSource::ConfigFile => {
// the config file token cannot be revoked
@@ -151,19 +151,19 @@ impl Service {
)
},
| ValidTokenSource::Database(_) => {
self.db.revoke_token(&token);
self.db.revoke_token(token);
Ok(())
},
}
}
/// Iterate over all valid registration tokens.
pub fn iterate_tokens(&self) -> impl Stream<Item = ValidToken> + Send + '_ {
pub fn iterate_tokens(&self) -> impl Stream<Item = ValidToken<'_>> + Send + '_ {
let db_tokens = self
.db
.iterate_and_clean_tokens()
.map(|(token, info)| ValidToken {
token: token.to_owned(),
token,
source: ValidTokenSource::Database(info),
});
+1 -1
View File
@@ -209,7 +209,7 @@ pub async fn try_auth(
}
},
| AuthData::RegistrationToken(t) => {
let token = t.token.trim().to_owned();
let token = t.token.trim();
if let Some(valid_token) = self
.services