Skip to content

Commit db86f41

Browse files
committed
changing callback -> onFailure in AuthOpts and authorizeCallback
1 parent da530cf commit db86f41

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

pkg/api/controller.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ type Controller struct {
112112
}
113113

114114
type AuthOpts struct {
115-
callback func(w http.ResponseWriter, r *http.Request, code int, v interface{})
115+
// this function is called in authorizeCallback when authorization fails
116+
onFailure func(w http.ResponseWriter, r *http.Request, httpStatusCode int, error interface{})
116117
}
117118

118119
var usageCounter = stats.NewUsageCounter()
@@ -4567,8 +4568,8 @@ func (c *Controller) LogCommits(w http.ResponseWriter, r *http.Request, reposito
45674568

45684569
func (c *Controller) HeadObject(w http.ResponseWriter, r *http.Request, repository, ref string, params apigen.HeadObjectParams) {
45694570
if !c.authorizeReq(w, r, "HeadObject", permissions.PermissionParams{Repository: &repository, Path: &params.Path},
4570-
&AuthOpts{callback: func(w http.ResponseWriter, r *http.Request, code int, v interface{}) {
4571-
writeResponse(w, r, code, nil)
4571+
&AuthOpts{onFailure: func(w http.ResponseWriter, r *http.Request, httpStatusCode int, error interface{}) {
4572+
writeResponse(w, r, httpStatusCode, nil)
45724573
}}) {
45734574
return
45744575
}
@@ -5874,27 +5875,27 @@ func paginationFor(hasMore bool, results interface{}, fieldName string) apigen.P
58745875
return pagination
58755876
}
58765877

5877-
func (c *Controller) authorizeCallback(w http.ResponseWriter, r *http.Request, perms permissions.Node, cb func(w http.ResponseWriter, r *http.Request, code int, v interface{})) bool {
5878+
func (c *Controller) authorizeCallback(w http.ResponseWriter, r *http.Request, perms permissions.Node, onFailure func(w http.ResponseWriter, r *http.Request, httpStatusCode int, error interface{})) bool {
58785879
ctx := r.Context()
58795880
user, err := auth.GetUser(ctx)
58805881
if err != nil {
5881-
cb(w, r, http.StatusUnauthorized, ErrAuthenticatingRequest)
5882+
onFailure(w, r, http.StatusUnauthorized, ErrAuthenticatingRequest)
58825883
return false
58835884
}
58845885
resp, err := c.Auth.Authorize(ctx, &auth.AuthorizationRequest{
58855886
Username: user.Username,
58865887
RequiredPermissions: perms,
58875888
})
58885889
if err != nil {
5889-
cb(w, r, http.StatusInternalServerError, err)
5890+
onFailure(w, r, http.StatusInternalServerError, err)
58905891
return false
58915892
}
58925893
if resp.Error != nil {
5893-
cb(w, r, http.StatusUnauthorized, resp.Error)
5894+
onFailure(w, r, http.StatusUnauthorized, resp.Error)
58945895
return false
58955896
}
58965897
if !resp.Allowed {
5897-
cb(w, r, http.StatusInternalServerError, "User does not have the required permissions")
5898+
onFailure(w, r, http.StatusInternalServerError, "User does not have the required permissions")
58985899
return false
58995900
}
59005901
return true
@@ -5910,11 +5911,11 @@ func (c *Controller) authorizeReq(w http.ResponseWriter, r *http.Request, operat
59105911
c.Logger.Error(fmt.Sprintf("missing permission descriptor for %s", operationId))
59115912
return false
59125913
}
5913-
callback := writeError
5914-
if opts != nil && opts.callback != nil {
5915-
callback = opts.callback
5914+
onFailure := writeError
5915+
if opts != nil && opts.onFailure != nil {
5916+
onFailure = opts.onFailure
59165917
}
5917-
return c.authorizeCallback(w, r, desc.Permission(params), callback)
5918+
return c.authorizeCallback(w, r, desc.Permission(params), onFailure)
59185919
}
59195920

59205921
func (c *Controller) isNameValid(name, nameType string) (bool, string) {

0 commit comments

Comments
 (0)