add cookie jar to allow cross site origin requests to carry cookies

This commit is contained in:
Juan Roig 2023-11-12 15:05:16 -05:00
parent aa816a646e
commit 78fd5e4e1f
3 changed files with 28 additions and 5 deletions

2
go.mod
View File

@ -1,3 +1,5 @@
module codeberg.org/flpolyaplus/aplus module codeberg.org/flpolyaplus/aplus
go 1.21.3 go 1.21.3
require golang.org/x/net v0.18.0 // indirect

2
go.sum
View File

@ -0,0 +1,2 @@
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=

29
main.go
View File

@ -4,17 +4,28 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"net/http/cookiejar"
"strings"
"golang.org/x/net/publicsuffix"
) )
var base_link string var base_link string
var token string var token string
func main() { func main() {
jar, _ := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
client := http.Client{ client := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error { CheckRedirect: func(req *http.Request, via []*http.Request) error {
fmt.Println("Redirected to:", req.URL) fmt.Println("Redirected to:", req.URL)
fmt.Println(req.Cookies())
return nil return nil
}, },
Jar: jar,
} }
initialize() initialize()
@ -43,23 +54,31 @@ func main() {
fmt.Printf("%s: %s\n", key, values) fmt.Printf("%s: %s\n", key, values)
} }
req, err := client.PostForm("https://floridapoly.aplusattendance.com/canvas", form_values) //req, _ := client.PostForm("https://floridapoly.aplusattendance.com/canvas", form_values)
req, _ := http.NewRequest(http.MethodPost, "https://floridapoly.aplusattendance.com/canvas", strings.NewReader(form_values.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Access-Control-Allow-Origin", "*")
req.Header.Set("Access-Control-Allow-Credentials", "true")
req.Header.Set("Access-Control-Allow-Headers", "Content-Type,access-control-allow-origin, access-control-allow-headers,access-control-allow-credentials")
resp, err = client.Do(req)
if err != nil { if err != nil {
fmt.Println("Error posting form to Aplus Attendance.") fmt.Println("Error posting form to Aplus Attendance.")
} }
body, err = io.ReadAll(req.Body) body, err = io.ReadAll(resp.Body)
if err != nil { if err != nil {
fmt.Println("Error reading body of post form response from aplus attendance.") fmt.Println("Error reading body of post form response from aplus attendance.")
} }
fmt.Println(string(body)) fmt.Println(string(body))
fmt.Println(req) //fmt.Println(resp)
fmt.Println(resp.Header)
fmt.Println(resp.Cookies())
// we need to submit the above post request correctly in order to submit this request. // we need to submit the above post request correctly in order to submit this request.
//resp, _ = client.Get("https://floridapoly.aplusattendance.com/canvas/student/?canvasCourse=7329&doStudentAuth=true") //resp, _ = client.Get("https://floridapoly.aplusattendance.com/canvas/student/?canvasCourse=7329&doStudentAuth=truee")
//body, _ = io.ReadAll(resp.Body) //body, _ = io.ReadAll(resp.Body)
//fmt.Println(string(body)) //fmt.Println(string(body))