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{},
|
prevReactionPoll: map[networkid.PortalKey]time.Time{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !login.Metadata.(*UserLoginMetadata).Session.HasAuthKey() {
|
||||||
|
return &client, nil
|
||||||
|
}
|
||||||
|
|
||||||
dispatcher := UpdateDispatcher{
|
dispatcher := UpdateDispatcher{
|
||||||
UpdateDispatcher: tg.NewUpdateDispatcher(),
|
UpdateDispatcher: tg.NewUpdateDispatcher(),
|
||||||
EntityHandler: client.onEntityUpdate,
|
EntityHandler: client.onEntityUpdate,
|
||||||
@@ -417,6 +422,7 @@ func (t *TelegramClient) onConnectionStateChange(reason string) func() {
|
|||||||
} else {
|
} else {
|
||||||
t.sendBadCredentials("You're not logged in")
|
t.sendBadCredentials("You're not logged in")
|
||||||
t.userLogin.Metadata.(*UserLoginMetadata).Session.AuthKey = nil
|
t.userLogin.Metadata.(*UserLoginMetadata).Session.AuthKey = nil
|
||||||
|
t.client = nil
|
||||||
if err := t.userLogin.Save(ctx); err != nil {
|
if err := t.userLogin.Save(ctx); err != nil {
|
||||||
log.Err(err).Msg("failed to save user login")
|
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) {
|
func (t *TelegramClient) onAuthError(err error) {
|
||||||
t.sendBadCredentials(err.Error())
|
t.sendBadCredentials(err.Error())
|
||||||
t.userLogin.Metadata.(*UserLoginMetadata).Session.AuthKey = nil
|
t.userLogin.Metadata.(*UserLoginMetadata).Session.AuthKey = nil
|
||||||
|
t.client = nil
|
||||||
if err := t.userLogin.Save(context.Background()); err != nil {
|
if err := t.userLogin.Save(context.Background()); err != nil {
|
||||||
t.main.Bridge.Log.Err(err).Msg("failed to save user login")
|
t.main.Bridge.Log.Err(err).Msg("failed to save user login")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelegramClient) Connect(ctx context.Context) error {
|
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")
|
t.sendBadCredentials("User does not have an auth key")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -626,6 +633,7 @@ func (t *TelegramClient) LogoutRemote(ctx context.Context) {
|
|||||||
Str("action", "logout_remote").
|
Str("action", "logout_remote").
|
||||||
Int64("user_id", t.telegramUserID).
|
Int64("user_id", t.telegramUserID).
|
||||||
Logger()
|
Logger()
|
||||||
|
log.Info().Msg("Logging out")
|
||||||
|
|
||||||
err := t.ScopedStore.DeleteUserState(ctx)
|
err := t.ScopedStore.DeleteUserState(ctx)
|
||||||
if err != nil {
|
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")
|
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)
|
_, err = t.client.API().AuthLogOut(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg("failed to logout on Telegram")
|
log.Err(err).Msg("failed to logout on Telegram")
|
||||||
|
|||||||
@@ -173,6 +173,10 @@ type UserLoginSession struct {
|
|||||||
Salt int64 `json:"salt,omitempty"`
|
Salt int64 `json:"salt,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u UserLoginSession) HasAuthKey() bool {
|
||||||
|
return len(u.AuthKey) == 256
|
||||||
|
}
|
||||||
|
|
||||||
type UserLoginMetadata struct {
|
type UserLoginMetadata struct {
|
||||||
Phone string `json:"phone"`
|
Phone string `json:"phone"`
|
||||||
Session UserLoginSession `json:"session"`
|
Session UserLoginSession `json:"session"`
|
||||||
@@ -185,7 +189,7 @@ type UserLoginMetadata struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *UserLoginSession) Load(_ context.Context) (*session.Data, error) {
|
func (s *UserLoginSession) Load(_ context.Context) (*session.Data, error) {
|
||||||
if len(s.AuthKey) != 256 {
|
if !s.HasAuthKey() {
|
||||||
return nil, session.ErrNotFound
|
return nil, session.ErrNotFound
|
||||||
}
|
}
|
||||||
keyID := crypto.Key(s.AuthKey).ID()
|
keyID := crypto.Key(s.AuthKey).ID()
|
||||||
|
|||||||
Reference in New Issue
Block a user