mirror of
https://gitgud.io/yats/libkiwi.git
synced 2026-06-29 23:52:03 -04:00
Add cookie convenience funcs
Fix test helper func
This commit is contained in:
@@ -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.")
|
||||
}
|
||||
|
||||
+38
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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.
|
||||
@@ -39,6 +41,7 @@ func NewKF(hc http.Client, host *url.URL) (KF, error) {
|
||||
return KF{
|
||||
client: hc,
|
||||
domain: u,
|
||||
Cookies: jar{hc.Jar},
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user