-
Notifications
You must be signed in to change notification settings - Fork 295
/
Copy pathsaml.go
263 lines (215 loc) · 6.23 KB
/
saml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package provider
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"math/big"
"net/http"
"net/url"
"strings"
"time"
"github.com/netlify/gotrue/models"
"github.com/netlify/gotrue/storage"
"github.com/netlify/gotrue/conf"
saml2 "github.com/russellhaering/gosaml2"
"github.com/russellhaering/gosaml2/types"
dsig "github.com/russellhaering/goxmldsig"
"github.com/gobuffalo/uuid"
"golang.org/x/oauth2"
)
type SamlProvider struct {
ServiceProvider *saml2.SAMLServiceProvider
}
type ConfigX509KeyStore struct {
InstanceID uuid.UUID
DB *storage.Connection
Conf conf.SamlProviderConfiguration
}
func getMetadata(url string) (*types.EntityDescriptor, error) {
res, err := http.Get(url)
if err != nil {
return nil, err
}
if res.StatusCode >= 300 {
return nil, fmt.Errorf("Request failed with status %s", res.Status)
}
rawMetadata, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
metadata := &types.EntityDescriptor{}
err = xml.Unmarshal(rawMetadata, metadata)
if err != nil {
return nil, err
}
// TODO: cache in memory
return metadata, nil
}
// NewSamlProvider creates a Saml account provider.
func NewSamlProvider(ext conf.SamlProviderConfiguration, db *storage.Connection, instanceId uuid.UUID) (*SamlProvider, error) {
if !ext.Enabled {
return nil, errors.New("SAML Provider is not enabled")
}
if _, err := url.Parse(ext.MetadataURL); err != nil {
return nil, fmt.Errorf("Metadata URL is invalid: %+v", err)
}
meta, err := getMetadata(ext.MetadataURL)
if err != nil {
return nil, fmt.Errorf("Fetching metadata failed: %+v", err)
}
baseURI, err := url.Parse(strings.Trim(ext.APIBase, "/"))
if err != nil || ext.APIBase == "" {
return nil, fmt.Errorf("Invalid API base URI: %s", ext.APIBase)
}
var ssoService types.SingleSignOnService
foundService := false
for _, service := range meta.IDPSSODescriptor.SingleSignOnServices {
if service.Binding == "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" {
ssoService = service
foundService = true
break
}
}
if !foundService {
return nil, errors.New("No valid SSO service found in IDP metadata")
}
certStore := dsig.MemoryX509CertificateStore{
Roots: []*x509.Certificate{},
}
for _, kd := range meta.IDPSSODescriptor.KeyDescriptors {
for _, xcert := range kd.KeyInfo.X509Data.X509Certificates {
if xcert.Data == "" {
continue
}
certData, err := base64.StdEncoding.DecodeString(xcert.Data)
if err != nil {
continue
}
idpCert, err := x509.ParseCertificate(certData)
if err != nil {
continue
}
certStore.Roots = append(certStore.Roots, idpCert)
}
}
keyStore := &ConfigX509KeyStore{
InstanceID: instanceId,
DB: db,
Conf: ext,
}
sp := &saml2.SAMLServiceProvider{
IdentityProviderSSOURL: ssoService.Location,
IdentityProviderIssuer: meta.EntityID,
AssertionConsumerServiceURL: baseURI.String() + "/saml/acs",
ServiceProviderIssuer: baseURI.String() + "/saml",
SignAuthnRequests: true,
AudienceURI: baseURI.String() + "/saml",
IDPCertificateStore: &certStore,
SPKeyStore: keyStore,
AllowMissingAttributes: true,
}
p := &SamlProvider{
ServiceProvider: sp,
}
return p, nil
}
func (p SamlProvider) AuthCodeURL(tokenString string, args ...oauth2.AuthCodeOption) string {
url, err := p.ServiceProvider.BuildAuthURL(tokenString)
if err != nil {
return ""
}
return url
}
func (p SamlProvider) SPMetadata() ([]byte, error) {
metadata, err := p.ServiceProvider.Metadata()
if err != nil {
return nil, err
}
// the typing for encryption methods currently causes the xml to violate the spec
// therefore they are removed since they are optional anyways and mostly unused
metadata.SPSSODescriptor.KeyDescriptors[1].EncryptionMethods = []types.EncryptionMethod{}
rawMetadata, err := xml.Marshal(metadata)
if err != nil {
return nil, err
}
return rawMetadata, nil
}
func (ks ConfigX509KeyStore) GetKeyPair() (*rsa.PrivateKey, []byte, error) {
if ks.Conf.SigningCert == "" && ks.Conf.SigningKey == "" {
return ks.CreateSigningCert()
}
keyPair, err := tls.X509KeyPair([]byte(ks.Conf.SigningCert), []byte(ks.Conf.SigningKey))
if err != nil {
return nil, nil, fmt.Errorf("Parsing key pair failed: %+v", err)
}
var privKey *rsa.PrivateKey
switch key := keyPair.PrivateKey.(type) {
case *rsa.PrivateKey:
privKey = key
default:
return nil, nil, errors.New("Private key is not an RSA key")
}
return privKey, keyPair.Certificate[0], nil
}
func (ks ConfigX509KeyStore) CreateSigningCert() (*rsa.PrivateKey, []byte, error) {
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, nil, err
}
currentTime := time.Now()
certBody := &x509.Certificate{
SerialNumber: big.NewInt(1),
NotBefore: currentTime.Add(-5 * time.Minute),
NotAfter: currentTime.Add(365 * 24 * time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{},
BasicConstraintsValid: true,
}
cert, err := x509.CreateCertificate(rand.Reader, certBody, certBody, &key.PublicKey, key)
if err != nil {
return nil, nil, fmt.Errorf("Failed to create certificate: %+v", err)
}
if err := ks.SaveConfig(cert, key); err != nil {
return nil, nil, fmt.Errorf("Saving signing keypair failed: %+v", err)
}
return key, cert, nil
}
func (ks ConfigX509KeyStore) SaveConfig(cert []byte, key *rsa.PrivateKey) error {
if ks.InstanceID == uuid.Nil {
return nil
}
pemCert := &pem.Block{
Type: "CERTIFICATE",
Bytes: cert,
}
certBytes := pem.EncodeToMemory(pemCert)
if certBytes == nil {
return errors.New("Could not encode certificate")
}
pemKey := &pem.Block{
Type: "PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
}
keyBytes := pem.EncodeToMemory(pemKey)
if keyBytes == nil {
return errors.New("Could not encode key")
}
instance, err := models.GetInstance(ks.DB, ks.InstanceID)
if err != nil {
return err
}
conf := instance.BaseConfig
conf.External.Saml.SigningCert = string(certBytes)
conf.External.Saml.SigningKey = string(keyBytes)
if err := instance.UpdateConfig(ks.DB, conf); err != nil {
return err
}
return nil
}