@@ -16,6 +16,11 @@ package authz
1616
1717import (
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
3335var (
3739 }
3840)
3941
42+ type ErrorMessage struct {
43+ Detail string `json:"detail"`
44+ }
45+
4046// Authenticate authorizes HTTP requests.
4147func (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.
148154func (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.
157164func (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.
165183func (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