Skip to content

Commit

Permalink
Merge pull request #21 from delta10/fix/return-error-code-from-author…
Browse files Browse the repository at this point in the history
…ization-service

Return error code from authorization service
  • Loading branch information
bartjkdp authored Oct 18, 2023
2 parents 3c8c1a1 + cc967b6 commit 3d08750
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions cmd/filter-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func main() {

utils.DelHopHeaders(r.Header)

authorizationResponse, ok := authorizeRequestWithService(config, path, r)
if !ok {
writeError(w, http.StatusUnauthorized, "unauthorized request")
authorizationStatusCode, authorizationResponse := authorizeRequestWithService(config, path, r)
if authorizationStatusCode != http.StatusOK {
writeError(w, authorizationStatusCode, "unauthorized request")
return
}

Expand Down Expand Up @@ -270,16 +270,16 @@ func main() {
}
}

func authorizeRequestWithService(config *config.Config, path config.Path, r *http.Request) (*AuthorizationResponse, bool) {
func authorizeRequestWithService(config *config.Config, path config.Path, r *http.Request) (int, *AuthorizationResponse) {
if config.AuthorizationServiceURL == "" {
log.Print("returned unauthenticated as there is no authorization service URL configured.")
return nil, false
return http.StatusInternalServerError, nil
}

authorizationServiceURL, err := url.Parse(config.AuthorizationServiceURL)
if err != nil {
log.Printf("could not parse authorization url: %s", err)
return nil, false
return http.StatusInternalServerError, nil
}

authorizationServiceURL.RawQuery = r.URL.RawQuery
Expand All @@ -302,30 +302,25 @@ func authorizeRequestWithService(config *config.Config, path config.Path, r *htt
resp, err := client.Do(request)
if err != nil {
log.Printf("could not fetch authorization response: %s", err)
return nil, false
return http.StatusInternalServerError, nil
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
log.Printf("authorization response is not ok")
return nil, false
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("could not read authorization response: %s", err)
return nil, false
return http.StatusInternalServerError, nil
}

responseData := AuthorizationResponse{}
err = json.Unmarshal(body, &responseData)
if err != nil {
log.Printf("could not unmarshal authorization response: %s", err)
return nil, false
return http.StatusInternalServerError, nil
}

return &responseData, true
return resp.StatusCode, &responseData
}

func writeError(w http.ResponseWriter, statusCode int, message string) {
Expand Down

0 comments on commit 3d08750

Please sign in to comment.