echo_middleware_path_auth

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2022 License: MIT Imports: 3 Imported by: 0

README

echo_middleware_path_auth

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

example) https://example.com/api/this_is_api_key

This this_is_api_key part can be dynamically submitted to authentication. For example, whether apikey is active, RateLimit is not exceeded, etc.

Much of this code is based on key_auth.go in labstack/echo and its test code.

Badges

MIT License Test

Usage/Examples

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/:apikey", 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))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultKeyAuthConfig is the default PathAuth middleware config.
	DefaultKeyAuthConfig = PathAuthConfig{
		Skipper: middleware.DefaultSkipper,
	}
)
View Source
var ErrKeyAuthMissing = echo.NewHTTPError(http.StatusBadRequest, "Missing key in the request")

ErrKeyAuthMissing is error type when PathAuth middleware is unable to extract value from lookups

Functions

func PathAuth

func PathAuth(param string, fn PathAuthValidator) echo.MiddlewareFunc

PathAuth returns an PathAuth middleware.

For valid key it calls the next handler. For invalid key, it sends "401 - Unauthorized" response. For missing key, it sends "400 - Bad Request" response.

func PathAuthWithConfig

func PathAuthWithConfig(config PathAuthConfig) echo.MiddlewareFunc

Types

type PathAuthConfig

type PathAuthConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper middleware.Skipper

	// Validator is a function to validate key.
	// Required.
	Validator PathAuthValidator

	Param string
}

PathAuthConfig defines the config for PathAuth middleware.

type PathAuthValidator

type PathAuthValidator func(auth string, c echo.Context) (bool, error)

PathAuthValidator defines a function to validate PathAuth credentials.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL