legacyprovisioning: add check for auth key on logout
Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
+13
-1
@@ -136,6 +136,11 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge
|
||||
|
||||
prevReactionPoll: map[networkid.PortalKey]time.Time{},
|
||||
}
|
||||
|
||||
if !login.Metadata.(*UserLoginMetadata).Session.HasAuthKey() {
|
||||
return &client, nil
|
||||
}
|
||||
|
||||
dispatcher := UpdateDispatcher{
|
||||
UpdateDispatcher: tg.NewUpdateDispatcher(),
|
||||
EntityHandler: client.onEntityUpdate,
|
||||
@@ -417,6 +422,7 @@ func (t *TelegramClient) onConnectionStateChange(reason string) func() {
|
||||
} 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")
|
||||
}
|
||||
@@ -427,13 +433,14 @@ func (t *TelegramClient) onConnectionStateChange(reason string) func() {
|
||||
func (t *TelegramClient) onAuthError(err error) {
|
||||
t.sendBadCredentials(err.Error())
|
||||
t.userLogin.Metadata.(*UserLoginMetadata).Session.AuthKey = nil
|
||||
t.client = nil
|
||||
if err := t.userLogin.Save(context.Background()); err != nil {
|
||||
t.main.Bridge.Log.Err(err).Msg("failed to save user login")
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TelegramClient) Connect(ctx context.Context) error {
|
||||
if len(t.userLogin.Metadata.(*UserLoginMetadata).Session.AuthKey) != 256 {
|
||||
if !t.userLogin.Metadata.(*UserLoginMetadata).Session.HasAuthKey() {
|
||||
t.sendBadCredentials("User does not have an auth key")
|
||||
return nil
|
||||
}
|
||||
@@ -626,6 +633,7 @@ func (t *TelegramClient) LogoutRemote(ctx context.Context) {
|
||||
Str("action", "logout_remote").
|
||||
Int64("user_id", t.telegramUserID).
|
||||
Logger()
|
||||
log.Info().Msg("Logging out")
|
||||
|
||||
err := t.ScopedStore.DeleteUserState(ctx)
|
||||
if err != nil {
|
||||
@@ -642,6 +650,10 @@ func (t *TelegramClient) LogoutRemote(ctx context.Context) {
|
||||
log.Err(err).Msg("failed to delete access hashes for user")
|
||||
}
|
||||
|
||||
if !t.userLogin.Metadata.(*UserLoginMetadata).Session.HasAuthKey() {
|
||||
log.Info().Msg("User does not have an auth key, not logging out")
|
||||
}
|
||||
|
||||
_, err = t.client.API().AuthLogOut(ctx)
|
||||
if err != nil {
|
||||
log.Err(err).Msg("failed to logout on Telegram")
|
||||
|
||||
@@ -173,6 +173,10 @@ type UserLoginSession struct {
|
||||
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"`
|
||||
@@ -185,7 +189,7 @@ type UserLoginMetadata struct {
|
||||
}
|
||||
|
||||
func (s *UserLoginSession) Load(_ context.Context) (*session.Data, error) {
|
||||
if len(s.AuthKey) != 256 {
|
||||
if !s.HasAuthKey() {
|
||||
return nil, session.ErrNotFound
|
||||
}
|
||||
keyID := crypto.Key(s.AuthKey).ID()
|
||||
|
||||
Reference in New Issue
Block a user