diff --git a/main.go b/main.go index a20424df10abade769244674ba0bd1a21e0d62b3..bcc14622ec686127e51a520e76298143678be7ab 100644 --- a/main.go +++ b/main.go @@ -34,20 +34,17 @@ func main() { fmt.Println("Server started") mux.HandleFunc("/auth_old", func(w http.ResponseWriter, r *http.Request) { + fmt.Println("*******************") fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- New auth request") query := r.URL.Query() fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- Query received - ", query) clientId := query.Get("client_id") + cozyOrigin := query.Get("redirect_uri") // here we use the redirect_uri param to transmit our stack url state := query.Get("state") - redirectUri := "https://oauth-proxy.wf.alpha.grandlyon.com/" - responseType := "code" - - authReq := "https://gw.hml.api.enedis.fr/group/espace-particuliers/consentement-linky/oauth2/authorize?client_id="+ clientId +"&duration=P6M&redirect_uri="+ redirectUri +"&response_type="+ responseType +"&state="+ state - - fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- Send request to auth endpoint", authReq) + 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) - fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- Endpoint response with status", response.Status) if err != nil { fmt.Println(err) } else { @@ -56,12 +53,6 @@ func main() { if err != nil { fmt.Println(err) } - // Define the replyUri - referer := r.Header.Get("Referer") - replyUri := strings.Replace(referer, "home.", "", 1) - replyUri = replyUri + "accounts/enedis-konnector/redirect" - fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- Reply to - ", replyUri) - // Get the response body as a string pageContent := string(contents) @@ -89,7 +80,6 @@ func main() { fmt.Println("No closing tag for state found.") } - usageStartIndex := strings.Index(pageContent, "&usage_point_id=") if usageStartIndex == -1 { fmt.Println("No usage found") @@ -111,14 +101,17 @@ func main() { fmt.Printf("Page state: %s\n", pageState) fmt.Printf("Page usage: %s\n", pageUsage) - redir := replyUri + "?code=" + pageCode + "&state="+ pageState +"&usage_point_id=" + pageUsage - fmt.Println(redir) - http.Redirect(w, r, redir, 302) + state := strings.Split(pageState, "-")[0] + host := strings.Split(pageState, "-")[1] + redir := "https://" + host + "/accounts/enedis-konnector/redirect?code=" + pageCode + "&state="+ state +"&usage_point_id=" + pageUsage + fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- Redirect to -", redir) + http.Redirect(w, r, redir, 302) } }) mux.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) { + fmt.Println("*******************") fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- New auth request") query := r.URL.Query() fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- Query received - ", query) @@ -164,6 +157,7 @@ func main() { }) mux.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) { + fmt.Println("*******************") fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- New redirect request") query := r.URL.Query() fmt.Println(query) @@ -186,6 +180,7 @@ func main() { }) mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) { + fmt.Println("*******************") fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- New token request") query := r.URL.Query() fmt.Println(query)