Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connector: send BAD_CREDENTIALS if error is an auth error #78

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions pkg/connector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,26 +376,38 @@ func connectTelegramClient(ctx context.Context, client *telegram.Client) (contex
}

func (t *TelegramClient) onDead() {
prevState := t.userLogin.BridgeState.GetPrev().StateEvent
if slices.Contains([]status.BridgeStateEvent{
status.StateTransientDisconnect,
status.StateBadCredentials,
status.StateLoggedOut,
status.StateUnknownError,
}, prevState) {
t.userLogin.Log.Warn().
Str("prev_state", string(prevState)).
Msg("client is dead, not sending transient disconnect, because already in an error state")
return
}
t.userLogin.BridgeState.Send(status.BridgeState{
StateEvent: status.StateTransientDisconnect,
Message: "Telegram client disconnected",
})
}

func (t *TelegramClient) sendBadCredentials(message string) {
t.userLogin.BridgeState.Send(status.BridgeState{
StateEvent: status.StateBadCredentials,
Error: "tg-no-auth",
Message: message,
})
}

func (t *TelegramClient) sendUnknownError(err error) {
t.userLogin.BridgeState.Send(status.BridgeState{
StateEvent: status.StateUnknownError,
Error: "tg-unknown-error",
Message: humanise.Error(err),
})
func (t *TelegramClient) sendBadCredentialsOrUnknownError(err error) {
if auth.IsUnauthorized(err) {
t.userLogin.BridgeState.Send(status.BridgeState{
StateEvent: status.StateBadCredentials,
Error: "tg-no-auth",
Message: humanise.Error(err),
})
} else {
t.userLogin.BridgeState.Send(status.BridgeState{
StateEvent: status.StateUnknownError,
Error: "tg-unknown-error",
Message: humanise.Error(err),
})
}
}

func (t *TelegramClient) onConnectionStateChange(reason string) func() {
Expand All @@ -419,7 +431,7 @@ func (t *TelegramClient) onConnectionStateChange(reason string) func() {
t.Disconnect()
t.Connect(ctx)
} else {
t.sendUnknownError(err)
t.sendBadCredentialsOrUnknownError(err)
}
} else if authStatus.Authorized {
t.userLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
Expand All @@ -430,7 +442,7 @@ func (t *TelegramClient) onConnectionStateChange(reason string) func() {
}

func (t *TelegramClient) onAuthError(ctx context.Context, err error) {
t.sendBadCredentials(humanise.Error(err))
t.sendBadCredentialsOrUnknownError(err)
t.userLogin.Metadata.(*UserLoginMetadata).ResetOnLogout()
t.client = nil
t.updatesManager.Reset()
Expand All @@ -441,35 +453,31 @@ func (t *TelegramClient) onAuthError(ctx context.Context, err error) {

func (t *TelegramClient) Connect(ctx context.Context) {
if !t.userLogin.Metadata.(*UserLoginMetadata).Session.HasAuthKey() {
t.sendBadCredentials("User does not have an auth key")
t.sendBadCredentialsOrUnknownError(errors.New("user does not have an auth key"))
return
}

var err error
ctx, t.clientCancel, err = connectTelegramClient(ctx, t.client)
if err != nil {
t.sendUnknownError(err)
t.sendBadCredentialsOrUnknownError(err)
return
}
go func() {
err = t.updatesManager.Run(ctx, t.client.API(), t.telegramUserID, updates.AuthOptions{})
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("failed to run updates manager")
if auth.IsUnauthorized(err) {
t.sendBadCredentials(err.Error())
} else {
t.sendUnknownError(err)
}
t.sendBadCredentialsOrUnknownError(err)
t.clientCancel()
}
}()

// Update the logged-in user's ghost info (this also updates the user
// login's remote name and profile).
if me, err := t.client.Self(ctx); err != nil {
t.sendUnknownError(err)
t.sendBadCredentialsOrUnknownError(err)
} else if _, err := t.updateGhost(ctx, t.telegramUserID, me); err != nil {
t.sendUnknownError(err)
t.sendBadCredentialsOrUnknownError(err)
} else {
t.userLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
}
Expand Down
Loading