Skip to content

Commit

Permalink
v2: improve handling of logins in bad credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Jun 27, 2024
1 parent 4e5db90 commit be21dac
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 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.20240626184459-c9314c6a63f8
maunium.net/go/mautrix v0.19.0-beta.1.0.20240627083250-e25578d435a2
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.20240626184459-c9314c6a63f8 h1:zxjQQrIOXw5MyE82I2SmWsgAFgh9lB8Ijjdilld59/U=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240626184459-c9314c6a63f8/go.mod h1:eu/C1dTewrW7yiFNiCKGm4zuWJANyt7zPjaY5g3f3r4=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240627083250-e25578d435a2 h1:HnP1dmvCVO/V22BdJre56Vu1IaEtMRUt86AD/1rmyME=
maunium.net/go/mautrix v0.19.0-beta.1.0.20240627083250-e25578d435a2/go.mod h1:eu/C1dTewrW7yiFNiCKGm4zuWJANyt7zPjaY5g3f3r4=
nhooyr.io/websocket v1.8.11 h1:f/qXNc2/3DpoSZkHt1DQu6rj4zGC8JmkkLkWss0MgN0=
nhooyr.io/websocket v1.8.11/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
19 changes: 19 additions & 0 deletions pkg/connector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,19 @@ func (s *SignalClient) GetPushConfigs() *bridgev2.PushConfig {
}

func (s *SignalClient) RegisterPushNotifications(ctx context.Context, pushType bridgev2.PushType, token string) error {
if s.Client == nil {
return bridgev2.ErrNotLoggedIn
}
if pushType != bridgev2.PushTypeFCM {
return fmt.Errorf("unsupported push type: %s", pushType)
}
return s.Client.RegisterFCM(ctx, token)
}

func (s *SignalClient) LogoutRemote(ctx context.Context) {
if s.Client == nil {
return
}
err := s.Client.StopReceiveLoops()
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to stop receive loops for logout")
Expand All @@ -88,6 +94,9 @@ func (s *SignalClient) LogoutRemote(ctx context.Context) {
}

func (s *SignalClient) IsThisUser(_ context.Context, userID networkid.UserID) bool {
if s.Client == nil {
return false
}
return userID == makeUserID(s.Client.Store.ACI)
}

Expand Down Expand Up @@ -200,13 +209,20 @@ func (s *SignalClient) Connect(ctx context.Context) error {
}

func (s *SignalClient) Disconnect() {
if s.Client == nil {
return
}
err := s.Client.StopReceiveLoops()
if err != nil {
s.UserLogin.Log.Err(err).Msg("Failed to stop receive loops")
}
}

func (s *SignalClient) tryConnect(ctx context.Context, retryCount int) {
if s.Client == nil {
s.UserLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateBadCredentials, Message: "You're not logged into Signal"})
return
}
ch, err := s.Client.StartReceiveLoops(ctx)
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to start receive loops")
Expand All @@ -225,5 +241,8 @@ func (s *SignalClient) tryConnect(ctx context.Context, retryCount int) {
}

func (s *SignalClient) IsLoggedIn() bool {
if s.Client == nil {
return false
}
return s.Client.IsLoggedIn()
}
12 changes: 6 additions & 6 deletions pkg/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,17 @@ func (s *SignalConnector) LoadUserLogin(ctx context.Context, login *bridgev2.Use
device, err := s.Store.DeviceByACI(ctx, aci)
if err != nil {
return fmt.Errorf("failed to get device from store: %w", err)
} else if device == nil {
return fmt.Errorf("%w: device not found in store", bridgev2.ErrNotLoggedIn)
}
sc := &SignalClient{
Main: s,
UserLogin: login,
Client: &signalmeow.Client{
Store: device,
},
}
sc.Client.EventHandler = sc.handleSignalEvent
if device != nil {
sc.Client = &signalmeow.Client{
Store: device,
EventHandler: sc.handleSignalEvent,
}
}
login.Client = sc
return nil
}

0 comments on commit be21dac

Please sign in to comment.