metadata: clear more things from user login metadata on auth error
Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
+6
-11
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
+1
-83
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user