Skip to content

Commit

Permalink
v2: allow re-authing existing login
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jun 20, 2024
1 parent 58a1b87 commit 70d747e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
golang.org/x/net v0.26.0
google.golang.org/protobuf v1.34.2
maunium.net/go/mautrix v0.19.0-beta.1.0.20240619204109-68d8ab6896fd
maunium.net/go/mautrix v0.19.0-beta.1.0.20240620135116-0418273bdbb1
nhooyr.io/websocket v1.8.11
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M=
maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240619204109-68d8ab6896fd h1:NZYFpztfJCdynpWJTGOyPcYfr+95zCut/IobMzaKCdU=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240619204109-68d8ab6896fd/go.mod h1:cxv1w6+syudmEpOewHYIQT9yO7TM5UOWmf6xEBVI4H4=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240620135116-0418273bdbb1 h1:sSz/VCo3GLtnAjMBpjfn7dtN1f7RDdZ+9OTZdaxZSJc=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240620135116-0418273bdbb1/go.mod h1:cxv1w6+syudmEpOewHYIQT9yO7TM5UOWmf6xEBVI4H4=
nhooyr.io/websocket v1.8.11 h1:f/qXNc2/3DpoSZkHt1DQu6rj4zGC8JmkkLkWss0MgN0=
nhooyr.io/websocket v1.8.11/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
17 changes: 9 additions & 8 deletions pkg/connector/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func (s *SignalConnector) CreateLogin(ctx context.Context, user *bridgev2.User,

type QRLogin struct {
User *bridgev2.User
Existing *bridgev2.UserLogin
Main *SignalConnector
cancelChan context.CancelFunc
ProvChan chan signalmeow.ProvisioningResponse
Expand Down Expand Up @@ -116,9 +115,6 @@ func (qr *QRLogin) Wait(ctx context.Context) (*bridgev2.LoginStep, error) {
return nil, ctx.Err()
}
newLoginID := makeUserLoginID(signalID)
if qr.Existing != nil && qr.Existing.ID != newLoginID {
return nil, fmt.Errorf("user ID mismatch for re-auth")
}

select {
case resp := <-qr.ProvChan:
Expand All @@ -131,9 +127,15 @@ func (qr *QRLogin) Wait(ctx context.Context) (*bridgev2.LoginStep, error) {
return nil, ctx.Err()
}

var ul *bridgev2.UserLogin
var err error
if qr.Existing == nil {
ul, err := qr.Main.Bridge.GetUserLoginByID(ctx, newLoginID)
if err != nil {
return nil, fmt.Errorf("failed to get existing login: %w", err)
}
if ul.UserMXID != qr.User.MXID {
// TODO delete old user login instead of failing new login
return nil, fmt.Errorf("login ID already in use by another user")
}
if ul == nil {
ul, err = qr.User.NewLogin(ctx, &database.UserLogin{
ID: newLoginID,
Metadata: database.UserLoginMetadata{
Expand All @@ -149,7 +151,6 @@ func (qr *QRLogin) Wait(ctx context.Context) (*bridgev2.LoginStep, error) {
return nil, fmt.Errorf("failed to save new login: %w", err)
}
} else {
ul = qr.Existing
ul.Metadata.Extra["phone"] = signalPhone
ul.Metadata.RemoteName = signalPhone
err = ul.Save(ctx)
Expand Down

0 comments on commit 70d747e

Please sign in to comment.