From 93c17a1fe4d248e4f56c8997c8d34312f80d8050 Mon Sep 17 00:00:00 2001 From: Yoan VALLET <ext.sopra.yvallet@grandlyon.com> Date: Tue, 9 Jun 2020 17:44:30 +0200 Subject: [PATCH] split redirect cozy uri Only instance name is passed to Enedis to not reach the 100 max chars --- main.go | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 8d184d0..1c0f877 100644 --- a/main.go +++ b/main.go @@ -15,9 +15,11 @@ import ( ) var ( - httpPort = flag.Int("http_port", LookupEnvOrInt("HTTP_PORT", 80), "HTTP port to serve on (defaults to 80)") - logLevel = flag.String("loglevel", LookupEnvOrString("LOGLEVEL", "debug"), "log level (debug, info, warning, error) (defaults to debug)") - redirectURI = flag.String("redirect_uri", LookupEnvOrString("REDIRECT_URI", "https://oauth-proxy.wf.alpha.grandlyon.com/redirect"), "Redirect URI (defaults to https://oauth-proxy.wf.alpha.grandlyon.com/redirect)") + httpPort = flag.Int("http_port", LookupEnvOrInt("HTTP_PORT", 80), "HTTP port to serve on (defaults to 80)") + logLevel = flag.String("loglevel", LookupEnvOrString("LOGLEVEL", "debug"), "log level (debug, info, warning, error) (defaults to debug)") + proxyRedirectURI = flag.String("proxy_redirect_uri", LookupEnvOrString("PROXY_REDIRECT_URI", "https://oauth-proxy.wf.alpha.grandlyon.com/redirect"), "Proxy redirect URI (defaults to https://oauth-proxy.wf.alpha.grandlyon.com/redirect)") + cozyDomain = flag.String("cozy_domain", LookupEnvOrString("COZY_DOMAIN", "cozy.wf.alpha.grandlyon.com"), "Cozy domain (defaults to cozy.wf.alpha.grandlyon.com)") + cozyRedirectURI = flag.String("cozy_redirect_uri", LookupEnvOrString("COZY_REDIRECT_URI", ".cozy.wf.alpha.grandlyon.com/accounts/enedis/redirect"), "Cozy redirect URI (defaults to /accounts/enedis/redirect)") ) type TokenResponse struct { @@ -92,15 +94,28 @@ 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 responseType := "code" + // 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&redirect_uri=" + *redirectURI + "&response_type=" + responseType + "&state=" + state + "-" + cozyOrigin + redirectUrl := authURL + "?client_id=" + clientId + "&duration=P6M&redirect_uri=" + *proxyRedirectURI + "&response_type=" + responseType + "&state=" + state + "-" + instanceName log.Debug("Redirect to - ", redirectUrl) http.Redirect(w, r, redirectUrl, 302) @@ -123,7 +138,9 @@ func main() { usagePointId := query.Get("usage_point_id") - redir := host + "?code=" + code + "&state=" + state + "&usage_point_id=" + usagePointId + cozyURL := "https://" + host + *cozyDomain + *cozyRedirectURI + + redir := cozyURL + "?code=" + code + "&state=" + state + "&usage_point_id=" + usagePointId log.Debug("Redirect to -", redir) http.Redirect(w, r, redir, 302) }) -- GitLab