forked from shelomentsevd/mtproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
79 lines (66 loc) · 1.66 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package mtproto
import (
"errors"
"fmt"
"strconv"
"strings"
)
func (m *MTProto) AuthSendCode(phonenumber string) (*TL_auth_sentCode, error) {
var authSentCode TL_auth_sentCode
flag := true
for flag {
x := <-m.InvokeAsync(TL_auth_sendCode{
Allow_flashcall: false,
Phone_number: phonenumber,
Current_number: TL_boolTrue{},
Api_id: m.appConfig.Id,
Api_hash: m.appConfig.Hash,
})
if x.err != nil {
// TODO: Maybe there are different ways to do it
// MTProto connected to new DC(see handleRPCError), trying to get data again
if strings.Contains(x.err.Error(), strconv.Itoa(errorSeeOther)) {
continue
}
return nil, x.err
}
switch x.data.(type) {
case TL_auth_sentCode:
authSentCode = x.data.(TL_auth_sentCode)
flag = false
default:
return nil, fmt.Errorf("Got: %T", x)
}
}
return &authSentCode, nil
}
func (m *MTProto) AuthSignIn(phoneNumber, phoneCode, phoneCodeHash string) (*TL_auth_authorization, error) {
if phoneNumber == "" || phoneCode == "" || phoneCodeHash == "" {
return nil, errors.New("MRProto::AuthSignIn one of function parameters is empty")
}
x := <-m.InvokeAsync(TL_auth_signIn{
Phone_number: phoneNumber,
Phone_code_hash: phoneCodeHash,
Phone_code: phoneCode,
})
if x.err != nil {
return nil, x.err
}
auth, ok := x.data.(TL_auth_authorization)
if !ok {
return nil, fmt.Errorf("RPC: %#v", x)
}
return &auth, nil
}
func (m *MTProto) AuthLogOut() (bool, error) {
var result bool
x := <-m.InvokeAsync(TL_auth_logOut{})
if x.err != nil {
return result, x.err
}
result, err := ToBool(x.data)
if err != nil {
return result, err
}
return result, err
}