|
1 | 1 | # gosaml2 |
2 | 2 |
|
3 | 3 | [](https://github.com/russellhaering/gosaml2/actions/workflows/test.yml?query=branch%3Amain) |
4 | | -[](https://godoc.org/github.com/russellhaering/gosaml2) |
| 4 | +[](https://pkg.go.dev/github.com/russellhaering/gosaml2/v2) |
5 | 5 |
|
6 | | -SAML 2.0 implemementation for Service Providers based on [etree](https://github.com/beevik/etree) |
7 | | -and [goxmldsig](https://github.com/russellhaering/goxmldsig), a pure Go |
8 | | -implementation of XML digital signatures. |
| 6 | +SAML 2.0 library for Go with both **Service Provider** and **Identity Provider** support. Built on [etree](https://github.com/beevik/etree) and a vendored pure-Go XML digital signatures implementation. |
| 7 | + |
| 8 | +## Features |
| 9 | + |
| 10 | +- **Service Provider (SP)**: Validate SAML responses, build AuthnRequests, generate SP metadata, single logout |
| 11 | +- **Identity Provider (IdP)**: Build SAML responses, validate AuthnRequests, generate IdP metadata, single logout |
| 12 | +- **Secure defaults**: SHA-1 disabled, signatures required, IDP-initiated SSO off by default |
| 13 | +- **Replay prevention**: `RequestTracker` interface with in-memory implementation for `InResponseTo` validation |
| 14 | +- **Encryption**: AES-GCM and AES-CBC assertion encryption/decryption |
| 15 | +- **HTTP bindings**: HTTP-POST and HTTP-Redirect with proper query-string signatures |
| 16 | +- **Metadata parsing**: `ParseEntityDescriptor` for configuring SP or IdP from partner metadata XML |
| 17 | +- **Comprehensive test suite**: 335 security tests covering signature validation, replay attacks, XML wrapping, and more |
9 | 18 |
|
10 | 19 | ## Installation |
11 | 20 |
|
12 | | -Install `gosaml2` into your `$GOPATH` using `go get`: |
| 21 | +``` |
| 22 | +go get github.com/russellhaering/gosaml2/v2 |
| 23 | +``` |
| 24 | + |
| 25 | +Requires Go 1.23 or later. |
| 26 | + |
| 27 | +## Quick Start: Service Provider |
| 28 | + |
| 29 | +```go |
| 30 | +package main |
13 | 31 |
|
| 32 | +import ( |
| 33 | + "context" |
| 34 | + "fmt" |
| 35 | + "log" |
| 36 | + "net/http" |
| 37 | + "time" |
| 38 | + |
| 39 | + saml2 "github.com/russellhaering/gosaml2/v2" |
| 40 | + "github.com/russellhaering/gosaml2/v2/sp" |
| 41 | + "github.com/russellhaering/gosaml2/v2/types" |
| 42 | +) |
| 43 | + |
| 44 | +func main() { |
| 45 | + // Parse your IdP's metadata XML to extract SSO URL and certificates. |
| 46 | + ed, err := saml2.ParseEntityDescriptor(idpMetadataXML) |
| 47 | + if err != nil { |
| 48 | + log.Fatal(err) |
| 49 | + } |
| 50 | + |
| 51 | + s := &sp.ServiceProvider{ |
| 52 | + EntityID: "https://my-app.example.com", |
| 53 | + ACSURL: "https://my-app.example.com/saml/acs", |
| 54 | + AudienceURIs: []string{"https://my-app.example.com"}, |
| 55 | + // Replay prevention (recommended). |
| 56 | + RequestTracker: sp.NewMemoryRequestTracker(5 * time.Minute), |
| 57 | + } |
| 58 | + // Configure IdP settings from metadata. |
| 59 | + if err := s.ConfigureFromMetadata(ed); err != nil { |
| 60 | + log.Fatal(err) |
| 61 | + } |
| 62 | + |
| 63 | + http.HandleFunc("/saml/acs", func(w http.ResponseWriter, r *http.Request) { |
| 64 | + r.ParseForm() |
| 65 | + info, err := s.RetrieveAssertionInfo(context.Background(), r.FormValue("SAMLResponse")) |
| 66 | + if err != nil { |
| 67 | + http.Error(w, "Forbidden", http.StatusForbidden) |
| 68 | + return |
| 69 | + } |
| 70 | + fmt.Fprintf(w, "Hello, %s\n", info.NameID) |
| 71 | + }) |
| 72 | +} |
14 | 73 | ``` |
15 | | -go get github.com/russellhaering/gosaml2 |
| 74 | + |
| 75 | +## Quick Start: Identity Provider |
| 76 | + |
| 77 | +```go |
| 78 | +package main |
| 79 | + |
| 80 | +import ( |
| 81 | + "context" |
| 82 | + "log" |
| 83 | + "net/http" |
| 84 | + |
| 85 | + "github.com/russellhaering/gosaml2/v2/idp" |
| 86 | + "github.com/russellhaering/gosaml2/v2/types" |
| 87 | +) |
| 88 | + |
| 89 | +func main() { |
| 90 | + identity := &idp.IdentityProvider{ |
| 91 | + EntityID: "https://idp.example.com", |
| 92 | + SSOURL: "https://idp.example.com/sso", |
| 93 | + SigningKeyStore: signingKey, // *saml2.KeyStore |
| 94 | + SignResponses: true, |
| 95 | + SignAssertions: true, |
| 96 | + ServiceProviders: map[string]*idp.SPConfig{ |
| 97 | + "https://sp.example.com": { |
| 98 | + EntityID: "https://sp.example.com", |
| 99 | + ACSURLs: []string{"https://sp.example.com/saml/acs"}, |
| 100 | + }, |
| 101 | + }, |
| 102 | + } |
| 103 | + |
| 104 | + http.HandleFunc("/sso", func(w http.ResponseWriter, r *http.Request) { |
| 105 | + reqInfo, err := identity.ValidateEncodedAuthnRequestPOST(context.Background(), r.FormValue("SAMLRequest")) |
| 106 | + if err != nil { |
| 107 | + http.Error(w, "Bad Request", http.StatusBadRequest) |
| 108 | + return |
| 109 | + } |
| 110 | + // Authenticate user, then build response... |
| 111 | + body, err := identity.BuildResponseBodyPost(reqInfo.SP.EntityID, &idp.AssertionParams{ |
| 112 | + NameID: "user@example.com", |
| 113 | + InResponseTo: reqInfo.ID, |
| 114 | + Recipient: reqInfo.ACSURL, |
| 115 | + }, r.FormValue("RelayState")) |
| 116 | + if err != nil { |
| 117 | + http.Error(w, "Internal Error", http.StatusInternalServerError) |
| 118 | + return |
| 119 | + } |
| 120 | + w.Write(body) |
| 121 | + }) |
| 122 | +} |
16 | 123 | ``` |
17 | 124 |
|
18 | | -## Example |
| 125 | +## Security Defaults |
| 126 | + |
| 127 | +gosaml2 v2 is secure by default: |
| 128 | + |
| 129 | +| Setting | Default | Override | |
| 130 | +|---------|---------|----------| |
| 131 | +| SHA-1 signatures | **Rejected** | `AllowSHA1: true` | |
| 132 | +| Response/assertion signatures | **Required** | `InsecureSkipSignatureValidation: true` | |
| 133 | +| IDP-initiated SSO | **Rejected** | `AllowIDPInitiated: true` | |
| 134 | +| Unsigned logout requests | **Rejected** | `InsecureSkipSignatureValidation: true` | |
| 135 | +| Conditions (NotBefore/NotOnOrAfter) | **Hard errors** | Not overridable | |
| 136 | +| Audience restriction | **Enforced** | Configure `AudienceURIs` | |
| 137 | +| Clock skew tolerance | **60 seconds** | `ClockSkew: duration` | |
| 138 | + |
| 139 | +## Examples |
| 140 | + |
| 141 | +- [SP example](examples/sp/demo.go) - Service Provider with Okta |
| 142 | +- [IdP example](examples/idp/demo.go) - Identity Provider serving SAML responses |
| 143 | + |
| 144 | +## Migration from v1 |
| 145 | + |
| 146 | +See [MIGRATING.md](MIGRATING.md) for a detailed guide on upgrading from gosaml2 v1. |
| 147 | + |
| 148 | +## Tested Identity Providers |
| 149 | + |
| 150 | +This library is meant to be a standards-compliant SAML implementation. The following identity providers have been tested: |
| 151 | + |
| 152 | +- Okta |
| 153 | +- Auth0 |
| 154 | +- Shibboleth |
| 155 | +- OneLogin |
| 156 | +- Azure Active Directory (Azure AD) |
| 157 | +- Keycloak |
| 158 | +- Google Workspace |
| 159 | +- Microsoft ADFS |
19 | 160 |
|
20 | | -See [demo.go](s2example/demo.go). |
| 161 | +If you find a standards-compliant identity provider that doesn't work, please submit a bug or pull request. |
21 | 162 |
|
22 | | -## Supported Identity Providers |
| 163 | +## Documentation |
23 | 164 |
|
24 | | -This library is meant to be a generic SAML implementation. If you find a |
25 | | -standards compliant identity provider that it doesn't work with please |
26 | | -submit a bug or pull request. |
| 165 | +- [GoDoc (pkg.go.dev)](https://pkg.go.dev/github.com/russellhaering/gosaml2/v2) |
| 166 | +- [Migration Guide](MIGRATING.md) |
| 167 | +- [Security Policy](SECURITY.md) |
27 | 168 |
|
28 | | -The following identity providers have been tested: |
| 169 | +## License |
29 | 170 |
|
30 | | -* Okta |
31 | | -* Auth0 |
32 | | -* Shibboleth |
33 | | -* Ipsilon |
34 | | -* OneLogin |
35 | | -* Azure Active Directory (Azure AD) |
| 171 | +Apache License 2.0 - see [LICENSE](LICENSE) for details. |
0 commit comments