echobasicauth

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: LGPL-3.0 Imports: 8 Imported by: 0

README

echo basic auth

Basic Auth middleware with constant time equality checks and optional IP whitelisting for Echo framework. CIDRs are supported for IP whitelisting as well

Usage

auth := &echobasicauth.Auth{Login: "test", Password: "test", IPs: []string{"127.0.0.1", "10.0.0.0/24"}}
e.Use(echobasicauth.NewMiddleware(auth))
// or you can use echobasicauth.NewValidator(auth) if you want to define the middleware yourself
IP validation without credentials

You can use AllowedIP directly to check if an IP is allowed without validating credentials:

auth := &echobasicauth.Auth{IPs: []string{"127.0.0.1", "10.0.0.0/24"}}
if auth.AllowedIP(c.RealIP()) {
    // IP is allowed
}

Documentation

Index

Constants

View Source
const ContextLoginKey = "echo-basic-auth.login"

ContextLoginKey is the key used to store the login after successful auth in the context

Variables

This section is empty.

Functions

func NewMiddleware

func NewMiddleware(auths ...*Auth) echo.MiddlewareFunc

NewMiddleware returns a new BasicAuth middleware instance

func NewValidator

func NewValidator(auths ...*Auth) middleware.BasicAuthValidator

NewValidator returns a new BasicAuthValidator

Types

type Auth

type Auth struct {
	Login    string   `json:"login" yaml:"login"`       // Basic auth login
	Password string   `json:"password" yaml:"password"` //nolint:gosec // this is an auth model, the field is intentional
	IPs      []string `json:"ips" yaml:"ips"`           // Allowed IPs and CIDRs
	// contains filtered or unexported fields
}

Auth model

func (*Auth) AllowedIP added in v1.4.0

func (a *Auth) AllowedIP(ip string) bool

AllowedIP checks if the given IP is allowed by this Auth's IP rules

Jump to

Keyboard shortcuts

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