auth

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package auth implements single-admin authentication for HTTP services.

Index

Constants

View Source
const (
	AdminSubject = "admin"
)

Variables

View Source
var (
	ErrMissingCredentials = &AuthError{Code: CodeMissingCredentials}
	ErrInvalidBearer      = &AuthError{Code: CodeInvalidBearer}
	ErrInvalidSession     = &AuthError{Code: CodeInvalidSession}
	ErrExpiredSession     = &AuthError{Code: CodeExpiredSession}
	ErrCSRF               = &AuthError{Code: CodeCSRF}
)

Functions

This section is empty.

Types

type AuthError

type AuthError struct {
	Code ErrorCode
}

AuthError is a typed authentication error returned by Authenticate and AuthenticateBearer. Callers can inspect Code or use errors.Is with the exported sentinel errors below.

func (*AuthError) Error

func (e *AuthError) Error() string

func (*AuthError) Is

func (e *AuthError) Is(target error) bool

Is allows errors.Is to compare authentication errors by code.

type AuthMethod

type AuthMethod string

AuthMethod identifies how a request was authenticated.

const (
	// AuthMethodBearer identifies a request authenticated with an admin bearer token.
	AuthMethodBearer AuthMethod = "bearer"
	// AuthMethodSession identifies a request authenticated with a signed browser session.
	AuthMethodSession AuthMethod = "session"
)

type ErrorCode

type ErrorCode string

ErrorCode classifies an authentication failure without exposing credentials.

const (
	CodeMissingCredentials ErrorCode = "missing_credentials"
	CodeInvalidBearer      ErrorCode = "invalid_bearer"
	CodeInvalidSession     ErrorCode = "invalid_session"
	CodeExpiredSession     ErrorCode = "expired_session"
	CodeCSRF               ErrorCode = "csrf_failed"
)

type Manager

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

Manager validates the single admin bearer token and issues signed browser sessions. It retains only the SHA-256 hash of the admin token; the plaintext admin token is not kept after initialization.

func NewManager

func NewManager(adminTokenPath, sessionKeyPath string, options ...Option) (*Manager, string, error)

NewManager loads or creates the admin token and session HMAC key at their caller-specified paths. Both files are created with, and normalized to, mode 0600. The returned bootstrapToken is non-empty only when this call created the admin token; callers may display it once for initial setup. An existing stored token is never returned.

func (*Manager) Authenticate

func (m *Manager) Authenticate(request *http.Request) (Principal, error)

Authenticate authenticates an HTTP request. A supplied Authorization header takes precedence over a session cookie. Cookie-authenticated state-changing requests require a matching X-CSRF-Token header; bearer-authenticated requests do not.

func (*Manager) AuthenticateBearer

func (m *Manager) AuthenticateBearer(token string) (Principal, error)

AuthenticateBearer validates token using a constant-time comparison and returns the admin principal on success.

func (*Manager) ClearSession

func (m *Manager) ClearSession(writer http.ResponseWriter, request *http.Request)

ClearSession expires the configured browser session cookie.

func (*Manager) CookieName

func (m *Manager) CookieName() string

CookieName returns the configured browser session cookie name.

func (*Manager) CurrentSession

func (m *Manager) CurrentSession(request *http.Request) (Session, error)

CurrentSession validates the signed browser session and returns the browser values needed to make CSRF-protected requests. It never exposes the bearer token or the session signing key.

func (*Manager) IssueSession

func (m *Manager) IssueSession(writer http.ResponseWriter, request *http.Request) (Session, error)

IssueSession writes a signed, expiring browser session cookie and returns its CSRF token. Call it only after the caller has authenticated an administrator. X-Forwarded-Proto is trusted for Secure-cookie detection, so deployments must ensure it is stripped or set only by a trusted reverse proxy.

type Option

type Option func(*managerOptions) error

Option configures a Manager.

func WithClock

func WithClock(clock func() time.Time) Option

WithClock supplies the clock used to issue and validate session expiry. It is useful when an application already has a controlled time source.

func WithCookieName

func WithCookieName(name string) Option

WithCookieName sets the name of the browser session cookie.

func WithSessionTTL

func WithSessionTTL(ttl time.Duration) Option

WithSessionTTL sets the lifetime used for newly issued browser sessions.

type Principal

type Principal struct {
	Subject string
	Method  AuthMethod
}

Principal is the authenticated identity for a request.

git-ci has one administrative identity, identified by AdminSubject.

type Session

type Session struct {
	CSRFToken string
	ExpiresAt time.Time
}

Session describes a newly issued browser session. CSRFToken is intended to be returned to the browser by the caller and supplied in X-CSRF-Token on state-changing cookie-authenticated requests.

Jump to

Keyboard shortcuts

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