Skip to content

Commit

Permalink
Merge pull request #637 from gotd/fix/auth-handle-401
Browse files Browse the repository at this point in the history
fix(auth): improve unauthorized errors handling
  • Loading branch information
ernado committed Dec 20, 2021
2 parents 09e25ef + c200af5 commit fdf0b30
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
4 changes: 0 additions & 4 deletions telegram/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,3 @@ func (c *Client) QR() qrlogin.QR {
qrlogin.Options{Migrate: c.MigrateTo},
)
}

func unauthorized(err error) bool {
return auth.IsKeyUnregistered(err)
}
9 changes: 9 additions & 0 deletions telegram/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import (
)

// IsKeyUnregistered reports whether err is AUTH_KEY_UNREGISTERED error.
//
// Deprecated: use IsUnauthorized.
func IsKeyUnregistered(err error) bool {
return tgerr.Is(err, "AUTH_KEY_UNREGISTERED")
}

// IsUnauthorized reports whether err is 401 UNAUTHORIZED.
//
// https://core.telegram.org/api/errors#401-unauthorized
func IsUnauthorized(err error) bool {
return tgerr.IsCode(err, 401)
}
2 changes: 1 addition & 1 deletion telegram/auth/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Status struct {
// Status gets authorization status of client.
func (c *Client) Status(ctx context.Context) (*Status, error) {
u, err := c.self(ctx)
if IsKeyUnregistered(err) {
if IsUnauthorized(err) {
return &Status{}, nil
}
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion telegram/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/gotd/td/internal/exchange"
"github.com/gotd/td/internal/tdsync"
"github.com/gotd/td/telegram/auth"
)

func (c *Client) runUntilRestart(ctx context.Context) error {
Expand All @@ -25,7 +26,7 @@ func (c *Client) runUntilRestart(ctx context.Context) error {
self, err := c.Self(ctx)
if err != nil {
// Ignore unauthorized errors.
if !unauthorized(err) {
if !auth.IsUnauthorized(err) {
c.log.Warn("Got error on self", zap.Error(err))
}
return nil
Expand Down
3 changes: 2 additions & 1 deletion telegram/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/gotd/td/internal/mtproto"
"github.com/gotd/td/internal/pool"
"github.com/gotd/td/telegram/auth"
"github.com/gotd/td/telegram/dcs"
"github.com/gotd/td/telegram/internal/manager"
"github.com/gotd/td/tg"
Expand Down Expand Up @@ -96,7 +97,7 @@ func (c *Client) dc(ctx context.Context, dcID int, max int64, dialer mtproto.Dia
_, err = c.transfer(ctx, tg.NewClient(p), dcID)
if err != nil {
// Ignore case then we are not authorized.
if unauthorized(err) {
if auth.IsUnauthorized(err) {
return p, nil
}

Expand Down

0 comments on commit fdf0b30

Please sign in to comment.