Skip to content

Commit fe75448

Browse files
committed
storage: get both the stable & unstable scopes when looking for devices
1 parent 1632b16 commit fe75448

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

crates/storage-pg/.sqlx/query-373f7eb215b0e515b000a37e55bd055954f697f257de026b74ec408938a52a1a.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

crates/storage-pg/.sqlx/query-5da7a197e0008f100ad4daa78f4aa6515f0fc9eb54075e8d6d15520d25b75172.json

Lines changed: 17 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/app_session.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,17 +499,24 @@ impl AppSessionRepository for PgAppSessionRepository<'_> {
499499
.instrument(span)
500500
.await?;
501501

502-
if let Ok(device_as_scope_token) = device.to_scope_token() {
502+
if let Ok([stable_device_as_scope_token, unstable_device_as_scope_token]) =
503+
device.to_scope_token()
504+
{
503505
let span = tracing::info_span!(
504506
"db.app_session.finish_sessions_to_replace_device.oauth2_sessions",
505507
{ DB_QUERY_TEXT } = tracing::field::Empty,
506508
);
507509
sqlx::query!(
508510
"
509-
UPDATE oauth2_sessions SET finished_at = $3 WHERE user_id = $1 AND $2 = ANY(scope_list) AND finished_at IS NULL
511+
UPDATE oauth2_sessions
512+
SET finished_at = $4
513+
WHERE user_id = $1
514+
AND ($2 = ANY(scope_list) OR $3 = ANY(scope_list))
515+
AND finished_at IS NULL
510516
",
511517
Uuid::from(user.id),
512-
device_as_scope_token.as_str(),
518+
stable_device_as_scope_token.as_str(),
519+
unstable_device_as_scope_token.as_str(),
513520
finished_at
514521
)
515522
.record(&span)
@@ -652,7 +659,10 @@ mod tests {
652659
.unwrap();
653660

654661
let device2 = Device::generate(&mut rng);
655-
let scope = Scope::from_iter([OPENID, device2.to_scope_token().unwrap()]);
662+
let scope: Scope = [OPENID]
663+
.into_iter()
664+
.chain(device2.to_scope_token().unwrap().into_iter())
665+
.collect();
656666

657667
// We're moving the clock forward by 1 minute between each session to ensure
658668
// we're getting consistent ordering in lists.

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ use mas_storage::{
1515
};
1616
use oauth2_types::scope::{Scope, ScopeToken};
1717
use rand::RngCore;
18-
use sea_query::{Expr, PgFunc, PostgresQueryBuilder, Query, enum_def, extension::postgres::PgExpr};
18+
use sea_query::{
19+
Condition, Expr, PgFunc, PostgresQueryBuilder, Query, SimpleExpr, enum_def,
20+
extension::postgres::PgExpr,
21+
};
1922
use sea_query_binder::SqlxBinder;
2023
use sqlx::PgConnection;
2124
use ulid::Ulid;
@@ -126,12 +129,19 @@ impl Filter for OAuth2SessionFilter<'_> {
126129
.ne(Expr::all(static_clients))
127130
}
128131
}))
129-
.add_option(self.device().map(|device| {
130-
if let Ok(scope_token) = device.to_scope_token() {
131-
Expr::val(scope_token.to_string()).eq(PgFunc::any(Expr::col((
132-
OAuth2Sessions::Table,
133-
OAuth2Sessions::ScopeList,
134-
))))
132+
.add_option(self.device().map(|device| -> SimpleExpr {
133+
if let Ok([stable_scope_token, unstable_scope_token]) = device.to_scope_token() {
134+
Condition::any()
135+
.add(
136+
Expr::val(stable_scope_token.to_string()).eq(PgFunc::any(Expr::col((
137+
OAuth2Sessions::Table,
138+
OAuth2Sessions::ScopeList,
139+
)))),
140+
)
141+
.add(Expr::val(unstable_scope_token.to_string()).eq(PgFunc::any(
142+
Expr::col((OAuth2Sessions::Table, OAuth2Sessions::ScopeList)),
143+
)))
144+
.into()
135145
} else {
136146
// If the device ID can't be encoded as a scope token, match no rows
137147
Expr::val(false).into()

0 commit comments

Comments
 (0)