auth

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BearerAuth

func BearerAuth(store *TokenStore) func(http.Handler) http.Handler

middleware that authenticates requests via the "Authorization: Bearer <token>" header against the given TokenStore. invalid or missing credentials produce a 401 Unauthorized

func ClientTLSConfig

func ClientTLSConfig(caFile, certFile, keyFile string) (*tls.Config, error)

caFile is the CA bundle used to verify server certificates. empty means the system roots. certFile/keyFile, when set, are presented to servers that require client certificates (mTLS). All-empty input yields a nil config, resulting in plain HTTP

func GenerateToken

func GenerateToken() (string, error)

GenerateToken mints a new random bearer token of the form "ph_<43 base64url chars>"

func HashToken

func HashToken(token string) string

func LoadCAPool

func LoadCAPool(path string) (*x509.CertPool, error)

reads PEM-encoded certificates into a verification pool

func RequireRole

func RequireRole(min Role) func(http.Handler) http.Handler

middleware enforcing a minimum role. must run after BearerAuth: requests without an authenticated identity are rejected with 401 Unauthorized, authenticated but insufficient roles get 403 Forbidden.

func RequireRoleHandler

func RequireRoleHandler(min Role, h http.HandlerFunc) http.HandlerFunc

per-route form of RequireRole for routers that attach authorization to individual handler

(chi cannot mount the same Route path inside two middleware-scoped groups, so the manager API wraps each handler instead :/)

func ServerTLSConfig

func ServerTLSConfig(certFile, keyFile, clientCAFile string) (*tls.Config, error)

certFile/keyFile are the server's own certificate pair (both or neither, an empty pair yields a nil config, resulting in plain HTTP). When clientCAFile is set, clients must present a certificate signed by that CA (mTLS). without it the server accepts any client (TLS w/o client auth)

func WithIdentity

func WithIdentity(ctx context.Context, id Identity) context.Context

Types

type Identity

type Identity struct {
	User string
	Role Role
}

the authenticated principal attached to a request context

func IdentityFromContext

func IdentityFromContext(ctx context.Context) (Identity, bool)

type Role

type Role string

the authorization role attached to a token

admin: read & write access; viewer: read-only access

const (
	RoleAdmin  Role = "admin"
	RoleViewer Role = "viewer"
)

func (Role) Allows

func (r Role) Allows(min Role) bool

type TokenStore

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

holds parsed token records for verification tokens themselves are never stored, only their SHA-256 hashes

func LoadTokenFile

func LoadTokenFile(path string) (*TokenStore, error)

func ParseTokenFile

func ParseTokenFile(data []byte) (*TokenStore, error)

validates and parses token file contents (YAML):

  • user: bipkis role: admin token_hash: "sha256:<64 hex chars>"
  • user: bopkis role: viewer token_hash: "sha256:..."

func (*TokenStore) Verify

func (s *TokenStore) Verify(token string) (Identity, bool)

checks a presented bearer token and returns the identity it maps to hashes are compared in constant time to prevent timing attack

Jump to

Keyboard shortcuts

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