basic_auth

package
v0.0.0-...-4f5bba3 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: MPL-2.0 Imports: 9 Imported by: 0

README

Basic Authentication Middleware

Basic Authentication is an authentication method where the client sends its credentials unencrypted to the server for validation. This middleware implements this process. A detailed description can also be found here.

The credentials can be stored in different forms. This module has an interface for authenticators, that provide the final verification, and concentrates on the protocol between the client and the server. Further a simple example authenticator is provided that is to be configured with the allowed credentials.

Example

finalHandler := midgard.StackMiddlewareHandler(
    []midgard.Middleware{
        util.Must(basic_auth.New(
            basic_auth.WithRealm("example realm"),
            basic_auth.WithAuthenticator(util.Must(
                map_auth.New(
                    map_auth.WithAuths(map[string]string{
                        "user0": "pass0",
                        "user1": "pass1",
                    }),
                ),
            )),
        )),
    },
    http.HandlerFunc(HelloHandler),
)

If no realm is specified using WithRealm the default Restricted is used. Not providing an authenticator is an error condition.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractUserPass

func ExtractUserPass(auth string) (user, pass string, found bool, err error)

ExtractUserPass extracts the username and the password out of the given header value for Authorization. It signalizes if the desired information exists or en error, when the auth string is unprocessable.

func New

func New(options ...func(handler *Handler) error) (defs.Middleware, error)

New generates a new basic authentication middleware.

func WithAuthenticator

func WithAuthenticator(auth Authenticator) func(h *Handler) error

WithAuthenticator sets the Authenticator to use.

func WithLogLevel

func WithLogLevel(level slog.Level) func(h *Handler) error

WithLogLevel configures the log level to use with the logger.

func WithLogger

func WithLogger(log *slog.Logger) func(h *Handler) error

WithLogger configures the logger to use.

func WithRealm

func WithRealm(realm string) func(h *Handler) error

WithRealm sets the realm to use

func WithRedirect

func WithRedirect(redirect string) func(h *Handler) error

Types

type Authenticator

type Authenticator interface {
	// Authenticate checks, if a given username and password are allowed
	// credentials
	Authenticate(username, password string) (bool, error)
}

Authenticator is an interface the basic auth handler uses to check if the given credentials match an allowed entry.

type Handler

type Handler struct {
	defs.MWBase
	// contains filtered or unexported fields
}

Handler holds the internal data of the basic authentication middleware.

func (*Handler) GetMWBase

func (h *Handler) GetMWBase() *defs.MWBase

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the basic auth functionality.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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