token

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 8 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// TokensFieldName is the command-line flag for inline tokens.
	// Format: "token1,token2,token3"
	TokensFieldName = "server.http.auth.token.tokens"

	// HeaderNameFieldName is the command-line flag for the header name.
	HeaderNameFieldName = "server.http.auth.token.header"

	// FilePathFieldName is the command-line flag for tokens file.
	// File should contain one token per line.
	FilePathFieldName = "server.http.auth.token.filepath"

	// HeaderNameDefault is the default header name for token authentication.
	HeaderNameDefault = "X-Auth-Token"
)

Variables

View Source
var Component = &component.Component{
	Init: component.StepFunc(func(container container.Container) error {
		return container.Provides(
			NewConfig,
			NewAuth,
		)
	}),
	Configuration: component.StepFunc(func(container container.Container) error {
		return container.Invoke(Configuration)
	}),
}

Component is a ready-to-use Compogo component that provides HTTP Token Authentication middleware.

Usage:

compogo.WithComponents(
    http.Component,
    token.Component,
)

Then in your router setup:

router.Use(tokenAuth.Middleware)

Functions

This section is empty.

Types

type Auth

type Auth struct {
	// contains filtered or unexported fields
}

Auth implements HTTP Token Authentication middleware. It validates incoming requests against configured tokens.

func NewAuth

func NewAuth(config *Config) *Auth

NewAuth creates a new Auth middleware instance with the given configuration.

func (*Auth) Middleware

func (auth *Auth) Middleware(next http.Handler) http.Handler

Middleware implements the http.Middleware interface. It checks for a valid token in the configured header:

  • If token missing or invalid, returns 401 Unauthorized
  • If valid, passes the request to the next handler

type Config

type Config struct {
	// HeaderName is the HTTP header to look for the token.
	HeaderName string

	// FilePath is the path to a file containing allowed tokens.
	FilePath string

	// Set of allowed tokens for O(1) lookup
	Tokens set.Set[string]
	// contains filtered or unexported fields
}

Config holds the token authentication configuration. It can be populated from command-line flags, config files, or a tokens file.

func Configuration

func Configuration(config *Config, configurator configurator.Configurator) (*Config, error)

Configuration applies configuration values to the Config struct. It reads from configurator and optionally from a tokens file. Returns an error if file reading fails.

func NewConfig

func NewConfig() *Config

NewConfig creates a new Config instance with default values.

Jump to

Keyboard shortcuts

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