Skip to content

Commit 1612e39

Browse files
committed
fail basic auth also when no authorize header present
Signed-off-by: Ivan Porto Carrero <ivan@flanders.co.nz>
1 parent d68539e commit 1612e39

5 files changed

Lines changed: 20 additions & 19 deletions

File tree

middleware/router_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestRouterMiddleware(t *testing.T) {
5050
assert.Equal(t, http.StatusMethodNotAllowed, recorder.Code)
5151

5252
methods := strings.Split(recorder.Header().Get("Allow"), ",")
53-
sort.Sort(sort.StringSlice(methods))
53+
sort.Strings(methods)
5454
assert.Equal(t, "GET,POST", strings.Join(methods, ","))
5555

5656
recorder = httptest.NewRecorder()
@@ -82,7 +82,7 @@ func TestRouterMiddleware(t *testing.T) {
8282
assert.Equal(t, http.StatusMethodNotAllowed, recorder.Code)
8383

8484
methods = strings.Split(recorder.Header().Get("Allow"), ",")
85-
sort.Sort(sort.StringSlice(methods))
85+
sort.Strings(methods)
8686
assert.Equal(t, "GET,POST", strings.Join(methods, ","))
8787

8888
recorder = httptest.NewRecorder()
@@ -232,11 +232,11 @@ func TestPathConverter(t *testing.T) {
232232
func TestExtractCompositParameters(t *testing.T) {
233233
// name is the composite parameter's name, value is the value of this composit parameter, pattern is the pattern to be matched
234234
cases := []struct {
235-
name string
236-
value string
235+
name string
236+
value string
237237
pattern string
238-
names []string
239-
values []string
238+
names []string
239+
values []string
240240
}{
241241
{name: "fragment", value: "gie", pattern: "e", names: []string{"fragment"}, values: []string{"gi"}},
242242
{name: "fragment", value: "t.simpson", pattern: ".{subfragment}", names: []string{"fragment", "subfragment"}, values: []string{"t", "simpson"}},

middleware/spec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func Spec(basePath string, b []byte, next http.Handler) http.Handler {
3434
rw.Header().Set("Content-Type", "application/json")
3535
rw.WriteHeader(http.StatusOK)
3636
//#nosec
37-
rw.Write(b)
37+
_, _ = rw.Write(b)
3838
return
3939
}
4040

middleware/untyped/api.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ func (d *API) validate() error {
245245

246246
func (d *API) verify(name string, registrations []string, expectations []string) error {
247247

248-
sort.Sort(sort.StringSlice(registrations))
249-
sort.Sort(sort.StringSlice(expectations))
248+
sort.Strings(registrations)
249+
sort.Strings(expectations)
250250

251251
expected := map[string]struct{}{}
252252
seen := map[string]struct{}{}
@@ -271,8 +271,8 @@ func (d *API) verify(name string, registrations []string, expectations []string)
271271
for k := range expected {
272272
unregistered = append(unregistered, k)
273273
}
274-
sort.Sort(sort.StringSlice(unspecified))
275-
sort.Sort(sort.StringSlice(unregistered))
274+
sort.Strings(unspecified)
275+
sort.Strings(unregistered)
276276

277277
if len(unregistered) > 0 || len(unspecified) > 0 {
278278
return &errors.APIVerificationFailed{

middleware/untyped/api_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,15 +247,15 @@ func TestUntypedAppValidation(t *testing.T) {
247247
api2.RegisterProducer("application/x-yaml", new(stubProducer))
248248

249249
expected := []string{"application/x-yaml"}
250-
sort.Sort(sort.StringSlice(expected))
250+
sort.Strings(expected)
251251
consumes := analyzed.ConsumesFor(analyzed.AllPaths()["/"].Get)
252-
sort.Sort(sort.StringSlice(consumes))
252+
sort.Strings(consumes)
253253
assert.Equal(t, expected, consumes)
254254
consumers := api1.ConsumersFor(consumes)
255255
assert.Len(t, consumers, 1)
256256

257257
produces := analyzed.ProducesFor(analyzed.AllPaths()["/"].Get)
258-
sort.Sort(sort.StringSlice(produces))
258+
sort.Strings(produces)
259259
assert.Equal(t, expected, produces)
260260
producers := api1.ProducersFor(produces)
261261
assert.Len(t, producers, 1)

security/authenticator.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,19 @@ func BasicAuth(authenticate UserPassAuthentication) runtime.Authenticator {
9696

9797
// BasicAuthBasicAuthRealm creates a basic auth authenticator with the provided authentication function and realm name
9898
func BasicAuthRealm(realm string, authenticate UserPassAuthentication) runtime.Authenticator {
99+
if realm == "" {
100+
realm = DefaultRealmName
101+
}
102+
99103
return HttpAuthenticator(func(r *http.Request) (bool, interface{}, error) {
100104
if usr, pass, ok := r.BasicAuth(); ok {
101105
p, err := authenticate(usr, pass)
102106
if err != nil {
103-
if realm == "" {
104-
realm = DefaultRealmName
105-
}
106107
*r = *r.WithContext(context.WithValue(r.Context(), failedBasicAuth, realm))
107108
}
108109
return true, p, err
109110
}
110-
111+
*r = *r.WithContext(context.WithValue(r.Context(), failedBasicAuth, realm))
111112
return false, nil, nil
112113
})
113114
}
@@ -132,7 +133,7 @@ func BasicAuthRealmCtx(realm string, authenticate UserPassAuthenticationCtx) run
132133
*r = *r.WithContext(ctx)
133134
return true, p, err
134135
}
135-
136+
*r = *r.WithContext(context.WithValue(r.Context(), failedBasicAuth, realm))
136137
return false, nil, nil
137138
})
138139
}

0 commit comments

Comments
 (0)