diff --git a/main.go b/main.go index 8345153047ad879b63802f40c068f183095b55ca..9f29bb371862c221d5b564b6f434b3a24463ccb1 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()