From c63c1f9715c13cf5b0d2372e168e3ff41539a420 Mon Sep 17 00:00:00 2001
From: Hugo <hnouts@grandlyon.com>
Date: Wed, 22 Apr 2020 17:34:32 +0200
Subject: [PATCH] changes related to suggestions from cozy forum

creating 2 endpoints to handle oauth transaction with enedis.
---
 main.go | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/main.go b/main.go
index 134954d..7f092a5 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)
-- 
GitLab