Skip to content

Commit 3b2a883

Browse files
committed
fix missing status code
1 parent eccfa9c commit 3b2a883

2 files changed

Lines changed: 68 additions & 21 deletions

File tree

path_auth.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,7 @@ var (
3131
)
3232

3333
// ErrKeyAuthMissing is error type when PathAuth middleware is unable to extract value from lookups
34-
type ErrKeyAuthMissing struct {
35-
Err error
36-
}
37-
38-
// Error returns errors text
39-
func (e *ErrKeyAuthMissing) Error() string {
40-
return e.Err.Error()
41-
}
42-
43-
// Unwrap unwraps error
44-
func (e *ErrKeyAuthMissing) Unwrap() error {
45-
return e.Err
46-
}
34+
var ErrKeyAuthMissing = echo.NewHTTPError(http.StatusBadRequest, "Missing key in the request")
4735

4836
// PathAuth returns an PathAuth middleware.
4937
//
@@ -74,20 +62,35 @@ func PathAuthWithConfig(config PathAuthConfig) echo.MiddlewareFunc {
7462
if config.Skipper(c) {
7563
return next(c)
7664
}
77-
valid, err := config.Validator(c.Param(config.Param), c)
78-
if err != nil {
65+
66+
if !extract(config.Param, c.ParamNames()) {
7967
return &echo.HTTPError{
80-
Code: http.StatusUnauthorized,
81-
Message: "Unauthorized",
82-
Internal: err,
68+
Code: http.StatusBadRequest,
69+
Message: http.StatusText(http.StatusBadRequest),
70+
Internal: ErrKeyAuthMissing,
8371
}
8472
}
8573

86-
if valid {
74+
valid, err := config.Validator(c.Param(config.Param), c)
75+
if err == nil && valid {
8776
return next(c)
8877
}
8978

90-
return echo.NewHTTPError(http.StatusBadRequest)
79+
return &echo.HTTPError{
80+
Code: http.StatusUnauthorized,
81+
Message: http.StatusText(http.StatusUnauthorized),
82+
Internal: err,
83+
}
9184
}
9285
}
9386
}
87+
88+
func extract(cParam string, params []string) bool {
89+
for _, param := range params {
90+
if cParam == param {
91+
return true
92+
}
93+
}
94+
95+
return false
96+
}

path_auth_test.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,53 @@ func TestKeyAuth(t *testing.T) {
6363
err := middlewareChain(c)
6464

6565
assert.Error(t, err)
66+
assert.EqualError(t, err, "code=401, message=Unauthorized, internal=some user defined error")
6667
assert.False(t, handlerCalled)
6768
})
69+
t.Run("auth no error failed", func(t *testing.T) {
70+
handlerCalled := false
71+
handler := func(c echo.Context) error {
72+
handlerCalled = true
73+
return c.String(http.StatusOK, "test")
74+
}
75+
middlewareChain := PathAuth("apikey", testKeyValidator)(handler)
6876

77+
e := echo.New()
78+
e.GET("/:apikey", middlewareChain)
79+
80+
req := httptest.NewRequest(http.MethodGet, "/", nil)
81+
rec := httptest.NewRecorder()
82+
83+
c := e.NewContext(req, rec)
84+
e.Router().Find(http.MethodGet, "/no-error", c)
85+
err := middlewareChain(c)
86+
87+
assert.Error(t, err)
88+
assert.EqualError(t, err, "code=401, message=Unauthorized")
89+
assert.False(t, handlerCalled)
90+
})
91+
t.Run("auth nokey", func(t *testing.T) {
92+
handlerCalled := false
93+
handler := func(c echo.Context) error {
94+
handlerCalled = true
95+
return c.String(http.StatusOK, "test")
96+
}
97+
middlewareChain := PathAuth("undef", testKeyValidator)(handler)
98+
99+
e := echo.New()
100+
e.GET("/:apikey", middlewareChain)
101+
102+
req := httptest.NewRequest(http.MethodGet, "/", nil)
103+
rec := httptest.NewRecorder()
104+
105+
c := e.NewContext(req, rec)
106+
e.Router().Find(http.MethodGet, "/error-key", c)
107+
err := middlewareChain(c)
108+
109+
assert.Error(t, err)
110+
assert.EqualError(t, err, "code=400, message=Bad Request, internal=code=400, message=Missing key in the request")
111+
assert.False(t, handlerCalled)
112+
})
69113
}
70114

71115
func TestPathAuthWithConfig(t *testing.T) {
@@ -103,7 +147,7 @@ func TestPathAuthWithConfig(t *testing.T) {
103147
return req
104148
},
105149
expectHandlerCalled: false,
106-
expectError: "code=400, message=Bad Request",
150+
expectError: "code=401, message=Unauthorized",
107151
},
108152
}
109153

0 commit comments

Comments
 (0)