diff --git a/main.go b/main.go index 9f29bb371862c221d5b564b6f434b3a24463ccb1..273d53d57e975d9783a5e7a5ebf3039beea79da3 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "net/http" "net/url" + "reflect" "os" "strconv" "strings" @@ -50,6 +51,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() @@ -164,21 +179,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) + } }) //ENEDIS TOKEN ENDPOINT @@ -279,7 +304,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)