Skip to content

Commit 42f6664

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

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

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

Lines changed: 10 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,13 @@ 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+
#[must_use]
136+
pub fn has_device(&self) -> bool {
137+
self.scope
138+
.iter()
139+
.any(|scope_token| Device::from_scope_token(scope_token).is_some())
140+
}
132141
}

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::{QueueJobRepositoryExt as _, 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: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
44
// Please see LICENSE files in the repository root for full details.
55

6-
use aide::{OperationIo, transform::TransformOperation};
6+
use aide::{NoApi, OperationIo, transform::TransformOperation};
77
use axum::{Json, response::IntoResponse};
88
use hyper::StatusCode;
99
use mas_axum_utils::record_error;
10+
use mas_data_model::BoxRng;
11+
use mas_storage::queue::{QueueJobRepositoryExt as _, SyncDevicesJob};
1012
use ulid::Ulid;
1113

1214
use crate::{
@@ -80,6 +82,7 @@ pub async fn handler(
8082
CallContext {
8183
mut repo, clock, ..
8284
}: CallContext,
85+
NoApi(mut rng): NoApi<BoxRng>,
8386
session_id: UlidPathParam,
8487
) -> Result<Json<SingleResponse<PersonalSession>>, RouteError> {
8588
let session_id = *session_id;
@@ -95,6 +98,18 @@ pub async fn handler(
9598

9699
let session = repo.personal_session().revoke(&clock, session).await?;
97100

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

100115
Ok(Json(SingleResponse::new_canonical(

0 commit comments

Comments
 (0)