Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 10 additions & 1 deletion crates/data-model/src/personal/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use oauth2_types::scope::Scope;
use serde::Serialize;
use ulid::Ulid;

use crate::{Client, InvalidTransitionError, User};
use crate::{Client, Device, InvalidTransitionError, User};

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
pub enum SessionState {
Expand Down Expand Up @@ -129,4 +129,13 @@ impl PersonalSession {
self.state = self.state.revoke(revoked_at)?;
Ok(self)
}

/// Returns whether the scope of this session contains a device scope;
/// in other words: whether this session has a device.
#[must_use]
pub fn has_device(&self) -> bool {
self.scope
.iter()
.any(|scope_token| Device::from_scope_token(scope_token).is_some())
}
}
9 changes: 9 additions & 0 deletions crates/handlers/src/admin/v1/personal_sessions/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use chrono::Duration;
use hyper::StatusCode;
use mas_axum_utils::record_error;
use mas_data_model::{BoxRng, TokenType, personal::session::PersonalSessionOwner};
use mas_storage::queue::{QueueJobRepositoryExt as _, SyncDevicesJob};
use oauth2_types::scope::Scope;
use schemars::JsonSchema;
use serde::Deserialize;
Expand Down Expand Up @@ -144,6 +145,14 @@ pub async fn handler(
)
.await?;

if session.has_device() {
// If the session has a device, then we are now
// creating a device and should schedule a device sync.
repo.queue_job()
.schedule_job(&mut rng, &clock, SyncDevicesJob::new(&actor_user))
.await?;
}

repo.save().await?;

Ok((
Expand Down
17 changes: 16 additions & 1 deletion crates/handlers/src/admin/v1/personal_sessions/revoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
// Please see LICENSE files in the repository root for full details.

use aide::{OperationIo, transform::TransformOperation};
use aide::{NoApi, OperationIo, transform::TransformOperation};
use axum::{Json, response::IntoResponse};
use hyper::StatusCode;
use mas_axum_utils::record_error;
use mas_data_model::BoxRng;
use mas_storage::queue::{QueueJobRepositoryExt as _, SyncDevicesJob};
use ulid::Ulid;

use crate::{
Expand Down Expand Up @@ -80,6 +82,7 @@ pub async fn handler(
CallContext {
mut repo, clock, ..
}: CallContext,
NoApi(mut rng): NoApi<BoxRng>,
session_id: UlidPathParam,
) -> Result<Json<SingleResponse<PersonalSession>>, RouteError> {
let session_id = *session_id;
Expand All @@ -95,6 +98,18 @@ pub async fn handler(

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

if session.has_device() {
// If the session has a device, then we are now
// deleting a device and should schedule a device sync to clean up.
repo.queue_job()
.schedule_job(
&mut rng,
&clock,
SyncDevicesJob::new_for_id(session.actor_user_id),
)
.await?;
}

repo.save().await?;

Ok(Json(SingleResponse::new_canonical(
Expand Down
Loading