-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
222 lines (210 loc) · 5.35 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
package sechat
import (
"errors"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
)
var (
ErrNetworkFkey = errors.New("unable to find network fkey")
ErrChatFkey = errors.New("unable to find chat fkey")
ErrChatUserID = errors.New("unable to find user ID")
ErrAuthURL = errors.New("unable to find auth URL")
ErrIncomplete = errors.New("incomplete login (invalid credentials)")
userIDRegexp = regexp.MustCompile(`^/users/(\d+)`)
)
// fetchLoginURL retrieves the URL of the page that contains the login form.
func (c *Conn) fetchLoginURL() (string, error) {
req, err := c.newRequest(
http.MethodGet, "https://stackexchange.com/users/signin", nil,
)
if err != nil {
return "", err
}
res, err := c.client.Do(req)
if err != nil {
return "", err
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(b), nil
}
// fetchNetworkFkey retrieves the network fkey from the login form so that it
// can be submitted during the login process.
func (c *Conn) fetchNetworkFkey(url string) (string, error) {
req, err := c.newRequest(http.MethodGet, url, nil)
if err != nil {
return "", err
}
res, err := c.client.Do(req)
if err != nil {
return "", err
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return "", err
}
fkey, ok := doc.Find("#fkey").Attr("value")
if !ok {
return "", ErrNetworkFkey
}
return fkey, nil
}
// fetchAuthURL exists due to a bug in golang.org/x/net/html that prevents
// noscript elements from being parsed correctly. This method works around it.
func (c *Conn) fetchAuthURL(res *http.Response) (string, error) {
doc, err := goquery.NewDocumentFromResponse(res)
if err != nil {
return "", err
}
noscriptDoc, err := goquery.NewDocumentFromReader(
strings.NewReader(doc.Find("noscript").Text()),
)
if err != nil {
return "", err
}
url, ok := noscriptDoc.Find("a").Attr("href")
if !ok {
return "", ErrAuthURL
}
return url, nil
}
// submitLoginForm submits the authentication information along with the fkey.
// A URL is returned which is necessary to complete the login process.
func (c *Conn) submitLoginForm(fkey string) (string, error) {
form := &url.Values{}
form.Set("email", c.email)
form.Set("password", c.password)
form.Set("affId", "11")
form.Set("fkey", fkey)
req, err := c.newRequest(
http.MethodPost,
"https://openid.stackexchange.com/affiliate/form/login/submit",
strings.NewReader(form.Encode()),
)
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
res, err := c.client.Do(req)
if err != nil {
return "", err
}
// fetchAuthURL ought to be part of this method but please see its
// docstring for an explanation of why it isn't
return c.fetchAuthURL(res)
}
// confirmOpenID submits the form that confirms the user wishes to log in with
// their Stack Exchange OpenID. This is only necessary for certain accounts.
func (c *Conn) confirmOpenID(res *http.Response) error {
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return err
}
var (
session = doc.Find("input[name=session]").AttrOr("value", "")
fkey = doc.Find("input[name=fkey]").AttrOr("value", "")
)
form := &url.Values{}
form.Set("session", session)
form.Set("fkey", fkey)
req, err := c.newRequest(
http.MethodPost,
"https://openid.stackexchange.com/account/prompt/submit",
strings.NewReader(form.Encode()),
)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
res, err = c.client.Do(req)
if err != nil {
return err
}
if res.Request.URL.Path != "/" {
return ErrIncomplete
}
return nil
}
// completeLogin finishes the login process.
func (c *Conn) completeLogin(authUrl string) error {
req, err := c.newRequest(http.MethodGet, authUrl, nil)
if err != nil {
return err
}
res, err := c.client.Do(req)
if err != nil {
return err
}
switch res.Request.URL.Path {
case "/account/prompt":
return c.confirmOpenID(res)
case "/":
return nil
default:
return ErrIncomplete
}
}
// fetchChatFkey loads the home page for chat in order to retrieve the fkey
// that is required to accompany every authenticated request.
func (c *Conn) fetchChatFkeyAndUserID() (string, int, error) {
req, err := c.newRequest(
http.MethodGet,
"https://chat.stackexchange.com",
nil,
)
if err != nil {
return "", 0, err
}
res, err := c.client.Do(req)
if err != nil {
return "", 0, err
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return "", 0, err
}
fkey, ok := doc.Find("#fkey").Attr("value")
if !ok {
return "", 0, ErrChatFkey
}
userID, ok := doc.Find(".topbar-menu-links a").Attr("href")
if !ok {
return "", 0, ErrChatUserID
}
m := userIDRegexp.FindStringSubmatch(userID)
if m == nil {
return "", 0, ErrChatUserID
}
return fkey, atoi(m[1]), nil
}
// auth performs the steps necessary to authenticate against the chat server.
func (c *Conn) auth() error {
loginURL, err := c.fetchLoginURL()
if err != nil {
return err
}
networkFkey, err := c.fetchNetworkFkey(loginURL)
if err != nil {
return err
}
authURL, err := c.submitLoginForm(networkFkey)
if err != nil {
return err
}
if err := c.completeLogin(authURL); err != nil {
return err
}
chatFkey, userID, err := c.fetchChatFkeyAndUserID()
if err != nil {
return err
}
c.fkey = chatFkey
c.user = userID
return nil
}