Skip to content

Commit 4d128d6

Browse files
author
Florian Hines
authored
Merge pull request #95 from netlify/checkauth-panic
CheckAuth should return if validation fails
2 parents 1f8fc46 + d525e19 commit 4d128d6

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

router/middleware.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@ func CheckAuth(secret string) Middleware {
3838
authHeader := r.Header.Get("Authorization")
3939
if authHeader == "" {
4040
HandleError(UnauthorizedError("This endpoint requires a Bearer token"), w, r)
41+
return
4142
}
4243

4344
matches := bearerRegexp.FindStringSubmatch(authHeader)
4445
if len(matches) != 2 {
4546
HandleError(UnauthorizedError("This endpoint requires a Bearer token"), w, r)
47+
return
4648
}
4749

4850
if secret != matches[1] {
4951
HandleError(UnauthorizedError("This endpoint requires a Bearer token"), w, r)
52+
return
5053
}
5154
}
5255

router/middleware_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package router
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/sirupsen/logrus"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestCheckAuth(t *testing.T) {
15+
validKey := "testkey"
16+
invalidKey := "nopekey"
17+
emptyKey := ""
18+
19+
makeRequest := func(req *http.Request) *httptest.ResponseRecorder {
20+
r := New(logrus.WithField("test", "CheckAuth"))
21+
r.Use(CheckAuth(validKey))
22+
r.Get("/", func(w http.ResponseWriter, r *http.Request) *HTTPError {
23+
return nil
24+
})
25+
rec := httptest.NewRecorder()
26+
r.ServeHTTP(rec, req)
27+
return rec
28+
}
29+
30+
t.Run("valid key", func(t *testing.T) {
31+
req, err := http.NewRequest("GET", "/", nil)
32+
require.NoError(t, err)
33+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", validKey))
34+
rsp := makeRequest(req)
35+
assert.Equal(t, http.StatusOK, rsp.Code)
36+
})
37+
t.Run("lower case bearer", func(t *testing.T) {
38+
req, err := http.NewRequest("GET", "/", nil)
39+
require.NoError(t, err)
40+
req.Header.Set("Authorization", fmt.Sprintf("bearer %s", validKey))
41+
rsp := makeRequest(req)
42+
assert.Equal(t, http.StatusOK, rsp.Code)
43+
})
44+
45+
t.Run("invalid key", func(t *testing.T) {
46+
req, err := http.NewRequest("GET", "/", nil)
47+
require.NoError(t, err)
48+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", invalidKey))
49+
rsp := makeRequest(req)
50+
assert.Equal(t, http.StatusUnauthorized, rsp.Code)
51+
})
52+
t.Run("no header", func(t *testing.T) {
53+
req, err := http.NewRequest("GET", "/", nil)
54+
require.NoError(t, err)
55+
rsp := makeRequest(req)
56+
assert.Equal(t, http.StatusUnauthorized, rsp.Code)
57+
})
58+
t.Run("empty key", func(t *testing.T) {
59+
req, err := http.NewRequest("GET", "/", nil)
60+
require.NoError(t, err)
61+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", emptyKey))
62+
rsp := makeRequest(req)
63+
assert.Equal(t, http.StatusUnauthorized, rsp.Code)
64+
})
65+
t.Run("invalid Authorization value", func(t *testing.T) {
66+
req, err := http.NewRequest("GET", "/", nil)
67+
require.NoError(t, err)
68+
req.Header.Set("Authorization", fmt.Sprintf("what even is this %s", invalidKey))
69+
rsp := makeRequest(req)
70+
assert.Equal(t, http.StatusUnauthorized, rsp.Code)
71+
})
72+
73+
}

0 commit comments

Comments
 (0)