Add cookies and CSRF to authentication flow

This commit is contained in:
Alessio 2022-12-22 23:09:46 -05:00
parent 3feaf9caa6
commit c0366a0978

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/http/cookiejar"
"net/url" "net/url"
"strings" "strings"
"time" "time"
@ -29,11 +30,18 @@ func NewGuestSession() API {
panic(err) panic(err)
} }
jar, err := cookiejar.New(nil)
if err != nil {
panic(err)
}
return API{ return API{
IsAuthenticated: false, IsAuthenticated: false,
GuestToken: guestAPIString, GuestToken: guestAPIString,
AuthenticationToken: "", AuthenticationToken: "",
Client: http.Client{Timeout: 10 * time.Second}, Client: http.Client{
Timeout: 10 * time.Second,
Jar: jar,
},
CSRFToken: "", CSRFToken: "",
} }
} }
@ -60,6 +68,7 @@ func (api *API) LogIn(username string, password string) {
err = api.do_http_POST(loginURL, login_body, &result) err = api.do_http_POST(loginURL, login_body, &result)
if err != nil { if err != nil {
fmt.Printf("%#v\n", api.Client.Jar)
panic(err) panic(err)
} }
@ -199,6 +208,10 @@ func (api API) do_http(url string, cursor string, result interface{}) error {
if api.IsAuthenticated { if api.IsAuthenticated {
// TODO authentication: add authentication headers/params // TODO authentication: add authentication headers/params
if api.CSRFToken == "" {
panic("No CSRF token set!")
}
req.Header.Set("x-csrf-token", api.CSRFToken)
} else { } else {
// Not authenticated; use guest token // Not authenticated; use guest token
if api.GuestToken == "" { if api.GuestToken == "" {
@ -223,7 +236,7 @@ func (api API) do_http(url string, cursor string, result interface{}) error {
for header := range resp.Header { for header := range resp.Header {
responseHeaders += fmt.Sprintf(" %s: %s\n", header, resp.Header.Get(header)) responseHeaders += fmt.Sprintf(" %s: %s\n", header, resp.Header.Get(header))
} }
return fmt.Errorf("HTTP %s\n%s\n%s", resp.Status, responseHeaders, content) return fmt.Errorf("HTTP Error. HTTP %s\n%s\nbody: %s", resp.Status, responseHeaders, content)
} }
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)