httpauth

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package httpauth provides authentication strategies for HTTP requests in go-polyscript.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Authenticator

type Authenticator interface {
	// Authenticate applies authentication to the HTTP request.
	// It should modify the request in-place to add authentication details.
	Authenticate(req *http.Request) error

	// AuthenticateWithContext applies authentication with context.
	// This allows for context cancellation during authentication.
	AuthenticateWithContext(ctx context.Context, req *http.Request) error

	// Name returns a descriptive name of the authentication method.
	Name() string
}

Authenticator defines the interface for HTTP authentication strategies.

type BasicAuth

type BasicAuth struct {
	Username string
	Password string
}

BasicAuth implements HTTP Basic Authentication according to RFC 7617. It adds an Authorization header with the format: "Basic base64(username:password)".

func NewBasicAuth

func NewBasicAuth(username, password string) *BasicAuth

NewBasicAuth creates a new BasicAuth authenticator with the given credentials. If username is empty, this authenticator will do nothing when called.

func (*BasicAuth) Authenticate

func (b *BasicAuth) Authenticate(req *http.Request) error

Authenticate applies basic authentication to the HTTP request by setting the Authorization header using the standard Basic auth format. If the username is empty, no authentication is applied.

func (*BasicAuth) AuthenticateWithContext

func (b *BasicAuth) AuthenticateWithContext(ctx context.Context, req *http.Request) error

AuthenticateWithContext applies basic authentication with context support. This respects context cancellation while applying authentication.

func (*BasicAuth) Name

func (b *BasicAuth) Name() string

Name returns the name of the authentication method.

type HeaderAuth

type HeaderAuth struct {
	Headers map[string]string
}

HeaderAuth implements authentication via custom HTTP headers. This is a flexible authenticator that can be used for various header-based authentication schemes like Bearer tokens, API keys, and custom authentication.

func NewBearerAuth

func NewBearerAuth(token string) *HeaderAuth

NewBearerAuth creates a HeaderAuth specifically configured for Bearer token authentication. This is a common authentication method for APIs that use JWT or OAuth tokens.

func NewHeaderAuth

func NewHeaderAuth(headers map[string]string) *HeaderAuth

NewHeaderAuth creates a new HeaderAuth authenticator with the given headers. Each key-value pair in the map will be set as a header in the HTTP request.

func (*HeaderAuth) Authenticate

func (h *HeaderAuth) Authenticate(req *http.Request) error

Authenticate applies header-based authentication to the HTTP request by setting all headers from the Headers map.

func (*HeaderAuth) AuthenticateWithContext

func (h *HeaderAuth) AuthenticateWithContext(ctx context.Context, req *http.Request) error

AuthenticateWithContext applies header-based authentication with context support. This respects context cancellation while applying authentication.

func (*HeaderAuth) Name

func (h *HeaderAuth) Name() string

Name returns the name of the authentication method.

type NoAuth

type NoAuth struct{}

NoAuth represents a no-authentication strategy. This authenticator performs no operations and is useful as a default or for explicitly indicating that no authentication is required.

func NewNoAuth

func NewNoAuth() *NoAuth

NewNoAuth creates a new NoAuth authenticator instance.

func (*NoAuth) Authenticate

func (n *NoAuth) Authenticate(req *http.Request) error

Authenticate does nothing as no authentication is needed. This method satisfies the Authenticator interface but performs no operations.

func (*NoAuth) AuthenticateWithContext

func (n *NoAuth) AuthenticateWithContext(ctx context.Context, req *http.Request) error

AuthenticateWithContext does nothing as no authentication is needed, but respects context cancellation.

func (*NoAuth) Name

func (n *NoAuth) Name() string

Name returns the name of the authentication method.

Jump to

Keyboard shortcuts

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