Skip to content

Commit

Permalink
feat: allow response rewrite being set from authorization service
Browse files Browse the repository at this point in the history
  • Loading branch information
bartjkdp committed Dec 10, 2023
1 parent 825ce6e commit 610fde0
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions cmd/filter-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ type ClaimsWithGroups struct {
}

type AuthorizationResponse struct {
User struct {
Id int64
Username string
Name string
}
Result bool `json:"result"`
ResponseFilter string `json:"response_filter"`
}

func main() {
Expand All @@ -53,12 +50,17 @@ func main() {

utils.DelHopHeaders(r.Header)

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

if !authorizationResponse.Result {
writeError(w, http.StatusUnauthorized, "result field is not true")
return
}

allowedMethods := path.AllowedMethods
if len(allowedMethods) == 0 {
allowedMethods = []string{"GET"}
Expand Down Expand Up @@ -183,13 +185,20 @@ func main() {

defer proxyResp.Body.Close()

if path.ResponseRewrite != "" && proxyResp.StatusCode == http.StatusOK {
if proxyResp.StatusCode == http.StatusOK && (path.ResponseRewrite != "" || authorizationResponse.ResponseFilter != "") {
body, _ := io.ReadAll(proxyResp.Body)

var result map[string]interface{}
json.Unmarshal(body, &result)

query, err := gojq.Parse(path.ResponseRewrite)
var responseRewrite = ""
if authorizationResponse.ResponseFilter != "" {
responseRewrite = authorizationResponse.ResponseFilter
} else {
responseRewrite = path.ResponseRewrite
}

query, err := gojq.Parse(responseRewrite)
if err != nil {
writeError(w, http.StatusInternalServerError, "could not parse filter")
return
Expand Down

0 comments on commit 610fde0

Please sign in to comment.