saml

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 19 Imported by: ? modules, 0 packages

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

Overview

Package saml authenticates SAML 2.0 Web Browser SSO responses (HTTP-POST binding) for service-provider initiated logins, as an authentication.FederatedProvider.

Begin sends a signed (RSA-SHA256) AuthnRequest with the HTTP-Redirect binding to the single registered ACS URL and stores the request ID and an independent RelayState through a caller-owned RequestStore. Complete consumes that request atomically, then accepts only a response that: is at most Options.MaxResponseBytes, round-trips as XML, has bounded depth and element count, contains exactly one unencrypted assertion and unique IDs, is signed (response or assertion) by a certificate from the IdP metadata, and whose issuer, destination, InResponseTo, audience, bearer subject confirmation, conditions and issue instants all match within Options.AllowedClockSkew. IdP-initiated logins are rejected.

Time checks use this package's own validation with Options.Clock (default time.Now) rather than the process-global clock and skew variables of github.com/crewjam/saml.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Claims

type Claims struct {
	NameID          string
	Attributes      map[string][]string
	AuthenticatedAt time.Time
	ExpiresAt       time.Time
}

Claims is the bounded, verified SAML data presented to a ClaimsMapper.

type ClaimsMapper

type ClaimsMapper func(context.Context, Claims) (authentication.Principal, error)

ClaimsMapper maps verified SAML claims to a protocol-neutral principal.

type MemoryRequestStore

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

MemoryRequestStore is a process-local RequestStore for tests and single-process callers. Distributed deployments should provide a durable atomic implementation.

func NewMemoryRequestStore

func NewMemoryRequestStore() *MemoryRequestStore

NewMemoryRequestStore returns an empty memory-backed request store.

func (*MemoryRequestStore) Consume

func (s *MemoryRequestStore) Consume(ctx context.Context, id string) (Request, error)

Consume atomically removes and returns the request with id.

func (*MemoryRequestStore) Put

func (s *MemoryRequestStore) Put(ctx context.Context, request Request) error

Put stores a valid request unless its ID is already present.

type Options

type Options struct {
	// Name identifies the provider (lower-cased and trimmed). Required.
	Name string
	// EntityID, MetadataURL and ACSURL are the service provider's https
	// URLs. Required.
	EntityID    *url.URL
	MetadataURL *url.URL
	ACSURL      *url.URL
	// IDPMetadata describes the identity provider. It must have an https
	// entity ID, exactly one HTTP-Redirect SSO endpoint and at least one
	// currently valid signing certificate, and must not be past ValidUntil.
	IDPMetadata *crewjamsaml.EntityDescriptor
	// Key and Certificate are the service provider's signing key pair: RSA
	// of at least 2048 bits with a currently valid certificate. Required.
	Key         *rsa.PrivateKey
	Certificate *x509.Certificate
	// RequestStore persists pending requests. Required.
	RequestStore RequestStore
	// Clock returns the current time used to validate key, certificate and
	// metadata lifetimes, request expiry and assertion conditions; its result
	// is converted to UTC. Defaults to time.Now.
	Clock func() time.Time
	// MaxResponseBytes bounds the decoded response. Zero selects the default
	// of 1 MiB.
	MaxResponseBytes int
	// AllowedClockSkew is the tolerated clock difference with the identity
	// provider. Zero selects the default of two minutes; negative values are
	// rejected.
	AllowedClockSkew time.Duration
	// Mapper maps verified claims to a principal. The default uses ID
	// "<idp entity ID>|<NameID>", copies attributes into Traits and sets
	// Methods to ["saml"].
	Mapper ClaimsMapper
}

Options configures a Provider. The provider deep-copies keys, certificates and metadata, so later changes by the caller have no effect.

type Provider

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

Provider is a SAML 2.0 service provider for one identity provider. It is safe for concurrent use when its RequestStore and Mapper are.

func New

func New(options Options) (*Provider, error)

New validates options and returns a Provider. It returns authentication.ErrInvalidCredential for invalid options.

func (*Provider) Begin

Begin starts a login. request.RedirectURI must equal the ACS URL. The pending request expires after ten minutes.

func (*Provider) Complete

Complete consumes the pending request and verifies the SAML response in callback.SAMLResponse (base64). It returns authentication.ErrInvalidCallback for malformed or oversized callbacks, authentication.ErrReplay for unknown or reused requests, authentication.ErrExpired for expired requests and authentication.ErrUnauthenticated for every verification failure.

func (*Provider) Name

func (p *Provider) Name() string

Name returns the normalized provider name.

type Request

type Request struct {
	ID         string
	RelayState string
	ACSURL     string
	ExpiresAt  time.Time
}

Request is the callback state for one service-provider initiated request.

type RequestStore

type RequestStore interface {
	Put(context.Context, Request) error
	Consume(context.Context, string) (Request, error)
}

RequestStore persists callback state. Put must fail with authentication.ErrReplay when the ID exists. Consume must atomically remove and return the request and return authentication.ErrReplay when none exists, so that concurrent callbacks cannot both consume it.

Jump to

Keyboard shortcuts

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