From 7ce3dacf00b463a5a4ddbd53ef3529c37f5bef66 Mon Sep 17 00:00:00 2001 From: Sumner Evans Date: Mon, 2 Dec 2024 12:24:49 -0700 Subject: [PATCH] metadata: clear more things from user login metadata on auth error Signed-off-by: Sumner Evans --- pkg/connector/client.go | 17 +++---- pkg/connector/config.go | 84 +--------------------------------- pkg/connector/metadata.go | 96 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 94 deletions(-) create mode 100644 pkg/connector/metadata.go diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 28301141..c52e0ee4 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -234,7 +234,7 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge OnSession: client.onConnectionStateChange("session"), OnConnected: client.onConnectionStateChange("connected"), PingCallback: client.onConnectionStateChange("ping"), - OnAuthError: client.onAuthError, + OnAuthError: func(err error) { client.onAuthError(context.Background(), err) }, PingTimeout: time.Duration(tc.Config.Ping.TimeoutSeconds) * time.Second, PingInterval: time.Duration(tc.Config.Ping.IntervalSeconds) * time.Second, Device: telegram.DeviceConfig{ @@ -428,21 +428,16 @@ func (t *TelegramClient) onConnectionStateChange(reason string) func() { } else if authStatus.Authorized { t.userLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected}) } else { - t.sendBadCredentials("You're not logged in") - t.userLogin.Metadata.(*UserLoginMetadata).Session.AuthKey = nil - t.client = nil - if err := t.userLogin.Save(ctx); err != nil { - log.Err(err).Msg("failed to save user login") - } + t.onAuthError(ctx, fmt.Errorf("not logged in")) } } } -func (t *TelegramClient) onAuthError(err error) { - t.sendBadCredentials(err.Error()) - t.userLogin.Metadata.(*UserLoginMetadata).Session.AuthKey = nil +func (t *TelegramClient) onAuthError(ctx context.Context, err error) { + t.sendBadCredentials(humanise.Error(err)) + t.userLogin.Metadata.(*UserLoginMetadata).ResetOnLogout() t.client = nil - if err := t.userLogin.Save(context.Background()); err != nil { + if err := t.userLogin.Save(ctx); err != nil { t.main.Bridge.Log.Err(err).Msg("failed to save user login") } } diff --git a/pkg/connector/config.go b/pkg/connector/config.go index d9ed2563..f30fa684 100644 --- a/pkg/connector/config.go +++ b/pkg/connector/config.go @@ -1,18 +1,13 @@ package connector import ( - "context" _ "embed" "fmt" "slices" - "github.com/gotd/td/crypto" - "github.com/gotd/td/session" up "go.mau.fi/util/configupgrade" "maunium.net/go/mautrix/bridgev2" "maunium.net/go/mautrix/bridgev2/bridgeconfig" - "maunium.net/go/mautrix/bridgev2/database" - "maunium.net/go/mautrix/bridgev2/networkid" "maunium.net/go/mautrix/id" "go.mau.fi/mautrix-telegram/pkg/connector/media" @@ -28,7 +23,7 @@ type MemberListConfig struct { func (c MemberListConfig) NormalizedMaxInitialSync() int { if c.MaxInitialSync < 0 { - return 10000 + return 10_000 } return c.MaxInitialSync } @@ -137,80 +132,3 @@ func (tg *TelegramConnector) ValidateConfig() error { } return nil } - -func (tg *TelegramConnector) GetDBMetaTypes() database.MetaTypes { - return database.MetaTypes{ - Ghost: func() any { return &GhostMetadata{} }, - Portal: func() any { return &PortalMetadata{} }, - Message: func() any { return &MessageMetadata{} }, - Reaction: nil, - UserLogin: func() any { return &UserLoginMetadata{} }, - } -} - -type GhostMetadata struct { - IsPremium bool `json:"is_premium,omitempty"` - IsBot bool `json:"is_bot,omitempty"` - IsChannel bool `json:"is_channel,omitempty"` - Blocked bool `json:"blocked,omitempty"` - Deleted bool `json:"deleted,omitempty"` -} - -type PortalMetadata struct { - IsSuperGroup bool `json:"is_supergroup,omitempty"` - ReadUpTo int `json:"read_up_to,omitempty"` - MessagesTTL int `json:"messages_ttl,omitempty"` -} - -type MessageMetadata struct { - ContentHash []byte `json:"content_hash,omitempty"` - ContentURI id.ContentURIString `json:"content_uri,omitempty"` -} - -type UserLoginSession struct { - AuthKey []byte `json:"auth_key,omitempty"` - Datacenter int `json:"dc_id,omitempty"` - ServerAddress string `json:"server_address,omitempty"` - ServerPort int `json:"port,omitempty"` - Salt int64 `json:"salt,omitempty"` -} - -func (u UserLoginSession) HasAuthKey() bool { - return len(u.AuthKey) == 256 -} - -type UserLoginMetadata struct { - Phone string `json:"phone"` - Session UserLoginSession `json:"session"` - TakeoutID int64 `json:"takeout_id,omitempty"` - - TakeoutDialogCrawlDone bool `json:"takeout_portal_crawl_done,omitempty"` - TakeoutDialogCrawlCursor networkid.PortalID `json:"takeout_portal_crawl_cursor,omitempty"` - - PinnedDialogs []networkid.PortalID `json:"pinned_dialogs,omitempty"` - - PushEncryptionKey []byte `json:"push_encryption_key,omitempty"` -} - -func (s *UserLoginSession) Load(_ context.Context) (*session.Data, error) { - if !s.HasAuthKey() { - return nil, session.ErrNotFound - } - keyID := crypto.Key(s.AuthKey).ID() - return &session.Data{ - DC: s.Datacenter, - Addr: s.ServerAddress, - AuthKey: s.AuthKey, - AuthKeyID: keyID[:], - Salt: s.Salt, - }, nil -} - -func (s *UserLoginSession) Save(ctx context.Context, data *session.Data) error { - s.Datacenter = data.DC - s.ServerAddress = data.Addr - s.AuthKey = data.AuthKey - s.Salt = data.Salt - // TODO save UserLogin to database? - return nil -} diff --git a/pkg/connector/metadata.go b/pkg/connector/metadata.go new file mode 100644 index 00000000..eaf50aa2 --- /dev/null +++ b/pkg/connector/metadata.go @@ -0,0 +1,96 @@ +package connector + +import ( + "context" + + "github.com/gotd/td/crypto" + "github.com/gotd/td/session" + "maunium.net/go/mautrix/bridgev2/database" + "maunium.net/go/mautrix/bridgev2/networkid" + "maunium.net/go/mautrix/id" +) + +func (tg *TelegramConnector) GetDBMetaTypes() database.MetaTypes { + return database.MetaTypes{ + Ghost: func() any { return &GhostMetadata{} }, + Portal: func() any { return &PortalMetadata{} }, + Message: func() any { return &MessageMetadata{} }, + Reaction: nil, + UserLogin: func() any { return &UserLoginMetadata{} }, + } +} + +type GhostMetadata struct { + IsPremium bool `json:"is_premium,omitempty"` + IsBot bool `json:"is_bot,omitempty"` + IsChannel bool `json:"is_channel,omitempty"` + Blocked bool `json:"blocked,omitempty"` + Deleted bool `json:"deleted,omitempty"` +} + +type PortalMetadata struct { + IsSuperGroup bool `json:"is_supergroup,omitempty"` + ReadUpTo int `json:"read_up_to,omitempty"` + MessagesTTL int `json:"messages_ttl,omitempty"` +} + +type MessageMetadata struct { + ContentHash []byte `json:"content_hash,omitempty"` + ContentURI id.ContentURIString `json:"content_uri,omitempty"` +} + +type UserLoginMetadata struct { + Phone string `json:"phone"` + Session UserLoginSession `json:"session"` + TakeoutID int64 `json:"takeout_id,omitempty"` + + TakeoutDialogCrawlDone bool `json:"takeout_portal_crawl_done,omitempty"` + TakeoutDialogCrawlCursor networkid.PortalID `json:"takeout_portal_crawl_cursor,omitempty"` + + PinnedDialogs []networkid.PortalID `json:"pinned_dialogs,omitempty"` + + PushEncryptionKey []byte `json:"push_encryption_key,omitempty"` +} + +func (u *UserLoginMetadata) ResetOnLogout() { + u.Session.AuthKey = nil + u.TakeoutID = 0 + u.TakeoutDialogCrawlDone = false + u.TakeoutDialogCrawlCursor = networkid.PortalID("") + u.PushEncryptionKey = nil +} + +type UserLoginSession struct { + AuthKey []byte `json:"auth_key,omitempty"` + Datacenter int `json:"dc_id,omitempty"` + ServerAddress string `json:"server_address,omitempty"` + ServerPort int `json:"port,omitempty"` + Salt int64 `json:"salt,omitempty"` +} + +func (u UserLoginSession) HasAuthKey() bool { + return len(u.AuthKey) == 256 +} + +func (s *UserLoginSession) Load(_ context.Context) (*session.Data, error) { + if !s.HasAuthKey() { + return nil, session.ErrNotFound + } + keyID := crypto.Key(s.AuthKey).ID() + return &session.Data{ + DC: s.Datacenter, + Addr: s.ServerAddress, + AuthKey: s.AuthKey, + AuthKeyID: keyID[:], + Salt: s.Salt, + }, nil +} + +func (s *UserLoginSession) Save(ctx context.Context, data *session.Data) error { + s.Datacenter = data.DC + s.ServerAddress = data.Addr + s.AuthKey = data.AuthKey + s.Salt = data.Salt + // TODO save UserLogin to database? + return nil +}