diff --git a/main.go b/main.go index 8345153047ad879b63802f40c068f183095b55ca..bc3dc7ca6294610d8ca8d59bd593288ecb442103 100644 --- a/main.go +++ b/main.go @@ -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)