-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
151 lines (134 loc) · 3.22 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package vkutil
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/url"
"github.com/fatih/color"
"github.com/zhuharev/vk"
)
func (api *Api) SignUp(firstName, lastName, phone string, args ...url.Values) (sid string, e error) {
uv := url.Values{
"first_name": {firstName},
"last_name": {lastName},
"phone": {phone},
"client_id": {fmt.Sprint(api.VkApi.ClientId)},
"client_secret": {api.VkApi.ClientSecret},
}
if len(args) > 0 {
for k, v := range args[0] {
if len(v) > 0 {
uv.Set(k, v[0])
}
}
}
resp, e := api.VkApi.Request(vk.METHOD_AUTH_SIGNUP, uv)
if e != nil {
return "", e
}
color.Green("%s", resp)
type Response struct {
Resp struct {
Sid string `json:"sid"`
} `json:"response"`
ResponseError
}
var r Response
e = json.Unmarshal(resp, &r)
if e != nil {
return "", e
}
if r.Error.Code == 14 && api.StdinAllow {
color.Red("Captcha")
color.Yellow("Open in browser %s", r.Error.CaptchaImg)
var captcha string
fmt.Scanln(&captcha)
var a = url.Values{}
if len(args) > 0 {
a = args[0]
}
a.Set("captcha_sid", r.Error.CaptchaSid)
a.Set("captcha_key", captcha)
return api.SignUp(firstName, lastName, phone, a)
}
if r.Error.Code != 0 {
return "", errors.New(r.Error.Msg)
}
return r.Resp.Sid, nil
}
func (api *Api) Confirm(phone string, code string, password string) (sid string, e error) {
resp, e := api.VkApi.Request(vk.METHOD_AUTH_CONFIRM, url.Values{
"phone": {phone},
"client_id": {fmt.Sprint(api.VkApi.ClientId)},
"client_secret": {api.VkApi.ClientSecret},
"code": {code},
"password": {password},
})
color.Green("Confirm with code %s", code)
if e != nil {
return "", e
}
type Response struct {
Resp struct {
Sid string `json:"sid"`
} `json:"response"`
ResponseError
}
color.Green("%s", resp)
var r Response
e = json.Unmarshal(resp, &r)
if e != nil {
return "", e
}
if r.Error.Code == 14 && api.StdinAllow {
color.Red("Captcha")
color.Yellow("Open in browser %s", r.Error.CaptchaImg)
var captcha string
fmt.Scanln(&captcha)
var a = url.Values{}
a.Set("captcha_sid", r.Error.CaptchaSid)
a.Set("captcha_key", captcha)
return api.Confirm(phone, code, password)
}
if r.Error.Code != 0 {
return "", errors.New(r.Error.Msg)
}
return r.Resp.Sid, nil
}
// DirectAuth autorize client and get access_token as official Vk's android app
func (api *Api) DirectAuth(login, password string) error {
var authURL = "https://oauth.vk.com/token"
var params = url.Values{
"grant_type": {"password"},
"client_id": {fmt.Sprint(Android)},
"client_secret": {AndroidSecret},
"username": {login},
"password": {password},
"v": {vk.VkAPIVersion},
"scope": {UPAll.String()},
}
res, e := api.VkApi.HTTPClient().Get(authURL + "?" + params.Encode())
if e != nil {
return e
}
defer res.Body.Close()
type TokenResponse struct {
Token string `json:"access_token"`
UserId int `json:"user_id"`
}
var tr TokenResponse
bts, e := ioutil.ReadAll(res.Body)
if e != nil {
return e
}
log.Printf("%s", bts)
e = json.Unmarshal(bts, &tr)
if e != nil {
return e
}
api.VkApi.AccessToken = tr.Token
api.VkApi.UserId = fmt.Sprint(tr.UserId)
return e
}