diff --git a/main.go b/main.go index 134954d94e00b724937ea970050787bcba7ff154..7f092a5e4a705a990cede2d8be4ae936faf6329d 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "net/http" + "io/ioutil" "strconv" // "strings" ) @@ -16,18 +17,29 @@ func main() { // Parse the flags flag.Parse() mux := http.NewServeMux() - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() - fmt.Println(query) // host := strings.Split(query.Get("state"), "-")[1] - host := query.Get("state") - fmt.Println(host) + 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") - fmt.Println(state) + response, err := http.Get("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/redirect&response_type=code&state="+ state +"-"+ cozyOrigin) + if err != nil { + fmt.Printf("%s", err) + } else { + defer response.Body.Close() + contents, err := ioutil.ReadAll(response.Body) + fmt.Printf("%s\n", string(contents)) + } + + }) + + mux.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) { + query := r.URL.Query() + state := strings.Split(query.Get("state"), "-")[0] + host := strings.Split(query.Get("state"), "-")[1] usagePointId := query.Get("usage_point_id") - fmt.Println(usagePointId) code := query.Get("code") - fmt.Println(code) redir := "https://" + host + "/accounts/enedisoauth/redirect?code=" + code + "&state="+ state +"&usage_point_id=" + usagePointId fmt.Println(redir) http.Redirect(w, r, redir, 302)