diff --git a/main.go b/main.go index 18421cd9161058c367a52134f3b582206b824950..c4afcd834a51e4aab1f4212744ace7bdd06540a2 100644 --- a/main.go +++ b/main.go @@ -138,9 +138,9 @@ func main() { // ENEDIS AUTH ENDPOINT mux.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) { - log.Debug("New auth request") + log.Debug("Enedis - auth - Received new auth request from Cozy") query := r.URL.Query() - log.Debug("Query received - ", query) + log.Debug("Enedis - auth - query received from Cozy - ", query) clientId := query.Get("client_id") state := query.Get("state") @@ -150,12 +150,12 @@ func main() { cozyOrigin := query.Get("redirect_uri") splitIndexStart := strings.Index(cozyOrigin, ":") if splitIndexStart == -1 { - log.Error("redirect_uri bad format " + cozyOrigin) + 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("redirect_uri bad format " + cozyOrigin) + log.Error("ProxyError: Enedis - auth - redirect_uri bad format from Cozy" + cozyOrigin) http.Error(w, http.StatusText(500), 500) } instanceName := cozyOrigin[splitIndexStart+3:splitIndexEnd] @@ -167,13 +167,13 @@ func main() { redirectUrl := authURL + "?client_id=" + clientId + "&duration=P6M&response_type=" + responseType + "&state=" + state + "-" + instanceName - log.Debug("Redirect to - ", redirectUrl) + log.Debug("Enedis - auth - Redirect user to Enedis - ", redirectUrl) http.Redirect(w, r, redirectUrl, 302) }) // GRDF ADICT AUTHORIZE ENDPOINT mux.HandleFunc("/grdf_authorize", func(w http.ResponseWriter, r *http.Request) { - log.Debug("New grdf auth request") + log.Debug("GRDF - grdf_authorize - Received new auth request from Cozy") query := r.URL.Query() log.Debug("Query received - ", query) @@ -182,12 +182,12 @@ func main() { cozyOrigin := query.Get("redirect_uri") splitIndexStart := strings.Index(cozyOrigin, ":") if splitIndexStart == -1 { - log.Error("redirect_uri bad format " + cozyOrigin) + 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("redirect_uri bad format " + cozyOrigin) + log.Error("ProxyError: GRDF - grdf_authorize - redirect_uri bad format " + cozyOrigin) http.Error(w, http.StatusText(500), 500) } instanceName := cozyOrigin[splitIndexStart+3:splitIndexEnd] @@ -197,14 +197,13 @@ func main() { redirectUrl := authURL + "?client_id=" + clientId + "&scope=openid&response_type=code&redirect_uri="+ redirectProxy + "&login_hint=Prénom|Nom||Ecolyo&state=" + state + "-" + instanceName - // TODO Add Login Hint in request - log.Debug("Redirect to - ", redirectUrl) + log.Debug("GRDF - grdf_authorize - Redirect user 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") + log.Debug("Enedis - redirect - Received redirect answer from Enedis") query := r.URL.Query() log.Debug(query) @@ -215,14 +214,14 @@ func main() { if (findItem(statusCodes, code)) { intCode, err := strconv.Atoi(code) if err != nil { - log.Print("status code string to int error: ", err) + log.Error("ProxyError: Enedis - redirect - string to int convert error for status code: ", err) } - log.Print("status code error : ", code) + 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.Warning("No host found") + log.Error("ProxyError: Enedis - redirect - No host found in query") } state := req_state[0:splitIndex] host := req_state[splitIndex+1:] @@ -232,14 +231,14 @@ func main() { cozyURL := "https://" + host + "." + *cozyDomain + *cozyRedirectURI redir := cozyURL + "?code=" + code + "&state=" + state + "&usage_point_id=" + usagePointId - log.Debug("Redirect to - ", redir) + log.Debug("Enedis - redirect - Redirect to Cozy stack - ", redir) http.Redirect(w, r, redir, 302) } }) //GRDF REDIRECT ENDPOINT mux.HandleFunc("/redirect-grdf", func(w http.ResponseWriter, r *http.Request) { - log.Debug("New redirection on grdf-redirect") + log.Debug("GRDF - redirect-grdf - Received redirect answer from GRDF") query := r.URL.Query() log.Debug(query) @@ -248,7 +247,7 @@ func main() { //TODO Get pce_id splitIndex := strings.Index(req_state, "-") if splitIndex == -1 { - log.Warning("No host found") + log.Error("ProxyError: GRDF - redirect-grdf - No host found") } state := req_state[0:splitIndex] host := req_state[splitIndex+1:] @@ -256,13 +255,13 @@ func main() { cozyURL := "https://" + host + "." + *cozyDomain + *cozyGrdfRedirectURI redir := cozyURL + "?code=" + code + "&state=" + state - log.Debug("Redirect to -", redir) + log.Debug("GRDF - redirect - Redirect to Cozy stack - ", redir) http.Redirect(w, r, redir, 302) }) //ENEDIS TOKEN ENDPOINT mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) { - log.Debug("New token request") + log.Debug("Enedis - token - Received new token request from Cozy") query := r.URL.Query() log.Debug(query) @@ -274,10 +273,10 @@ func main() { // For request token params are into query parameters if len(query) == 0 { - log.Warn("No params found in url query - Trying to catch them from body") + log.Debug("Enedis - token - No params found in url query, trying to catch them from body") contents, err := ioutil.ReadAll(r.Body) if err != nil { - log.Error(err) + log.Error("ProxyError: Enedis - token - Unable to read the body: ", err) } params, err := url.ParseQuery(string(contents)) @@ -334,12 +333,12 @@ func main() { data.Set("grant_type", "refresh_token") } - log.Debug("Send request to token endpoint: ", tokenUrl) + log.Debug("Enedis - token - Send request to Enedis token endpoint: ", tokenUrl) response, err := http.PostForm(tokenUrl, data) if err != nil { - log.Error(err) + log.Error("ProxyError: Enedis - token - Unable to post the request: ", err) } else { - log.Debug("Endpoint response with status ", response.Status) + 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 @@ -349,6 +348,7 @@ func main() { 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 } @@ -356,10 +356,13 @@ func main() { // 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) } } @@ -367,7 +370,7 @@ func main() { //GRDF TOKEN ENDPOINT mux.HandleFunc("/grdf_token", func(w http.ResponseWriter, r *http.Request) { - log.Debug("New GRDF token request") + log.Debug("GRDF - grdf_token - Received new token request from Cozy") query := r.URL.Query() log.Debug(query) @@ -382,17 +385,17 @@ func main() { // For request token params are into query parameters if len(query) == 0 { - log.Warn("No params found in url query \nStack probably asks for a refresh token \nTrying to catch them from body") + log.Debug("GRDF - grdf_token - No params found in url query \nStack probably asks for a refresh token \nTrying to catch them from body") contents, err := ioutil.ReadAll(r.Body) if err != nil { - log.Error(err) + log.Error("ProxyError: GRDF - grdf_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) + log.Error("ProxyError: GRDF - grdf_token - Unable to parse the query: ", err) http.Error(w, err.Error(), 500) return } @@ -436,19 +439,20 @@ func main() { data.Set("redirect_uri", redirectUri) data.Set("code", code) - log.Debug("data sent is : ", data) - log.Debug("Send request to access_token endpoint with authorization_code: ", tokenUrl) + log.Debug("GRDF - grdf_token - data sent is : ", data) + log.Debug("GRDF - grdf_token - Send request to access_token endpoint with authorization_code: ", tokenUrl) response, err := http.PostForm(tokenUrl, data) if err != nil { - log.Error(err) + log.Error("ProxyError: GRDF - grdf_token - Unable to post the request: ", err) } else { - log.Debug("Endpoint response with status ", response.Status) + 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 } @@ -468,7 +472,7 @@ func main() { var token GrdfConsentementToken err := json.Unmarshal(payload, &token) if err != nil { - log.Error(err.Error()) + log.Error("ProxyError: GRDF - grdf_token - Unable to unmarshal payload from token: ", err) http.Error(w, err.Error(), 500) return } @@ -478,7 +482,7 @@ func main() { var consentements GrdfConsentement err2 := json.Unmarshal([]byte(token.Consentements), &consentements) if err2 != nil { - log.Error(err2.Error()) + log.Error("ProxyError: GRDF - grdf_token - Unable to unmarshal consentement information: ", err2) http.Error(w, err2.Error(), 500) return } @@ -489,11 +493,12 @@ func main() { } } if len(pce) <= 0 { - log.Error("No PCE found") + 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) } } @@ -507,13 +512,13 @@ func main() { data2.Set("redirect_uri", redirectUri) data2.Set("scope", "/adict/v1") - log.Debug("data sent is : ", data2) - log.Debug("Send request to access_token endpoint with client_credentials: ", tokenUrl) + log.Debug("GRDF - grdf_token - data sent is : ", data2) + log.Debug("GRDF - grdf_token - Send request to access_token endpoint with client_credentials: ", tokenUrl) response2, err2 := http.PostForm(tokenUrl, data2) if err2 != nil { - log.Error(err2) + log.Error("ProxyError: GRDF - grdf_token - Unable to post the request: ", err2) } else { - log.Debug("Endpoint response with status ", response2.Status) + 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 @@ -523,6 +528,7 @@ func main() { 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 } @@ -534,10 +540,13 @@ func main() { 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) } }