-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathuserAuth.go
62 lines (52 loc) · 1.33 KB
/
userAuth.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
package gotgproto
import (
"bufio"
"context"
"errors"
"fmt"
"os"
"strings"
"github.com/gotd/td/telegram/auth"
"github.com/gotd/td/tg"
)
// noSignUp can be embedded to prevent signing up.
type noSignUp struct{}
func (noSignUp) SignUp(_ context.Context) (auth.UserInfo, error) {
return auth.UserInfo{}, errors.New("not implemented")
}
func (noSignUp) AcceptTermsOfService(_ context.Context, tos tg.HelpTermsOfService) error {
return &auth.SignUpRequired{TermsOfService: tos}
}
// termAuth implements authentication via terminal.
type termAuth struct {
client *auth.Client
noSignUp
phone string
}
func (a termAuth) Phone(_ context.Context) (string, error) {
if a.phone != "" {
return a.phone, nil
}
fmt.Print("Enter Phone Number: ")
phone, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSpace(phone), nil
}
func (termAuth) Password(_ context.Context) (string, error) {
fmt.Print("Enter 2FA password: ")
pass, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSpace(pass), nil
}
func (termAuth) Code(_ context.Context, _ *tg.AuthSentCode) (string, error) {
fmt.Print("Enter Code: ")
code, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSpace(code), nil
}