login: allow retrying phone codes and 2fa passwords (#131)
This commit is contained in:
+17
-5
@@ -159,8 +159,22 @@ func (bl *baseLogin) makeClient(ctx context.Context, dispatcher *tg.UpdateDispat
|
|||||||
}
|
}
|
||||||
|
|
||||||
var passwordLoginStep = &bridgev2.LoginStep{
|
var passwordLoginStep = &bridgev2.LoginStep{
|
||||||
Type: bridgev2.LoginStepTypeUserInput,
|
Type: bridgev2.LoginStepTypeUserInput,
|
||||||
StepID: LoginStepIDPassword,
|
StepID: LoginStepIDPassword,
|
||||||
|
Instructions: "You have two-factor authentication enabled.",
|
||||||
|
UserInputParams: &bridgev2.LoginUserInputParams{
|
||||||
|
Fields: []bridgev2.LoginInputDataField{{
|
||||||
|
Type: bridgev2.LoginInputFieldTypePassword,
|
||||||
|
ID: LoginStepIDPassword,
|
||||||
|
Name: "Password",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var passwordIncorrectLoginStep = &bridgev2.LoginStep{
|
||||||
|
Type: bridgev2.LoginStepTypeUserInput,
|
||||||
|
StepID: LoginStepIDPasswordIncorrect,
|
||||||
|
Instructions: "Incorrect password, please try again. Use the official Telegram app to reset your password if you've forgotten it.",
|
||||||
UserInputParams: &bridgev2.LoginUserInputParams{
|
UserInputParams: &bridgev2.LoginUserInputParams{
|
||||||
Fields: []bridgev2.LoginInputDataField{{
|
Fields: []bridgev2.LoginInputDataField{{
|
||||||
Type: bridgev2.LoginInputFieldTypePassword,
|
Type: bridgev2.LoginInputFieldTypePassword,
|
||||||
@@ -179,9 +193,7 @@ func (bl *baseLogin) submitPassword(ctx context.Context, password, loginPhone st
|
|||||||
authorization, err := bl.client.Auth().Password(ctx, password)
|
authorization, err := bl.client.Auth().Password(ctx, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, auth.ErrPasswordInvalid) {
|
if errors.Is(err, auth.ErrPasswordInvalid) {
|
||||||
// TODO re-prompt password instead of cancelling
|
return passwordIncorrectLoginStep, nil
|
||||||
bl.Cancel()
|
|
||||||
return nil, ErrInvalidPassword
|
|
||||||
}
|
}
|
||||||
bl.Cancel()
|
bl.Cancel()
|
||||||
return nil, fmt.Errorf("failed to submit password: %w", err)
|
return nil, fmt.Errorf("failed to submit password: %w", err)
|
||||||
|
|||||||
+33
-16
@@ -30,9 +30,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
LoginStepIDPhoneNumber = "fi.mau.telegram.login.phone_number"
|
LoginStepIDPhoneNumber = "fi.mau.telegram.login.phone_number"
|
||||||
LoginStepIDCode = "fi.mau.telegram.login.code"
|
LoginStepIDCode = "fi.mau.telegram.login.code"
|
||||||
LoginStepIDPassword = "fi.mau.telegram.login.password"
|
LoginStepIDCodeIncorrect = "fi.mau.telegram.login.code.incorrect"
|
||||||
|
LoginStepIDPassword = "fi.mau.telegram.login.password"
|
||||||
|
LoginStepIDPasswordIncorrect = "fi.mau.telegram.login.password.incorrect"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PhoneLogin struct {
|
type PhoneLogin struct {
|
||||||
@@ -86,6 +88,32 @@ func (pl *PhoneLogin) SubmitUserInput(ctx context.Context, input map[string]stri
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var phoneLoginStep = &bridgev2.LoginStep{
|
||||||
|
Type: bridgev2.LoginStepTypeUserInput,
|
||||||
|
StepID: LoginStepIDCode,
|
||||||
|
UserInputParams: &bridgev2.LoginUserInputParams{
|
||||||
|
Fields: []bridgev2.LoginInputDataField{{
|
||||||
|
Type: bridgev2.LoginInputFieldType2FACode,
|
||||||
|
ID: LoginStepIDCode,
|
||||||
|
Name: "Code",
|
||||||
|
Description: "The code was sent to the Telegram app on your phone",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var phoneCodeIncorrectStep = &bridgev2.LoginStep{
|
||||||
|
Type: bridgev2.LoginStepTypeUserInput,
|
||||||
|
StepID: LoginStepIDCodeIncorrect,
|
||||||
|
Instructions: "Incorrect code",
|
||||||
|
UserInputParams: &bridgev2.LoginUserInputParams{
|
||||||
|
Fields: []bridgev2.LoginInputDataField{{
|
||||||
|
Type: bridgev2.LoginInputFieldType2FACode,
|
||||||
|
ID: LoginStepIDCode,
|
||||||
|
Name: "Code",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func (pl *PhoneLogin) submitNumber(ctx context.Context, phone string) (*bridgev2.LoginStep, error) {
|
func (pl *PhoneLogin) submitNumber(ctx context.Context, phone string) (*bridgev2.LoginStep, error) {
|
||||||
if phone == "" {
|
if phone == "" {
|
||||||
return nil, fmt.Errorf("phone number is empty")
|
return nil, fmt.Errorf("phone number is empty")
|
||||||
@@ -105,18 +133,7 @@ func (pl *PhoneLogin) submitNumber(ctx context.Context, phone string) (*bridgev2
|
|||||||
switch s := sentCode.(type) {
|
switch s := sentCode.(type) {
|
||||||
case *tg.AuthSentCode:
|
case *tg.AuthSentCode:
|
||||||
pl.hash = s.PhoneCodeHash
|
pl.hash = s.PhoneCodeHash
|
||||||
return &bridgev2.LoginStep{
|
return phoneLoginStep, nil
|
||||||
Type: bridgev2.LoginStepTypeUserInput,
|
|
||||||
StepID: LoginStepIDCode,
|
|
||||||
UserInputParams: &bridgev2.LoginUserInputParams{
|
|
||||||
Fields: []bridgev2.LoginInputDataField{{
|
|
||||||
Type: bridgev2.LoginInputFieldType2FACode,
|
|
||||||
ID: LoginStepIDCode,
|
|
||||||
Name: "Code",
|
|
||||||
Description: "The code was sent to the Telegram app on your phone",
|
|
||||||
}},
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
case *tg.AuthSentCodeSuccess:
|
case *tg.AuthSentCodeSuccess:
|
||||||
switch authorization := s.Authorization.(type) {
|
switch authorization := s.Authorization.(type) {
|
||||||
case *tg.AuthAuthorization:
|
case *tg.AuthAuthorization:
|
||||||
@@ -140,7 +157,7 @@ func (pl *PhoneLogin) submitCode(ctx context.Context, code string) (*bridgev2.Lo
|
|||||||
pl.codeSubmitted = true
|
pl.codeSubmitted = true
|
||||||
return passwordLoginStep, nil
|
return passwordLoginStep, nil
|
||||||
} else if errors.Is(err, auth.ErrPhoneCodeInvalid) {
|
} else if errors.Is(err, auth.ErrPhoneCodeInvalid) {
|
||||||
return nil, ErrPhoneCodeInvalid
|
return phoneCodeIncorrectStep, nil
|
||||||
} else if errors.Is(err, &auth.SignUpRequired{}) {
|
} else if errors.Is(err, &auth.SignUpRequired{}) {
|
||||||
return nil, ErrSignUpNotSupported
|
return nil, ErrSignUpNotSupported
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ func (c *Client) SignIn(ctx context.Context, phone, code, codeHash string) (*tg.
|
|||||||
return nil, ErrPasswordAuthNeeded
|
return nil, ErrPasswordAuthNeeded
|
||||||
}
|
}
|
||||||
if tgerr.Is(err, "PHONE_CODE_INVALID") {
|
if tgerr.Is(err, "PHONE_CODE_INVALID") {
|
||||||
return nil, ErrPasswordAuthNeeded
|
return nil, ErrPhoneCodeInvalid
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "sign in")
|
return nil, errors.Wrap(err, "sign in")
|
||||||
|
|||||||
Reference in New Issue
Block a user