From 8ceb8d697d6c0c2c5ad1f2a8989604f25c5d63e7 Mon Sep 17 00:00:00 2001 From: "ext.sopra.yvallet@grandlyon.com" <ext.sopra.yvallet@grandlyon.com> Date: Mon, 25 Jan 2021 11:40:42 +0100 Subject: [PATCH] feat: review error logs --- main.go | 365 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 192 insertions(+), 173 deletions(-) diff --git a/main.go b/main.go index c4afcd8..c0add7a 100644 --- a/main.go +++ b/main.go @@ -148,27 +148,27 @@ func main() { // 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("ProxyError: Enedis - auth - redirect_uri bad format from Cozy" + cozyOrigin) - http.Error(w, http.StatusText(500), 500) - } - splitIndexEnd := strings.Index(cozyOrigin, ".") - if splitIndexEnd == -1 { - log.Error("ProxyError: Enedis - auth - redirect_uri bad format from Cozy" + cozyOrigin) + if len(clientId) > 0 && len(state) > 0 && len(cozyOrigin) > 0 { + splitIndexStart := strings.Index(cozyOrigin, ":") + splitIndexEnd := strings.Index(cozyOrigin, ".") + if splitIndexStart > -1 && splitIndexEnd > -1 { + 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("Enedis - auth - Redirect user to Enedis - ", redirectUrl) + http.Redirect(w, r, redirectUrl, 302) + } else { + log.Error("ProxyError: Enedis - auth - redirect_uri bad format from Cozy " + cozyOrigin) + http.Error(w, http.StatusText(500), 500) + } + } else { + log.Error("CozyError: Enedis - auth - Missing parameters in request") 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("Enedis - auth - Redirect user to Enedis - ", redirectUrl) - http.Redirect(w, r, redirectUrl, 302) + } }) // GRDF ADICT AUTHORIZE ENDPOINT @@ -180,25 +180,26 @@ func main() { clientId := query.Get("client_id") state := query.Get("state") cozyOrigin := query.Get("redirect_uri") - splitIndexStart := strings.Index(cozyOrigin, ":") - if splitIndexStart == -1 { - log.Error("ProxyError: GRDF - grdf_authorize - redirect_uri bad format " + cozyOrigin) - http.Error(w, http.StatusText(500), 500) - } - splitIndexEnd := strings.Index(cozyOrigin, ".") - if splitIndexEnd == -1 { - log.Error("ProxyError: GRDF - grdf_authorize - redirect_uri bad format " + cozyOrigin) + if len(clientId) > 0 && len(state) > 0 && len(cozyOrigin) > 0 { + splitIndexStart := strings.Index(cozyOrigin, ":") + splitIndexEnd := strings.Index(cozyOrigin, ".") + if splitIndexStart > -1 && splitIndexEnd > -1 { + instanceName := cozyOrigin[splitIndexStart+3:splitIndexEnd] + redirectProxy := *cozyProxyURI + "/redirect-grdf" + authURL := "https://sofit-sso-oidc.grdf.fr/openam/oauth2/realms/externeGrdf/authorize" + + redirectUrl := authURL + "?client_id=" + clientId + "&scope=openid&response_type=code&redirect_uri="+ redirectProxy + "&login_hint=Prénom|Nom||Ecolyo&state=" + state + "-" + instanceName + + log.Debug("GRDF - grdf_authorize - Redirect user to - ", redirectUrl) + http.Redirect(w, r, redirectUrl, 302) + } else { + log.Error("ProxyError: GRDF - grdf_authorize - redirect_uri bad format " + cozyOrigin) + http.Error(w, http.StatusText(500), 500) + } + } else { + log.Error("ProxyError: GRDF - grdf_authorize - Missing parameters in request") http.Error(w, http.StatusText(500), 500) } - instanceName := cozyOrigin[splitIndexStart+3:splitIndexEnd] - - redirectProxy := *cozyProxyURI + "/redirect-grdf" - authURL := "https://sofit-sso-oidc.grdf.fr/openam/oauth2/realms/externeGrdf/authorize" - - redirectUrl := authURL + "?client_id=" + clientId + "&scope=openid&response_type=code&redirect_uri="+ redirectProxy + "&login_hint=Prénom|Nom||Ecolyo&state=" + state + "-" + instanceName - - log.Debug("GRDF - grdf_authorize - Redirect user to - ", redirectUrl) - http.Redirect(w, r, redirectUrl, 302) }) //ENEDIS REDIRECT ENDPOINT @@ -211,31 +212,44 @@ func main() { req_state := query.Get("state") statusCodes := [4]string{"400", "403", "500", "503"} - if (findItem(statusCodes, code)) { - intCode, err := strconv.Atoi(code) - if err != nil { - log.Error("ProxyError: Enedis - redirect - string to int convert error for status code: ", err) + if len(code) > 0 && len(req_state) > 0 { + if findItem(statusCodes, code) { + intCode, err := strconv.Atoi(code) + if err != nil { + log.Error("ProxyError: Enedis - redirect - string to int convert error for status code: ", err) + http.Error(w, http.StatusText(500), 500) + return + } + log.Error("EnedisError: Enedis - redirect - status code error : ", code) + http.Error(w, http.StatusText(intCode), intCode) + return } - log.Error("EnedisError: Enedis - redirect - status code error : ", code) - http.Error(w, http.StatusText(intCode), intCode) - } else { + splitIndex := strings.Index(req_state, "-") if splitIndex == -1 { log.Error("ProxyError: Enedis - redirect - No host found in query") + http.Error(w, http.StatusText(500), 500) + return } state := req_state[0:splitIndex] host := req_state[splitIndex+1:] - usagePointId := query.Get("usage_point_id") - cozyURL := "https://" + host + "." + *cozyDomain + *cozyRedirectURI - redir := cozyURL + "?code=" + code + "&state=" + state + "&usage_point_id=" + usagePointId log.Debug("Enedis - redirect - Redirect to Cozy stack - ", redir) http.Redirect(w, r, redir, 302) + } else { + log.Error("ProxyError: Enedis - redirect - Missing parameters in request") + http.Error(w, http.StatusText(500), 500) } }) + //ENEDIS WRONG REDIRECT ENDPOINT + mux.HandleFunc("/redirect/", func(w http.ResponseWriter, r *http.Request) { + log.Error("EnedisError: Enedis - redirect - wrong route received from Enedis ") + http.Error(w, http.StatusText(500), 500) + }) + //GRDF REDIRECT ENDPOINT mux.HandleFunc("/redirect-grdf", func(w http.ResponseWriter, r *http.Request) { log.Debug("GRDF - redirect-grdf - Received redirect answer from GRDF") @@ -244,19 +258,26 @@ func main() { code := query.Get("code") req_state := query.Get("state") - //TODO Get pce_id - splitIndex := strings.Index(req_state, "-") - if splitIndex == -1 { - log.Error("ProxyError: GRDF - redirect-grdf - No host found") - } - state := req_state[0:splitIndex] - host := req_state[splitIndex+1:] + + if len(code) > 0 && len(req_state) > 0 { + splitIndex := strings.Index(req_state, "-") + if splitIndex == -1 { + log.Error("ProxyError: GRDF - redirect-grdf - No host found") + http.Error(w, http.StatusText(500), 500) + return + } + state := req_state[0:splitIndex] + host := req_state[splitIndex+1:] - cozyURL := "https://" + host + "." + *cozyDomain + *cozyGrdfRedirectURI + cozyURL := "https://" + host + "." + *cozyDomain + *cozyGrdfRedirectURI - redir := cozyURL + "?code=" + code + "&state=" + state - log.Debug("GRDF - redirect - Redirect to Cozy stack - ", redir) - http.Redirect(w, r, redir, 302) + redir := cozyURL + "?code=" + code + "&state=" + state + log.Debug("GRDF - redirect - Redirect to Cozy stack - ", redir) + http.Redirect(w, r, redir, 302) + } else { + log.Error("ProxyError: GRDF - redirect - Missing parameters in request") + http.Error(w, http.StatusText(500), 500) + } }) //ENEDIS TOKEN ENDPOINT @@ -277,8 +298,9 @@ func main() { contents, err := ioutil.ReadAll(r.Body) if err != nil { log.Error("ProxyError: Enedis - token - Unable to read the body: ", err) - } - + http.Error(w, err.Error(), 500) + return + } params, err := url.ParseQuery(string(contents)) if err != nil { log.Error(err) @@ -300,7 +322,6 @@ func main() { if val, ok := params["refresh_token"]; ok { refreshToken = val[0] } - } else { // Retrieve params from query clientId = query.Get("client_id") @@ -337,34 +358,33 @@ func main() { response, err := http.PostForm(tokenUrl, data) if err != nil { log.Error("ProxyError: Enedis - token - Unable to post the request: ", err) - } else { - log.Debug("Enedis - token - Enedis Endpoint response with status ", response.Status) - defer response.Body.Close() - if response.StatusCode >= 200 && response.StatusCode <= 299 { - // Set Content-Type in response header - w.Header().Add("Content-Type", "application/json") - - // Decode response Body using the defined type "TokenResponse" - data := EnedisTokenResponse{} - decodeError := json.NewDecoder(response.Body).Decode(&data) - if decodeError != nil { - log.Error("ProxyError: Enedis - token - Unable to decode data: ", decodeError) - http.Error(w, decodeError.Error(), 500) - return - } - - // Response with json data - jsonError := json.NewEncoder(w).Encode(data) - if jsonError != nil { - log.Error("ProxyError: Enedis - token - Unable to encode data: ", jsonError) - http.Error(w, jsonError.Error(), 500) - return - } - log.Info("Enedis - token - Response correctly to Cozy stack") - } else { - log.Error("EnedisError: Enedis - token - Enedis response with status code: ", response.StatusCode) - http.Error(w, http.StatusText(response.StatusCode), response.StatusCode) + http.Error(w, http.StatusText(500), 500) + return + } + log.Debug("Enedis - token - Enedis Endpoint response with status ", response.Status) + defer response.Body.Close() + if response.StatusCode >= 200 && response.StatusCode <= 299 { + // Set Content-Type in response header + w.Header().Add("Content-Type", "application/json") + // Decode response Body using the defined type "TokenResponse" + data := EnedisTokenResponse{} + decodeError := json.NewDecoder(response.Body).Decode(&data) + if decodeError != nil { + log.Error("ProxyError: Enedis - token - Unable to decode data: ", decodeError) + http.Error(w, decodeError.Error(), 500) + return + } + // Response with json data + jsonError := json.NewEncoder(w).Encode(data) + if jsonError != nil { + log.Error("ProxyError: Enedis - token - Unable to encode data: ", jsonError) + http.Error(w, jsonError.Error(), 500) + return } + log.Info("Enedis - token - Response correctly to Cozy stack") + } else { + log.Error("EnedisError: Enedis - token - Enedis response with status code: ", response.StatusCode) + http.Error(w, http.StatusText(response.StatusCode), response.StatusCode) } }) @@ -392,7 +412,6 @@ func main() { http.Error(w, err.Error(), 500) return } - params, err := url.ParseQuery(string(contents)) if err != nil { log.Error("ProxyError: GRDF - grdf_token - Unable to parse the query: ", err) @@ -408,7 +427,6 @@ func main() { if val, ok := params["grant_type"]; ok { grantType = val[0] } - } else { // Retrieve params from query clientId = query.Get("client_id") @@ -417,7 +435,6 @@ func main() { grantType = query.Get("grant_type") } - // Print out the result log.WithFields(log.Fields{ "client_id": clientId, @@ -444,63 +461,65 @@ func main() { response, err := http.PostForm(tokenUrl, data) if err != nil { log.Error("ProxyError: GRDF - grdf_token - Unable to post the request: ", err) - } else { - log.Debug("GRDF - grdf_token - GRDF Endpoint response with status ", response.Status) - defer response.Body.Close() - if response.StatusCode >= 200 && response.StatusCode <= 299 { - // Decode response Body using the defined type "GrdfTokenResponse" - data := GrdfTokenResponse{} - decodeError := json.NewDecoder(response.Body).Decode(&data) - if decodeError != nil { - log.Error("ProxyError: GRDF - grdf_token - Unable to decode data: ", err) - http.Error(w, decodeError.Error(), 500) - return - } - // Check if IdToken exist - // Decode the token and retrieve the pce from it - if len(data.IdToken) > 0 { - IdToken = data.IdToken - s := strings.Split(IdToken, ".") - if len(s[1]) > 0 { - payload, _ := base64.StdEncoding.DecodeString(s[1]) - // Check if the payload is well ended - if payload[len(payload)-1] != 125 { - payload = append(payload, []byte{125}...) - } - log.Debug(string(payload)) - // Decode the payload from the token - var token GrdfConsentementToken - err := json.Unmarshal(payload, &token) - if err != nil { - log.Error("ProxyError: GRDF - grdf_token - Unable to unmarshal payload from token: ", err) - http.Error(w, err.Error(), 500) + http.Error(w, http.StatusText(500), 500) + return + } + log.Debug("GRDF - grdf_token - GRDF Endpoint response with status ", response.Status) + defer response.Body.Close() + if response.StatusCode >= 200 && response.StatusCode <= 299 { + // Decode response Body using the defined type "GrdfTokenResponse" + data := GrdfTokenResponse{} + decodeError := json.NewDecoder(response.Body).Decode(&data) + if decodeError != nil { + log.Error("ProxyError: GRDF - grdf_token - Unable to decode data: ", err) + http.Error(w, decodeError.Error(), 500) + return + } + // Check if IdToken exist + // Decode the token and retrieve the pce from it + if len(data.IdToken) > 0 { + IdToken = data.IdToken + s := strings.Split(IdToken, ".") + if len(s[1]) > 0 { + payload, _ := base64.StdEncoding.DecodeString(s[1]) + // Check if the payload is well ended + if payload[len(payload)-1] != 125 { + payload = append(payload, []byte{125}...) + } + log.Debug(string(payload)) + // Decode the payload from the token + var token GrdfConsentementToken + err := json.Unmarshal(payload, &token) + if err != nil { + log.Error("ProxyError: GRDF - grdf_token - Unable to unmarshal payload from token: ", err) + http.Error(w, err.Error(), 500) + return + } + log.Debug("Consentements found : ", token.Consentements) + // Decode the consentement information + if len(token.Consentements) > 0 { + var consentements GrdfConsentement + err2 := json.Unmarshal([]byte(token.Consentements), &consentements) + if err2 != nil { + log.Error("ProxyError: GRDF - grdf_token - Unable to unmarshal consentement information: ", err2) + http.Error(w, err2.Error(), 500) return } - log.Debug("Consentements found : ", token.Consentements) - // Decode the consentement information - if len(token.Consentements) > 0 { - var consentements GrdfConsentement - err2 := json.Unmarshal([]byte(token.Consentements), &consentements) - if err2 != nil { - log.Error("ProxyError: GRDF - grdf_token - Unable to unmarshal consentement information: ", err2) - http.Error(w, err2.Error(), 500) - return - } - if len(consentements[0].Pce) > 0 { - pce = consentements[0].Pce - } + if len(consentements[0].Pce) > 0 { + pce = consentements[0].Pce } } } - if len(pce) <= 0 { - log.Error("ProxyError: GRDF - grdf_token - No PCE found") - http.Error(w, http.StatusText(500), 500) - return - } - } else { - log.Error("GRDFError: GRDF - grdf_token - GRDF response with status code: ", response.StatusCode) - http.Error(w, http.StatusText(response.StatusCode), response.StatusCode) } + if len(pce) <= 0 { + log.Error("ProxyError: GRDF - grdf_token - No PCE found") + http.Error(w, http.StatusText(500), 500) + return + } + } else { + log.Error("GRDFError: GRDF - grdf_token - GRDF response with status code: ", response.StatusCode) + http.Error(w, http.StatusText(response.StatusCode), response.StatusCode) + return } } @@ -517,38 +536,38 @@ func main() { response2, err2 := http.PostForm(tokenUrl, data2) if err2 != nil { log.Error("ProxyError: GRDF - grdf_token - Unable to post the request: ", err2) - } else { - log.Debug("GRDF - grdf_token - Endpoint response with status ", response2.Status) - defer response2.Body.Close() - if response2.StatusCode >= 200 && response2.StatusCode <= 299 { - // Set Content-Type in response header - w.Header().Add("Content-Type", "application/json") - - // Decode response Body using the defined type "GrdfTokenResponse" - data := GrdfTokenResponse{} - decodeError := json.NewDecoder(response2.Body).Decode(&data) - if decodeError != nil { - log.Error("ProxyError: GRDF - grdf_token - Unable to decode data: ", decodeError) - http.Error(w, decodeError.Error(), 500) - return - } - if grantType != "refresh_token"{ - data.RefreshToken = "-" - data.Pce = pce - data.IdToken = IdToken - } - - jsonError := json.NewEncoder(w).Encode(data) - if jsonError != nil { - log.Error("ProxyError: GRDF - grdf_token - Unable to encode data: ", jsonError) - http.Error(w, jsonError.Error(), 500) - return - } - log.Info("GRDF - grdf_token - Response correctly to Cozy stack") - } else { - log.Error("GRDFError: GRDF - grdf_token - GRDF response with status code: ", response2.StatusCode) - http.Error(w, http.StatusText(response2.StatusCode), response2.StatusCode) + http.Error(w, http.StatusText(500), 500) + return + } + log.Debug("GRDF - grdf_token - Endpoint response with status ", response2.Status) + defer response2.Body.Close() + if response2.StatusCode >= 200 && response2.StatusCode <= 299 { + // Set Content-Type in response header + w.Header().Add("Content-Type", "application/json") + // Decode response Body using the defined type "GrdfTokenResponse" + data := GrdfTokenResponse{} + decodeError := json.NewDecoder(response2.Body).Decode(&data) + if decodeError != nil { + log.Error("ProxyError: GRDF - grdf_token - Unable to decode data: ", decodeError) + http.Error(w, decodeError.Error(), 500) + return } + if grantType != "refresh_token"{ + data.RefreshToken = "-" + data.Pce = pce + data.IdToken = IdToken + } + + jsonError := json.NewEncoder(w).Encode(data) + if jsonError != nil { + log.Error("ProxyError: GRDF - grdf_token - Unable to encode data: ", jsonError) + http.Error(w, jsonError.Error(), 500) + return + } + log.Info("GRDF - grdf_token - Response correctly to Cozy stack") + } else { + log.Error("GRDFError: GRDF - grdf_token - GRDF response with status code: ", response2.StatusCode) + http.Error(w, http.StatusText(response2.StatusCode), response2.StatusCode) } }) -- GitLab