From 51f8cbd530ef85742ca9dac7260e5ddea63c9cf6 Mon Sep 17 00:00:00 2001
From: Yoan VALLET <ext.sopra.yvallet@grandlyon.com>
Date: Mon, 18 May 2020 09:40:41 +0200
Subject: [PATCH] update main.go - fix redir on redirect endpoint

---
 main.go | 29 ++++++++---------------------
 1 file changed, 8 insertions(+), 21 deletions(-)

diff --git a/main.go b/main.go
index ae312a1..6a948dc 100644
--- a/main.go
+++ b/main.go
@@ -125,10 +125,11 @@ func main() {
 
 		clientId := query.Get("client_id")
 		state := query.Get("state")
+		cozyOrigin := query.Get("redirect_uri") // here we use the redirect_uri param to transmit our stack url
 		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
+		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 +"-"+ cozyOrigin
 
 		fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- Send request to auth endpoint", authReq)
 		response, err := http.Get(authReq)
@@ -168,28 +169,14 @@ func main() {
 		fmt.Println(query)
 
 		code := query.Get("code")
-		state := query.Get("state")
+		req_state := query.Get("state")
+		state := strings.Split(req_state, "-")[0]
+		host := strings.Split(req_state, "-")[1]
+
 		usagePointId := query.Get("usage_point_id")
 
-		// Get the redirect uri from the referer
-		referer := r.Header.Get("Referer")
-		redirectUri := ""
-		redirectStartIndex := strings.Index(referer, "&redirect_uri=")
-		if redirectStartIndex == -1 {
-			fmt.Println("No redirect found")
-		}
-		redirectStartIndex += len("&redirect_uri=")
-		splitRefer := string([]byte(referer[redirectStartIndex:len(referer)]))
-		fmt.Println(splitRefer)
-		redirectEndIndex := strings.Index(splitRefer, "&")
-		if redirectEndIndex == -1 {
-			redirectUri = splitRefer
-		}
-		redirectUri = string([]byte(splitRefer[0:redirectEndIndex]))
-		fmt.Println(redirectUri)
-		
-		redir := "https://" + redirectUri + "/accounts/enedis-konnector/redirect?code=" + code + "&state="+ state +"&usage_point_id=" + usagePointId
-		fmt.Println(redir)
+		redir := host + "?code=" + code + "&state="+ state +"&usage_point_id=" + usagePointId
+		fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "- Redirect to -", redir)
 		http.Redirect(w, r, redir, 302)
 	})
 
-- 
GitLab