|
| 1 | +package rye |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + |
| 7 | + "context" |
| 8 | + |
| 9 | + . "github.com/onsi/ginkgo" |
| 10 | + . "github.com/onsi/gomega" |
| 11 | +) |
| 12 | + |
| 13 | +const AUTH_HEADER_NAME = "Authorization" |
| 14 | + |
| 15 | +var _ = Describe("Auth Middleware", func() { |
| 16 | + var ( |
| 17 | + request *http.Request |
| 18 | + response *httptest.ResponseRecorder |
| 19 | + |
| 20 | + testHandler func(http.ResponseWriter, *http.Request) *Response |
| 21 | + ) |
| 22 | + |
| 23 | + BeforeEach(func() { |
| 24 | + response = httptest.NewRecorder() |
| 25 | + }) |
| 26 | + |
| 27 | + Context("auth", func() { |
| 28 | + var ( |
| 29 | + fakeAuth *recorder |
| 30 | + ) |
| 31 | + |
| 32 | + BeforeEach(func() { |
| 33 | + fakeAuth = &recorder{} |
| 34 | + |
| 35 | + testHandler = NewMiddlewareAuth(fakeAuth.authFunc) |
| 36 | + request = &http.Request{ |
| 37 | + Header: map[string][]string{}, |
| 38 | + } |
| 39 | + }) |
| 40 | + |
| 41 | + It("passes the header to the auth func", func() { |
| 42 | + testAuth := "foobar" |
| 43 | + request.Header.Add(AUTH_HEADER_NAME, testAuth) |
| 44 | + resp := testHandler(response, request) |
| 45 | + |
| 46 | + Expect(resp).To(BeNil()) |
| 47 | + Expect(fakeAuth.header).To(Equal(testAuth)) |
| 48 | + }) |
| 49 | + |
| 50 | + Context("when no header is found", func() { |
| 51 | + It("errors", func() { |
| 52 | + resp := testHandler(response, request) |
| 53 | + |
| 54 | + Expect(resp).ToNot(BeNil()) |
| 55 | + Expect(resp.Err).ToNot(BeNil()) |
| 56 | + Expect(resp.Err.Error()).To(ContainSubstring("no authentication")) |
| 57 | + }) |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + Context("Basic Auth", func() { |
| 62 | + var ( |
| 63 | + username = "user1" |
| 64 | + pass = "mypass" |
| 65 | + ) |
| 66 | + |
| 67 | + BeforeEach(func() { |
| 68 | + testHandler = NewMiddlewareAuth(NewBasicAuthFunc(map[string]string{ |
| 69 | + username: pass, |
| 70 | + })) |
| 71 | + |
| 72 | + request = &http.Request{ |
| 73 | + Header: map[string][]string{}, |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + It("validates the password", func() { |
| 78 | + request.SetBasicAuth(username, pass) |
| 79 | + resp := testHandler(response, request) |
| 80 | + |
| 81 | + Expect(resp.Err).To(BeNil()) |
| 82 | + }) |
| 83 | + |
| 84 | + It("adds the username to context", func() { |
| 85 | + request.SetBasicAuth(username, pass) |
| 86 | + resp := testHandler(response, request) |
| 87 | + |
| 88 | + Expect(resp.Err).To(BeNil()) |
| 89 | + |
| 90 | + ctxUname := resp.Context.Value(AUTH_USERNAME_KEY) |
| 91 | + uname, ok := ctxUname.(string) |
| 92 | + Expect(ok).To(BeTrue()) |
| 93 | + Expect(uname).To(Equal(username)) |
| 94 | + }) |
| 95 | + |
| 96 | + It("preserves the request context", func() { |
| 97 | + |
| 98 | + }) |
| 99 | + |
| 100 | + It("errors if username unknown", func() { |
| 101 | + request.SetBasicAuth("noname", pass) |
| 102 | + resp := testHandler(response, request) |
| 103 | + |
| 104 | + Expect(resp.Err).ToNot(BeNil()) |
| 105 | + Expect(resp.Err.Error()).To(ContainSubstring("invalid auth")) |
| 106 | + }) |
| 107 | + |
| 108 | + It("errors if password wrong", func() { |
| 109 | + request.SetBasicAuth(username, "wrong") |
| 110 | + resp := testHandler(response, request) |
| 111 | + |
| 112 | + Expect(resp.Err).ToNot(BeNil()) |
| 113 | + Expect(resp.Err.Error()).To(ContainSubstring("invalid auth")) |
| 114 | + }) |
| 115 | + |
| 116 | + Context("parseBasicAuth", func() { |
| 117 | + It("errors if header not basic", func() { |
| 118 | + request.Header.Add(AUTH_HEADER_NAME, "wrong") |
| 119 | + resp := testHandler(response, request) |
| 120 | + |
| 121 | + Expect(resp.Err).ToNot(BeNil()) |
| 122 | + Expect(resp.Err.Error()).To(ContainSubstring("invalid auth")) |
| 123 | + }) |
| 124 | + |
| 125 | + It("errors if header not base64", func() { |
| 126 | + request.Header.Add(AUTH_HEADER_NAME, "Basic ------") |
| 127 | + resp := testHandler(response, request) |
| 128 | + |
| 129 | + Expect(resp.Err).ToNot(BeNil()) |
| 130 | + Expect(resp.Err.Error()).To(ContainSubstring("invalid auth")) |
| 131 | + }) |
| 132 | + |
| 133 | + It("errors if header wrong format", func() { |
| 134 | + request.Header.Add(AUTH_HEADER_NAME, "Basic YXNkZgo=") // asdf no `:` |
| 135 | + resp := testHandler(response, request) |
| 136 | + |
| 137 | + Expect(resp.Err).ToNot(BeNil()) |
| 138 | + Expect(resp.Err.Error()).To(ContainSubstring("invalid auth")) |
| 139 | + }) |
| 140 | + }) |
| 141 | + }) |
| 142 | +}) |
| 143 | + |
| 144 | +type recorder struct { |
| 145 | + header string |
| 146 | +} |
| 147 | + |
| 148 | +func (r *recorder) authFunc(ctx context.Context, s string) *Response { |
| 149 | + r.header = s |
| 150 | + return nil |
| 151 | +} |
0 commit comments