Skip to content

Commit

Permalink
make admin requered to enforce auth
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Jan 2, 2019
1 parent d6a9988 commit f883e6f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 1 addition & 2 deletions middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ func (a *Authenticator) refreshExpiredToken(w http.ResponseWriter, claims token.
// AdminOnly middleware allows access for admins only
func (a *Authenticator) AdminOnly(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {

user, err := token.GetUserInfo(r)
if err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
Expand All @@ -141,7 +140,7 @@ func (a *Authenticator) AdminOnly(next http.Handler) http.Handler {
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
return a.auth(true)(http.HandlerFunc(fn))
}

// basic auth for admin user
Expand Down
13 changes: 13 additions & 0 deletions middleware/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,19 @@ func TestAdminRequired(t *testing.T) {
resp, err = client.Do(req)
require.NoError(t, err)
assert.Equal(t, 403, resp.StatusCode, "valid token user, not admin")

req, err = http.NewRequest("GET", server.URL+"/auth", nil)
require.NoError(t, err)
resp, err = client.Do(req)
require.NoError(t, err)
assert.Equal(t, 401, resp.StatusCode, "not authorized")

req, err = http.NewRequest("GET", server.URL+"/auth", nil)
require.NoError(t, err)
req.Header.Add("X-JWT", "bad bad token")
resp, err = client.Do(req)
require.NoError(t, err)
assert.Equal(t, 401, resp.StatusCode, "not authorized")
}

func makeTestMux(t *testing.T, a *Authenticator, required bool) http.Handler {
Expand Down

0 comments on commit f883e6f

Please sign in to comment.