storage: add a user-provided human name to OAuth 2.0 sessions

This commit is contained in:
Quentin Gliech
2025-04-25 13:48:18 +02:00
parent a10e517d4e
commit 7ec87b1855
7 changed files with 32 additions and 4 deletions

View File

@@ -83,6 +83,7 @@ pub struct Session {
pub user_agent: Option<String>,
pub last_active_at: Option<DateTime<Utc>>,
pub last_active_ip: Option<IpAddr>,
pub human_name: Option<String>,
}
impl std::ops::Deref for Session {

View File

@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "\n SELECT oauth2_session_id\n , user_id\n , user_session_id\n , oauth2_client_id\n , scope_list\n , created_at\n , finished_at\n , user_agent\n , last_active_at\n , last_active_ip as \"last_active_ip: IpAddr\"\n FROM oauth2_sessions\n\n WHERE oauth2_session_id = $1\n ",
"query": "\n SELECT oauth2_session_id\n , user_id\n , user_session_id\n , oauth2_client_id\n , scope_list\n , created_at\n , finished_at\n , user_agent\n , last_active_at\n , last_active_ip as \"last_active_ip: IpAddr\"\n , human_name\n FROM oauth2_sessions\n\n WHERE oauth2_session_id = $1\n ",
"describe": {
"columns": [
{
@@ -52,6 +52,11 @@
"ordinal": 9,
"name": "last_active_ip: IpAddr",
"type_info": "Inet"
},
{
"ordinal": 10,
"name": "human_name",
"type_info": "Text"
}
],
"parameters": {
@@ -69,8 +74,9 @@
true,
true,
true,
true,
true
]
},
"hash": "5a2e9b5002c1927c0035c22e393172b36ab46a4377b46618205151ea041886d5"
"hash": "6b8d28b76d7ab33178b46dbb28c11e41d86f22b3fa899a952cad00129e59bee6"
}

View File

@@ -23,7 +23,7 @@
"Left": []
},
"nullable": [
false,
true,
true,
null
]

View File

@@ -0,0 +1,8 @@
-- Copyright 2025 New Vector Ltd.
--
-- SPDX-License-Identifier: AGPL-3.0-only
-- Please see LICENSE in the repository root for full details.
-- Add a user-provided human name to OAuth 2.0 sessions
ALTER TABLE oauth2_sessions
ADD COLUMN human_name TEXT;

View File

@@ -192,6 +192,7 @@ impl TryFrom<AppSessionLookup> for AppSession {
user_agent,
last_active_at,
last_active_ip,
human_name,
};
Ok(AppSession::OAuth2(Box::new(session)))
@@ -299,7 +300,10 @@ impl AppSessionRepository for PgAppSessionRepository<'_> {
AppSessionLookupIden::ScopeList,
)
.expr_as(Expr::cust("NULL"), AppSessionLookupIden::DeviceId)
.expr_as(Expr::cust("NULL"), AppSessionLookupIden::HumanName)
.expr_as(
Expr::col((OAuth2Sessions::Table, OAuth2Sessions::HumanName)),
AppSessionLookupIden::HumanName,
)
.expr_as(
Expr::col((OAuth2Sessions::Table, OAuth2Sessions::CreatedAt)),
AppSessionLookupIden::CreatedAt,

View File

@@ -83,6 +83,7 @@ pub enum OAuth2Sessions {
UserAgent,
LastActiveAt,
LastActiveIp,
HumanName,
}
#[derive(sea_query::Iden)]

View File

@@ -55,6 +55,7 @@ struct OAuthSessionLookup {
user_agent: Option<String>,
last_active_at: Option<DateTime<Utc>>,
last_active_ip: Option<IpAddr>,
human_name: Option<String>,
}
impl TryFrom<OAuthSessionLookup> for Session {
@@ -90,6 +91,7 @@ impl TryFrom<OAuthSessionLookup> for Session {
user_agent: value.user_agent,
last_active_at: value.last_active_at,
last_active_ip: value.last_active_ip,
human_name: value.human_name,
})
}
}
@@ -195,6 +197,7 @@ impl OAuth2SessionRepository for PgOAuth2SessionRepository<'_> {
, user_agent
, last_active_at
, last_active_ip as "last_active_ip: IpAddr"
, human_name
FROM oauth2_sessions
WHERE oauth2_session_id = $1
@@ -270,6 +273,7 @@ impl OAuth2SessionRepository for PgOAuth2SessionRepository<'_> {
user_agent: None,
last_active_at: None,
last_active_ip: None,
human_name: None,
})
}
@@ -392,6 +396,10 @@ impl OAuth2SessionRepository for PgOAuth2SessionRepository<'_> {
Expr::col((OAuth2Sessions::Table, OAuth2Sessions::LastActiveIp)),
OAuthSessionLookupIden::LastActiveIp,
)
.expr_as(
Expr::col((OAuth2Sessions::Table, OAuth2Sessions::HumanName)),
OAuthSessionLookupIden::HumanName,
)
.from(OAuth2Sessions::Table)
.apply_filter(filter)
.generate_pagination(