diff --git a/src/api/client/session.rs b/src/api/client/session.rs index 7ec24c28b..e4d8c9f7e 100644 --- a/src/api/client/session.rs +++ b/src/api/client/session.rs @@ -28,7 +28,7 @@ use ruma::{ }, assign, }; -use service::users::DeviceToken; +use service::users::{DeviceToken, PreviousTokenAction}; use crate::{Ruma, client_ip::ClientIp}; @@ -205,7 +205,7 @@ pub(crate) async fn login_route( let device_id = if let Some(existing_device_id) = existing_device_id { services .users - .set_token(&user_id, &existing_device_id, token.clone()) + .set_token(&user_id, &existing_device_id, token.clone(), PreviousTokenAction::Clear) .await?; existing_device_id diff --git a/src/service/oauth/mod.rs b/src/service/oauth/mod.rs index ddd1e3174..85e5a5da6 100644 --- a/src/service/oauth/mod.rs +++ b/src/service/oauth/mod.rs @@ -31,7 +31,7 @@ use crate::{ ResponseMode, TokenRequest, TokenRequestType, TokenResponse, TokenType, }, }, - users::{self, DeviceToken}, + users::{self, DeviceToken, PreviousTokenAction}, }; pub mod client_metadata; @@ -640,7 +640,12 @@ impl Service { self.services .users - .set_token(&authorizing_user, &device_id, access_token.clone()) + .set_token( + &authorizing_user, + &device_id, + access_token.clone(), + PreviousTokenAction::Replace, + ) .await .expect("should be able to set access token"); @@ -747,7 +752,7 @@ impl Service { let new_refresh_token = Self::generate_token(); - if refresh_token == session_info.current_refresh_token { + let previous_token_action = if refresh_token == session_info.current_refresh_token { // The client is refreshing with their current refresh token. // We need to generate a new refresh token and save this token as // the previous one. @@ -767,6 +772,7 @@ impl Service { } session_info.previous_refresh_token = Some(refresh_token); + PreviousTokenAction::Replace } else if Some(&refresh_token) == session_info.previous_refresh_token.as_ref() { // The client is refreshing with their previous refresh token, // probably because they didn't get the new one we made for them. We @@ -781,6 +787,7 @@ impl Service { session_info .current_refresh_token .clone_from(&new_refresh_token); + PreviousTokenAction::Preserve } else { // The client is trying to use a refresh token that isn't their // current or previous one, but still has information in the @@ -792,7 +799,7 @@ impl Service { ); self.db.refreshtoken_refreshtokeninfo.remove(&refresh_token); return Err(OAuthError::invalid_grant("Invalid refresh token")); - } + }; session_info.temporary_scopes.clear(); @@ -806,6 +813,7 @@ impl Service { &refresh_token_info.user_id, &refresh_token_info.device_id, new_access_token.clone(), + previous_token_action, ) .await .expect("should be able to set token"); diff --git a/src/service/users/device.rs b/src/service/users/device.rs index 3e9fd5d9b..f9e51e772 100644 --- a/src/service/users/device.rs +++ b/src/service/users/device.rs @@ -17,6 +17,18 @@ use serde_json::json; use crate::users::increment; +#[derive(Clone, Copy, Debug)] +pub enum PreviousTokenAction { + /// Discard any previously saved access token for this device. + Clear, + /// Save the replaced access token as the previous token, overwriting any + /// existing one. + Replace, + /// Keep an existing previous access token if one is already saved; + /// otherwise save the replaced access token. + Preserve, +} + #[must_use] #[derive(Clone)] pub struct DeviceToken { @@ -71,7 +83,8 @@ impl super::Service { self.db.userdeviceid_metadata.put(key, Json(device)); if let Some(token) = token { - self.set_token(user_id, &device_id, token).await?; + self.set_token(user_id, &device_id, token, PreviousTokenAction::Clear) + .await?; } Ok(device_id) @@ -165,6 +178,7 @@ impl super::Service { user_id: &UserId, device_id: &DeviceId, DeviceToken { token, max_age }: DeviceToken, + previous_token_action: PreviousTokenAction, ) -> Result<()> { let key = (user_id, device_id); if self.db.userdeviceid_metadata.qry(&key).await.is_err() { @@ -187,11 +201,21 @@ impl super::Service { self.db.token_userdeviceid.remove(&old_token); // It will be removed from userdeviceid_token by the insert later - // Save the previous token only if we don't already have one saved - // for this device. - if !self.db.userdeviceid_previoustoken.contains(&key).await { - self.db.userdeviceid_previoustoken.put_raw(key, &old_token); + match previous_token_action { + | PreviousTokenAction::Clear => { + self.db.userdeviceid_previoustoken.del(key); + }, + | PreviousTokenAction::Replace => { + self.db.userdeviceid_previoustoken.put_raw(key, &old_token); + }, + | PreviousTokenAction::Preserve => { + if !self.db.userdeviceid_previoustoken.contains(&key).await { + self.db.userdeviceid_previoustoken.put_raw(key, &old_token); + } + }, } + } else if matches!(previous_token_action, PreviousTokenAction::Clear) { + self.db.userdeviceid_previoustoken.del(key); } // Assign token to user device combination diff --git a/src/service/users/mod.rs b/src/service/users/mod.rs index 7865876a5..a09396a0e 100644 --- a/src/service/users/mod.rs +++ b/src/service/users/mod.rs @@ -11,7 +11,7 @@ use std::{mem, sync::Arc}; pub use account::{AccessTokenStatus, AccountStatus}; use conduwuit::{Err, Error, Result, err, utils}; use database::Map; -pub use device::DeviceToken; +pub use device::{DeviceToken, PreviousTokenAction}; pub use profile::ProfileFieldChange; use ruma::{UserId, api::error::ErrorKind, encryption::CrossSigningKey, serde::Raw}; use serde::{Deserialize, Serialize};