Skip to content

Commit f0e14ee

Browse files
authored
Merge pull request #3926 from element-hq/rei/syn2mas_8_atrtdevs
syn2mas: migrate access tokens, refresh tokens and devices
2 parents 59df8a9 + 7181cc8 commit f0e14ee

File tree

43 files changed

+1640
-95
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1640
-95
lines changed

Cargo.lock

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

crates/cli/src/commands/syn2mas.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ impl Options {
223223
}
224224
let mut writer = MasWriter::new(mas_connection, writer_mas_connections).await?;
225225

226+
let clock = SystemClock::default();
226227
// TODO is this rng ok?
227228
#[allow(clippy::disallowed_methods)]
228229
let mut rng = thread_rng();
@@ -233,6 +234,7 @@ impl Options {
233234
&mut reader,
234235
&mut writer,
235236
&mas_matrix.homeserver,
237+
&clock,
236238
&mut rng,
237239
&provider_id_mappings,
238240
)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub struct CompatSession {
7272
pub state: CompatSessionState,
7373
pub user_id: Ulid,
7474
pub device: Option<Device>,
75+
pub human_name: Option<String>,
7576
pub user_session_id: Option<Ulid>,
7677
pub created_at: DateTime<Utc>,
7778
pub is_synapse_admin: bool,
Lines changed: 15 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Copyright 2025 New Vector Ltd.
2+
--
3+
-- SPDX-License-Identifier: AGPL-3.0-only
4+
-- Please see LICENSE in the repository root for full details.
5+
6+
ALTER TABLE compat_sessions
7+
-- Stores a human-readable name for the device.
8+
-- syn2mas behaviour: Will be populated from the device name in Synapse.
9+
ADD COLUMN human_name TEXT;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Copyright 2025 New Vector Ltd.
2+
--
3+
-- SPDX-License-Identifier: AGPL-3.0-only
4+
-- Please see LICENSE in the repository root for full details.
5+
6+
ALTER TABLE users
7+
-- Track whether users are guests.
8+
-- Although guest support is not present in MAS yet, syn2mas should import
9+
-- these users and therefore we should track their state.
10+
ADD COLUMN is_guest BOOLEAN NOT NULL DEFAULT FALSE;

crates/storage-pg/src/app_session.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ mod priv_ {
6464
pub(super) user_id: Option<Uuid>,
6565
pub(super) scope_list: Option<Vec<String>>,
6666
pub(super) device_id: Option<String>,
67+
pub(super) human_name: Option<String>,
6768
pub(super) created_at: DateTime<Utc>,
6869
pub(super) finished_at: Option<DateTime<Utc>>,
6970
pub(super) is_synapse_admin: Option<bool>,
@@ -91,6 +92,7 @@ impl TryFrom<AppSessionLookup> for AppSession {
9192
user_id,
9293
scope_list,
9394
device_id,
95+
human_name,
9496
created_at,
9597
finished_at,
9698
is_synapse_admin,
@@ -141,6 +143,7 @@ impl TryFrom<AppSessionLookup> for AppSession {
141143
state,
142144
user_id: user_id.into(),
143145
device,
146+
human_name,
144147
user_session_id,
145148
created_at,
146149
is_synapse_admin,
@@ -294,6 +297,7 @@ impl AppSessionRepository for PgAppSessionRepository<'_> {
294297
AppSessionLookupIden::ScopeList,
295298
)
296299
.expr_as(Expr::cust("NULL"), AppSessionLookupIden::DeviceId)
300+
.expr_as(Expr::cust("NULL"), AppSessionLookupIden::HumanName)
297301
.expr_as(
298302
Expr::col((OAuth2Sessions::Table, OAuth2Sessions::CreatedAt)),
299303
AppSessionLookupIden::CreatedAt,
@@ -343,6 +347,10 @@ impl AppSessionRepository for PgAppSessionRepository<'_> {
343347
Expr::col((CompatSessions::Table, CompatSessions::DeviceId)),
344348
AppSessionLookupIden::DeviceId,
345349
)
350+
.expr_as(
351+
Expr::col((CompatSessions::Table, CompatSessions::HumanName)),
352+
AppSessionLookupIden::HumanName,
353+
)
346354
.expr_as(
347355
Expr::col((CompatSessions::Table, CompatSessions::CreatedAt)),
348356
AppSessionLookupIden::CreatedAt,

crates/storage-pg/src/compat/refresh_token.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,16 +204,25 @@ impl CompatRefreshTokenRepository for PgCompatRefreshTokenRepository<'_> {
204204
r#"
205205
UPDATE compat_refresh_tokens
206206
SET consumed_at = $2
207-
WHERE compat_refresh_token_id = $1
207+
WHERE compat_session_id = $1
208+
AND consumed_at IS NULL
208209
"#,
209-
Uuid::from(compat_refresh_token.id),
210+
Uuid::from(compat_refresh_token.session_id),
210211
consumed_at,
211212
)
212213
.traced()
213214
.execute(&mut *self.conn)
214215
.await?;
215216

216-
DatabaseError::ensure_affected_rows(&res, 1)?;
217+
// This can affect multiple rows in case we've imported refresh tokens
218+
// from Synapse. What we care about is that it at least affected one,
219+
// which is what we're checking here
220+
if res.rows_affected() == 0 {
221+
return Err(DatabaseError::RowsAffected {
222+
expected: 1,
223+
actual: 0,
224+
});
225+
}
217226

218227
let compat_refresh_token = compat_refresh_token
219228
.consume(consumed_at)

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ impl<'c> PgCompatSessionRepository<'c> {
4848
struct CompatSessionLookup {
4949
compat_session_id: Uuid,
5050
device_id: Option<String>,
51+
human_name: Option<String>,
5152
user_id: Uuid,
5253
user_session_id: Option<Uuid>,
5354
created_at: DateTime<Utc>,
@@ -85,6 +86,7 @@ impl TryFrom<CompatSessionLookup> for CompatSession {
8586
user_id: value.user_id.into(),
8687
user_session_id: value.user_session_id.map(Ulid::from),
8788
device,
89+
human_name: value.human_name,
8890
created_at: value.created_at,
8991
is_synapse_admin: value.is_synapse_admin,
9092
user_agent: value.user_agent.map(UserAgent::parse),
@@ -101,6 +103,7 @@ impl TryFrom<CompatSessionLookup> for CompatSession {
101103
struct CompatSessionAndSsoLoginLookup {
102104
compat_session_id: Uuid,
103105
device_id: Option<String>,
106+
human_name: Option<String>,
104107
user_id: Uuid,
105108
user_session_id: Option<Uuid>,
106109
created_at: DateTime<Utc>,
@@ -143,6 +146,7 @@ impl TryFrom<CompatSessionAndSsoLoginLookup> for (CompatSession, Option<CompatSs
143146
state,
144147
user_id: value.user_id.into(),
145148
device,
149+
human_name: value.human_name,
146150
user_session_id: value.user_session_id.map(Ulid::from),
147151
created_at: value.created_at,
148152
is_synapse_admin: value.is_synapse_admin,
@@ -286,6 +290,7 @@ impl CompatSessionRepository for PgCompatSessionRepository<'_> {
286290
r#"
287291
SELECT compat_session_id
288292
, device_id
293+
, human_name
289294
, user_id
290295
, user_session_id
291296
, created_at
@@ -356,6 +361,7 @@ impl CompatSessionRepository for PgCompatSessionRepository<'_> {
356361
state: CompatSessionState::default(),
357362
user_id: user.id,
358363
device: Some(device),
364+
human_name: None,
359365
user_session_id: browser_session.map(|s| s.id),
360366
created_at,
361367
is_synapse_admin,
@@ -453,6 +459,10 @@ impl CompatSessionRepository for PgCompatSessionRepository<'_> {
453459
Expr::col((CompatSessions::Table, CompatSessions::DeviceId)),
454460
CompatSessionAndSsoLoginLookupIden::DeviceId,
455461
)
462+
.expr_as(
463+
Expr::col((CompatSessions::Table, CompatSessions::HumanName)),
464+
CompatSessionAndSsoLoginLookupIden::HumanName,
465+
)
456466
.expr_as(
457467
Expr::col((CompatSessions::Table, CompatSessions::UserId)),
458468
CompatSessionAndSsoLoginLookupIden::UserId,

0 commit comments

Comments
 (0)