From 57940f38eaef17f5453d7785823fdcee4b313159 Mon Sep 17 00:00:00 2001 From: strawberry Date: Thu, 25 Jul 2024 23:07:54 -0400 Subject: [PATCH] set last_seen_ip on new/initial device creation this is not automatically updating, but at least have something useful there instead of nothing Signed-off-by: strawberry --- src/api/client/account.rs | 10 +++++++--- src/api/client/session.rs | 24 +++++++++++++++++------- src/service/users/data.rs | 3 ++- src/service/users/mod.rs | 3 ++- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/api/client/account.rs b/src/api/client/account.rs index f093c459..10e63d71 100644 --- a/src/api/client/account.rs +++ b/src/api/client/account.rs @@ -286,9 +286,13 @@ pub(crate) async fn register_route( let token = utils::random_string(TOKEN_LENGTH); // Create device for this account - services - .users - .create_device(&user_id, &device_id, &token, body.initial_device_display_name.clone())?; + services.users.create_device( + &user_id, + &device_id, + &token, + body.initial_device_display_name.clone(), + Some(client.to_string()), + )?; debug_info!(%user_id, %device_id, "User account was created"); diff --git a/src/api/client/session.rs b/src/api/client/session.rs index 32f3ed29..7c2a9718 100644 --- a/src/api/client/session.rs +++ b/src/api/client/session.rs @@ -1,4 +1,5 @@ use axum::extract::State; +use axum_client_ip::InsecureClientIp; use ruma::{ api::client::{ error::ErrorKind, @@ -33,8 +34,9 @@ struct Claims { /// /// Get the supported login types of this server. One of these should be used as /// the `type` field when logging in. +#[tracing::instrument(skip_all, fields(%client), name = "register")] pub(crate) async fn get_login_types_route( - _body: Ruma, + InsecureClientIp(client): InsecureClientIp, _body: Ruma, ) -> Result { Ok(get_login_types::v3::Response::new(vec![ get_login_types::v3::LoginType::Password(PasswordLoginType::default()), @@ -56,8 +58,9 @@ pub(crate) async fn get_login_types_route( /// Note: You can use [`GET /// /_matrix/client/r0/login`](fn.get_supported_versions_route.html) to see /// supported login types. +#[tracing::instrument(skip_all, fields(%client), name = "register")] pub(crate) async fn login_route( - State(services): State, body: Ruma, + State(services): State, InsecureClientIp(client): InsecureClientIp, body: Ruma, ) -> Result { // Validate login method // TODO: Other login methods @@ -176,9 +179,13 @@ pub(crate) async fn login_route( if device_exists { services.users.set_token(&user_id, &device_id, &token)?; } else { - services - .users - .create_device(&user_id, &device_id, &token, body.initial_device_display_name.clone())?; + services.users.create_device( + &user_id, + &device_id, + &token, + body.initial_device_display_name.clone(), + Some(client.to_string()), + )?; } // send client well-known if specified so the client knows to reconfigure itself @@ -214,8 +221,9 @@ pub(crate) async fn login_route( /// last seen ts) /// - Forgets to-device events /// - Triggers device list updates +#[tracing::instrument(skip_all, fields(%client), name = "register")] pub(crate) async fn logout_route( - State(services): State, body: Ruma, + State(services): State, InsecureClientIp(client): InsecureClientIp, body: Ruma, ) -> Result { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_ref().expect("user is authenticated"); @@ -241,8 +249,10 @@ pub(crate) async fn logout_route( /// Note: This is equivalent to calling [`GET /// /_matrix/client/r0/logout`](fn.logout_route.html) from each device of this /// user. +#[tracing::instrument(skip_all, fields(%client), name = "register")] pub(crate) async fn logout_all_route( - State(services): State, body: Ruma, + State(services): State, InsecureClientIp(client): InsecureClientIp, + body: Ruma, ) -> Result { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); diff --git a/src/service/users/data.rs b/src/service/users/data.rs index 2dcde7ce..e75a2e93 100644 --- a/src/service/users/data.rs +++ b/src/service/users/data.rs @@ -246,6 +246,7 @@ impl Data { /// Adds a new device to a user. pub(super) fn create_device( &self, user_id: &UserId, device_id: &DeviceId, token: &str, initial_device_display_name: Option, + client_ip: Option, ) -> Result<()> { // This method should never be called for nonexistent users. We shouldn't assert // though... @@ -266,7 +267,7 @@ impl Data { &serde_json::to_vec(&Device { device_id: device_id.into(), display_name: initial_device_display_name, - last_seen_ip: None, // TODO + last_seen_ip: client_ip, last_seen_ts: Some(MilliSecondsSinceUnixEpoch::now()), }) .expect("Device::to_string never fails."), diff --git a/src/service/users/mod.rs b/src/service/users/mod.rs index 46084415..bdb07310 100644 --- a/src/service/users/mod.rs +++ b/src/service/users/mod.rs @@ -328,9 +328,10 @@ impl Service { /// Adds a new device to a user. pub fn create_device( &self, user_id: &UserId, device_id: &DeviceId, token: &str, initial_device_display_name: Option, + client_ip: Option, ) -> Result<()> { self.db - .create_device(user_id, device_id, token, initial_device_display_name) + .create_device(user_id, device_id, token, initial_device_display_name, client_ip) } /// Removes a device from a user.