Skip to content

Commit 8bfe2d2

Browse files
authored
Allow setting custom names on sessions (#4459)
2 parents acce088 + 4f9e75c commit 8bfe2d2

File tree

60 files changed

+1479
-174
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1479
-174
lines changed

crates/cli/src/commands/manage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl Options {
305305

306306
let compat_session = repo
307307
.compat_session()
308-
.add(&mut rng, &clock, &user, device, None, admin)
308+
.add(&mut rng, &clock, &user, device, None, admin, None)
309309
.await?;
310310

311311
let token = TokenType::CompatAccessToken.generate(&mut rng);

crates/data-model/src/oauth2/authorization_grant.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ pub struct AuthorizationGrant {
160160
pub response_type_id_token: bool,
161161
pub created_at: DateTime<Utc>,
162162
pub login_hint: Option<String>,
163+
pub locale: Option<String>,
163164
}
164165

165166
impl std::ops::Deref for AuthorizationGrant {
@@ -263,6 +264,7 @@ impl AuthorizationGrant {
263264
response_type_id_token: false,
264265
created_at: now,
265266
login_hint: Some(String::from("mxid:@example-user:example.com")),
267+
locale: Some(String::from("fr")),
266268
}
267269
}
268270
}

crates/data-model/src/oauth2/session.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub struct Session {
8383
pub user_agent: Option<String>,
8484
pub last_active_at: Option<DateTime<Utc>>,
8585
pub last_active_ip: Option<IpAddr>,
86+
pub human_name: Option<String>,
8687
}
8788

8889
impl std::ops::Deref for Session {

crates/handlers/src/admin/model.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ pub struct CompatSession {
184184

185185
/// The time this session was finished
186186
pub finished_at: Option<DateTime<Utc>>,
187+
188+
/// The user-provided name, if any
189+
pub human_name: Option<String>,
187190
}
188191

189192
impl
@@ -210,6 +213,7 @@ impl
210213
last_active_at: session.last_active_at,
211214
last_active_ip: session.last_active_ip,
212215
finished_at,
216+
human_name: session.human_name,
213217
}
214218
}
215219
}
@@ -237,6 +241,7 @@ impl CompatSession {
237241
last_active_at: Some(DateTime::default()),
238242
last_active_ip: Some([1, 2, 3, 4].into()),
239243
finished_at: None,
244+
human_name: Some("Laptop".to_owned()),
240245
},
241246
Self {
242247
id: Ulid::from_bytes([0x02; 16]),
@@ -249,6 +254,7 @@ impl CompatSession {
249254
last_active_at: Some(DateTime::default()),
250255
last_active_ip: Some([1, 2, 3, 4].into()),
251256
finished_at: Some(DateTime::default()),
257+
human_name: None,
252258
},
253259
Self {
254260
id: Ulid::from_bytes([0x03; 16]),
@@ -261,6 +267,7 @@ impl CompatSession {
261267
last_active_at: None,
262268
last_active_ip: None,
263269
finished_at: None,
270+
human_name: None,
264271
},
265272
]
266273
}
@@ -301,6 +308,9 @@ pub struct OAuth2Session {
301308

302309
/// The last IP address used by the session
303310
last_active_ip: Option<IpAddr>,
311+
312+
/// The user-provided name, if any
313+
human_name: Option<String>,
304314
}
305315

306316
impl From<mas_data_model::Session> for OAuth2Session {
@@ -316,6 +326,7 @@ impl From<mas_data_model::Session> for OAuth2Session {
316326
user_agent: session.user_agent,
317327
last_active_at: session.last_active_at,
318328
last_active_ip: session.last_active_ip,
329+
human_name: session.human_name,
319330
}
320331
}
321332
}
@@ -335,6 +346,7 @@ impl OAuth2Session {
335346
user_agent: Some("Mozilla/5.0".to_owned()),
336347
last_active_at: Some(DateTime::default()),
337348
last_active_ip: Some("127.0.0.1".parse().unwrap()),
349+
human_name: Some("Laptop".to_owned()),
338350
},
339351
Self {
340352
id: Ulid::from_bytes([0x02; 16]),
@@ -347,6 +359,7 @@ impl OAuth2Session {
347359
user_agent: None,
348360
last_active_at: None,
349361
last_active_ip: None,
362+
human_name: None,
350363
},
351364
Self {
352365
id: Ulid::from_bytes([0x03; 16]),
@@ -359,6 +372,7 @@ impl OAuth2Session {
359372
user_agent: Some("Mozilla/5.0".to_owned()),
360373
last_active_at: Some(DateTime::default()),
361374
last_active_ip: Some("127.0.0.1".parse().unwrap()),
375+
human_name: None,
362376
},
363377
]
364378
}

crates/handlers/src/admin/v1/compat_sessions/get.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ mod tests {
107107
let device = Device::generate(&mut rng);
108108
let session = repo
109109
.compat_session()
110-
.add(&mut rng, &state.clock, &user, device, None, false)
110+
.add(&mut rng, &state.clock, &user, device, None, false, None)
111111
.await
112112
.unwrap();
113113
repo.save().await.unwrap();
@@ -119,7 +119,7 @@ mod tests {
119119
let response = state.request(request).await;
120120
response.assert_status(StatusCode::OK);
121121
let body: serde_json::Value = response.json();
122-
assert_json_snapshot!(body, @r###"
122+
assert_json_snapshot!(body, @r#"
123123
{
124124
"data": {
125125
"type": "compat-session",
@@ -133,7 +133,8 @@ mod tests {
133133
"user_agent": null,
134134
"last_active_at": null,
135135
"last_active_ip": null,
136-
"finished_at": null
136+
"finished_at": null,
137+
"human_name": null
137138
},
138139
"links": {
139140
"self": "/api/admin/v1/compat-sessions/01FSHN9AG0QHEHKX2JNQ2A2D07"
@@ -143,7 +144,7 @@ mod tests {
143144
"self": "/api/admin/v1/compat-sessions/01FSHN9AG0QHEHKX2JNQ2A2D07"
144145
}
145146
}
146-
"###);
147+
"#);
147148
}
148149

149150
#[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]

crates/handlers/src/admin/v1/compat_sessions/list.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ mod tests {
251251

252252
let device = Device::generate(&mut rng);
253253
repo.compat_session()
254-
.add(&mut rng, &state.clock, &alice, device, None, false)
254+
.add(&mut rng, &state.clock, &alice, device, None, false, None)
255255
.await
256256
.unwrap();
257257
let device = Device::generate(&mut rng);
@@ -260,7 +260,7 @@ mod tests {
260260

261261
let session = repo
262262
.compat_session()
263-
.add(&mut rng, &state.clock, &bob, device, None, false)
263+
.add(&mut rng, &state.clock, &bob, device, None, false, None)
264264
.await
265265
.unwrap();
266266
state.clock.advance(Duration::minutes(1));
@@ -276,7 +276,7 @@ mod tests {
276276
let response = state.request(request).await;
277277
response.assert_status(StatusCode::OK);
278278
let body: serde_json::Value = response.json();
279-
assert_json_snapshot!(body, @r###"
279+
assert_json_snapshot!(body, @r#"
280280
{
281281
"meta": {
282282
"count": 2
@@ -294,7 +294,8 @@ mod tests {
294294
"user_agent": null,
295295
"last_active_at": null,
296296
"last_active_ip": null,
297-
"finished_at": null
297+
"finished_at": null,
298+
"human_name": null
298299
},
299300
"links": {
300301
"self": "/api/admin/v1/compat-sessions/01FSHNB530AAPR7PEV8KNBZD5Y"
@@ -312,7 +313,8 @@ mod tests {
312313
"user_agent": null,
313314
"last_active_at": null,
314315
"last_active_ip": null,
315-
"finished_at": "2022-01-16T14:43:00Z"
316+
"finished_at": "2022-01-16T14:43:00Z",
317+
"human_name": null
316318
},
317319
"links": {
318320
"self": "/api/admin/v1/compat-sessions/01FSHNCZP0PPF7X0EVMJNECPZW"
@@ -325,7 +327,7 @@ mod tests {
325327
"last": "/api/admin/v1/compat-sessions?page[last]=10"
326328
}
327329
}
328-
"###);
330+
"#);
329331

330332
// Filter by user
331333
let request = Request::get(format!(
@@ -337,7 +339,7 @@ mod tests {
337339
let response = state.request(request).await;
338340
response.assert_status(StatusCode::OK);
339341
let body: serde_json::Value = response.json();
340-
assert_json_snapshot!(body, @r###"
342+
assert_json_snapshot!(body, @r#"
341343
{
342344
"meta": {
343345
"count": 1
@@ -355,7 +357,8 @@ mod tests {
355357
"user_agent": null,
356358
"last_active_at": null,
357359
"last_active_ip": null,
358-
"finished_at": null
360+
"finished_at": null,
361+
"human_name": null
359362
},
360363
"links": {
361364
"self": "/api/admin/v1/compat-sessions/01FSHNB530AAPR7PEV8KNBZD5Y"
@@ -368,7 +371,7 @@ mod tests {
368371
"last": "/api/admin/v1/compat-sessions?filter[user]=01FSHN9AG0MZAA6S4AF7CTV32E&page[last]=10"
369372
}
370373
}
371-
"###);
374+
"#);
372375

373376
// Filter by status (active)
374377
let request = Request::get("/api/admin/v1/compat-sessions?filter[status]=active")
@@ -377,7 +380,7 @@ mod tests {
377380
let response = state.request(request).await;
378381
response.assert_status(StatusCode::OK);
379382
let body: serde_json::Value = response.json();
380-
assert_json_snapshot!(body, @r###"
383+
assert_json_snapshot!(body, @r#"
381384
{
382385
"meta": {
383386
"count": 1
@@ -395,7 +398,8 @@ mod tests {
395398
"user_agent": null,
396399
"last_active_at": null,
397400
"last_active_ip": null,
398-
"finished_at": null
401+
"finished_at": null,
402+
"human_name": null
399403
},
400404
"links": {
401405
"self": "/api/admin/v1/compat-sessions/01FSHNB530AAPR7PEV8KNBZD5Y"
@@ -408,7 +412,7 @@ mod tests {
408412
"last": "/api/admin/v1/compat-sessions?filter[status]=active&page[last]=10"
409413
}
410414
}
411-
"###);
415+
"#);
412416

413417
// Filter by status (finished)
414418
let request = Request::get("/api/admin/v1/compat-sessions?filter[status]=finished")
@@ -417,7 +421,7 @@ mod tests {
417421
let response = state.request(request).await;
418422
response.assert_status(StatusCode::OK);
419423
let body: serde_json::Value = response.json();
420-
assert_json_snapshot!(body, @r###"
424+
assert_json_snapshot!(body, @r#"
421425
{
422426
"meta": {
423427
"count": 1
@@ -435,7 +439,8 @@ mod tests {
435439
"user_agent": null,
436440
"last_active_at": null,
437441
"last_active_ip": null,
438-
"finished_at": "2022-01-16T14:43:00Z"
442+
"finished_at": "2022-01-16T14:43:00Z",
443+
"human_name": null
439444
},
440445
"links": {
441446
"self": "/api/admin/v1/compat-sessions/01FSHNCZP0PPF7X0EVMJNECPZW"
@@ -448,6 +453,6 @@ mod tests {
448453
"last": "/api/admin/v1/compat-sessions?filter[status]=finished&page[last]=10"
449454
}
450455
}
451-
"###);
456+
"#);
452457
}
453458
}

crates/handlers/src/admin/v1/oauth2_sessions/get.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ mod tests {
110110
response.assert_status(StatusCode::OK);
111111
let body: serde_json::Value = response.json();
112112
assert_eq!(body["data"]["type"], "oauth2-session");
113-
insta::assert_json_snapshot!(body, @r###"
113+
insta::assert_json_snapshot!(body, @r#"
114114
{
115115
"data": {
116116
"type": "oauth2-session",
@@ -124,7 +124,8 @@ mod tests {
124124
"scope": "urn:mas:admin",
125125
"user_agent": null,
126126
"last_active_at": null,
127-
"last_active_ip": null
127+
"last_active_ip": null,
128+
"human_name": null
128129
},
129130
"links": {
130131
"self": "/api/admin/v1/oauth2-sessions/01FSHN9AG0MKGTBNZ16RDR3PVY"
@@ -134,7 +135,7 @@ mod tests {
134135
"self": "/api/admin/v1/oauth2-sessions/01FSHN9AG0MKGTBNZ16RDR3PVY"
135136
}
136137
}
137-
"###);
138+
"#);
138139
}
139140

140141
#[sqlx::test(migrator = "mas_storage_pg::MIGRATOR")]

crates/handlers/src/admin/v1/oauth2_sessions/list.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ mod tests {
331331
let response = state.request(request).await;
332332
response.assert_status(StatusCode::OK);
333333
let body: serde_json::Value = response.json();
334-
insta::assert_json_snapshot!(body, @r###"
334+
insta::assert_json_snapshot!(body, @r#"
335335
{
336336
"meta": {
337337
"count": 1
@@ -349,7 +349,8 @@ mod tests {
349349
"scope": "urn:mas:admin",
350350
"user_agent": null,
351351
"last_active_at": null,
352-
"last_active_ip": null
352+
"last_active_ip": null,
353+
"human_name": null
353354
},
354355
"links": {
355356
"self": "/api/admin/v1/oauth2-sessions/01FSHN9AG0MKGTBNZ16RDR3PVY"
@@ -362,6 +363,6 @@ mod tests {
362363
"last": "/api/admin/v1/oauth2-sessions?page[last]=10"
363364
}
364365
}
365-
"###);
366+
"#);
366367
}
367368
}

0 commit comments

Comments
 (0)