forked from celestix/gotgproto
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthHelper.go
122 lines (111 loc) · 3.18 KB
/
authHelper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package gotgproto
import (
"context"
"fmt"
"github.com/gotd/td/telegram/auth"
"github.com/gotd/td/tg"
"github.com/pkg/errors"
)
func IfAuthNecessary(c *auth.Client, ctx context.Context, flow Flow) error {
auth, err := c.Status(ctx)
if err != nil {
return errors.Wrap(err, "get auth status")
}
if auth.Authorized {
return nil
}
if err := authFlow(flow, ctx, c); err != nil {
return errors.Wrap(err, "auth flow")
}
return nil
}
type Flow auth.Flow
func (f Flow) handleSignUp(ctx context.Context, client auth.FlowClient, phone, hash string, s *auth.SignUpRequired) error {
if err := f.Auth.AcceptTermsOfService(ctx, s.TermsOfService); err != nil {
return errors.Wrap(err, "confirm TOS")
}
info, err := f.Auth.SignUp(ctx)
if err != nil {
return errors.Wrap(err, "sign up info not provided")
}
if _, err := client.SignUp(ctx, auth.SignUp{
PhoneNumber: phone,
PhoneCodeHash: hash,
FirstName: info.FirstName,
LastName: info.LastName,
}); err != nil {
return errors.Wrap(err, "sign up")
}
return nil
}
func authFlow(f Flow, ctx context.Context, client *auth.Client) error {
if f.Auth == nil {
return errors.New("no UserAuthenticator provided")
}
phone, err := f.Auth.Phone(ctx)
if err != nil {
return errors.Wrap(err, "get phone")
}
sentCode, err := client.SendCode(ctx, phone, f.Options)
if err != nil {
return errors.Wrap(err, "send code")
}
switch s := sentCode.(type) {
case *tg.AuthSentCode:
hash := s.PhoneCodeHash
code, err := f.Auth.Code(ctx, s)
if err != nil {
return errors.Wrap(err, "get code")
}
_, signInErr := client.SignIn(ctx, phone, code, hash)
if errors.Is(signInErr, auth.ErrPasswordAuthNeeded) {
err = signInErr
for i := 0; err != nil && i < 3; i++ {
if i != 0 {
fmt.Println("The 2FA Code you just entered seems to be incorrect,")
fmt.Println("Attempts Left:", 3-i)
fmt.Println("Please try again.... ")
}
password, err1 := f.Auth.Password(ctx)
if err1 != nil {
return errors.Wrap(err1, "get password")
}
_, err = client.Password(ctx, password)
}
if err != nil {
return errors.Wrap(err, "sign in with password")
}
// if _, err := client.Password(ctx, password); err != nil {
// return errors.Wrap(err, "sign in with password")
// }
return nil
}
var signUpRequired *auth.SignUpRequired
if errors.As(signInErr, &signUpRequired) {
return f.handleSignUp(ctx, client, phone, hash, signUpRequired)
}
if signInErr != nil {
// fmt.Println("\n\n", signInErr.Error(), "\n\n ")
return errors.Wrap(signInErr, "sign in")
}
case *tg.AuthSentCodeSuccess:
switch a := s.Authorization.(type) {
case *tg.AuthAuthorization:
// Looks that we are already authorized.
return nil
case *tg.AuthAuthorizationSignUpRequired:
if err := f.handleSignUp(ctx, client, phone, "", &auth.SignUpRequired{
TermsOfService: a.TermsOfService,
}); err != nil {
// TODO: not sure that blank hash will work here
return errors.Wrap(err, "sign up after auth sent code success")
}
return nil
default:
return errors.Errorf("unexpected authorization type: %T", a)
}
default:
return errors.Errorf("unexpected sent code type: %T", sentCode)
}
return nil
}