diff --git a/main.go b/main.go index 771bececfc4eb65fbf213eb5d3d4c7414291e179..0b86900bcfc786c0a3c7fefbcc005a82594e5b68 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,7 @@ var ( cozyGrdfRedirectURI = flag.String("cozy_redirect_uri", LookupEnvOrString("COZY_REDIRECT_URI", "/accounts/grdfgrandlyon/redirect"), "Cozy redirect URI (defaults to /accounts/grdfgrandlyon/redirect)") ) -type TokenResponse struct { +type EnedisTokenResponse struct { AccessToken string `json:"access_token"` TokenType string `json:"token_type"` ExpiresIn int `json:"expires_in"` @@ -34,6 +34,14 @@ type TokenResponse struct { UsagePointId string `json:"usage_points_id"` } +type GrdfTokenResponse struct { + AccessToken string `json:"access_token"` + IdToken string `json:"id_token"` + TokenType string `json:"token_type"` + ExpiresIn int `json:"expires_in"` + Scope string `json:"scope"` +} + func LookupEnvOrString(key string, defaultVal string) string { if val, ok := os.LookupEnv(key); ok { return val @@ -336,7 +344,7 @@ func main() { w.Header().Add("Content-Type", "application/json") // Decode response Body using the defined type "TokenResponse" - data := TokenResponse{} + data := EnedisTokenResponse{} decodeError := json.NewDecoder(response.Body).Decode(&data) if decodeError != nil { http.Error(w, decodeError.Error(), 500) @@ -368,18 +376,71 @@ func main() { scope := "" // 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") + contents, err := ioutil.ReadAll(r.Body) + if err != nil { + log.Error(err) + } + pageContent := string(contents) + //Check for client_id + clientIdStartIndex := strings.Index(pageContent, "client_id=") + if clientIdStartIndex == -1 { + log.Error("No client_id found") + http.Error(w, http.StatusText(500), 500) + } + clientIdStartIndex += 10 + clientId = pageContent[clientIdStartIndex : clientIdStartIndex+36] + //Check for client_secret + clientSecretStartIndex := strings.Index(pageContent, "client_secret=") + if clientSecretStartIndex == -1 { + log.Error("No client_secret found") + http.Error(w, http.StatusText(500), 500) + } + clientSecretStartIndex += 14 + clientSecret = pageContent[clientSecretStartIndex : clientSecretStartIndex+36] + //Check for code + codeStartIndex := strings.Index(pageContent, "code=") + if codeStartIndex == -1 { + log.Info("No code found (optional param)") + } else { + codeStartIndex += 5 + code = pageContent[codeStartIndex : codeStartIndex+30] + } + //Check for grant_type + grandTypeStartIndex := strings.Index(pageContent, "grant_type=") + if grandTypeStartIndex == -1 { + log.Error("No grant_type found") + http.Error(w, http.StatusText(500), 500) + } + grandTypeStartIndex += 11 + tempGrandTypeString := pageContent[grandTypeStartIndex:] + grandTypeEndIndex := strings.Index(tempGrandTypeString, "&") + if grandTypeEndIndex == -1 { + log.Error("No closing tag for grant_type found") + http.Error(w, http.StatusText(500), 500) + } + grantType = tempGrandTypeString[0:grandTypeEndIndex] + + } else { // Retrieve params from query clientId = query.Get("client_id") clientSecret = query.Get("client_secret") code = query.Get("code") grantType = query.Get("grant_type") scope = query.Get("scope") + redirectUri = query.Get("redirect_uri") + } + + // Print out the result log.WithFields(log.Fields{ "client_id": clientId, "client_secret": clientSecret, "code": code, "grant_type": grantType, + "redirect_uri": redirectUri "scope": scope, }).Debug("result") @@ -389,6 +450,7 @@ func main() { data.Set("client_id", clientId) data.Set("client_secret", clientSecret) data.Set("grant_type", grantType) + data.Set("redirect_uri", redirectUri) if grantType == "authorization_code" { data.Set("code", code) } else { @@ -406,14 +468,17 @@ func main() { // Set Content-Type in response header w.Header().Add("Content-Type", "application/json") - // Decode response Body using the defined type "TokenResponse" - data := TokenResponse{} + // Decode response Body using the defined type "GrdfTokenResponse" + data := GrdfTokenResponse{} decodeError := json.NewDecoder(response.Body).Decode(&data) if decodeError != nil { http.Error(w, decodeError.Error(), 500) return } - + log.Info("json token data: ", data) + // if data.id_token { + // DECODE JWT + // } // Response with json data jsonError := json.NewEncoder(w).Encode(data) if jsonError != nil {