diff --git a/main.go b/main.go index 2cdf294e449fe2bdbff2c68c08754fe1d2fd2308..d35a1151d425dc76082bd3f9299a419c8e23986c 100644 --- a/main.go +++ b/main.go @@ -90,8 +90,8 @@ func main() { if splitIndex == -1 { fmt.Println("No host found") } - state := string([]byte(req_state[0:splitIndex])) - host := string([]byte(req_state[splitIndex+1:len(req_state)])) + state := req_state[0:splitIndex] + host := req_state[splitIndex+1:len(req_state)] usagePointId := query.Get("usage_point_id") @@ -106,17 +106,78 @@ func main() { query := r.URL.Query() fmt.Println(query) - contents, err := ioutil.ReadAll(r.Body) - if err != nil { - fmt.Println(err) - } - fmt.Println(string(contents)) + clientId := "" + clientSecret := "" + code := "" + grantType := "" + refreshToken := "" - clientId := query.Get("client_id") - clientSecret := query.Get("client_secret") - code := query.Get("code") - grantType := query.Get("grant_type") - refreshToken := query.Get("refresh_token") + if len(query) == 0 { + fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "No url params found - check in the body") + contents, err := ioutil.ReadAll(r.Body) + if err != nil { + fmt.Println(err) + } + pageContent := string(contents) + //Check for client_id + clientIdStartIndex := strings.Index(pageContent, "client_id=") + if clientIdStartIndex == -1 { + fmt.Println("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 { + fmt.Println("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 { + fmt.Println("No code found") + } else { + codeStartIndex += 5 + code = pageContent[codeStartIndex:codeStartIndex + 30] + } + //Check for grant_type + grandTypeStartIndex := strings.Index(pageContent, "grant_type=") + if grandTypeStartIndex == -1 { + fmt.Println("No grant_type found") + http.Error(w, http.StatusText(500), 500) + } + grandTypeStartIndex += 11 + tempGrandTypeString := pageContent[grandTypeStartIndex:len(pageContent)] + grandTypeEndIndex := strings.Index(tempGrandTypeString, "&") + if grandTypeEndIndex == -1 { + fmt.Println("No closing tag for grant_type found") + http.Error(w, http.StatusText(500), 500) + } + grantType = tempGrandTypeString[0:grandTypeEndIndex] + //Check for refresh_token + refershTokenStartIndex := strings.Index(pageContent, "refresh_token=") + if refershTokenStartIndex == -1 { + fmt.Println("No code found") + } + refershTokenStartIndex += 14 + refreshToken = pageContent[refershTokenStartIndex:refershTokenStartIndex + 46] + } else { + clientId = query.Get("client_id") + clientSecret = query.Get("client_secret") + code = query.Get("code") + grantType = query.Get("grant_type") + refreshToken = query.Get("refresh_token") + } + // Print out the result + fmt.Printf("client_id: %s\n", clientId) + fmt.Printf("client_secret: %s\n", clientSecret) + fmt.Printf("code: %s\n", code) + fmt.Printf("grant_type: %s\n", grantType) + fmt.Printf("refresh_token: %s\n", refreshToken) + tokenUrl := "https://gw.hml.api.enedis.fr/v1/oauth2/token"