gosaml2 Documentation
repository·main·Indexed 18 days ago
https://github.com/russellhaering/gosaml2A pure Go implementation of the SAML 2.0 protocol designed for Service Providers. It utilizes etree for XML processing and goxmldsig for XML digital signatures. The library supports generating and validating signed AuthnRequests and LogoutRequests, handling HTTP Redirect and POST bindings, and is verified to work with Identity Providers including Okta, Auth0, Shibboleth, Ipsilon, OneLogin, and Azure Active Directory.
What's inside gosaml2
How to add new fuzzers to gosaml2
mainTo extend the fuzzing coverage of
gosaml2, follow these steps:- Implement the new fuzzer in the
internal/fuzz/directory. - Update the
build.shscript to include the new fuzzer in the compilation step and ensure its seed corpus is created. - If the fuzzer requires specific arguments or configurations, create a corresponding
.optionsfile (e.g.,my_new_fuzzer.options).
- Implement the new fuzzer in the
Run gosaml2 fuzzers locally
mainYou can run the built-in fuzzing targets for
gosaml2using Go's native fuzzing capabilities. These targets are located in the./internal/fuzz/directory. Use the-fuzzflag to specify the target function and-fuzztimeto limit the duration of the fuzzing session.go test -fuzz=FuzzDecodeResponse ./internal/fuzz/ -fuzztime=30s go test -fuzz=FuzzLogoutResponse ./internal/fuzz/ -fuzztime=30s go test -fuzz=FuzzBuildRequest ./internal/fuzz/ -fuzztime=30sInstall gosaml2
mainInstall
gosaml2into your$GOPATHusing thego getcommand.go get github.com/russellhaering/gosaml2Test OSS-Fuzz integration locally with Docker
mainYou can test the OSS-Fuzz integration for
gosaml2on your local machine using Docker and the OSS-Fuzz infrastructure scripts. This requires cloning theoss-fuzzrepository and using itsinfra/helper.pyscript to manage the build and execution lifecycle.# Clone OSS-Fuzz git clone https://github.com/google/oss-fuzz cd oss-fuzz # Build the image python infra/helper.py build_image gosaml2 # Build the fuzzers python infra/helper.py build_fuzzers gosaml2 # Run the fuzzers python infra/helper.py run_fuzzer gosaml2 fuzz_decode_responseUnderstand AssertionInfo and WarningInfo
mainWhen processing SAML responses, the library provides structured data to inspect the assertion:
AssertionInfo: Contains theNameID,Values(attributes),SessionIndex,AuthnInstant, and the rawAssertions. It also includesResponseSignatureValidatedto confirm if the response was signed.WarningInfo: Provides details on potential security or protocol issues found during validation, such as:OneTimeUse: Whether the assertion was intended for single use.ProxyRestriction: Details on whether proxy restrictions were violated.NotInAudience: Whether the assertion was intended for a different audience.InvalidTime: Whether the assertion's time bounds were invalid.
Configure RequestedAuthnContext
mainThe
RequestedAuthnContextstruct allows the Service Provider to request specific authentication mechanisms from the Identity Provider.Comparison: The comparison policy (e.g., using constants likeAuthnPolicyMatchExact).Contexts: A slice of strings representingAuthnContextClassRefs. For example, to force password authentication, use[]string{AuthnContextPasswordProtectedTransport}.
Note: Leaving this unset allows the IdP to choose the authentication method, which is recommended for maximum compatibility.
sp.RequestedAuthnContext = &saml2.RequestedAuthnContext{ Comparison: "urn:oasis:names:tc:SAML:2.0:authnmethods-replay:exact", Contexts: []string{"urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport"}, }How SAML bindings (Redirect vs POST) are handled
mainThe library supports two primary SAML bindings:
HTTP Redirect Binding: The SAML message is compressed (DEFLATE), Base64 encoded, and appended as a query parameter (
SAMLRequest) to the IdP's SSO URL. Ifsp.SignAuthnRequestsis enabled, the library also signs the query string parameters (includingSigAlgandSignature) according to the SAML specification.- Use
BuildAuthURLRedirectorBuildLogoutURLRedirectto generate these URLs. - Use
AuthRedirectfor a direct HTTP redirect.
- Use
HTTP POST Binding: The SAML message is Base64 encoded and sent within an HTML
<form>via an HTTP POST request.- Use
BuildAuthBodyPostto generate the complete HTML form (including the auto-submitting JavaScript) required for this binding.
- Use
Configure the SAMLServiceProvider
mainThe
SAMLServiceProviderstruct is the central configuration object for the SAML2 client. It contains settings for Identity Provider (IdP) endpoints, Service Provider (SP) metadata, authentication request behavior, and security validation.Key configuration areas include:
- IdP Endpoints:
IdentityProviderSSOURL,IdentityProviderSSOBinding,IdentityProviderSLOURL,IdentityProviderSLOBinding, andIdentityProviderIssuer. - SP Metadata:
AssertionConsumerServiceURL,ServiceProviderSLOURL, andServiceProviderIssuer. - Security & Validation:
IDPCertificateStore,ClockSkew(for time-based validation),ValidateEncryptionCert,SkipSignatureValidation, andAllowMissingAttributes. - Authn Requests:
SignAuthnRequests,SignAuthnRequestsAlgorithm,ForceAuthn, andIsPassive.
sp := &saml2.SAMLServiceProvider{ IdentityProviderSSOURL: "https://idp.example.com/sso", IdentityProviderSSOBinding: "HTTP-Redirect", IdentityProviderIssuer: "https://idp.example.com/issuer", AssertionConsumerServiceURL: "https://sp.example.com/acs", ServiceProviderIssuer: "https://sp.example.com/issuer", IDPCertificateStore: myCertStore, ClockSkew: 10 * time.Second, }- IdP Endpoints:
Reference of available fuzzing targets
mainThe following fuzzing targets are implemented in the
internal/fuzzdirectory to target specific SAML operations and XML processing:FuzzDecodeResponse: Fuzzes SAML response decoding and validation.FuzzLogoutResponse: Fuzzes SAML logout response decoding.FuzzBuildRequest: Fuzzes SAML authentication request building.FuzzXMLValidation: Fuzzes XML validation to catch parsing vulnerabilities.
Supported Identity Providers
maingosaml2 is designed as a generic SAML implementation. The following Identity Providers (IdPs) have been tested and verified to work with the library:
- Okta
- Auth0
- Shibboleth
- Ipsilon
- OneLogin
- Azure Active Directory (Azure AD)
Sign SAML XML elements manually
mainIf you have an existing
etree.Elementrepresenting a SAML request (AuthnRequest or LogoutRequest) and need to sign it, use theSignAuthnRequestorSignLogoutRequestmethods.These methods follow the SAML schema requirement where the
<Signature>element is inserted immediately after the<Issuer>element and before all other children.// Example: Manually signing an element signedEl, err := sp.SignAuthnRequest(myElement) if err != nil { // handle error }