-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.go
201 lines (161 loc) · 3.96 KB
/
oauth.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package sender
import (
"bufio"
"errors"
"io"
"log"
"net/url"
"os"
"regexp"
"strconv"
"strings"
)
const act = "authcheck_code"
const (
authcheckRegexp = `authcheck_code&hash=([^\"]+)`
tokenRegexp = `access_token=([^&]+)`
)
const (
grantType = "password"
version = "5.131"
)
type OauthResponse struct {
URI string `json:"redirect_uri,omitempty"`
Token string `json:"access_token,omitempty"`
}
var (
errResponseStatus = errors.New("response is not 200")
err2faCode = errors.New("your 2fa code is not a number")
errFragmentToken = errors.New("cannot find access token")
errHashNotFound = errors.New("not found match for hash")
)
func getAccessToken(auth Auth, setup Setup) string {
if setup.SaveOauth {
file, err := os.OpenFile(setup.OauthFile, os.O_RDWR|os.O_CREATE, 0600)
checkErr(err)
defer file.Close()
stat, err := file.Stat()
checkErr(err)
if stat.Size() != 0 {
// TODO check token valid
return decode(auth.Password, file)
}
// file has no data
token := requestAccessToken(auth)
encode(auth.Password, token, file)
return token
}
return requestAccessToken(auth)
}
func requestAccessToken(auth Auth) string {
response := getOauthResponse(auth)
if response.Token != "" {
return response.Token
}
// get hash for special oauth link
hash := getAuthHash(response.URI)
code := get2faCode()
loginURI := getLoginURI(hash, code)
resp, err := client.Get(loginURI)
checkErr(err)
defer resp.Body.Close()
// it can be 301 when wrong
if resp.StatusCode != 200 {
log.Fatal(errResponseStatus)
}
fragment := resp.Request.URL.Fragment
token := getTokenFromFragment(fragment)
return token
}
func getOauthResponse(auth Auth) *OauthResponse {
uri := getOauthURI(auth)
resp, err := client.Get(uri)
checkErr(err)
defer resp.Body.Close()
oauthResp := &OauthResponse{}
getStructFromJSON(resp.Body, oauthResp)
return oauthResp
}
func getOauthURI(auth Auth) string {
rawQuery := getOauthRawQuery(auth)
uri := &url.URL{
Scheme: "https",
Host: "oauth.vk.com",
Path: "token",
RawQuery: rawQuery,
}
return uri.String()
}
func getOauthRawQuery(auth Auth) string {
query := url.Values{}
query.Set("client_id", strconv.Itoa(auth.ClientID))
query.Add("client_secret", auth.ClientSecret)
query.Add("grant_type", grantType)
query.Add("password", auth.Password)
query.Add("username", auth.Username)
query.Add("version", version)
query.Add("2fa_supported", "1")
return query.Encode()
}
func getAuthHash(oauthRedirectURI string) string {
resp, err := client.Get(oauthRedirectURI)
checkErr(err)
defer resp.Body.Close()
re := regexp.MustCompile(authcheckRegexp)
hash := getMatchFromReader(resp.Body, re)
return hash
}
func getMatchFromReader(body io.ReadCloser, re *regexp.Regexp) string {
sc := bufio.NewScanner(body)
var line string
var matches []string
for sc.Scan() {
line = sc.Text()
matches = re.FindStringSubmatch(line)
if len(matches) != 0 {
// [0] is full string
// [1] is the first substring
return matches[1]
}
}
panic(errHashNotFound)
}
func get2faCode() string {
reader := bufio.NewReader(os.Stdin)
println("enter 2fa code:")
text, err := reader.ReadString('\n')
checkErr(err)
text = strings.TrimSpace(text)
ok, err := regexp.MatchString(`\d+`, text)
if !ok || err != nil {
log.Fatal(err2faCode)
}
return text
}
func getLoginURI(hash string, code string) string {
rawQuery := getLoginRawQuery(hash, code)
uri := url.URL{
Scheme: "https",
Host: "m.vk.com",
Path: "login",
RawQuery: rawQuery,
}
return uri.String()
}
func getLoginRawQuery(hash string, code string) string {
query := url.Values{}
query.Set("act", act)
query.Add("hash", hash)
query.Add("code", code)
return query.Encode()
}
func getTokenFromFragment(fragment string) string {
re := regexp.MustCompile(tokenRegexp)
matches := re.FindStringSubmatch(fragment)
if len(matches) != 0 {
// [0] is full string
// [1] is the first substring
return matches[1]
}
panic(errFragmentToken)
}