authentication

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package authentication runs ordered trusted authentication handlers. It never trusts cookies or forwarded headers automatically and writes no HTTP response. Hosting must terminate failed credentials (401); exhaustion remains anonymous for authorization to decide. Anonymous role denial is ordinarily 403.

Index

Examples

Constants

View Source
const MicrosoftIdentityProviderClaim = "urn:cratis:arc:identity:provider"

MicrosoftIdentityProviderClaim is Arc-authored forwarded provider metadata.

Variables

View Source
var (
	// ErrFailed identifies recognized rejected credentials.
	ErrFailed = errors.New("authentication failed")
	// ErrInvalidHandler identifies a nil or typed-nil handler.
	ErrInvalidHandler = errors.New("invalid authentication handler")
	// ErrInvalidPrincipal identifies an anonymous success principal.
	ErrInvalidPrincipal = errors.New("invalid authenticated principal")
	// ErrInvalidRequest identifies a nil request, context or chain.
	ErrInvalidRequest = errors.New("invalid authentication request")
)

Functions

This section is empty.

Types

type Chain

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

Chain is immutable and concurrent-safe when its borrowed handlers are. Zero is an empty chain. No callback is run after a terminal success or failure.

func New

func New(handlers ...Handler) (*Chain, error)

New copies the registration slice, rejecting nil and typed-nil handlers.

func (*Chain) Authenticate

func (c *Chain) Authenticate(ctx context.Context, request *http.Request) (Result, error)

Authenticate installs the supplied context on a request clone. Context, headers and URL are isolated from the caller; Body is shared and handlers must not read it. Other request data is not guaranteed isolated. Errors are infrastructure failures, not credential rejection.

type Failure

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

Failure represents rejected credentials, not an infrastructure error.

func (*Failure) Error

func (e *Failure) Error() string

Error returns generic text; it never discloses the diagnostic reason.

func (*Failure) Reason

func (e *Failure) Reason() string

Reason returns local diagnostics, not automatically client-visible text.

func (*Failure) Unwrap

func (e *Failure) Unwrap() error

Unwrap identifies ErrFailed.

type Handler

type Handler interface {
	Authenticate(context.Context, *http.Request) (Result, error)
}

Handler authenticates synchronously, borrowing request data for the call only. Handlers must not read Body: it is shared with the caller, not cloned. Shared handlers must support concurrent calls and honor cancellation. Errors are server faults logged by the host and must not contain or wrap token text, credentials or header values. Use Failed for credential rejection; its diagnostic reason is never logged by Arc ingress.

func HostPrincipal

func HostPrincipal() Handler

HostPrincipal reads only trusted typed identity context metadata. It does not authenticate headers, JWT payloads, Basic credentials or display cookies.

Example
package main

import (
	"context"
	"fmt"
	"net/http/httptest"

	"github.com/cratis/arc.go/authentication"
	"github.com/cratis/arc.go/identity"
)

func main() {
	chain, err := authentication.New(authentication.HostPrincipal())
	if err != nil {
		panic(err)
	}
	ctx := identity.WithPrincipal(context.Background(), identity.System("jobs"))
	result, err := chain.Authenticate(ctx, httptest.NewRequest("GET", "/", nil))
	if err != nil {
		panic(err)
	}
	actor, ok := result.Principal()
	fmt.Println(actor.Name(), ok)
}
Output:
[System] true

func MicrosoftIdentityPlatform

func MicrosoftIdentityPlatform(options MicrosoftIdentityOptions) (Handler, error)

MicrosoftIdentityPlatform explicitly constructs a borrowed concurrent-safe handler; it is never registered automatically and does not validate JWTs.

type HandlerFunc

type HandlerFunc func(context.Context, *http.Request) (Result, error)

HandlerFunc adapts a trusted authentication callback.

func (HandlerFunc) Authenticate

func (f HandlerFunc) Authenticate(ctx context.Context, request *http.Request) (Result, error)

Authenticate invokes f with borrowed request data.

type MicrosoftIdentityOptions

type MicrosoftIdentityOptions struct {
	TrustForwardedIdentityHeaders bool
	MaxPrincipalBytes             int
	// Logger receives a warning once per process when untrusted forwarded headers
	// are ignored. Nil disables diagnostics; header values are never logged.
	Logger *slog.Logger
}

MicrosoftIdentityOptions configures the unsigned forwarded-principal adapter. TrustForwardedIdentityHeaders requires an isolating, credential-validating ingress which strips caller headers, writes verified replacements and prevents direct backend access. Base64 is not authentication proof. Zero trust ignores all headers. MaxPrincipalBytes zero selects 64 KiB; negative is invalid.

type Result

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

Result is immutable; zero is anonymous. Success and failure are terminal.

func Anonymous

func Anonymous() Result

Anonymous returns the nonterminal anonymous result.

func Authenticated

func Authenticated(principal identity.Principal) (Result, error)

Authenticated returns success only for a trusted authenticated snapshot.

func Failed

func Failed(reason string) Result

Failed records a terminal credential rejection. reason is local diagnostics.

func (Result) Failure

func (r Result) Failure() *Failure

Failure returns immutable diagnostic failure data or nil.

func (Result) Principal

func (r Result) Principal() (identity.Principal, bool)

Principal returns the trusted authenticated principal and presence.

Jump to

Keyboard shortcuts

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