diff --git a/setup/mscs/msc3861/msc3861_user_verifier.go b/setup/mscs/msc3861/msc3861_user_verifier.go index c0f4342b..cb73106f 100644 --- a/setup/mscs/msc3861/msc3861_user_verifier.go +++ b/setup/mscs/msc3861/msc3861_user_verifier.go @@ -283,8 +283,6 @@ func (m *MSC3861UserVerifier) getUserByAccessToken(ctx context.Context, token st Msg: strings.Join([]string{"Invalid device ID in scope: ", deviceID}, ""), } } - logger.Debugf("deviceID is: %s", deviceID) - logger.Debugf("scope is: %+v", scopes) userDeviceExists := false { @@ -302,14 +300,13 @@ func (m *MSC3861UserVerifier) getUserByAccessToken(ctx context.Context, token st } } } - logger.Debugf("userDeviceExists is: %t", userDeviceExists) if !userDeviceExists { var rs api.PerformDeviceCreationResponse deviceDisplayName := "OIDC-native client" if err := m.userAPI.PerformDeviceCreation(ctx, &api.PerformDeviceCreationRequest{ Localpart: localpart, ServerName: m.serverName, - AccessToken: token, + AccessToken: "", DeviceID: &deviceID, DeviceDisplayName: &deviceDisplayName, // TODO: Cannot add IPAddr and Useragent values here. Should we care about it here? diff --git a/userapi/storage/postgres/deltas/2024123101250000_drop_primary_key_constraint.go b/userapi/storage/postgres/deltas/2024123101250000_drop_primary_key_constraint.go new file mode 100644 index 00000000..0bf7d386 --- /dev/null +++ b/userapi/storage/postgres/deltas/2024123101250000_drop_primary_key_constraint.go @@ -0,0 +1,25 @@ +package deltas + +import ( + "context" + "database/sql" + "fmt" +) + +func UpDropPrimaryKeyConstraint(ctx context.Context, tx *sql.Tx) error { + _, err := tx.ExecContext(ctx, ` +ALTER TABLE userapi_devices DROP CONSTRAINT userapi_devices_pkey;`) + if err != nil { + return fmt.Errorf("failed to execute upgrade: %w", err) + } + return nil +} + +func DownDropPrimaryKeyConstraint(ctx context.Context, tx *sql.Tx) error { + _, err := tx.ExecContext(ctx, ` + ALTER TABLE userapi_devices ADD CONSTRAINT userapi_devices_pkey PRIMARY KEY (access_token);`) + if err != nil { + return fmt.Errorf("failed to execute downgrade: %w", err) + } + return nil +} diff --git a/userapi/storage/postgres/devices_table.go b/userapi/storage/postgres/devices_table.go index b5feea07..e7645244 100644 --- a/userapi/storage/postgres/devices_table.go +++ b/userapi/storage/postgres/devices_table.go @@ -116,10 +116,16 @@ func NewPostgresDevicesTable(db *sql.DB, serverName spec.ServerName) (tables.Dev return nil, err } m := sqlutil.NewMigrator(db) - m.AddMigrations(sqlutil.Migration{ - Version: "userapi: add last_seen_ts", - Up: deltas.UpLastSeenTSIP, - }) + m.AddMigrations( + sqlutil.Migration{ + Version: "userapi: add last_seen_ts", + Up: deltas.UpLastSeenTSIP, + }, + sqlutil.Migration{ + Version: "userapi: drop primary key constraint", + Up: deltas.UpDropPrimaryKeyConstraint, + }, + ) err = m.Up(context.Background()) if err != nil { return nil, err diff --git a/userapi/storage/sqlite3/deltas/2024123101150000_drop_primary_key_constraint.go b/userapi/storage/sqlite3/deltas/2024123101150000_drop_primary_key_constraint.go new file mode 100644 index 00000000..def7a75e --- /dev/null +++ b/userapi/storage/sqlite3/deltas/2024123101150000_drop_primary_key_constraint.go @@ -0,0 +1,65 @@ +package deltas + +import ( + "context" + "database/sql" + "fmt" +) + +func UpDropPrimaryKeyConstraint(ctx context.Context, tx *sql.Tx) error { + _, err := tx.ExecContext(ctx, ` + ALTER TABLE userapi_devices RENAME TO userapi_devices_tmp; + CREATE TABLE userapi_devices ( + access_token TEXT, + session_id INTEGER, + device_id TEXT , + localpart TEXT , + server_name TEXT NOT NULL, + created_ts BIGINT, + display_name TEXT, + last_seen_ts BIGINT, + ip TEXT, + user_agent TEXT, + UNIQUE (localpart, device_id) + ); + INSERT + INTO userapi_devices ( + access_token, session_id, device_id, localpart, created_ts, display_name, last_seen_ts, ip, user_agent + ) SELECT + access_token, session_id, device_id, localpart, created_ts, display_name, created_ts, '', '' + FROM userapi_devices_tmp; + DROP TABLE userapi_devices_tmp;`) + if err != nil { + return fmt.Errorf("failed to execute upgrade: %w", err) + } + return nil +} + +func DownDropPrimaryKeyConstraint(ctx context.Context, tx *sql.Tx) error { + _, err := tx.ExecContext(ctx, ` +ALTER TABLE userapi_devices RENAME TO userapi_devices_tmp; +CREATE TABLE userapi_devices ( + access_token TEXT PRIMARY KEY, + session_id INTEGER, + device_id TEXT , + localpart TEXT , + server_name TEXT NOT NULL, + created_ts BIGINT, + display_name TEXT, + last_seen_ts BIGINT, + ip TEXT, + user_agent TEXT, + UNIQUE (localpart, device_id) + ); + INSERT + INTO userapi_devices ( + access_token, session_id, device_id, localpart, created_ts, display_name, last_seen_ts, ip, user_agent + ) SELECT + access_token, session_id, device_id, localpart, created_ts, display_name, created_ts, '', '' + FROM userapi_devices_tmp; + DROP TABLE userapi_devices_tmp;`) + if err != nil { + return fmt.Errorf("failed to execute downgrade: %w", err) + } + return nil +} diff --git a/userapi/storage/sqlite3/devices_table.go b/userapi/storage/sqlite3/devices_table.go index d5d1fed3..2eb88109 100644 --- a/userapi/storage/sqlite3/devices_table.go +++ b/userapi/storage/sqlite3/devices_table.go @@ -102,10 +102,16 @@ func NewSQLiteDevicesTable(db *sql.DB, serverName spec.ServerName) (tables.Devic return nil, err } m := sqlutil.NewMigrator(db) - m.AddMigrations(sqlutil.Migration{ - Version: "userapi: add last_seen_ts", - Up: deltas.UpLastSeenTSIP, - }) + m.AddMigrations( + sqlutil.Migration{ + Version: "userapi: add last_seen_ts", + Up: deltas.UpLastSeenTSIP, + }, + sqlutil.Migration{ + Version: "userapi: drop primary key constraint", + Up: deltas.UpDropPrimaryKeyConstraint, + }, + ) if err = m.Up(context.Background()); err != nil { return nil, err }