Skip to content

Commit 3ef66c6

Browse files
authored
Merge pull request #4 from bootjp/feature/1
Configure README.md and Add test cases
2 parents 71e2c0a + eccfa9c commit 3ef66c6

File tree

3 files changed

+74
-13
lines changed

3 files changed

+74
-13
lines changed

README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
1-
# echo_middleware_path_auth
1+
2+
# echo_middleware_path_auth
3+
4+
middleware for path-based authentication of labstack echo. Best when using apikey for path.
5+
6+
7+
Much of this code is based on [key_auth.go in labstack/echo and its test code](https://github.com/labstack/echo/blob/01d7d01bbc1948cd308b2ae93a131654e6dba195/middleware/key_auth.go).
8+
9+
## Badges
10+
11+
12+
[![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](https://choosealicense.com/licenses/mit/)
13+
[![Test](https://github.com/bootjp/echo_middleware_path_auth/actions/workflows/test.yml/badge.svg)](https://github.com/bootjp/echo_middleware_path_auth/actions/workflows/test.yml)
14+
15+
16+
## Usage/Examples
17+
18+
```go
19+
package main
20+
21+
import (
22+
pa "github.com/bootjp/echo_middleware_path_auth"
23+
"github.com/labstack/echo/v4"
24+
"github.com/labstack/echo/v4/middleware"
25+
)
26+
27+
func main() {
28+
29+
e := echo.New()
30+
// group route
31+
e.Group("/api", pa.PathAuth("apikey", func(auth string, c echo.Context) (bool, error) {
32+
// add your logic
33+
return true, nil
34+
}))
35+
36+
// single route
37+
yourHttpHandler := func(c echo.Context) error { return c.String(200, "OK") }
38+
yourPathAuthLogic := func(auth string, c echo.Context) (bool, error) {
39+
return true, nil
40+
}
41+
42+
e.GET("/api/:apikey", yourHttpHandler, pa.PathAuth("apikey", yourPathAuthLogic))
43+
44+
// with config
45+
config := pa.PathAuthConfig{}
46+
config.Skipper = middleware.DefaultSkipper
47+
config.Param = "apikey"
48+
config.Validator = yourPathAuthLogic
49+
e.GET("/api/:apikey", yourHttpHandler, pa.PathAuthWithConfig(config))
50+
}
51+
```

path_auth.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,11 @@ type (
1616
// Required.
1717
Validator PathAuthValidator
1818

19-
// ErrorHandler defines a function which is executed for an invalid key.
20-
// It may be used to define a custom error.
21-
ErrorHandler PathAuthErrorHandler
22-
2319
Param string
2420
}
2521

2622
// PathAuthValidator defines a function to validate PathAuth credentials.
2723
PathAuthValidator func(auth string, c echo.Context) (bool, error)
28-
29-
// PathAuthErrorHandler defines a function which is executed for an invalid key.
30-
PathAuthErrorHandler func(err error, c echo.Context) error
3124
)
3225

3326
var (
@@ -94,7 +87,7 @@ func PathAuthWithConfig(config PathAuthConfig) echo.MiddlewareFunc {
9487
return next(c)
9588
}
9689

97-
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
90+
return echo.NewHTTPError(http.StatusBadRequest)
9891
}
9992
}
10093
}

path_auth_test.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,32 @@ func TestPathAuthWithConfig(t *testing.T) {
7979
expectError string
8080
}{
8181
{
82-
name: "ok, default config",
82+
name: "ok success",
8383
givenRequestFunc: func() *http.Request {
84-
req := httptest.NewRequest(http.MethodPost, "/valid-key", nil)
85-
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationForm)
84+
req := httptest.NewRequest(http.MethodGet, "/valid-key", nil)
8685
return req
8786
},
8887
expectHandlerCalled: true,
8988
expectError: "",
9089
},
90+
{
91+
name: "ng user error",
92+
givenRequestFunc: func() *http.Request {
93+
req := httptest.NewRequest(http.MethodGet, "/error-key", nil)
94+
return req
95+
},
96+
expectHandlerCalled: false,
97+
expectError: "code=401, message=Unauthorized, internal=some user defined error",
98+
},
99+
{
100+
name: "ng no valid no error",
101+
givenRequestFunc: func() *http.Request {
102+
req := httptest.NewRequest(http.MethodGet, "/bad", nil)
103+
return req
104+
},
105+
expectHandlerCalled: false,
106+
expectError: "code=400, message=Bad Request",
107+
},
91108
}
92109

93110
for _, tc := range testCases {
@@ -117,7 +134,8 @@ func TestPathAuthWithConfig(t *testing.T) {
117134
e.GET("/:apikey", middlewareChain)
118135
rec := httptest.NewRecorder()
119136
c := e.NewContext(req, rec)
120-
e.Router().Find(http.MethodGet, "/valid-key", c)
137+
// use params
138+
e.Router().Find(http.MethodGet, req.URL.Path, c)
121139
err := middlewareChain(c)
122140

123141
assert.Equal(t, tc.expectHandlerCalled, handlerCalled)

0 commit comments

Comments
 (0)