Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
# echo_middleware_path_auth

# echo_middleware_path_auth

middleware for path-based authentication of labstack echo. Best when using apikey for path.


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).

## Badges


[![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](https://choosealicense.com/licenses/mit/)
[![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)


## Usage/Examples

```go
package main

import (
pa "github.com/bootjp/echo_middleware_path_auth"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)

func main() {

e := echo.New()
// group route
e.Group("/api", pa.PathAuth("apikey", func(auth string, c echo.Context) (bool, error) {
// add your logic
return true, nil
}))

// single route
yourHttpHandler := func(c echo.Context) error { return c.String(200, "OK") }
yourPathAuthLogic := func(auth string, c echo.Context) (bool, error) {
return true, nil
}

e.GET("/api/:apikey", yourHttpHandler, pa.PathAuth("apikey", yourPathAuthLogic))

// with config
config := pa.PathAuthConfig{}
config.Skipper = middleware.DefaultSkipper
config.Param = "apikey"
config.Validator = yourPathAuthLogic
e.GET("/api/:apikey", yourHttpHandler, pa.PathAuthWithConfig(config))
}
```
9 changes: 1 addition & 8 deletions path_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@ type (
// Required.
Validator PathAuthValidator

// ErrorHandler defines a function which is executed for an invalid key.
// It may be used to define a custom error.
ErrorHandler PathAuthErrorHandler

Param string
}

// PathAuthValidator defines a function to validate PathAuth credentials.
PathAuthValidator func(auth string, c echo.Context) (bool, error)

// PathAuthErrorHandler defines a function which is executed for an invalid key.
PathAuthErrorHandler func(err error, c echo.Context) error
)

var (
Expand Down Expand Up @@ -94,7 +87,7 @@ func PathAuthWithConfig(config PathAuthConfig) echo.MiddlewareFunc {
return next(c)
}

return echo.NewHTTPError(http.StatusBadRequest, err.Error())
return echo.NewHTTPError(http.StatusBadRequest)
}
}
}
26 changes: 22 additions & 4 deletions path_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,32 @@ func TestPathAuthWithConfig(t *testing.T) {
expectError string
}{
{
name: "ok, default config",
name: "ok success",
givenRequestFunc: func() *http.Request {
req := httptest.NewRequest(http.MethodPost, "/valid-key", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationForm)
req := httptest.NewRequest(http.MethodGet, "/valid-key", nil)
return req
},
expectHandlerCalled: true,
expectError: "",
},
{
name: "ng user error",
givenRequestFunc: func() *http.Request {
req := httptest.NewRequest(http.MethodGet, "/error-key", nil)
return req
},
expectHandlerCalled: false,
expectError: "code=401, message=Unauthorized, internal=some user defined error",
},
{
name: "ng no valid no error",
givenRequestFunc: func() *http.Request {
req := httptest.NewRequest(http.MethodGet, "/bad", nil)
return req
},
expectHandlerCalled: false,
expectError: "code=400, message=Bad Request",
},
}

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

assert.Equal(t, tc.expectHandlerCalled, handlerCalled)
Expand Down