-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.go
230 lines (211 loc) · 5.14 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package bingauth
import (
"encoding/json"
"fmt"
"strings"
"github.com/Harry-zklcdc/bing-lib/lib/request"
"golang.org/x/net/html"
)
const (
TYPE_PASSWD = "passwd"
TYPE_EMAIL = "email"
TYPE_DEVICE = "device"
)
type AuthStruct struct {
Account string
Password string
LoginType string
reqClient *request.Client
Id string
Lcid string
Uaid string
FlowToken string
Ppft string
Cobrandid string
UrlGetCredentialType string
CredentialType getCredentialTypeResp
UrlPostMsa string // Passwd Login Part 1
UrlPost string // Passwd Login Part 1
UrlSessionState string // Device Login Part 1
// Get Cookie
ActionUrl string
PassportData struct {
NAPExp string
PPRID string
NAP string
ANON string
ANONExp string
T string
}
}
func NewAuth(account, password, loginType string) *AuthStruct {
return &AuthStruct{
Account: account,
Password: password,
LoginType: loginType,
reqClient: request.NewRequest().SetUserAgent(USER_AGENT),
}
}
func (a *AuthStruct) SetCookie(cookies string) (err error) {
a.reqClient.SetCookies(cookies)
return
}
func (a *AuthStruct) SetContext(ctx []byte) (err error) {
cookies := a.reqClient.Cookies
err = json.Unmarshal(ctx, a)
if err != nil {
return
}
a.reqClient.Cookies = cookies
return
}
func (a *AuthStruct) Auth() (cookies string, err error) {
if err = a.getSession(); err != nil {
return "", fmt.Errorf("get session failed: %v", err)
}
if err = a.getCredentialType(); err != nil {
return "", fmt.Errorf("get credential type failed: %v", err)
}
switch a.LoginType {
case TYPE_PASSWD:
if err = a.passwdLoginPost1(); err != nil {
return "", fmt.Errorf("passwd login post1 failed: %v", err)
}
case TYPE_EMAIL:
cookies, err = a.emailLoginPost1()
if err != nil {
return "", fmt.Errorf("email login post1 failed: %v", err)
}
return cookies, fmt.Errorf("email login need code to continue")
case TYPE_DEVICE:
cookies, code, err := a.deviceLoginPost1()
if err != nil {
return "", fmt.Errorf("device login post1 failed: %v", err)
}
return cookies, fmt.Errorf("device login need handler to continue, code: %s", code)
default:
return "", fmt.Errorf("unknown login type")
}
if err = a.keepLoginPost(); err != nil {
return "", fmt.Errorf("keep login post failed: %v", err)
}
cookies, err = a.passport()
if err != nil {
return "", fmt.Errorf("get passport failed: %v", err)
}
return
}
func (a *AuthStruct) AuthEmail(code string) (cookies string, err error) {
if err = a.emailLoginPost2(code); err != nil {
return "", fmt.Errorf("email login post2 failed: %v", err)
}
if err = a.keepLoginPost(); err != nil {
return "", fmt.Errorf("keep login post failed: %v", err)
}
cookies, err = a.passport()
if err != nil {
return "", fmt.Errorf("get passport failed: %v", err)
}
return
}
func (a *AuthStruct) AuthDevice() (cookies string, err error) {
if err = a.deviceLoginPost2(); err != nil {
return "", fmt.Errorf("device login post2 failed: %v", err)
}
if err = a.deviceLoginPost3(); err != nil {
return "", fmt.Errorf("device login post3 failed: %v", err)
}
if err = a.keepLoginPost(); err != nil {
return "", fmt.Errorf("keep login post failed: %v", err)
}
cookies, err = a.passport()
if err != nil {
return "", fmt.Errorf("get passport failed: %v", err)
}
return
}
func (a *AuthStruct) GetLoginType() string {
return a.LoginType
}
func getValue(s string) string {
return strings.Split(s, "'")[1]
}
func (a *AuthStruct) findHtmlValue(n *html.Node) (node *html.Node) {
if n.Type == html.ElementNode && n.Data == "form" {
for _, v := range n.Attr {
if v.Key == "action" {
a.ActionUrl = v.Val
}
}
}
if n.Type == html.ElementNode && n.Data == "input" {
for _, v := range n.Attr {
if v.Key == "id" && v.Val == "NAPExp" {
for _, v := range n.Attr {
if v.Key == "value" {
a.PassportData.NAPExp = v.Val
}
}
}
}
}
if n.Type == html.ElementNode && n.Data == "input" {
for _, v := range n.Attr {
if v.Key == "id" && v.Val == "pprid" {
for _, v := range n.Attr {
if v.Key == "value" {
a.PassportData.PPRID = v.Val
}
}
}
}
}
if n.Type == html.ElementNode && n.Data == "input" {
for _, v := range n.Attr {
if v.Key == "id" && v.Val == "NAP" {
for _, v := range n.Attr {
if v.Key == "value" {
a.PassportData.NAP = v.Val
}
}
}
}
}
if n.Type == html.ElementNode && n.Data == "input" {
for _, v := range n.Attr {
if v.Key == "id" && v.Val == "ANON" {
for _, v := range n.Attr {
if v.Key == "value" {
a.PassportData.ANON = v.Val
}
}
}
}
}
if n.Type == html.ElementNode && n.Data == "input" {
for _, v := range n.Attr {
if v.Key == "id" && v.Val == "ANONExp" {
for _, v := range n.Attr {
if v.Key == "value" {
a.PassportData.ANONExp = v.Val
}
}
}
}
}
if n.Type == html.ElementNode && n.Data == "input" {
for _, v := range n.Attr {
if v.Key == "id" && v.Val == "t" {
for _, v := range n.Attr {
if v.Key == "value" {
a.PassportData.T = v.Val
}
}
}
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
a.findHtmlValue(c)
}
return
}