Documentation
¶
Index ¶
- Variables
- func FetchMetadata(ctx context.Context, rawURL string, opts FetchOptions) ([]byte, error)
- type Config
- type FetchOptions
- type IdPConfig
- type MemoryReplayCache
- type ReplayCache
- type ServiceProvider
- func (p *ServiceProvider) AuthnRequest(relayState string) (redirectURL string, requestID string, err error)
- func (p *ServiceProvider) EntityID() string
- func (p *ServiceProvider) IDPEntityID() string
- func (p *ServiceProvider) Metadata() ([]byte, error)
- func (p *ServiceProvider) ParseResponse(r *http.Request, possibleRequestIDs []string) (*protocol.Identity, error)
- func (p *ServiceProvider) ParseResponseXML(raw []byte, possibleRequestIDs []string, currentURL *url.URL) (*protocol.Identity, error)
- func (p *ServiceProvider) SSOURL() string
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMissingEntityID means Config.EntityID was empty. ErrMissingEntityID = errString("kexcore-saml: EntityID is required") // ErrMissingACSURL means Config.ACSURL was empty. ErrMissingACSURL = errString("kexcore-saml: ACSURL is required") // ErrInvalidACSURL means ACSURL, SLOURL or MetadataURL was not an // absolute http(s) URL. ErrInvalidACSURL = errString("kexcore-saml: URL must be absolute and use http or https") // ErrMissingIdP means neither metadata nor SSOURL and certificates were // supplied. ErrMissingIdP = errString("kexcore-saml: IdP metadata or SSOURL with certificates is required") // ErrNoSSOEndpoint means the identity provider publishes no usable // SingleSignOnService. ErrNoSSOEndpoint = errString("kexcore-saml: identity provider publishes no SingleSignOnService") // ErrNoSigningCertificate means no IdP signing certificate could be // resolved, so no assertion can be trusted. ErrNoSigningCertificate = errString("kexcore-saml: identity provider publishes no signing certificate") // ErrReplay is returned when an assertion ID has already been consumed. // It is the engine's replay sentinel. ErrReplay = saml.ErrAssertionReplayed )
Sentinel errors returned by the service provider engine.
Functions ¶
func FetchMetadata ¶
FetchMetadata downloads and round-trip validates an identity provider metadata document.
Fetching is deliberately separate from New: the host decides when metadata is refreshed, how it is cached and what trust anchors it accepts. New only consumes bytes.
Types ¶
type Config ¶
type Config struct {
// EntityID is the SP's SAML entity identifier, for example
// https://id.example.com/saml/sp/acme/okta.
EntityID string
// ACSURL is the absolute URL of the Assertion Consumer Service.
ACSURL string
// SLOURL is the optional absolute URL of the Single Logout Service.
SLOURL string
// MetadataURL is the optional public location of the SP metadata.
MetadataURL string
// IdP describes the identity provider this SP trusts.
IdP IdPConfig
// AllowIDPInitiated accepts unsolicited responses, which carry no
// InResponseTo. Off by default: an IdP-initiated response cannot be tied
// to a login the SP started.
AllowIDPInitiated bool
// AllowSHA1 permits SHA-1 signatures from the identity provider. Off by
// default; enable it only for a legacy IdP that cannot do better.
AllowSHA1 bool
// NameIDFormat is requested in AuthnRequests. Empty leaves the
// NameIDPolicy unspecified, which most IdPs accept.
NameIDFormat protocol.NameIDFormat
// Key and Certificate sign AuthnRequests and are published in the SP
// metadata. Optional: many IdPs do not require signed requests.
Key crypto.Signer
Certificate *x509.Certificate
// EncryptionKey and EncryptionCertificate decrypt encrypted assertions.
// Optional. EncryptionKey must be an RSA key.
EncryptionKey *rsa.PrivateKey
EncryptionCertificate *x509.Certificate
// DecryptionKeys are tried after EncryptionKey (or Key) while rotating
// the SP encryption key.
DecryptionKeys []*rsa.PrivateKey
// ReplayCache records consumed assertion IDs. It must be shared by every
// instance that accepts assertions for this SP. When nil, an in-memory
// cache is used, which is only safe for a single instance.
ReplayCache ReplayCache
// MetadataValidDuration sets validUntil on the generated SP metadata.
// Zero uses the engine default.
MetadataValidDuration time.Duration
// Clock overrides the time source, mainly for tests.
Clock *dsig.Clock
}
Config describes one SAML Service Provider.
One Config corresponds to one (tenant, identity provider) pair: EntityID and ACSURL are unique per pair, which is what keeps a response issued for one tenant from being replayed into another.
type FetchOptions ¶
type FetchOptions struct {
// HTTPClient is used for the request. A client with a 10 second timeout
// is used when nil.
HTTPClient *http.Client
// MaxBytes caps the document size. Defaults to 1 MiB.
MaxBytes int64
// AllowInsecureHTTP permits plain http URLs. Off by default so that
// metadata is fetched over TLS.
AllowInsecureHTTP bool
}
FetchOptions configures FetchMetadata.
type IdPConfig ¶
type IdPConfig struct {
// EntityID selects the identity provider inside an EntitiesDescriptor
// and, when no metadata is supplied, names the IdP explicitly. Optional
// when MetadataXML contains exactly one IdP.
EntityID string
// MetadataXML is the raw IdP metadata document. EntityDescriptor and
// EntitiesDescriptor are both accepted. It is validated with
// pkg/xmlsig before parsing.
MetadataXML []byte
// SSOURL overrides the SingleSignOnService location from the metadata.
// Required when MetadataXML is empty.
SSOURL string
// SLOURL overrides the SingleLogoutService location from the metadata.
SLOURL string
// Certificates are PEM or base64 DER encoded IdP signing certificates.
// They are added to the certificates found in MetadataXML and are
// required when MetadataXML is empty.
Certificates []string
}
IdPConfig is the identity provider side of a Config.
type MemoryReplayCache ¶
type MemoryReplayCache struct {
// contains filtered or unexported fields
}
MemoryReplayCache is a process-local ReplayCache.
It is only correct for a single instance. A deployment with more than one server must supply a shared implementation (Redis, SQL) through Config.ReplayCache; otherwise a captured assertion can be replayed against another instance.
func NewMemoryReplayCache ¶
func NewMemoryReplayCache() *MemoryReplayCache
NewMemoryReplayCache returns an empty in-memory cache.
type ReplayCache ¶
type ReplayCache interface {
// Consume records assertionID until expiresAt. It returns ErrReplay when
// the ID was already recorded.
Consume(assertionID string, expiresAt time.Time) error
}
ReplayCache is the same contract as saml.AssertionReplayCache. Every implementation must be safe for concurrent use and, in a multi-instance deployment, shared (Redis, SQL) across instances.
type ServiceProvider ¶
type ServiceProvider struct {
// contains filtered or unexported fields
}
ServiceProvider is the SAML SP engine for one (tenant, identity provider) pair.
It is safe for concurrent use. The host owns routing, tenancy and user storage; the engine owns the SAML wire format and its validation.
func New ¶
func New(cfg Config) (*ServiceProvider, error)
New validates the configuration and builds an SP engine.
func (*ServiceProvider) AuthnRequest ¶
func (p *ServiceProvider) AuthnRequest(relayState string) (redirectURL string, requestID string, err error)
AuthnRequest starts a SP-initiated login.
It returns the redirect URL to send the browser to and the request ID. The ID must be persisted by the host (together with the tenant, provider and return URL) and passed back to ParseResponse as a possible request ID, so that an unsolicited response cannot be accepted in its place.
func (*ServiceProvider) EntityID ¶
func (p *ServiceProvider) EntityID() string
EntityID returns the SP entity identifier.
func (*ServiceProvider) IDPEntityID ¶
func (p *ServiceProvider) IDPEntityID() string
IDPEntityID returns the identity provider's entity identifier.
func (*ServiceProvider) Metadata ¶
func (p *ServiceProvider) Metadata() ([]byte, error)
Metadata returns the SP metadata document, ready to be served at the configured MetadataURL and handed to the identity provider.
func (*ServiceProvider) ParseResponse ¶
func (p *ServiceProvider) ParseResponse(r *http.Request, possibleRequestIDs []string) (*protocol.Identity, error)
ParseResponse validates a POSTed SAML response and returns the identity it asserts.
possibleRequestIDs are the IDs of AuthnRequests this SP is still waiting for. Pass nil only when AllowIDPInitiated is enabled.
func (*ServiceProvider) ParseResponseXML ¶
func (p *ServiceProvider) ParseResponseXML(raw []byte, possibleRequestIDs []string, currentURL *url.URL) (*protocol.Identity, error)
ParseResponseXML validates a decoded SAML response without an HTTP request. It exists for tests, replay tooling and hosts that terminate the POST elsewhere. currentURL defaults to the configured ACS URL.
func (*ServiceProvider) SSOURL ¶
func (p *ServiceProvider) SSOURL() string
SSOURL returns the identity provider's SingleSignOnService location the engine will send AuthnRequests to.