Skip to content
Snippets Groups Projects
Commit 4303069f authored by Hugo NOUTS's avatar Hugo NOUTS
Browse files

parse enedis code containing status code when error

strconv.Atoi string code into int status code
parent 8d744da4
No related branches found
No related tags found
No related merge requests found
Pipeline #7366 failed
......@@ -50,6 +50,20 @@ func LookupEnvOrInt(key string, defaultVal int) int {
return defaultVal
}
func findItem(arrayType interface{}, item interface{}) bool {
arr := reflect.ValueOf(arrayType)
if arr.Kind() != reflect.Array {
panic("Invalid data-type")
}
for i := 0; i < arr.Len(); i++ {
if arr.Index(i).Interface() == item {
return true
}
}
return false
}
func main() {
// Parse the flags
flag.Parse()
......@@ -127,21 +141,31 @@ func main() {
code := query.Get("code")
req_state := query.Get("state")
statusCodes := [4]string{"400", "403", "500", "503"}
splitIndex := strings.Index(req_state, "-")
if splitIndex == -1 {
log.Warning("No host found")
}
state := req_state[0:splitIndex]
host := req_state[splitIndex+1:]
if (findItem(statusCodes, code)) {
intCode, err := strconv.Atoi(code)
if err != nil {
log.Print("status code string to int error: ", err)
}
log.Print("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")
}
state := req_state[0:splitIndex]
host := req_state[splitIndex+1:]
usagePointId := query.Get("usage_point_id")
usagePointId := query.Get("usage_point_id")
cozyURL := "https://" + host + "." + *cozyDomain + *cozyRedirectURI
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)
redir := cozyURL + "?code=" + code + "&state=" + state + "&usage_point_id=" + usagePointId
log.Debug("Redirect to -", redir)
http.Redirect(w, r, redir, 302)
}
})
mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
......@@ -241,7 +265,7 @@ func main() {
data.Set("grant_type", "refresh_token")
}
log.Debug("Send request to token endpoint", tokenUrl)
log.Debug("Send request to token endpoint: ", tokenUrl)
response, err := http.PostForm(tokenUrl, data)
if err != nil {
log.Error(err)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment