-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
auth.go
58 lines (48 loc) · 1.25 KB
/
auth.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
package e2etest
import (
"context"
"github.com/cenkalti/backoff/v4"
"github.com/go-faster/errors"
"github.com/gotd/td/telegram/auth"
)
func (s *Suite) createFlow(ctx context.Context) (auth.Flow, error) {
var ua auth.UserAuthenticator
for {
ua = auth.Test(s.rand, s.dc)
phone, err := ua.Phone(ctx)
if err != nil {
return auth.Flow{}, err
}
s.usedMux.Lock()
if _, ok := s.used[phone]; !ok {
s.used[phone] = struct{}{}
s.usedMux.Unlock()
break
}
s.usedMux.Unlock()
}
return auth.NewFlow(ua, auth.SendCodeOptions{}), nil
}
// Authenticate authenticates client on test server.
func (s *Suite) Authenticate(ctx context.Context, client auth.FlowClient) error {
for {
flow, err := s.createFlow(ctx)
if err != nil {
return errors.Wrap(err, "create flow")
}
if err := flow.Run(ctx, client); err != nil {
if errors.Is(err, auth.ErrPasswordNotProvided) {
continue
}
return errors.Wrap(err, "run flow")
}
return nil
}
}
// RetryAuthenticate authenticates client on test server.
func (s *Suite) RetryAuthenticate(ctx context.Context, client auth.FlowClient) error {
bck := backoff.WithContext(backoff.NewExponentialBackOff(), ctx)
return backoff.Retry(func() error {
return s.Authenticate(ctx, client)
}, bck)
}