From e46ca43d4cd6e44972123856bbe047eb0517768e Mon Sep 17 00:00:00 2001 From: Juan Roig Date: Mon, 13 Nov 2023 02:08:03 -0500 Subject: [PATCH] Move aplus requests to new launch_aplus function --- README.md | 12 ++++++++- aplus.go | 28 +++++++++++++++++++++ canvas.go | 17 +++++++++++++ main.go | 75 +++---------------------------------------------------- 4 files changed, 60 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index f08a35e..5735615 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,13 @@ # aplus -aplus cli \ No newline at end of file +aplus cli + +# Usage + +# Launch interactive prompt +aplus + +# Enter code immediately +aplus -c +aplus --code + diff --git a/aplus.go b/aplus.go index ae5e054..3f3640b 100644 --- a/aplus.go +++ b/aplus.go @@ -75,3 +75,31 @@ func extract_attribute(input string, attribute string) string { return input[start : start+end] } + +func launch_aplus(course_code int) { + aplus_link := fmt.Sprintf("%s/courses/%d/external_tools/sessionless_launch?id=913&access_token=%s", base_link, course_code, token) // 7329 always works + aplus := get_aplus(token, aplus_link, client) + + resp, err := client.Get(aplus) + + if err != nil { + fmt.Println("Error while performing GET request to aplus auth link.") + } + + defer resp.Body.Close() + body, _ := io.ReadAll(resp.Body) + + form_str := get_form_from_request_body(body) + + form_values := parse_form(form_str) + + resp, _ = client.PostForm("https://floridapoly.aplusattendance.com/canvas", form_values) + + body, err = io.ReadAll(resp.Body) + + if err != nil { + fmt.Println("Error reading body of post form response from aplus attendance.") + } + + fmt.Println(string(body)) +} diff --git a/canvas.go b/canvas.go index 33c7f6b..3aa8050 100644 --- a/canvas.go +++ b/canvas.go @@ -5,18 +5,35 @@ import ( "fmt" "io" "net/http" + "net/http/cookiejar" "os" + + "golang.org/x/net/publicsuffix" ) func initialize() { base_link = "https://floridapolytechnic.instructure.com/api/v1" token = os.Getenv("CANVAS_API_KEY") + + jar, _ := cookiejar.New(&cookiejar.Options{ + PublicSuffixList: publicsuffix.List, + }) + + client = http.Client{ + CheckRedirect: func(req *http.Request, via []*http.Request) error { + fmt.Println("Redirected to:", req.URL) + fmt.Println(req.Cookies()) + return nil + }, + Jar: jar, + } } func get_courses(token string, link string, client http.Client) []CanvasCourse { resp, err := client.Get(link) if err != nil { fmt.Println("Error performing GET request to get courses.") + return nil } defer resp.Body.Close() body, _ := io.ReadAll(resp.Body) diff --git a/main.go b/main.go index 473b027..8f669e1 100644 --- a/main.go +++ b/main.go @@ -1,86 +1,19 @@ package main import ( - "fmt" - "io" "net/http" - "net/http/cookiejar" - "strings" - - "golang.org/x/net/publicsuffix" ) var base_link string var token string +var client http.Client func main() { - - jar, _ := cookiejar.New(&cookiejar.Options{ - PublicSuffixList: publicsuffix.List, - }) - - client := http.Client{ - CheckRedirect: func(req *http.Request, via []*http.Request) error { - fmt.Println("Redirected to:", req.URL) - fmt.Println(req.Cookies()) - return nil - }, - Jar: jar, - } - initialize() // select_course() + course_code := 7329 + launch_aplus(course_code) - aplus_link := fmt.Sprintf("%s/courses/%d/external_tools/sessionless_launch?id=913&access_token=%s", base_link, 7329, token) // 7329 always works - aplus := get_aplus(token, aplus_link, client) - fmt.Println(aplus) - resp, err := client.Get(aplus) - - if err != nil { - fmt.Println("Error while performing GET request to aplus auth link.") - } - - defer resp.Body.Close() - body, _ := io.ReadAll(resp.Body) - - form_str := get_form_from_request_body(body) - fmt.Println(form_str) - - form_values := parse_form(form_str) - - fmt.Println("Form Values:") - for key, values := range form_values { - fmt.Printf("%s: %s\n", key, 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 { - fmt.Println("Error posting form to Aplus Attendance.") - } - - body, err = io.ReadAll(resp.Body) - - if err != nil { - fmt.Println("Error reading body of post form response from aplus attendance.") - } - - fmt.Println(string(body)) - //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. - //resp, _ = client.Get("https://floridapoly.aplusattendance.com/canvas/student/?canvasCourse=7329&doStudentAuth=truee") - //body, _ = io.ReadAll(resp.Body) - - //fmt.Println(string(body)) - //fmt.Println(req) + // enter_code() }