Skip to content

Allow setting custom names on sessions #4459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/cli/src/commands/manage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ impl Options {

let compat_session = repo
.compat_session()
.add(&mut rng, &clock, &user, device, None, admin)
.add(&mut rng, &clock, &user, device, None, admin, None)
.await?;

let token = TokenType::CompatAccessToken.generate(&mut rng);
Expand Down
2 changes: 2 additions & 0 deletions crates/data-model/src/oauth2/authorization_grant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ pub struct AuthorizationGrant {
pub response_type_id_token: bool,
pub created_at: DateTime<Utc>,
pub login_hint: Option<String>,
pub locale: Option<String>,
}

impl std::ops::Deref for AuthorizationGrant {
Expand Down Expand Up @@ -263,6 +264,7 @@ impl AuthorizationGrant {
response_type_id_token: false,
created_at: now,
login_hint: Some(String::from("mxid:@example-user:example.com")),
locale: Some(String::from("fr")),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/data-model/src/oauth2/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions crates/handlers/src/admin/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ pub struct CompatSession {

/// The time this session was finished
pub finished_at: Option<DateTime<Utc>>,

/// The user-provided name, if any
pub human_name: Option<String>,
}

impl
Expand All @@ -210,6 +213,7 @@ impl
last_active_at: session.last_active_at,
last_active_ip: session.last_active_ip,
finished_at,
human_name: session.human_name,
}
}
}
Expand Down Expand Up @@ -237,6 +241,7 @@ impl CompatSession {
last_active_at: Some(DateTime::default()),
last_active_ip: Some([1, 2, 3, 4].into()),
finished_at: None,
human_name: Some("Laptop".to_owned()),
},
Self {
id: Ulid::from_bytes([0x02; 16]),
Expand All @@ -249,6 +254,7 @@ impl CompatSession {
last_active_at: Some(DateTime::default()),
last_active_ip: Some([1, 2, 3, 4].into()),
finished_at: Some(DateTime::default()),
human_name: None,
},
Self {
id: Ulid::from_bytes([0x03; 16]),
Expand All @@ -261,6 +267,7 @@ impl CompatSession {
last_active_at: None,
last_active_ip: None,
finished_at: None,
human_name: None,
},
]
}
Expand Down Expand Up @@ -301,6 +308,9 @@ pub struct OAuth2Session {

/// The last IP address used by the session
last_active_ip: Option<IpAddr>,

/// The user-provided name, if any
human_name: Option<String>,
}

impl From<mas_data_model::Session> for OAuth2Session {
Expand All @@ -316,6 +326,7 @@ impl From<mas_data_model::Session> for OAuth2Session {
user_agent: session.user_agent,
last_active_at: session.last_active_at,
last_active_ip: session.last_active_ip,
human_name: session.human_name,
}
}
}
Expand All @@ -335,6 +346,7 @@ impl OAuth2Session {
user_agent: Some("Mozilla/5.0".to_owned()),
last_active_at: Some(DateTime::default()),
last_active_ip: Some("127.0.0.1".parse().unwrap()),
human_name: Some("Laptop".to_owned()),
},
Self {
id: Ulid::from_bytes([0x02; 16]),
Expand All @@ -347,6 +359,7 @@ impl OAuth2Session {
user_agent: None,
last_active_at: None,
last_active_ip: None,
human_name: None,
},
Self {
id: Ulid::from_bytes([0x03; 16]),
Expand All @@ -359,6 +372,7 @@ impl OAuth2Session {
user_agent: Some("Mozilla/5.0".to_owned()),
last_active_at: Some(DateTime::default()),
last_active_ip: Some("127.0.0.1".parse().unwrap()),
human_name: None,
},
]
}
Expand Down
9 changes: 5 additions & 4 deletions crates/handlers/src/admin/v1/compat_sessions/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ mod tests {
let device = Device::generate(&mut rng);
let session = repo
.compat_session()
.add(&mut rng, &state.clock, &user, device, None, false)
.add(&mut rng, &state.clock, &user, device, None, false, None)
.await
.unwrap();
repo.save().await.unwrap();
Expand All @@ -119,7 +119,7 @@ mod tests {
let response = state.request(request).await;
response.assert_status(StatusCode::OK);
let body: serde_json::Value = response.json();
assert_json_snapshot!(body, @r###"
assert_json_snapshot!(body, @r#"
{
"data": {
"type": "compat-session",
Expand All @@ -133,7 +133,8 @@ mod tests {
"user_agent": null,
"last_active_at": null,
"last_active_ip": null,
"finished_at": null
"finished_at": null,
"human_name": null
},
"links": {
"self": "/api/admin/v1/compat-sessions/01FSHN9AG0QHEHKX2JNQ2A2D07"
Expand All @@ -143,7 +144,7 @@ mod tests {
"self": "/api/admin/v1/compat-sessions/01FSHN9AG0QHEHKX2JNQ2A2D07"
}
}
"###);
"#);
}

#[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
Expand Down
35 changes: 20 additions & 15 deletions crates/handlers/src/admin/v1/compat_sessions/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ mod tests {

let device = Device::generate(&mut rng);
repo.compat_session()
.add(&mut rng, &state.clock, &alice, device, None, false)
.add(&mut rng, &state.clock, &alice, device, None, false, None)
.await
.unwrap();
let device = Device::generate(&mut rng);
Expand All @@ -260,7 +260,7 @@ mod tests {

let session = repo
.compat_session()
.add(&mut rng, &state.clock, &bob, device, None, false)
.add(&mut rng, &state.clock, &bob, device, None, false, None)
.await
.unwrap();
state.clock.advance(Duration::minutes(1));
Expand All @@ -276,7 +276,7 @@ mod tests {
let response = state.request(request).await;
response.assert_status(StatusCode::OK);
let body: serde_json::Value = response.json();
assert_json_snapshot!(body, @r###"
assert_json_snapshot!(body, @r#"
{
"meta": {
"count": 2
Expand All @@ -294,7 +294,8 @@ mod tests {
"user_agent": null,
"last_active_at": null,
"last_active_ip": null,
"finished_at": null
"finished_at": null,
"human_name": null
},
"links": {
"self": "/api/admin/v1/compat-sessions/01FSHNB530AAPR7PEV8KNBZD5Y"
Expand All @@ -312,7 +313,8 @@ mod tests {
"user_agent": null,
"last_active_at": null,
"last_active_ip": null,
"finished_at": "2022-01-16T14:43:00Z"
"finished_at": "2022-01-16T14:43:00Z",
"human_name": null
},
"links": {
"self": "/api/admin/v1/compat-sessions/01FSHNCZP0PPF7X0EVMJNECPZW"
Expand All @@ -325,7 +327,7 @@ mod tests {
"last": "/api/admin/v1/compat-sessions?page[last]=10"
}
}
"###);
"#);

// Filter by user
let request = Request::get(format!(
Expand All @@ -337,7 +339,7 @@ mod tests {
let response = state.request(request).await;
response.assert_status(StatusCode::OK);
let body: serde_json::Value = response.json();
assert_json_snapshot!(body, @r###"
assert_json_snapshot!(body, @r#"
{
"meta": {
"count": 1
Expand All @@ -355,7 +357,8 @@ mod tests {
"user_agent": null,
"last_active_at": null,
"last_active_ip": null,
"finished_at": null
"finished_at": null,
"human_name": null
},
"links": {
"self": "/api/admin/v1/compat-sessions/01FSHNB530AAPR7PEV8KNBZD5Y"
Expand All @@ -368,7 +371,7 @@ mod tests {
"last": "/api/admin/v1/compat-sessions?filter[user]=01FSHN9AG0MZAA6S4AF7CTV32E&page[last]=10"
}
}
"###);
"#);

// Filter by status (active)
let request = Request::get("/api/admin/v1/compat-sessions?filter[status]=active")
Expand All @@ -377,7 +380,7 @@ mod tests {
let response = state.request(request).await;
response.assert_status(StatusCode::OK);
let body: serde_json::Value = response.json();
assert_json_snapshot!(body, @r###"
assert_json_snapshot!(body, @r#"
{
"meta": {
"count": 1
Expand All @@ -395,7 +398,8 @@ mod tests {
"user_agent": null,
"last_active_at": null,
"last_active_ip": null,
"finished_at": null
"finished_at": null,
"human_name": null
},
"links": {
"self": "/api/admin/v1/compat-sessions/01FSHNB530AAPR7PEV8KNBZD5Y"
Expand All @@ -408,7 +412,7 @@ mod tests {
"last": "/api/admin/v1/compat-sessions?filter[status]=active&page[last]=10"
}
}
"###);
"#);

// Filter by status (finished)
let request = Request::get("/api/admin/v1/compat-sessions?filter[status]=finished")
Expand All @@ -417,7 +421,7 @@ mod tests {
let response = state.request(request).await;
response.assert_status(StatusCode::OK);
let body: serde_json::Value = response.json();
assert_json_snapshot!(body, @r###"
assert_json_snapshot!(body, @r#"
{
"meta": {
"count": 1
Expand All @@ -435,7 +439,8 @@ mod tests {
"user_agent": null,
"last_active_at": null,
"last_active_ip": null,
"finished_at": "2022-01-16T14:43:00Z"
"finished_at": "2022-01-16T14:43:00Z",
"human_name": null
},
"links": {
"self": "/api/admin/v1/compat-sessions/01FSHNCZP0PPF7X0EVMJNECPZW"
Expand All @@ -448,6 +453,6 @@ mod tests {
"last": "/api/admin/v1/compat-sessions?filter[status]=finished&page[last]=10"
}
}
"###);
"#);
}
}
7 changes: 4 additions & 3 deletions crates/handlers/src/admin/v1/oauth2_sessions/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ mod tests {
response.assert_status(StatusCode::OK);
let body: serde_json::Value = response.json();
assert_eq!(body["data"]["type"], "oauth2-session");
insta::assert_json_snapshot!(body, @r###"
insta::assert_json_snapshot!(body, @r#"
{
"data": {
"type": "oauth2-session",
Expand All @@ -124,7 +124,8 @@ mod tests {
"scope": "urn:mas:admin",
"user_agent": null,
"last_active_at": null,
"last_active_ip": null
"last_active_ip": null,
"human_name": null
},
"links": {
"self": "/api/admin/v1/oauth2-sessions/01FSHN9AG0MKGTBNZ16RDR3PVY"
Expand All @@ -134,7 +135,7 @@ mod tests {
"self": "/api/admin/v1/oauth2-sessions/01FSHN9AG0MKGTBNZ16RDR3PVY"
}
}
"###);
"#);
}

#[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]
Expand Down
7 changes: 4 additions & 3 deletions crates/handlers/src/admin/v1/oauth2_sessions/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ mod tests {
let response = state.request(request).await;
response.assert_status(StatusCode::OK);
let body: serde_json::Value = response.json();
insta::assert_json_snapshot!(body, @r###"
insta::assert_json_snapshot!(body, @r#"
{
"meta": {
"count": 1
Expand All @@ -349,7 +349,8 @@ mod tests {
"scope": "urn:mas:admin",
"user_agent": null,
"last_active_at": null,
"last_active_ip": null
"last_active_ip": null,
"human_name": null
},
"links": {
"self": "/api/admin/v1/oauth2-sessions/01FSHN9AG0MKGTBNZ16RDR3PVY"
Expand All @@ -362,6 +363,6 @@ mod tests {
"last": "/api/admin/v1/oauth2-sessions?page[last]=10"
}
}
"###);
"#);
}
}
Loading
Loading