From d51ee5082682db10733c8dcd7466231d4600ddeb Mon Sep 17 00:00:00 2001
From: Hugo <hnouts@grandlyon.com>
Date: Wed, 29 Jul 2020 15:36:24 +0200
Subject: [PATCH] work in progress

---
 main.go | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/main.go b/main.go
index 8345153..9f29bb3 100644
--- a/main.go
+++ b/main.go
@@ -86,6 +86,7 @@ func main() {
 		io.WriteString(w, "OK\n")
 	})
 
+	// ENEDIS AUTH ENDPOINT
 	mux.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) {
 		log.Debug("New auth request")
 		query := r.URL.Query()
@@ -120,6 +121,42 @@ func main() {
 		http.Redirect(w, r, redirectUrl, 302)
 	})
 
+	// GRDF ADICT ACCESS_TOKEN ENDPOINT
+	mux.HandleFunc("/grdfAccess", func(w http.ResponseWriter, r *http.Request) {
+		log.Debug("New grdf auth request")
+		query := r.URL.Query()
+		log.Debug("Query received - ", query)
+
+		clientId := query.Get("client_id")
+		state := query.Get("state")
+		scope := "/adict/v1"
+		// here we use the redirect_uri param to transmit our stack url
+		// We keep only the instance name to not reach the 100 max char of redirectUrl
+		cozyOrigin := query.Get("redirect_uri")
+		splitIndexStart := strings.Index(cozyOrigin, ":")
+		if splitIndexStart == -1 {
+			log.Error("redirect_uri bad format " + cozyOrigin)
+			http.Error(w, http.StatusText(500), 500)
+		}
+		splitIndexEnd := strings.Index(cozyOrigin, ".")
+		if splitIndexEnd == -1 {
+			log.Error("redirect_uri bad format " + cozyOrigin)
+			http.Error(w, http.StatusText(500), 500)
+		}
+		instanceName := cozyOrigin[splitIndexStart+3:splitIndexEnd]
+
+		// DEV API
+		// authURL := "https://gw.hml.api.enedis.fr/dataconnect/v1/oauth2/authorize"
+		// PROD API
+		authURL := "https://mon-compte-particulier.enedis.fr/dataconnect/v1/oauth2/authorize"
+
+		redirectUrl := authURL + "?client_id=" + clientId + "&duration=P6M&response_type=" + responseType + "&state=" + state + "-" + instanceName
+
+		log.Debug("Redirect to - ", redirectUrl)
+		http.Redirect(w, r, redirectUrl, 302)
+	})
+
+	//ENEDIS REDIRECT ENDPOINT
 	mux.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
 		log.Debug("New redirect request")
 		query := r.URL.Query()
@@ -144,6 +181,7 @@ func main() {
 		http.Redirect(w, r, redir, 302)
 	})
 
+	//ENEDIS TOKEN ENDPOINT
 	mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
 		log.Debug("New token request")
 		query := r.URL.Query()
-- 
GitLab