Skip to content

Commit baf9ab6

Browse files
committed
fix in responses
1 parent c8cde84 commit baf9ab6

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

pkg/authz/authenticate.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ package authz
1616

1717
import (
1818
"context"
19+
"encoding/json"
20+
"net/http"
21+
"net/url"
22+
"strings"
23+
1924
"github.com/greenpau/go-authcrunch/pkg/authz/bypass"
2025
"github.com/greenpau/go-authcrunch/pkg/authz/handlers"
2126
"github.com/greenpau/go-authcrunch/pkg/errors"
@@ -25,9 +30,6 @@ import (
2530
addrutil "github.com/greenpau/go-authcrunch/pkg/util/addr"
2631
"github.com/greenpau/go-authcrunch/pkg/util/validate"
2732
"go.uber.org/zap"
28-
"net/http"
29-
"net/url"
30-
"strings"
3133
)
3234

3335
var (
@@ -37,6 +39,10 @@ var (
3739
}
3840
)
3941

42+
type ErrorMessage struct {
43+
Detail string `json:"detail"`
44+
}
45+
4046
// Authenticate authorizes HTTP requests.
4147
func (g *Gatekeeper) Authenticate(w http.ResponseWriter, r *http.Request, ar *requests.AuthorizationRequest) error {
4248
// Perform authorization bypass checks
@@ -122,9 +128,9 @@ func (g *Gatekeeper) handleUnauthorizedUser(w http.ResponseWriter, r *http.Reque
122128

123129
if !g.config.AuthRedirectDisabled {
124130
return g.handleAuthorizeWithRedirect(w, r, ar)
131+
} else {
132+
return g.handleAuthorizeWithOther(w, r, ar)
125133
}
126-
127-
return err
128134
}
129135

130136
// expireAuthCookies sends cookie delete in HTTP response.
@@ -147,25 +153,38 @@ func (g *Gatekeeper) expireAuthCookies(w http.ResponseWriter, r *http.Request) {
147153
// basic authentication and API keys.
148154
func (g *Gatekeeper) handleAuthorizeWithAuthFailed(w http.ResponseWriter, r *http.Request, ar *requests.AuthorizationRequest) error {
149155
g.expireAuthCookies(w, r)
156+
w.Header().Set("Content-Type", "application/json")
150157
w.WriteHeader(401)
151-
w.Write([]byte(`401 Unauthorized`))
158+
json.NewEncoder(w).Encode(ErrorMessage{"Unauthorized"})
152159
return ar.Response.Error
153160
}
154161

155162
// handleAuthorizeWithBadRequest handles failed authorization requests where
156163
// user data was insufficient to establish a user.
157164
func (g *Gatekeeper) handleAuthorizeWithBadRequest(w http.ResponseWriter, r *http.Request, ar *requests.AuthorizationRequest) error {
158165
g.expireAuthCookies(w, r)
166+
w.Header().Set("Content-Type", "application/json")
159167
w.WriteHeader(400)
160-
w.Write([]byte(`400 Bad Request`))
168+
json.NewEncoder(w).Encode(ErrorMessage{"Bad Request"})
169+
return ar.Response.Error
170+
}
171+
172+
// handleAuthorizeWithOther handles failed authorization requests where
173+
// other reasons.
174+
func (g *Gatekeeper) handleAuthorizeWithOther(w http.ResponseWriter, r *http.Request, ar *requests.AuthorizationRequest) error {
175+
g.expireAuthCookies(w, r)
176+
w.Header().Set("Content-Type", "application/json")
177+
w.WriteHeader(401)
178+
json.NewEncoder(w).Encode(ErrorMessage{"Unauthorized"})
161179
return ar.Response.Error
162180
}
163181

164182
// handleAuthorizeWithForbidden handles forbidden responses.
165183
func (g *Gatekeeper) handleAuthorizeWithForbidden(w http.ResponseWriter, r *http.Request, ar *requests.AuthorizationRequest) error {
166184
if g.config.ForbiddenURL == "" {
185+
w.Header().Set("Content-Type", "application/json")
167186
w.WriteHeader(403)
168-
w.Write([]byte(`Forbidden`))
187+
json.NewEncoder(w).Encode(ErrorMessage{"Forbidden"})
169188
return ar.Response.Error
170189
}
171190

@@ -184,8 +203,9 @@ func (g *Gatekeeper) handleAuthorizeWithForbidden(w http.ResponseWriter, r *http
184203
} else {
185204
w.Header().Set("Location", g.config.ForbiddenURL)
186205
}
206+
w.Header().Set("Content-Type", "application/json")
187207
w.WriteHeader(303)
188-
w.Write([]byte(`Forbidden`))
208+
json.NewEncoder(w).Encode(ErrorMessage{"Forbidden"})
189209
return ar.Response.Error
190210
}
191211

0 commit comments

Comments
 (0)