Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 15dfca6

Browse files
author
Sandy
authored
Merge pull request #23 from openset/develop
Add: saveCredential
2 parents d9567aa + e3ecf43 commit 15dfca6

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

‎internal/leetcode/base.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package leetcode
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"io/ioutil"
67
"net/http"
8+
"net/http/cookiejar"
9+
"net/url"
710
"os"
811
"os/user"
912
"path"
@@ -15,6 +18,15 @@ func checkErr(err error) {
1518
}
1619
}
1720

21+
func client() *http.Client {
22+
jar, err := cookiejar.New(nil)
23+
// jar.SetCookies()
24+
checkErr(err)
25+
return &http.Client{
26+
Jar: jar,
27+
}
28+
}
29+
1830
func getCsrfToken(cookies []*http.Cookie) string {
1931
for _, cookie := range cookies {
2032
if cookie.Name == "csrftoken" {
@@ -38,7 +50,10 @@ func getPath(f string) string {
3850
func saveCookies(cookies []*http.Cookie) {
3951
data, err := json.Marshal(cookies)
4052
checkErr(err)
41-
err = ioutil.WriteFile(getPath(cookiesFile), data, 0755)
53+
dst := bytes.Buffer{}
54+
err = json.Indent(&dst, data, "", "\t")
55+
checkErr(err)
56+
err = ioutil.WriteFile(getPath(cookiesFile), dst.Bytes(), 0755)
4257
checkErr(err)
4358
}
4459

@@ -49,3 +64,9 @@ func getCookies() (cookies []*http.Cookie) {
4964
checkErr(err)
5065
return
5166
}
67+
68+
func saveCredential(data url.Values) {
69+
u := url.UserPassword(data.Get("login"), data.Get("password"))
70+
err := ioutil.WriteFile(getPath(credentialsFile), []byte(u.String()), 0755)
71+
checkErr(err)
72+
}

‎internal/leetcode/login.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import (
66
"strings"
77
)
88

9-
func AccountsLogin(userstring, pwd string) (*http.Response, error) {
9+
func AccountsLogin(username, password string) (*http.Response, error) {
1010
resp, err := http.Head(AccountsLoginUrl)
1111
checkErr(err)
12+
defer resp.Body.Close()
1213
cookies := resp.Cookies()
1314
saveCookies(cookies)
1415
csrftoken := getCsrfToken(cookies)
1516
data := url.Values{
16-
"login": {user},
17-
"password": {pwd},
17+
"login": {username},
18+
"password": {password},
1819
"csrfmiddlewaretoken": {csrftoken},
1920
}
2021
req, err := http.NewRequest("POST", AccountsLoginUrl, strings.NewReader(data.Encode()))
@@ -26,6 +27,10 @@ func AccountsLogin(user string, pwd string) (*http.Response, error) {
2627
}
2728
resp, err = http.DefaultClient.Do(req)
2829
checkErr(err)
30+
defer resp.Body.Close()
2931
saveCookies(resp.Cookies())
32+
if resp.StatusCode == 200 {
33+
saveCredential(data)
34+
}
3035
return resp, err
3136
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /