saml

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2025 License: MIT Imports: 32 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

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDecodePEMBlock      = errors.New("failed to decode PEM block containing certificate")
	ErrPrivateKeyNotRSAKey = errors.New("private key is not an RSA key")
)
View Source
var (
	// ErrNoSigningCertificate indicates no signing certificate was found in metadata.
	ErrNoSigningCertificate = errors.New("no signing certificate found in metadata")
	// ErrInvalidCertificate indicates the certificate data is malformed.
	ErrInvalidCertificate = errors.New("invalid certificate data")
	// ErrSPNotInAllowedList indicates the SP is not in the allowed list.
	ErrSPNotInAllowedList = errors.New("SP not found in allowed list")
	// ErrNoMetadataURL indicates no metadata URL configured for SP.
	ErrNoMetadataURL = errors.New("no metadata URL configured for SP")
)
View Source
var (
	ErrPrivateKeyIsNil        = errors.New("private key is nil")
	ErrPrivateKeyNotRSA       = errors.New("private key is not an RSA key")
	ErrEntityNotFound         = errors.New("entity not found")
	ErrAuthRequestNotFound    = errors.New("auth request not found")
	ErrInvalidAuthRequestType = errors.New("invalid auth request type")
	ErrLogoutContextNotFound  = errors.New("logout context not found")
	ErrNoSPSSODescriptor      = errors.New("no SPSSODescriptor found in metadata")
	ErrNoSingleLogoutService  = errors.New("no SingleLogoutService found in metadata")
)

Functions

func FetchMetadataWithRetry

func FetchMetadataWithRetry(ctx context.Context, client *http.Client, metadataURL url.URL, cfg config.Config) (*saml.EntityDescriptor, error)

FetchMetadataWithRetry attempts to fetch SAML metadata with exponential backoff retry logic.

func GenerateTestCertificate

func GenerateTestCertificate(t *testing.T) (string, string)

GenerateTestCertificate generates a self-signed certificate and private key for testing.

func GetSPSigningCertificateFromMetadata

func GetSPSigningCertificateFromMetadata(ctx context.Context, cfg config.Config, spEntityID string, cache *SPCertificateCache) (*x509.Certificate, error)

GetSPSigningCertificateFromMetadata retrieves the signing certificate from SP metadata.

func LoadCertificate

func LoadCertificate(certPath, keyPath string) (tls.Certificate, error)

LoadCertificate loads and parses the SP certificate and private key.

func ReadMetadataFromURLWithRetry

func ReadMetadataFromURLWithRetry(ctx context.Context, client *http.Client, metadataURL string, cfg config.Config) ([]byte, error)

ReadMetadataFromURLWithRetry attempts to read SAML metadata from URL with exponential backoff retry logic.

Types

type AuthRequest

type AuthRequest struct {
	ID                       string
	ApplicationID            string
	RelayState               string
	AccessConsumerServiceURL string
	BindingType              string
	AuthRequestID            string
	Issuer                   string
	Destination              string
	UserID                   string
	IsDone                   bool
	CompletedAt              time.Time
	// Assertion data from the actual IdP
	Assertion *saml.Assertion
}

AuthRequest implements the models.AuthRequestInt interface.

func (*AuthRequest) Done

func (a *AuthRequest) Done() bool

func (*AuthRequest) GetAccessConsumerServiceURL

func (a *AuthRequest) GetAccessConsumerServiceURL() string

func (*AuthRequest) GetApplicationID

func (a *AuthRequest) GetApplicationID() string

func (*AuthRequest) GetAuthRequestID

func (a *AuthRequest) GetAuthRequestID() string

func (*AuthRequest) GetBindingType

func (a *AuthRequest) GetBindingType() string

func (*AuthRequest) GetDestination

func (a *AuthRequest) GetDestination() string

func (*AuthRequest) GetID

func (a *AuthRequest) GetID() string

func (*AuthRequest) GetIssuer

func (a *AuthRequest) GetIssuer() string

func (*AuthRequest) GetRelayState

func (a *AuthRequest) GetRelayState() string

func (*AuthRequest) GetUserID

func (a *AuthRequest) GetUserID() string

type IDP

type IDP struct {
	EntityID   string
	IDP        *provider.Provider
	IDPStorage *Storage
}

IDP represents the SAML Identity Provider configuration.

func CreateProxyIDP

func CreateProxyIDP(cfg config.Config) (*IDP, error)

CreateProxyIDP creates a SAML Identity Provider middleware from the configuration.

func (*IDP) GetProvider

func (i *IDP) GetProvider() *provider.Provider

GetProvider returns the zitadel/saml provider.

func (*IDP) GetStorage

func (i *IDP) GetStorage() *Storage

GetStorage returns the IDP's storage for testing purposes.

type LogoutContext

type LogoutContext struct {
	ID                string          // Unique logout session ID
	OriginType        string          // "sp" or "idp" - who initiated the logout
	OriginID          string          // Entity ID of logout initiator
	TargetID          string          // Entity ID of logout target
	RelayState        string          // Original relay state to be preserved
	CreatedAt         time.Time       // For cleanup of expired contexts
	LogoutRequestID   string          // Original logout request ID for tracking
	ProcessedRequests map[string]bool // Track processed request IDs to prevent replay
}

LogoutContext stores temporary state for a logout flow. This follows the same stateless philosophy as AuthRequest, storing minimal information only during the logout process.

type SPCertificateCache

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

SPCertificateCache caches SP signing certificates to avoid repeated metadata fetches.

func NewSPCertificateCache

func NewSPCertificateCache() *SPCertificateCache

NewSPCertificateCache creates a new certificate cache.

func (*SPCertificateCache) Get

func (c *SPCertificateCache) Get(entityID string) (*x509.Certificate, bool)

Get retrieves a certificate from the cache.

func (*SPCertificateCache) Set

func (c *SPCertificateCache) Set(entityID string, cert *x509.Certificate)

Set stores a certificate in the cache.

type ServiceProvider

type ServiceProvider struct {
	ID         string
	Middleware *samlsp.Middleware
}

ServiceProvider represents a SAML Service Provider for a specific IDP.

type ServiceProviders

type ServiceProviders struct {
	Providers map[string]*ServiceProvider
	Default   *ServiceProvider
}

ServiceProviders manages multiple SAML Service Providers.

func CreateServiceProviders

func CreateServiceProviders(ctx context.Context, cfg config.Config) (*ServiceProviders, error)

CreateServiceProviders creates Service Providers for all configured IDP.

func (*ServiceProviders) GetProvider

func (s *ServiceProviders) GetProvider(id string) *ServiceProvider

GetProvider returns the IDP Service Provider for the given ID or the default if not found.

type SingleLogoutService

type SingleLogoutService struct {
	Binding          string
	Location         string
	ResponseLocation string
}

SingleLogoutService represents a parsed SingleLogoutService endpoint from SP metadata.

type Storage

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

Storage implements the zitadel/saml Storage interfaces.

func NewStorage

func NewStorage(cfg config.Config) (*Storage, error)

NewStorage creates a new Storage.

func (*Storage) AddAuthRequestForTesting

func (s *Storage) AddAuthRequestForTesting(authRequest *AuthRequest)

AddAuthRequestForTesting adds an auth request to storage for testing purposes.

func (*Storage) AuthRequestByID

func (s *Storage) AuthRequestByID(_ context.Context, id string) (models.AuthRequestInt, error)

func (*Storage) CheckAndMarkLogoutRequestProcessed

func (s *Storage) CheckAndMarkLogoutRequestProcessed(contextID, requestID string) (bool, error)

CheckAndMarkLogoutRequestProcessed checks if a logout request ID has been processed and marks it as processed if not. Returns true if this is a replay.

func (*Storage) CleanupCompletedAuthRequests

func (s *Storage) CleanupCompletedAuthRequests(maxAge time.Duration) int

CleanupCompletedAuthRequests removes completed auth requests older than the specified duration.

func (*Storage) CreateAuthRequest

func (s *Storage) CreateAuthRequest(_ context.Context, authnRequest *samlp.AuthnRequestType, appID, bindingType, relayState, userID string) (models.AuthRequestInt, error)

func (*Storage) CreateLogoutContext

func (s *Storage) CreateLogoutContext(originType, originID, targetID, relayState string) *LogoutContext

CreateLogoutContext creates a new logout context for tracking logout flow state.

func (*Storage) DeleteLogoutContext

func (s *Storage) DeleteLogoutContext(id string)

DeleteLogoutContext removes a logout context from storage.

func (*Storage) GetAllowedSPs

func (s *Storage) GetAllowedSPs() []config.SPConfig

GetAllowedSPs returns the list of allowed service providers from configuration.

func (*Storage) GetCA

func (*Storage) GetConfig

func (s *Storage) GetConfig() config.Config

GetConfig returns the configuration.

func (*Storage) GetEntityByID

func (s *Storage) GetEntityByID(ctx context.Context, entityID string) (*serviceprovider.ServiceProvider, error)

func (*Storage) GetEntityIDByAppID

func (s *Storage) GetEntityIDByAppID(_ context.Context, appID string) (string, error)

func (*Storage) GetLogoutContext

func (s *Storage) GetLogoutContext(id string) (*LogoutContext, error)

GetLogoutContext retrieves a logout context by ID.

func (*Storage) GetMetadataSigningKey

func (s *Storage) GetMetadataSigningKey(_ context.Context) (*key.CertificateAndKey, error)

func (*Storage) GetResponseSigningKey

func (s *Storage) GetResponseSigningKey(_ context.Context) (*key.CertificateAndKey, error)

func (*Storage) GetSPCertificateCache

func (s *Storage) GetSPCertificateCache() *SPCertificateCache

GetSPCertificateCache returns the SP certificate cache.

func (*Storage) GetSingleLogoutServiceFromSP

func (s *Storage) GetSingleLogoutServiceFromSP(ctx context.Context, entityID string) (*SingleLogoutService, error)

GetSingleLogoutServiceFromSP extracts SingleLogoutService endpoint from SP metadata.

func (*Storage) Health

func (s *Storage) Health(_ context.Context) error

func (*Storage) SetEntityIDMapping

func (s *Storage) SetEntityIDMapping(appID, entityID string)

SetEntityIDMapping sets the entity ID mapping for an app ID.

func (*Storage) SetUserinfoWithLoginName

func (s *Storage) SetUserinfoWithLoginName(
	_ context.Context,
	userinfo models.AttributeSetter,
	loginName string,
	_ []int,
) error

func (*Storage) SetUserinfoWithUserID

func (s *Storage) SetUserinfoWithUserID(
	ctx context.Context,
	authRequestID string,
	userinfo models.AttributeSetter,
	userID string,
	_ []int,
) error

Jump to

Keyboard shortcuts

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