Skip to content

Commit afaf271

Browse files
committed
Add test for samlidp
1 parent a97ac98 commit afaf271

File tree

4 files changed

+76
-26
lines changed

4 files changed

+76
-26
lines changed

identity_provider.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ type IdentityProvider struct {
110110
EntityIDConstructor EntityIDConstructor
111111
}
112112

113+
// EntityIDConstructor is a function that returns the entityID for customization.
113114
type EntityIDConstructor func() string
114115

115116
// Metadata returns the metadata structure for this identity provider.
@@ -337,16 +338,9 @@ func (idp *IdentityProvider) ServeIDPInitiated(w http.ResponseWriter, r *http.Re
337338
}
338339
}
339340

340-
// createDefaultEntityIDConstructor creates a function to return entityID from metadataURL.
341-
func createDefaultEntityIDConstructor(metadataURL url.URL) func() string {
342-
return func() string {
343-
return metadataURL.String()
344-
}
345-
}
346-
347341
func (idp *IdentityProvider) getEntityID() string {
348342
if idp.EntityIDConstructor == nil {
349-
return createDefaultEntityIDConstructor(idp.MetadataURL)()
343+
return idp.MetadataURL.String()
350344
}
351345
return idp.EntityIDConstructor()
352346
}

samlidp/samlidp.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ import (
1818

1919
// Options represent the parameters to New() for creating a new IDP server
2020
type Options struct {
21-
URL url.URL
22-
Key crypto.PrivateKey
23-
Signer crypto.Signer
24-
Logger logger.Interface
25-
Certificate *x509.Certificate
26-
Store Store
21+
URL url.URL
22+
Key crypto.PrivateKey
23+
Signer crypto.Signer
24+
Logger logger.Interface
25+
Certificate *x509.Certificate
26+
Store Store
27+
EntityIDConstructor saml.EntityIDConstructor
2728
}
2829

2930
// Server represents an IDP server. The server provides the following URLs:
@@ -59,12 +60,13 @@ func New(opts Options) (*Server, error) {
5960
s := &Server{
6061
serviceProviders: map[string]*saml.EntityDescriptor{},
6162
IDP: saml.IdentityProvider{
62-
Key: opts.Key,
63-
Signer: opts.Signer,
64-
Logger: logr,
65-
Certificate: opts.Certificate,
66-
MetadataURL: metadataURL,
67-
SSOURL: ssoURL,
63+
Key: opts.Key,
64+
Signer: opts.Signer,
65+
Logger: logr,
66+
Certificate: opts.Certificate,
67+
MetadataURL: metadataURL,
68+
SSOURL: ssoURL,
69+
EntityIDConstructor: opts.EntityIDConstructor,
6870
},
6971
logger: logr,
7072
Store: opts.Store,

samlidp/samlidp_test.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ func mustParseCertificate(pemStr []byte) *x509.Certificate {
6666
return cert
6767
}
6868

69+
func setupTestVariables() {
70+
saml.TimeNow = func() time.Time {
71+
rv, _ := time.Parse("Mon Jan 2 15:04:05 MST 2006", "Mon Dec 1 01:57:09 UTC 2015")
72+
return rv
73+
}
74+
jwt.TimeFunc = saml.TimeNow
75+
saml.RandReader = &testRandomReader{}
76+
}
77+
6978
type ServerTest struct {
7079
SPKey *rsa.PrivateKey
7180
SPCertificate *x509.Certificate
@@ -79,12 +88,7 @@ type ServerTest struct {
7988

8089
func NewServerTest(t *testing.T) *ServerTest {
8190
test := ServerTest{}
82-
saml.TimeNow = func() time.Time {
83-
rv, _ := time.Parse("Mon Jan 2 15:04:05 MST 2006", "Mon Dec 1 01:57:09 UTC 2015")
84-
return rv
85-
}
86-
jwt.TimeFunc = saml.TimeNow
87-
saml.RandReader = &testRandomReader{}
91+
setupTestVariables()
8892

8993
test.SPKey = mustParsePrivateKey(golden.Get(t, "sp_key.pem")).(*rsa.PrivateKey)
9094
test.SPCertificate = mustParseCertificate(golden.Get(t, "sp_cert.pem"))
@@ -143,3 +147,28 @@ func TestHTTPCanSSORequest(t *testing.T) {
143147
w.Body.String())
144148
golden.Assert(t, w.Body.String(), "http_sso_response.html")
145149
}
150+
151+
func TestHTTPMetadataResponseWithCustomEntityID(t *testing.T) {
152+
setupTestVariables()
153+
154+
server, err := New(Options{
155+
Certificate: mustParseCertificate(golden.Get(t, "idp_cert.pem")),
156+
Key: mustParsePrivateKey(golden.Get(t, "idp_key.pem")).(*rsa.PrivateKey),
157+
Logger: logger.DefaultLogger,
158+
URL: url.URL{Scheme: "https", Host: "idp.example.com"},
159+
Store: &MemoryStore{},
160+
EntityIDConstructor: func() string {
161+
return "https://idp.example.com/idp-id"
162+
},
163+
})
164+
assert.Check(t, err)
165+
166+
w := httptest.NewRecorder()
167+
r, _ := http.NewRequest("GET", "https://idp.example.com/metadata", nil)
168+
server.ServeHTTP(w, r)
169+
assert.Check(t, is.Equal(http.StatusOK, w.Code))
170+
assert.Check(t,
171+
strings.HasPrefix(w.Body.String(), "<EntityDescriptor"),
172+
w.Body.String())
173+
golden.Assert(t, w.Body.String(), "http_metadata_response_with_custom_entity_id.html")
174+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<EntityDescriptor xmlns="urn:oasis:names:tc:SAML:2.0:metadata" validUntil="2015-12-03T01:57:09Z" cacheDuration="PT48H" entityID="https://idp.example.com/idp-id">
2+
<IDPSSODescriptor xmlns="urn:oasis:names:tc:SAML:2.0:metadata" protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
3+
<KeyDescriptor use="signing">
4+
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
5+
<X509Data xmlns="http://www.w3.org/2000/09/xmldsig#">
6+
<X509Certificate xmlns="http://www.w3.org/2000/09/xmldsig#">MIIB7zCCAVgCCQDFzbKIp7b3MTANBgkqhkiG9w0BAQUFADA8MQswCQYDVQQGEwJVUzELMAkGA1UECAwCR0ExDDAKBgNVBAoMA2ZvbzESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTEzMTAwMjAwMDg1MVoXDTE0MTAwMjAwMDg1MVowPDELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkdBMQwwCgYDVQQKDANmb28xEjAQBgNVBAMMCWxvY2FsaG9zdDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA1PMHYmhZj308kWLhZVT4vOulqx/9ibm5B86fPWwUKKQ2i12MYtz07tzukPymisTDhQaqyJ8Kqb/6JjhmeMnEOdTvSPmHO8m1ZVveJU6NoKRn/mP/BD7FW52WhbrUXLSeHVSKfWkNk6S4hk9MV9TswTvyRIKvRsw0X/gfnqkroJcCAwEAATANBgkqhkiG9w0BAQUFAAOBgQCMMlIO+GNcGekevKgkakpMdAqJfs24maGb90DvTLbRZRD7Xvn1MnVBBS9hzlXiFLYOInXACMW5gcoRFfeTQLSouMM8o57h0uKjfTmuoWHLQLi6hnF+cvCsEFiJZ4AbF+DgmO6TarJ8O05t8zvnOwJlNCASPZRH/JmF8tX0hoHuAQ==</X509Certificate>
7+
</X509Data>
8+
</KeyInfo>
9+
</KeyDescriptor>
10+
<KeyDescriptor use="encryption">
11+
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
12+
<X509Data xmlns="http://www.w3.org/2000/09/xmldsig#">
13+
<X509Certificate xmlns="http://www.w3.org/2000/09/xmldsig#">MIIB7zCCAVgCCQDFzbKIp7b3MTANBgkqhkiG9w0BAQUFADA8MQswCQYDVQQGEwJVUzELMAkGA1UECAwCR0ExDDAKBgNVBAoMA2ZvbzESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTEzMTAwMjAwMDg1MVoXDTE0MTAwMjAwMDg1MVowPDELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkdBMQwwCgYDVQQKDANmb28xEjAQBgNVBAMMCWxvY2FsaG9zdDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA1PMHYmhZj308kWLhZVT4vOulqx/9ibm5B86fPWwUKKQ2i12MYtz07tzukPymisTDhQaqyJ8Kqb/6JjhmeMnEOdTvSPmHO8m1ZVveJU6NoKRn/mP/BD7FW52WhbrUXLSeHVSKfWkNk6S4hk9MV9TswTvyRIKvRsw0X/gfnqkroJcCAwEAATANBgkqhkiG9w0BAQUFAAOBgQCMMlIO+GNcGekevKgkakpMdAqJfs24maGb90DvTLbRZRD7Xvn1MnVBBS9hzlXiFLYOInXACMW5gcoRFfeTQLSouMM8o57h0uKjfTmuoWHLQLi6hnF+cvCsEFiJZ4AbF+DgmO6TarJ8O05t8zvnOwJlNCASPZRH/JmF8tX0hoHuAQ==</X509Certificate>
14+
</X509Data>
15+
</KeyInfo>
16+
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"></EncryptionMethod>
17+
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes192-cbc"></EncryptionMethod>
18+
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc"></EncryptionMethod>
19+
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"></EncryptionMethod>
20+
</KeyDescriptor>
21+
<NameIDFormat>urn:oasis:names:tc:SAML:2.0:nameid-format:transient</NameIDFormat>
22+
<SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="https://idp.example.com/sso"></SingleSignOnService>
23+
<SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="https://idp.example.com/sso"></SingleSignOnService>
24+
</IDPSSODescriptor>
25+
</EntityDescriptor>

0 commit comments

Comments
 (0)