Skip to content

Commit c5c2947

Browse files
committed
When adding or revoking personal sessions, schedule needed device syncs
1 parent eeba7e1 commit c5c2947

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use oauth2_types::scope::Scope;
1010
use serde::Serialize;
1111
use ulid::Ulid;
1212

13-
use crate::{Client, InvalidTransitionError, User};
13+
use crate::{Client, Device, InvalidTransitionError, User};
1414

1515
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
1616
pub enum SessionState {
@@ -129,4 +129,12 @@ impl PersonalSession {
129129
self.state = self.state.revoke(revoked_at)?;
130130
Ok(self)
131131
}
132+
133+
/// Returns whether the scope of this session contains a device scope;
134+
/// in other words: whether this session has a device.
135+
pub fn has_device(&self) -> bool {
136+
self.scope
137+
.iter()
138+
.any(|scope_token| Device::from_scope_token(scope_token).is_some())
139+
}
132140
}

crates/handlers/src/admin/v1/personal_sessions/add.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use chrono::Duration;
99
use hyper::StatusCode;
1010
use mas_axum_utils::record_error;
1111
use mas_data_model::{BoxRng, TokenType, personal::session::PersonalSessionOwner};
12+
use mas_storage::queue::SyncDevicesJob;
1213
use oauth2_types::scope::Scope;
1314
use schemars::JsonSchema;
1415
use serde::Deserialize;
@@ -144,6 +145,14 @@ pub async fn handler(
144145
)
145146
.await?;
146147

148+
if session.has_device() {
149+
// If the session has a device, then we are now
150+
// creating a device and should schedule a device sync.
151+
repo.queue_job()
152+
.schedule_job(&mut rng, &clock, SyncDevicesJob::new(&actor_user))
153+
.await?;
154+
}
155+
147156
repo.save().await?;
148157

149158
Ok((

crates/handlers/src/admin/v1/personal_sessions/revoke.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use aide::{OperationIo, transform::TransformOperation};
77
use axum::{Json, response::IntoResponse};
88
use hyper::StatusCode;
99
use mas_axum_utils::record_error;
10+
use mas_storage::queue::{QueueJobRepositoryExt as _, SyncDevicesJob};
1011
use ulid::Ulid;
1112

1213
use crate::{
@@ -80,6 +81,7 @@ pub async fn handler(
8081
CallContext {
8182
mut repo, clock, ..
8283
}: CallContext,
84+
NoApi(mut rng): NoApi<BoxRng>,
8385
session_id: UlidPathParam,
8486
) -> Result<Json<SingleResponse<PersonalSession>>, RouteError> {
8587
let session_id = *session_id;
@@ -95,6 +97,18 @@ pub async fn handler(
9597

9698
let session = repo.personal_session().revoke(&clock, session).await?;
9799

100+
if session.has_device() {
101+
// If the session has a device, then we are now
102+
// deleting a device and should schedule a device sync to clean up.
103+
repo.queue_job()
104+
.schedule_job(
105+
&mut rng,
106+
&clock,
107+
SyncDevicesJob::new_for_id(session.actor_user_id),
108+
)
109+
.await?;
110+
}
111+
98112
repo.save().await?;
99113

100114
Ok(Json(SingleResponse::new_canonical(

0 commit comments

Comments
 (0)