Documentation
¶
Overview ¶
Package samlidp a rudimentary SAML identity provider suitable for testing or as a starting point for a more complex service.
The administrative endpoints (/services, /users, /sessions and /shortcuts) are not authenticated: anyone who can reach the server can read and change its users, service providers, sessions and shortcuts. Set Options.AdminMiddleware to protect them, or do not expose the server to untrusted clients.
Index ¶
- Variables
- type MemoryStore
- type Options
- type Server
- func (s *Server) AddSessionParticipant(_ *http.Request, session *saml.Session, participant saml.SessionParticipant) error
- func (s *Server) GetServiceProvider(_ *http.Request, serviceProviderID string) (*saml.EntityDescriptor, error)
- func (s *Server) GetSession(w http.ResponseWriter, r *http.Request, req *saml.IdpAuthnRequest) *saml.Session
- func (s *Server) HandleDeleteService(w http.ResponseWriter, r *http.Request)
- func (s *Server) HandleDeleteSession(w http.ResponseWriter, r *http.Request)
- func (s *Server) HandleDeleteShortcut(w http.ResponseWriter, r *http.Request)
- func (s *Server) HandleDeleteUser(w http.ResponseWriter, r *http.Request)
- func (s *Server) HandleGetService(w http.ResponseWriter, r *http.Request)
- func (s *Server) HandleGetSession(w http.ResponseWriter, r *http.Request)
- func (s *Server) HandleGetShortcut(w http.ResponseWriter, r *http.Request)
- func (s *Server) HandleGetUser(w http.ResponseWriter, r *http.Request)
- func (s *Server) HandleIDPInitiated(w http.ResponseWriter, r *http.Request)
- func (s *Server) HandleListServices(w http.ResponseWriter, _ *http.Request)
- func (s *Server) HandleListSessions(w http.ResponseWriter, _ *http.Request)
- func (s *Server) HandleListShortcuts(w http.ResponseWriter, _ *http.Request)
- func (s *Server) HandleListUsers(w http.ResponseWriter, _ *http.Request)
- func (s *Server) HandleLogin(w http.ResponseWriter, r *http.Request)
- func (s *Server) HandleLogout(w http.ResponseWriter, r *http.Request)
- func (s *Server) HandlePutService(w http.ResponseWriter, r *http.Request)
- func (s *Server) HandlePutShortcut(w http.ResponseWriter, r *http.Request)
- func (s *Server) HandlePutUser(w http.ResponseWriter, r *http.Request)
- func (s *Server) InitializeHTTP()
- func (s *Server) InvalidateSession(w http.ResponseWriter, r *http.Request, req *saml.IdpLogoutRequest) error
- func (s *Server) SessionParticipants(_ *http.Request, match func(*saml.Session) bool) ([]saml.SessionParticipant, error)
- type Service
- type Shortcut
- type Store
- type User
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("not found")
ErrNotFound is returned from Store.Get() when a stored item is not present
var MaxServiceMetadataSize int64 = 10 << 20
MaxServiceMetadataSize is the size in bytes of the largest service provider metadata document the service endpoints read. A larger request body is refused rather than read into memory in full.
Functions ¶
This section is empty.
Types ¶
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore is an implementation of Store that resides completely in memory.
func (*MemoryStore) Get ¶
func (s *MemoryStore) Get(key string, value interface{}) error
Get fetches the data stored in `key` and unmarshals it into `value`.
func (*MemoryStore) List ¶
func (s *MemoryStore) List(prefix string) ([]string, error)
List returns all the keys that start with `prefix`. The prefix is stripped from each returned value. So if keys are ["aa", "ab", "cd"] then List("a") would produce []string{"a", "b"}
func (*MemoryStore) Put ¶
func (s *MemoryStore) Put(key string, value interface{}) error
Put marshals `value` and stores it in `key`.
type Options ¶
type Options struct {
URL url.URL
Key crypto.PrivateKey
Signer crypto.Signer
Logger logger.Interface
Certificate *x509.Certificate
Store Store
LoginFormTemplate *template.Template
// AdminMiddleware, if set, wraps the handlers of the administrative
// endpoints (/services, /users, /sessions and /shortcuts), for example to
// require authentication. It is not applied to /metadata, /sso and
// /login, /slo and /logout, which must stay reachable by browsers and service
// providers.
// If nil, the administrative endpoints are unauthenticated.
AdminMiddleware func(http.Handler) http.Handler
// ExtraCertificates are additional signing certificates published in
// the metadata, for example to announce a new certificate before a key
// rollover. They are never used to sign anything. See
// saml.IdentityProvider.ExtraCertificates.
ExtraCertificates []*x509.Certificate
// MetadataScopes are published in the metadata as Shibboleth
// shibmd:Scope extension elements. See
// saml.IdentityProvider.MetadataScopes.
MetadataScopes []saml.ShibbolethScope
// SOAPLogout, if true, has /slo also accept LogoutRequests that service
// providers send server to server with the SOAP binding, and advertises
// a SOAP SingleLogoutService for it in the metadata (see
// saml.IdentityProvider.LogoutBindings). The sessions are then ended by
// NameID and SessionIndex, without the browser (see
// Server.InvalidateSession), and the logout is propagated only to the
// service providers that also offer a SOAP SingleLogoutService.
//
// It is off by default: a service provider that finds a SOAP endpoint in
// the metadata may start using it instead of the browser, and a logout
// that starts without a browser cannot reach the service providers that
// only offer front-channel logout, which a browser-based logout reaches.
// It also needs /slo to be reachable from the service providers'
// servers.
SOAPLogout bool
// SPMetadataCertificates, if set, are the certificates that service
// provider metadata registered through PUT or POST /services/:id must
// be signed with (saml-metadata-2.0-os §3, §4.3.3.2): the service
// providers' own metadata signing certificates, or their federation's,
// obtained from a source you trust. The root element of the metadata
// must then carry a valid enveloped signature made with one of them,
// and only the content the signature covers is stored; unsigned
// metadata, or metadata whose signature does not verify, is refused
// with 400 Bad Request. See saml.VerifyMetadataSignature.
//
// If empty (the default), service provider metadata is accepted
// without checking any signature it carries.
SPMetadataCertificates []*x509.Certificate
}
Options represent the parameters to New() for creating a new IDP server
type Server ¶
type Server struct {
http.Handler
IDP saml.IdentityProvider // the underlying IDP
Store Store // the data store
LoginFormTemplate *template.Template
// AdminMiddleware, if set, wraps the administrative endpoints. See
// Options.AdminMiddleware. Call InitializeHTTP after changing it.
AdminMiddleware func(http.Handler) http.Handler
// SPMetadataCertificates, if set, are the certificates that service
// provider metadata registered through /services/:id must be signed
// with. See Options.SPMetadataCertificates.
SPMetadataCertificates []*x509.Certificate
// contains filtered or unexported fields
}
Server represents an IDP server. The server provides the following URLs:
/metadata - the SAML metadata
/sso - the SAML endpoint to initiate an authentication flow
/slo - the SAML single logout endpoint (SP-initiated logout,
also over SOAP if Options.SOAPLogout is set, and the
service providers' answers to IdP-initiated logout)
/logout - log the browser's session out of the IdP and of every
service provider it was used for (IdP-initiated logout)
/login - prompt for a username and password if no session established
/login/:shortcut - kick off an IDP-initiated authentication flow
/services - RESTful interface to Service objects
/users - RESTful interface to User objects
/sessions - RESTful interface to Session objects
/shortcuts - RESTful interface to Shortcut objects
The RESTful interfaces are administrative endpoints. They are only protected if AdminMiddleware is set.
func (*Server) AddSessionParticipant ¶ added in v0.6.2
func (s *Server) AddSessionParticipant(_ *http.Request, session *saml.Session, participant saml.SessionParticipant) error
AddSessionParticipant implements saml.SessionParticipantTracker. It records participant under the session's ID, unless it is already recorded.
func (*Server) GetServiceProvider ¶
func (s *Server) GetServiceProvider(_ *http.Request, serviceProviderID string) (*saml.EntityDescriptor, error)
GetServiceProvider returns the Service Provider metadata for the service provider ID, which is typically the service provider's metadata URL. If an appropriate service provider cannot be found then the returned error must be os.ErrNotExist.
func (*Server) GetSession ¶
func (s *Server) GetSession(w http.ResponseWriter, r *http.Request, req *saml.IdpAuthnRequest) *saml.Session
GetSession returns the *Session for this request.
If the remote user has specified a username and password in the request then it is validated against the user database. If valid it sets a cookie and returns the newly created session object.
If the remote user has specified invalid credentials then a login form is returned with an English-language toast telling the user their password was invalid.
If a session cookie already exists and represents a valid session, then the session is returned, unless the AuthnRequest has ForceAuthn="true": the user must then authenticate again, so the login form is sent and the assertion that follows carries a new AuthnInstant (saml-core §3.4.1).
If neither credentials nor a usable session exist, this function sends a login form and returns nil. If the AuthnRequest has IsPassive="true", it sends no login form: it answers with a SAML error Response with the status Responder and the second-level status NoPassive instead (saml-core §3.4.1 and §3.2.2.2). A passive request with a usable session gets that session as usual.
func (*Server) HandleDeleteService ¶
func (s *Server) HandleDeleteService(w http.ResponseWriter, r *http.Request)
HandleDeleteService handles the `DELETE /services/:id` request.
func (*Server) HandleDeleteSession ¶
func (s *Server) HandleDeleteSession(w http.ResponseWriter, r *http.Request)
HandleDeleteSession handles the `DELETE /sessions/:id` request. It invalidates the specified session.
func (*Server) HandleDeleteShortcut ¶
func (s *Server) HandleDeleteShortcut(w http.ResponseWriter, r *http.Request)
HandleDeleteShortcut handles the `DELETE /shortcuts/:id` request.
func (*Server) HandleDeleteUser ¶
func (s *Server) HandleDeleteUser(w http.ResponseWriter, r *http.Request)
HandleDeleteUser handles the `DELETE /users/:id` request.
func (*Server) HandleGetService ¶
func (s *Server) HandleGetService(w http.ResponseWriter, r *http.Request)
HandleGetService handles the `GET /services/:id` request and responds with the service metadata in XML format.
func (*Server) HandleGetSession ¶
func (s *Server) HandleGetSession(w http.ResponseWriter, r *http.Request)
HandleGetSession handles the `GET /sessions/:id` request and responds with the session object in JSON format.
func (*Server) HandleGetShortcut ¶
func (s *Server) HandleGetShortcut(w http.ResponseWriter, r *http.Request)
HandleGetShortcut handles the `GET /shortcuts/:id` request and responds with the shortcut object in JSON format.
func (*Server) HandleGetUser ¶
func (s *Server) HandleGetUser(w http.ResponseWriter, r *http.Request)
HandleGetUser handles the `GET /users/:id` request and responds with the user object in JSON format. The HashedPassword field is excluded.
func (*Server) HandleIDPInitiated ¶
func (s *Server) HandleIDPInitiated(w http.ResponseWriter, r *http.Request)
HandleIDPInitiated handles a request for an IDP initiated login flow. It looks up the specified shortcut, generates the appropriate SAML assertion and redirects the user via the HTTP-POST binding to the service providers ACS URL.
func (*Server) HandleListServices ¶
func (s *Server) HandleListServices(w http.ResponseWriter, _ *http.Request)
HandleListServices handles the `GET /services/` request and responds with a JSON formatted list of service names.
func (*Server) HandleListSessions ¶
func (s *Server) HandleListSessions(w http.ResponseWriter, _ *http.Request)
HandleListSessions handles the `GET /sessions/` request and responds with a JSON formatted list of session names.
func (*Server) HandleListShortcuts ¶
func (s *Server) HandleListShortcuts(w http.ResponseWriter, _ *http.Request)
HandleListShortcuts handles the `GET /shortcuts/` request and responds with a JSON formatted list of shortcut names.
func (*Server) HandleListUsers ¶
func (s *Server) HandleListUsers(w http.ResponseWriter, _ *http.Request)
HandleListUsers handles the `GET /users/` request and responds with a JSON formatted list of user names.
func (*Server) HandleLogin ¶
func (s *Server) HandleLogin(w http.ResponseWriter, r *http.Request)
HandleLogin handles the `POST /login` and `GET /login` forms. If credentials are present in the request body, then they are validated. For valid credentials, the response is a 200 OK and the JSON session object. For invalid credentials, the HTML login prompt form is sent.
func (*Server) HandleLogout ¶ added in v0.6.2
func (s *Server) HandleLogout(w http.ResponseWriter, r *http.Request)
HandleLogout handles `GET /logout` and `POST /logout`: IdP-initiated single logout of the browser's session. The session cookie is expired, the session is logged out of every service provider it was used for (see saml.IdentityProvider.StartLogout), and then the session is deleted. When the logout is done, a page tells the user they are logged out.
func (*Server) HandlePutService ¶
func (s *Server) HandlePutService(w http.ResponseWriter, r *http.Request)
HandlePutService handles the `PUT /shortcuts/:id` request. It accepts the XML-formatted service metadata in the request body and stores it.
If SPMetadataCertificates is set, the metadata must be signed with one of them, and only the content the signature covers is stored; unsigned metadata, or metadata whose signature does not verify, is refused with 400 Bad Request.
func (*Server) HandlePutShortcut ¶
func (s *Server) HandlePutShortcut(w http.ResponseWriter, r *http.Request)
HandlePutShortcut handles the `PUT /shortcuts/:id` request. It accepts a JSON formatted shortcut object in the request body and stores it.
func (*Server) HandlePutUser ¶
func (s *Server) HandlePutUser(w http.ResponseWriter, r *http.Request)
HandlePutUser handles the `PUT /users/:id` request. It accepts a JSON formatted user object in the request body and stores it. If the PlaintextPassword field is present then it is hashed and stored in HashedPassword. If the PlaintextPassword field is not present then HashedPassword retains it's stored value.
func (*Server) InitializeHTTP ¶
func (s *Server) InitializeHTTP()
InitializeHTTP sets up the HTTP handler for the server. (This function is called automatically for you by New, but you may need to call it yourself if you don't create the object using New.)
func (*Server) InvalidateSession ¶ added in v0.6.1
func (s *Server) InvalidateSession(w http.ResponseWriter, r *http.Request, req *saml.IdpLogoutRequest) error
InvalidateSession implements saml.SessionInvalidator. It is called by the IdP's ServeSLO for a valid LogoutRequest from a service provider and deletes the sessions the request refers to (see saml.IdpLogoutRequest.MatchesSession):
- the browser's session, named by its session cookie, if it matches the request. The session cookie is then expired.
- if the request carries any SessionIndex, any stored session with one of those indexes and the request's NameID. This ends the session when the browser did not send the session cookie, for example because the LogoutRequest arrived in a cross-site POST. The SessionIndex is a random value the IdP only discloses in the assertions it issues, so a service provider can only end sessions it took part in this way.
Without a SessionIndex, only the browser's own session is ended.
A LogoutRequest received with the SOAP binding (if Options.SOAPLogout is set) comes from the service provider's server, so there is no session cookie: every stored session the request matches is ended. Without a SessionIndex, that is every session of the request's NameID, as saml-core-2.0-os §3.7.3.2 asks, and the same sessions whose other participants ServeSLO logs out. Any service provider registered with the server can thus end all the sessions of a NameID it knows, with a request signed with its key; include a SessionIndex to end only that session.
Sessions are stored by NameID, so a request that identifies the principal with a BaseID (accepted only if the IdP's AcceptLogoutRequestBaseID is set) ends no session and returns an error matching saml.ErrUnsupported, and ServeSLO answers it with StatusResponder.
func (*Server) SessionParticipants ¶ added in v0.6.2
func (s *Server) SessionParticipants(_ *http.Request, match func(*saml.Session) bool) ([]saml.SessionParticipant, error)
SessionParticipants implements saml.SessionParticipantTracker. It returns the participants recorded for every stored session that match selects.
type Service ¶
type Service struct {
// Name is the name of the service provider
Name string
// Metdata is the XML metadata of the service provider.
Metadata saml.EntityDescriptor
}
Service represents a configured SP for whom this IDP provides authentication services.
type Shortcut ¶
type Shortcut struct {
// The name of the shortcut.
Name string `json:"name"`
// The entity ID of the service provider to use for this shortcut, i.e.
// https://someapp.example.com/saml/metadata.
ServiceProviderID string `json:"service_provider"`
// If specified then the relay state is the fixed string provided
RelayState *string `json:"relay_state,omitempty"`
// If true then the URL suffix is used as the relayState. So for example, a user
// requesting https://idp.example.com/login/myservice/foo will get redirected
// to the myservice endpoint with a RelayState of "foo".
URISuffixAsRelayState bool `json:"url_suffix_as_relay_state,omitempty"`
}
Shortcut represents an IDP-initiated SAML flow. When a user navigates to /login/:shortcut it initiates the login flow to the specified service provider with the specified RelayState.
type Store ¶
type Store interface {
// Get fetches the data stored in `key` and unmarshals it into `value`.
Get(key string, value interface{}) error
// Put marshals `value` and stores it in `key`.
Put(key string, value interface{}) error
// Delete removes `key`
Delete(key string) error
// List returns all the keys that start with `prefix`. The prefix is
// stripped from each returned value. So if keys are ["aa", "ab", "cd"]
// then List("a") would produce []string{"a", "b"}
List(prefix string) ([]string, error)
}
Store is an interface that describes an abstract key-value store.
type User ¶
type User struct {
Name string `json:"name"`
PlaintextPassword *string `json:"password,omitempty"` // not stored
HashedPassword []byte `json:"hashed_password,omitempty"`
Groups []string `json:"groups,omitempty"`
Email string `json:"email,omitempty"`
CommonName string `json:"common_name,omitempty"`
Surname string `json:"surname,omitempty"`
GivenName string `json:"given_name,omitempty"`
ScopedAffiliation string `json:"scoped_affiliation,omitempty"`
}
User represents a stored user. The data here are used to populate user once the user has authenticated.