diff --git a/auth.go b/auth.go index fd122c9..9a20a80 100644 --- a/auth.go +++ b/auth.go @@ -123,16 +123,14 @@ func (kf *KF) TwoFactorAuth(ctx context.Context, resp *http.Response, code uint3 } func (kf *KF) IsLoggedIn() bool { - return getCookie(kf.client.Jar, kf.domain, "xf_user") != nil + return kf.Cookies.GetCookie(kf.domain, "xf_user") != nil } func (kf *KF) RefreshSession(ctx context.Context) (string, error) { const COOKIE_NAME = "xf_session" - jar := kf.client.Jar - // Clear any existing session token to request a new one. - setCookie(jar, kf.domain, &http.Cookie{ + kf.Cookies.SetCookie(kf.domain, &http.Cookie{ Name: COOKIE_NAME, Value: "", }) @@ -143,7 +141,7 @@ func (kf *KF) RefreshSession(ctx context.Context) (string, error) { } resp.Body.Close() - session := getCookie(jar, kf.domain, COOKIE_NAME) + session := kf.Cookies.GetCookie(kf.domain, COOKIE_NAME) if session == nil { return "", errors.New("Failed to get new xf_session cookie.") } diff --git a/cookies.go b/cookies.go new file mode 100644 index 0000000..ead8e21 --- /dev/null +++ b/cookies.go @@ -0,0 +1,38 @@ +package libkiwi + +import ( + "net/http" + "net/url" +) + +// http.CookieJar interface wrapper to add convenience functions. +type jar struct { + http.CookieJar +} + +func (j *jar) GetCookie(u *url.URL, name string) *http.Cookie { + cookies := j.Cookies(u) + for _, c := range cookies { + if c.Name == name { + return c + } + } + + return nil +} + +func (j *jar) SetCookie(u *url.URL, newCookie *http.Cookie) { + cookies := j.Cookies(u) + + for i, c := range cookies { + if c.Name == newCookie.Name { + cookies[i] = newCookie + j.SetCookies(u, cookies) + return + } + } + + // Append if not already existing. + cookies = append(cookies, newCookie) + j.SetCookies(u, cookies) +} diff --git a/http.go b/http.go index c7e2e3b..173c906 100644 --- a/http.go +++ b/http.go @@ -14,33 +14,6 @@ import ( "golang.org/x/net/html/atom" ) -func getCookie(jar http.CookieJar, u *url.URL, name string) *http.Cookie { - cookies := jar.Cookies(u) - for _, c := range cookies { - if c.Name == name { - return c - } - } - - return nil -} - -func setCookie(jar http.CookieJar, u *url.URL, newCookie *http.Cookie) { - cookies := jar.Cookies(u) - - for i, c := range cookies { - if c.Name == newCookie.Name { - cookies[i] = newCookie - jar.SetCookies(u, cookies) - return - } - } - - // Append if not already existing. - cookies = append(cookies, newCookie) - jar.SetCookies(u, cookies) -} - // Get XFToken (data-csrf) from page html. func XFToken(page io.Reader) (string, error) { z := html.NewTokenizer(page) diff --git a/libkiwi.go b/libkiwi.go index 2b0f777..1445f35 100644 --- a/libkiwi.go +++ b/libkiwi.go @@ -18,6 +18,8 @@ const _USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefo type KF struct { client http.Client domain *url.URL + + Cookies jar } // Supply your own http.Client to route through any proxies. @@ -37,8 +39,9 @@ func NewKF(hc http.Client, host *url.URL) (KF, error) { } return KF{ - client: hc, - domain: u, + client: hc, + domain: u, + Cookies: jar{hc.Jar}, }, nil } diff --git a/libkiwi_test.go b/libkiwi_test.go index 0123b28..e05b714 100644 --- a/libkiwi_test.go +++ b/libkiwi_test.go @@ -58,7 +58,7 @@ func TestGetPost(t *testing.T) { t.Logf("Post text: %s\n", post.TextContent()) } -func newTestKF() (*KF, error) { +func newTestKF() (KF, error) { host := os.Getenv("TEST_HOST") if host == "" { host = _TEST_HOST @@ -66,7 +66,7 @@ func newTestKF() (*KF, error) { u, err := url.Parse("https://" + host) if err != nil { - return nil, err + return KF{}, err } p := proxy.FromEnvironment() @@ -76,7 +76,7 @@ func newTestKF() (*KF, error) { jar, err := cookiejar.New(nil) if err != nil { - return nil, err + return KF{}, err } hc := http.Client{