Move and rename 'token' to 'access_token' where applicable.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2025-06-18 00:37:54 +00:00
parent 61cbd38284
commit b3a47566ff
5 changed files with 69 additions and 56 deletions

View File

@@ -110,7 +110,7 @@ pub(crate) async fn login_route(
} else { } else {
services services
.users .users
.set_token(&user_id, &device_id, &access_token) .set_access_token(&user_id, &device_id, &access_token)
.await?; .await?;
} }

View File

@@ -57,9 +57,13 @@ pub(super) async fn auth(
}; };
let token = if let Some(token) = token { let token = if let Some(token) = token {
match services.appservice.find_from_token(token).await { match services
.appservice
.find_from_access_token(token)
.await
{
| Some(reg_info) => Token::Appservice(Box::new(reg_info)), | Some(reg_info) => Token::Appservice(Box::new(reg_info)),
| _ => match services.users.find_from_token(token).await { | _ => match services.users.find_from_access_token(token).await {
| Ok((user_id, device_id)) => Token::User((user_id, device_id)), | Ok((user_id, device_id)) => Token::User((user_id, device_id)),
| _ => Token::Invalid, | _ => Token::Invalid,
}, },

View File

@@ -115,7 +115,7 @@ impl Service {
.map(|info| info.registration) .map(|info| info.registration)
} }
pub async fn find_from_token(&self, token: &str) -> Option<RegistrationInfo> { pub async fn find_from_access_token(&self, token: &str) -> Option<RegistrationInfo> {
self.read() self.read()
.await .await
.values() .values()

View File

@@ -2,8 +2,8 @@ use std::sync::Arc;
use futures::{Stream, StreamExt}; use futures::{Stream, StreamExt};
use ruma::{ use ruma::{
DeviceId, MilliSecondsSinceUnixEpoch, UserId, api::client::device::Device, DeviceId, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedUserId, UserId,
events::AnyToDeviceEvent, serde::Raw, api::client::device::Device, events::AnyToDeviceEvent, serde::Raw,
}; };
use serde_json::json; use serde_json::json;
use tuwunel_core::{ use tuwunel_core::{
@@ -38,7 +38,8 @@ pub async fn create_device(
increment(&self.db.userid_devicelistversion, user_id.as_bytes()); increment(&self.db.userid_devicelistversion, user_id.as_bytes());
self.db.userdeviceid_metadata.put(key, Json(val)); self.db.userdeviceid_metadata.put(key, Json(val));
self.set_token(user_id, device_id, token).await self.set_access_token(user_id, device_id, token)
.await
} }
/// Removes a device from a user. /// Removes a device from a user.
@@ -88,6 +89,62 @@ pub fn all_device_ids<'a>(
.map(|(_, device_id): (Ignore, &DeviceId)| device_id) .map(|(_, device_id): (Ignore, &DeviceId)| device_id)
} }
/// Replaces the access token of one device.
#[implement(super::Service)]
pub async fn set_access_token(
&self,
user_id: &UserId,
device_id: &DeviceId,
token: &str,
) -> Result {
let key = (user_id, device_id);
if self
.db
.userdeviceid_metadata
.qry(&key)
.await
.is_err()
{
return Err!(Database(error!(
?user_id,
?device_id,
"User does not exist or device has no metadata."
)));
}
// Remove old token
if let Ok(old_token) = self.db.userdeviceid_token.qry(&key).await {
self.db.token_userdeviceid.remove(&old_token);
// It will be removed from userdeviceid_token by the insert later
}
// Assign token to user device combination
self.db.userdeviceid_token.put_raw(key, token);
self.db.token_userdeviceid.raw_put(token, key);
Ok(())
}
/// Find out which user an access token belongs to.
#[implement(super::Service)]
pub async fn find_from_access_token(&self, token: &str) -> Result<(OwnedUserId, OwnedDeviceId)> {
self.db
.token_userdeviceid
.get(token)
.await
.deserialized()
}
#[implement(super::Service)]
pub async fn get_access_token(&self, user_id: &UserId, device_id: &DeviceId) -> Result<String> {
let key = (user_id, device_id);
self.db
.userdeviceid_token
.qry(&key)
.await
.deserialized()
}
#[implement(super::Service)] #[implement(super::Service)]
pub async fn add_to_device_event( pub async fn add_to_device_event(
&self, &self,

View File

@@ -7,7 +7,7 @@ use std::sync::Arc;
use futures::{Stream, StreamExt, TryFutureExt}; use futures::{Stream, StreamExt, TryFutureExt};
use ruma::{ use ruma::{
DeviceId, OwnedDeviceId, OwnedMxcUri, OwnedUserId, UserId, OwnedMxcUri, OwnedUserId, UserId,
api::client::filter::FilterDefinition, api::client::filter::FilterDefinition,
events::{GlobalAccountDataEventType, ignored_user_list::IgnoredUserListEvent}, events::{GlobalAccountDataEventType, ignored_user_list::IgnoredUserListEvent},
}; };
@@ -188,15 +188,6 @@ impl Service {
#[inline] #[inline]
pub async fn count(&self) -> usize { self.db.userid_password.count().await } pub async fn count(&self) -> usize { self.db.userid_password.count().await }
/// Find out which user an access token belongs to.
pub async fn find_from_token(&self, token: &str) -> Result<(OwnedUserId, OwnedDeviceId)> {
self.db
.token_userdeviceid
.get(token)
.await
.deserialized()
}
/// Returns an iterator over all users on this homeserver (offered for /// Returns an iterator over all users on this homeserver (offered for
/// compatibility) /// compatibility)
#[allow( #[allow(
@@ -337,45 +328,6 @@ impl Service {
} }
} }
pub async fn get_token(&self, user_id: &UserId, device_id: &DeviceId) -> Result<String> {
let key = (user_id, device_id);
self.db
.userdeviceid_token
.qry(&key)
.await
.deserialized()
}
/// Replaces the access token of one device.
pub async fn set_token(&self, user_id: &UserId, device_id: &DeviceId, token: &str) -> Result {
let key = (user_id, device_id);
if self
.db
.userdeviceid_metadata
.qry(&key)
.await
.is_err()
{
return Err!(Database(error!(
?user_id,
?device_id,
"User does not exist or device has no metadata."
)));
}
// Remove old token
if let Ok(old_token) = self.db.userdeviceid_token.qry(&key).await {
self.db.token_userdeviceid.remove(&old_token);
// It will be removed from userdeviceid_token by the insert later
}
// Assign token to user device combination
self.db.userdeviceid_token.put_raw(key, token);
self.db.token_userdeviceid.raw_put(token, key);
Ok(())
}
/// Creates a new sync filter. Returns the filter id. /// Creates a new sync filter. Returns the filter id.
pub fn create_filter(&self, user_id: &UserId, filter: &FilterDefinition) -> String { pub fn create_filter(&self, user_id: &UserId, filter: &FilterDefinition) -> String {
let filter_id = utils::random_string(4); let filter_id = utils::random_string(4);