From 0686885f5a232672f79dab8bd8b8513e62cfb887 Mon Sep 17 00:00:00 2001 From: bootjp / Yoshiaki Ueda Date: Thu, 10 Nov 2022 20:37:56 +0900 Subject: [PATCH 1/2] update --- README.md | 51 ++++++++++++++++++++++++++++++++++++++++++++++- path_auth.go | 9 +-------- path_auth_test.go | 26 ++++++++++++++++++++---- 3 files changed, 73 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 8a9a6e0..8784391 100644 --- a/README.md +++ b/README.md @@ -1 +1,50 @@ -# echo_middleware_path_auth \ No newline at end of file + +# echo_middleware_path_auth + +middleware for path-based authentication of labstack echo. Best when using apikey for path. + + + +## 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)) +} +``` diff --git a/path_auth.go b/path_auth.go index f59b644..d122c11 100644 --- a/path_auth.go +++ b/path_auth.go @@ -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 ( @@ -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) } } } diff --git a/path_auth_test.go b/path_auth_test.go index 8c5fa08..ff25cf7 100644 --- a/path_auth_test.go +++ b/path_auth_test.go @@ -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 { @@ -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) From eccfa9c80cd244eca476073fe2401f061316c2ea Mon Sep 17 00:00:00 2001 From: bootjp / Yoshiaki Ueda Date: Thu, 10 Nov 2022 20:40:29 +0900 Subject: [PATCH 2/2] fix README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8784391..6bbf3f9 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ 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