From 3a856557ef2ee14dabc6bb2c9a2fc45f00e6294d Mon Sep 17 00:00:00 2001
From: Hugo <hnouts.dev@gmail.com>
Date: Thu, 4 Feb 2021 14:28:11 +0100
Subject: [PATCH] changed some log level to error / info accordingly. Added an
 oauth / refresh token condition to the logRoute

---
 main.go | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)

diff --git a/main.go b/main.go
index 5fc2f26..4156210 100644
--- a/main.go
+++ b/main.go
@@ -122,7 +122,7 @@ func logRoute(key string) {
 	case "Ping_grdf_new_token":
 		log.Debug("Grdf - Received - Token - new /token request from Cozy")
 
-	case "Enedis_success":
+	case "Enedis_success_oauth":
 		log.Debug("Enedis - Success - Oauth - oauth dance successfully done")
 	case "Enedis_success_token":
 		log.Debug("Enedis - Success - Token - Enedis gave a new token or refresh token to the cozy stack")
@@ -132,18 +132,18 @@ func logRoute(key string) {
 		log.Debug("Grdf - Success - Token - Grdf gave a new token to the cozy stack")
 
 	case "Enedis_error":
-		log.Debug("Enedis - Error - Oauth - something wrong happened with enedis")
+		log.Error("Enedis - Error - Oauth - something wrong happened with enedis")
 	case "Enedis_redirect_error":
-		log.Debug("Enedis - Error - Redirect - an error occured with enedis redirection")
+		log.Error("Enedis - Error - Redirect - an error occured with enedis redirection")
 	case "Enedis_token_error":
-		log.Debug("Enedis - Error - Token - an error occured with enedis /token endpoint")
+		log.Error("Enedis - Error - Token - an error occured with enedis /token endpoint")
 
 	case "Grdf_error":
-		log.Debug("Grdf - Error - Oauth - something wrong happened with grdf")
+		log.Error("Grdf - Error - Oauth - something wrong happened with grdf")
 	case "Grdf_redirect_error":
-		log.Debug("Grdf - Error - Redirect - an error occured with grdf redirection")
+		log.Error("Grdf - Error - Redirect - an error occured with grdf redirection")
 	case "Grdf_token_error":
-		log.Debug("Grdf - Error - Token - an error occured with grdf /token endpoint")
+		log.Error("Grdf - Error - Token - an error occured with grdf /token endpoint")
 
 	case "Proxy_error":
 		log.Error("Proxy - Error - Oauth - something wrong happened in the proxy")
@@ -232,11 +232,11 @@ func main() {
 				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("WL - EnedisSuccess - Authorize - Redirecting user to Enedis: ", redirectUrl)
+				log.Info("WL - EnedisSuccess - Authorize - Redirecting user to Enedis: ", redirectUrl)
 				http.Redirect(w, r, redirectUrl, 302)
 			} else {
 				logRoute("Proxy_error_enedis_auth")
-				log.Debug("WL - ProxyError - Enedis - Authorize - redirect_uri bad format from Cozy " + cozyOrigin)
+				log.Error("WL - ProxyError - Enedis - Authorize - redirect_uri bad format from Cozy " + cozyOrigin)
 				http.Error(w, http.StatusText(500), 500)
 			}
 		} else {
@@ -265,11 +265,11 @@ func main() {
 
 				redirectUrl := authURL + "?client_id=" + clientId + "&scope=openid&response_type=code&redirect_uri=" + redirectProxy + "&login_hint=Prénom|Nom||Ecolyo&state=" + state + "-" + instanceName
 
-				log.Debug("WL - GrdfSuccess - Authorize - Redirect user to: ", redirectUrl)
+				log.Info("WL - GrdfSuccess - Authorize - Redirect user to: ", redirectUrl)
 				http.Redirect(w, r, redirectUrl, 302)
 			} else {
 				logRoute("Proxy_error_grdf_auth")
-				log.Debug("WL - ProxyError - Grdf - Authorize - redirect_uri bad format from Cozy " + cozyOrigin)
+				log.Error("WL - ProxyError - Grdf - Authorize - redirect_uri bad format from Cozy " + cozyOrigin)
 				http.Error(w, http.StatusText(500), 500)
 			}
 		} else {
@@ -294,12 +294,12 @@ func main() {
 				intCode, err := strconv.Atoi(code)
 				if err != nil {
 					logRoute("Proxy_error_enedis_redirect")
-					log.Debug("WL - ProxyError - Enedis - Redirect - String to int convert error for status code: ", err)
+					log.Error("WL - ProxyError - Enedis - Redirect - String to int convert error for status code: ", err)
 					http.Error(w, http.StatusText(500), 500)
 					return
 				}
 				logRoute("Enedis_redirect_error")
-				log.Debug("WL - EnedisError - Redirect - status code error: ", code)
+				log.Error("WL - EnedisError - Redirect - status code error: ", code)
 				http.Error(w, http.StatusText(intCode), intCode)
 				return
 			}
@@ -307,7 +307,7 @@ func main() {
 			splitIndex := strings.Index(req_state, "-")
 			if splitIndex == -1 {
 				logRoute("Proxy_error_enedis_redirect")
-				log.Debug("WL - ProxyError - Enedis - Redirect - No host found in query")
+				log.Error("WL - ProxyError - Enedis - Redirect - No host found in query")
 				http.Error(w, http.StatusText(500), 500)
 				return
 			}
@@ -316,11 +316,11 @@ func main() {
 			usagePointId := query.Get("usage_point_id")
 			cozyURL := "https://" + host + "." + *cozyDomain + *cozyRedirectURI
 			redir := cozyURL + "?code=" + code + "&state=" + state + "&usage_point_id=" + usagePointId
-			log.Debug("WL - EnedisSuccess - Redirect - Redirecting user to  Cozy stack: ", redir)
+			log.Info("WL - EnedisSuccess - Redirect - Redirecting user to  Cozy stack: ", redir)
 			http.Redirect(w, r, redir, 302)
 		} else {
 			logRoute("Proxy_error_enedis_redirect")
-			log.Debug("WL - ProxyError - Enedis - Redirect - Missing parameters in request")
+			log.Error("WL - ProxyError - Enedis - Redirect - Missing parameters in request")
 			http.Error(w, http.StatusText(500), 500)
 		}
 	})
@@ -355,7 +355,7 @@ func main() {
 			cozyURL := "https://" + host + "." + *cozyDomain + *cozyGrdfRedirectURI
 
 			redir := cozyURL + "?code=" + code + "&state=" + state
-			log.Debug("WL - GrdfSuccess - Redirect - Redirect to Cozy stack: ", redir)
+			log.Info("WL - GrdfSuccess - Redirect - Redirect to Cozy stack: ", redir)
 			http.Redirect(w, r, redir, 302)
 		} else {
 			logRoute("Proxy_error_grdf_redirect")
@@ -475,7 +475,11 @@ func main() {
 				http.Error(w, jsonError.Error(), 500)
 				return
 			}
-			logRoute("Enedis_success_token")
+			if refreshToken != "" {
+				logRoute("Enedis_success_token")
+			} else {
+				logRoute("Enedis_success_oauth")
+			}
 			log.Info("WL - EnedisSuccess - Token - Respond correctly to Cozy stack")
 		} else {
 			logRoute("Enedis_token_error")
-- 
GitLab