Skip to content

Commit 0686885

Browse files
committed
update
1 parent 3763175 commit 0686885

File tree

3 files changed

+73
-13
lines changed

3 files changed

+73
-13
lines changed

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
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+
8+
## Badges
9+
10+
11+
[![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](https://choosealicense.com/licenses/mit/)
12+
[![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)
13+
14+
15+
## Usage/Examples
16+
17+
```go
18+
package main
19+
20+
import (
21+
pa "github.com/bootjp/echo_middleware_path_auth"
22+
"github.com/labstack/echo/v4"
23+
"github.com/labstack/echo/v4/middleware"
24+
)
25+
26+
func main() {
27+
28+
e := echo.New()
29+
// group route
30+
e.Group("/api", pa.PathAuth("apikey", func(auth string, c echo.Context) (bool, error) {
31+
// add your logic
32+
return true, nil
33+
}))
34+
35+
// single route
36+
yourHttpHandler := func(c echo.Context) error { return c.String(200, "OK") }
37+
yourPathAuthLogic := func(auth string, c echo.Context) (bool, error) {
38+
return true, nil
39+
}
40+
41+
e.GET("/api/:apikey", yourHttpHandler, pa.PathAuth("apikey", yourPathAuthLogic))
42+
43+
// with config
44+
config := pa.PathAuthConfig{}
45+
config.Skipper = middleware.DefaultSkipper
46+
config.Param = "apikey"
47+
config.Validator = yourPathAuthLogic
48+
e.GET("/api/:apikey", yourHttpHandler, pa.PathAuthWithConfig(config))
49+
}
50+
```

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)