Skip to content

Commit 272c80c

Browse files
committed
When revoking a personal session, also revoke its PAT
1 parent 33528fa commit 272c80c

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

crates/storage-pg/.sqlx/query-9e8152d445f9996b221ad3690ba982ad01035296bf4539ca5620a043924a7292.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/storage-pg/src/personal/session.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ use mas_storage::{
2020
personal::{PersonalSessionFilter, PersonalSessionRepository, PersonalSessionState},
2121
};
2222
use oauth2_types::scope::Scope;
23+
use opentelemetry_semantic_conventions::trace::DB_QUERY_TEXT;
2324
use rand::RngCore;
2425
use sea_query::{
2526
Cond, Condition, Expr, PgFunc, PostgresQueryBuilder, Query, SimpleExpr, enum_def,
2627
extension::postgres::PgExpr as _,
2728
};
2829
use sea_query_binder::SqlxBinder as _;
2930
use sqlx::PgConnection;
31+
use tracing::{Instrument as _, info_span};
3032
use ulid::Ulid;
3133
use uuid::Uuid;
3234

@@ -311,15 +313,38 @@ impl PersonalSessionRepository for PgPersonalSessionRepository<'_> {
311313
clock: &dyn Clock,
312314
session: PersonalSession,
313315
) -> Result<PersonalSession, Self::Error> {
314-
let finished_at = clock.now();
316+
let revoked_at = clock.now();
317+
318+
{
319+
// Revoke dependent PATs
320+
let span = info_span!(
321+
"db.personal_session.revoke.tokens",
322+
{ DB_QUERY_TEXT } = tracing::field::Empty,
323+
);
324+
325+
sqlx::query!(
326+
r#"
327+
UPDATE personal_access_tokens
328+
SET revoked_at = $2
329+
WHERE personal_session_id = $1 AND revoked_at IS NULL
330+
"#,
331+
Uuid::from(session.id),
332+
revoked_at,
333+
)
334+
.record(&span)
335+
.execute(&mut *self.conn)
336+
.instrument(span)
337+
.await?;
338+
}
339+
315340
let res = sqlx::query!(
316341
r#"
317342
UPDATE personal_sessions
318343
SET revoked_at = $2
319344
WHERE personal_session_id = $1
320345
"#,
321346
Uuid::from(session.id),
322-
finished_at,
347+
revoked_at,
323348
)
324349
.traced()
325350
.execute(&mut *self.conn)
@@ -328,7 +353,7 @@ impl PersonalSessionRepository for PgPersonalSessionRepository<'_> {
328353
DatabaseError::ensure_affected_rows(&res, 1)?;
329354

330355
session
331-
.finish(finished_at)
356+
.finish(revoked_at)
332357
.map_err(DatabaseError::to_invalid_operation)
333358
}
334359

0 commit comments

Comments
 (0)