From 53dec19878519dd8897acf84209650ab9a67c67a Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 10 Apr 2026 22:58:16 +0300 Subject: [PATCH] login: increase buffer for QR renewal --- pkg/connector/login.go | 1 + pkg/connector/loginqr.go | 8 +++----- pkg/gotd/telegram/auth/qrlogin/qrlogin.go | 11 ++++++----- pkg/gotd/telegram/auth/qrlogin/token.go | 7 ++++--- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/pkg/connector/login.go b/pkg/connector/login.go index 2b6bc267..5502f341 100644 --- a/pkg/connector/login.go +++ b/pkg/connector/login.go @@ -133,6 +133,7 @@ func (bl *baseLogin) makeClient(ctx context.Context, dispatcher *tg.UpdateDispat Logger: zaplog, Device: bl.main.deviceConfig(), UpdateHandler: updateManager, + NoUpdates: true, }) bl.ctx, bl.cancel = context.WithTimeoutCause(log.WithContext(bl.main.Bridge.BackgroundCtx), LoginTimeout, ErrLoginTimeout) diff --git a/pkg/connector/loginqr.go b/pkg/connector/loginqr.go index 7f8c151b..0fa65323 100644 --- a/pkg/connector/loginqr.go +++ b/pkg/connector/loginqr.go @@ -70,7 +70,8 @@ func (ql *QRLogin) Start(ctx context.Context) (*bridgev2.LoginStep, error) { loggedIn := make(chan struct{}) dispatcher := tg.NewUpdateDispatcher() dispatcher.OnLoginToken(func(ctx context.Context, e tg.Entities, update *tg.UpdateLoginToken) error { - loggedIn <- struct{}{} + log.Debug().Msg("Received updateLoginToken") + close(loggedIn) return nil }) err := ql.makeClient(ctx, &dispatcher) @@ -78,13 +79,10 @@ func (ql *QRLogin) Start(ctx context.Context) (*bridgev2.LoginStep, error) { return nil, err } - qr := qrlogin.NewQR(ql.client.API(), ql.main.Config.APIID, ql.main.Config.APIHash, qrlogin.Options{ - Migrate: ql.client.MigrateTo, - }) ql.qrToken = make(chan qrlogin.Token) ql.auth = make(chan qrAuthResult) go func() { - auth, err := qr.Auth(ql.ctx, loggedIn, func(ctx context.Context, token qrlogin.Token) error { + auth, err := ql.client.QR().Auth(ql.ctx, loggedIn, func(ctx context.Context, token qrlogin.Token) error { ql.qrToken <- token return nil }) diff --git a/pkg/gotd/telegram/auth/qrlogin/qrlogin.go b/pkg/gotd/telegram/auth/qrlogin/qrlogin.go index 64c44b04..510e3ead 100644 --- a/pkg/gotd/telegram/auth/qrlogin/qrlogin.go +++ b/pkg/gotd/telegram/auth/qrlogin/qrlogin.go @@ -140,6 +140,8 @@ func OnLoginToken(d interface { return loggedIn } +const QRRenewBuffer = 3 * time.Second + // Auth generates new QR login token, shows it and awaits acceptation. // // NB: Show callback may be called more than once if QR expires. @@ -150,7 +152,7 @@ func (q QR) Auth( exceptIDs ...int64, ) (*tg.AuthAuthorization, error) { until := func(token Token) time.Duration { - return token.Expires().Sub(q.clock.Now()).Truncate(time.Second) + return token.Expires().Sub(q.clock.Now()) - QRRenewBuffer } token, err := q.Export(ctx, exceptIDs...) @@ -173,7 +175,7 @@ func (q QR) Auth( defer clock.StopTimer(timer) for { - if err := show(ctx, token); err != nil { + if err = show(ctx, token); err != nil { return nil, errors.Wrap(err, "show") } @@ -181,18 +183,17 @@ func (q QR) Auth( case <-ctx.Done(): return nil, ctx.Err() case <-timer.C(): - t, err := q.Export(ctx, exceptIDs...) + token, err = q.Export(ctx, exceptIDs...) if err != nil { return nil, err } - if t.Empty() { + if token.Empty() { // If empty token, it means AuthLoginTokenSuccess was returned. // QR was scanned and accepted, break to import. break } - token = t timer.Reset(until(token)) continue diff --git a/pkg/gotd/telegram/auth/qrlogin/token.go b/pkg/gotd/telegram/auth/qrlogin/token.go index f79b4af9..eec4075a 100644 --- a/pkg/gotd/telegram/auth/qrlogin/token.go +++ b/pkg/gotd/telegram/auth/qrlogin/token.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "image" "net/url" + "strings" "time" "github.com/go-faster/errors" @@ -33,7 +34,7 @@ func ParseTokenURL(u string) (Token, error) { if q.Get("token") == "" { return Token{}, errors.New("token is empty") } - token, err := base64.URLEncoding.DecodeString(q.Get("token")) + token, err := base64.RawURLEncoding.DecodeString(strings.TrimRight(q.Get("token"), "=")) if err != nil { return Token{}, err } @@ -56,7 +57,7 @@ func (t Token) Expires() time.Time { // String implements fmt.Stringer. func (t Token) String() string { - return base64.URLEncoding.EncodeToString(t.token) + return base64.RawURLEncoding.EncodeToString(t.token) } // Empty reports whether token is empty. @@ -68,7 +69,7 @@ func (t Token) Empty() bool { // // See https://core.telegram.org/api/qr-login#exporting-a-login-token. func (t Token) URL() string { - return "tg://login?token=" + base64.URLEncoding.EncodeToString(t.token) + return "tg://login?token=" + base64.RawURLEncoding.EncodeToString(t.token) } // Image returns QR image.