diff --git a/main.go b/main.go index 4f759a789da910f9bdb0bcfa4b7b26ad520b4be0..3642231fefae6d6a78fbd08d26d89a2c8f42aa70 100644 --- a/main.go +++ b/main.go @@ -94,18 +94,64 @@ func main() { }) - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() fmt.Println(query) - state := strings.Split(query.Get("state"), "-")[0] - host := strings.Split(query.Get("state"), "-")[1] - fmt.Println(host) - fmt.Println(state) - usagePointId := query.Get("usage_point_id") - code := query.Get("code") - redir := "https://" + host + "/accounts/enedisoauth/redirect?code=" + code + "&state="+ state +"&usage_point_id=" + usagePointId - fmt.Println(redir) - http.Redirect(w, r, redir, 302) + clientId := query.Get("client_id") + cozyOrigin := "hugo.pchugo.wf.alpha.grandlyon.com" + state := query.Get("state") + authReq := "https://gw.hml.api.enedis.fr/group/espace-particuliers/consentement-linky/oauth2/authorize?client_id="+ clientId +"&duration=P6M&redirect_uri=https://oauth-proxy.wf.alpha.grandlyon.com/&response_type=code&state="+ state +"-"+ cozyOrigin + fmt.Println(authReq) + response, err := http.Get(authReq) + if err != nil { + fmt.Println(err) + } else { + defer response.Body.Close() + contents, err := ioutil.ReadAll(response.Body) + if err != nil { + fmt.Println(err) + } + pageContent := string(contents) + codeStartIndex := strings.Index(pageContent, "?code=") + if codeStartIndex == -1 { + fmt.Println("No code found") + } + codeStartIndex += 6 + codeEndIndex := strings.Index(pageContent, "&state=") + if codeEndIndex == -1 { + fmt.Println("No closing tag for code found.") + } + stateStartIndex := strings.Index(pageContent, "&state=") + if stateStartIndex == -1 { + fmt.Println("No state found") + } + stateStartIndex += 7 + stateEndIndex := strings.Index(pageContent, "&usage_point_id=") + if stateEndIndex == -1 { + fmt.Println("No closing tag for state found.") + } + usageStartIndex := strings.Index(pageContent, "&usage_point_id=") + if usageStartIndex == -1 { + fmt.Println("No usage found") + } + usageStartIndex += 16 + usageEndIndex := strings.Index(pageContent, "&usage_point_id=") + if usageEndIndex == -1 { + fmt.Println("No closing tag for usage found.") + } + usageEndIndex += 30 + pageCode := string([]byte(pageContent[codeStartIndex:codeEndIndex])) + pageState := string([]byte(pageContent[stateStartIndex:stateEndIndex])) + pageUsage := string([]byte(pageContent[usageStartIndex:usageEndIndex])) + fmt.Printf("Page code: %s\n", pageCode) + fmt.Printf("Page state: %s\n", pageState) + fmt.Printf("Page usage: %s\n", pageUsage) + state := strings.Split(pageState, "-")[0] + host := strings.Split(pageState, "-")[1] + redir := "https://" + host + "/accounts/enedisoauth/redirect?code=" + pageCode + "&state="+ state +"&usage_point_id=" + pageUsage + fmt.Println(redir) + http.Redirect(w, r, redir, 302) + } }) http.ListenAndServe(":"+strconv.Itoa(*httpPort), mux)