mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-27 19:07:33 +00:00
fix: Overwrite previous access token on non-retry refreshes
Previously, set_token skipped saving the replaced access token whenever a previous token entry already existed. If a client refreshed again with its current refresh token before making a valid API request (such as after going idle until expiry), a dropped response would leave the server holding an older access token and cause a hard logout. Let callers explicitly specify whether to clear, replace, or preserve the previous access token.
This commit is contained in:
4 files changed
+44
-12
No files matched your search
@@ -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
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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};
|
||||
|
||||
Reference in new issue
Block a user