diff --git a/saml/response_test.go b/saml/response_test.go index 574b38b..840d886 100644 --- a/saml/response_test.go +++ b/saml/response_test.go @@ -9,13 +9,14 @@ import ( "testing" "time" - "github.com/hashicorp/cap/saml" - "github.com/hashicorp/cap/saml/models/core" - testprovider "github.com/hashicorp/cap/saml/test" "github.com/jonboulle/clockwork" saml2 "github.com/russellhaering/gosaml2" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/hashicorp/cap/saml" + "github.com/hashicorp/cap/saml/models/core" + testprovider "github.com/hashicorp/cap/saml/test" ) var testExpiredResp = `PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHNhbWwycDpSZXNwb25zZSBEZXN0aW5hdGlvbj0iaHR0cDovL2xvY2FsaG9zdDo4MDAwL3NhbWwvYWNzIiBJRD0iXzg4NDljMmVlNTMyZmNkYjc4MWYyYTE3NzZlYWMzNzQxIiBJblJlc3BvbnNlVG89ImJjNWE1YmFhLTk0ZTAtNThhOC04NzJjLWU1MTQ5MWQyYjNlZSIgSXNzdWVJbnN0YW50PSIyMDIzLTA4LTI1VDE0OjMyOjUzLjY4MFoiIFZlcnNpb249IjIuMCIgeG1sbnM6c2FtbDJwPSJ1cm46b2FzaXM6bmFtZXM6dGM6U0FNTDoyLjA6cHJvdG9jb2wiIHhtbG5zOnhzZD0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEiPjxzYW1sMjpJc3N1ZXIgeG1sbnM6c2FtbDI9InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDphc3NlcnRpb24iPmh0dHBzOi8vc2FtbHRlc3QuaWQvc2FtbC9pZHA8L3NhbWwyOklzc3Vlcj48ZHM6U2lnbmF0dXJlIHhtbG5zOmRzPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwLzA5L3htbGRzaWcjIj48ZHM6U2lnbmVkSW5mbz48ZHM6Q2Fub25pY2FsaXphdGlvbk1ldGhvZCBBbGdvcml0aG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lczp0YzpTQU1MOjIuMDphdHRybmFtZS1mb3JtYXQ6dXJpIj48c2FtbDI6QXR0cmlidXRlVmFsdWU+TW9ydHkgU21pdGg8L3NhbWwyOkF0dHJpYnV0ZVZhbHVlPjwvc2FtbDI6QXR0cmlidXRlPjxzYW1sMjpBdHRyaWJ1dGUgRnJpZW5kbHlOYW1lPSJnaXZlbk5hbWUiIE5hbWU9InVybjpvaWQ6Mi41LjQuNDIiIE5hbWVGb3JtYXQ9InVybjpvYXNpczpuYW1lczp0YzpTQU1MOjIuMDphdHRybmFtZS1mb3JtYXQ6dXJpIj48c2FtbDI6QXR0cmlidXRlVmFsdWU+TW9ydGltZXI8L3NhbWwyOkF0dHJpYnV0ZVZhbHVlPjwvc2FtbDI6QXR0cmlidXRlPjwvc2FtbDI6QXR0cmlidXRlU3RhdGVtZW50Pjwvc2FtbDI6QXNzZXJ0aW9uPjwvc2FtbDJwOlJlc3BvbnNlPg==` @@ -24,12 +25,16 @@ var testExpiredResp = `PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHNhb func TestServiceProvider_ParseResponse(t *testing.T) { t.Parallel() const ( - testRequestId = "bc5a5baa-94e0-58a8-872c-e51491d2b3ee" - testEntityID = "http://saml.julz/example" - testAcs = "http://localhost:8000/saml/acs" - metadataURL = "https://samltest.id/saml/idp" + testRequestId = "test-request-id" + testEntityID = "http://hashicorp-cap.test" + testAcs = "http://hashicorp-cap.test/saml/acs" ) + tp := testprovider.StartTestProvider(t) + defer tp.Close() + + metadataURL := fmt.Sprintf("%s/saml/metadata", tp.ServerURL()) + testCfg, err := saml.NewConfig(testEntityID, testAcs, metadataURL) require.NoError(t, err) testSp, err := saml.NewServiceProvider(testCfg) @@ -38,7 +43,7 @@ func TestServiceProvider_ParseResponse(t *testing.T) { fakeTime, err := time.Parse("2006-01-02 15:04:05", "2023-08-25 14:33:53") require.NoError(t, err) - testCfgWithBadMetadata, err := saml.NewConfig(testEntityID, testAcs, "https://samltest.id/saml/idp-invalid") + testCfgWithBadMetadata, err := saml.NewConfig(testEntityID, testAcs, fmt.Sprintf("%s/saml/invalid", tp.ServerURL())) require.NoError(t, err) testSpWithInvalidMetadataURL, err := saml.NewServiceProvider(testCfgWithBadMetadata) require.NoError(t, err) @@ -55,12 +60,10 @@ func TestServiceProvider_ParseResponse(t *testing.T) { wantErrAs error }{ { - name: "success", - sp: testSp, - samlResp: testExpiredResp, - opts: []saml.Option{ - saml.WithClock(clockwork.NewFakeClockAt(fakeTime)), - }, + name: "success", + sp: testSp, + samlResp: base64.StdEncoding.EncodeToString([]byte(tp.SamlResponse(t, testprovider.WithResponseSigned()))), + opts: []saml.Option{}, requestID: testRequestId, }, { @@ -139,19 +142,19 @@ func TestServiceProvider_ParseResponse(t *testing.T) { wantErrContains: "unable to validate encoded response: illegal base64 data", }, { - name: "err-in-response-to", - sp: testSp, - samlResp: testExpiredResp, - opts: []saml.Option{ - saml.WithClock(clockwork.NewFakeClockAt(fakeTime)), - }, + name: "err-in-response-to", + sp: testSp, + samlResp: base64.StdEncoding.EncodeToString([]byte(tp.SamlResponse(t, testprovider.WithResponseSigned()))), requestID: "invalid-request-id", wantErrContains: "doesn't match the expected requestID (invalid-request-id)", }, { - name: "expired", - sp: testSp, - samlResp: testExpiredResp, + name: "expired", + sp: testSp, + samlResp: base64.StdEncoding.EncodeToString([]byte(tp.SamlResponse(t, + testprovider.WithResponseSigned(), + testprovider.WithResponseExpired(), + ))), requestID: "request-id", wantErrAs: &saml2.ErrInvalidValue{}, wantErrContains: "unable to validate encoded response: Expired NotOnOrAfter value", @@ -191,13 +194,13 @@ func TestServiceProvider_ParseResponse(t *testing.T) { } return } - require.NoError(err) + require.NoError(err, tc.name) assert.Equal(testRequestId, got.InResponseTo) - assert.Equal("http://localhost:8000/saml/acs", got.Destination) + assert.Equal("http://hashicorp-cap.test/saml/acs", got.Destination) assert.Equal("urn:oasis:names:tc:SAML:2.0:status:Success", got.Status.StatusCode.Value) - assert.Equal(metadataURL, got.Issuer()) - assert.Equal("msmith@samltest.id", got.Assertions()[0].Subject.NameID.Value) - assert.Equal("_35ea90b711d6f385345f0dbdd7d0ed5b", got.Assertions()[0].ID) + assert.Equal("http://test.idp", got.Issuer()) + assert.Equal("name-id", got.Assertions()[0].Subject.NameID.Value) + assert.Equal("assertion-id", got.Assertions()[0].ID) }) } } @@ -213,7 +216,7 @@ func TestServiceProvider_ParseResponseCustomACS(t *testing.T) { defer tp.Close() cfg, err := saml.NewConfig( - "http://test.me/entity", + "http://hashicorp-cap.test", "http://test.me/saml/acs", fmt.Sprintf("%s/saml/metadata", tp.ServerURL()), ) @@ -288,7 +291,7 @@ const responseUnsigned = ` - http://test.me/entity + http://hashicorp-cap.test @@ -313,11 +316,11 @@ const responseUnsigned = ` const testRespNoAssertions = ` - https://samltest.id/saml/idp + xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">http://test.idp @@ -363,11 +366,11 @@ NBp9UZome44qZAYH1iqrpmmjsfI9pJItsgWu3kXPjhSfj1AJGR1l9JGvJrHki1iHTA== - https://samltest.id/saml/idp + xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">http://test.idp @@ -410,13 +413,13 @@ NBp9UZome44qZAYH1iqrpmmjsfI9pJItsgWu3kXPjhSfj1AJGR1l9JGvJrHki1iHTA== - https://samltest.id/saml/idp + http://test.idp msmith@samltest.id - + @@ -470,11 +473,11 @@ NBp9UZome44qZAYH1iqrpmmjsfI9pJItsgWu3kXPjhSfj1AJGR1l9JGvJrHki1iHTA== - https://samltest.id/saml/idp + xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">http://test.idp @@ -517,19 +520,19 @@ NBp9UZome44qZAYH1iqrpmmjsfI9pJItsgWu3kXPjhSfj1AJGR1l9JGvJrHki1iHTA== - https://samltest.id/saml/idp + http://test.idp msmith@samltest.id - + - http://saml.julz/example + http://hashicorp-cap.test @@ -578,11 +581,11 @@ NBp9UZome44qZAYH1iqrpmmjsfI9pJItsgWu3kXPjhSfj1AJGR1l9JGvJrHki1iHTA== - https://samltest.id/saml/idp + xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">http://test.idp @@ -625,11 +628,11 @@ NBp9UZome44qZAYH1iqrpmmjsfI9pJItsgWu3kXPjhSfj1AJGR1l9JGvJrHki1iHTA== - https://samltest.id/saml/idp + http://test.idp - http://saml.julz/example + http://hashicorp-cap.test @@ -678,11 +681,11 @@ NBp9UZome44qZAYH1iqrpmmjsfI9pJItsgWu3kXPjhSfj1AJGR1l9JGvJrHki1iHTA== - https://samltest.id/saml/idp + xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion">http://test.idp @@ -725,19 +728,18 @@ NBp9UZome44qZAYH1iqrpmmjsfI9pJItsgWu3kXPjhSfj1AJGR1l9JGvJrHki1iHTA== - https://samltest.id/saml/idp + http://test.idp - msmith@samltest.id - + - . - http://saml.julz/example + http://hashicorp-cap.test diff --git a/saml/test/provider.go b/saml/test/provider.go index b4b9d7f..025577d 100644 --- a/saml/test/provider.go +++ b/saml/test/provider.go @@ -19,6 +19,9 @@ import ( "testing" "time" + "github.com/beevik/etree" + "github.com/russellhaering/gosaml2/types" + dsig "github.com/russellhaering/goxmldsig" "github.com/stretchr/testify/require" "github.com/hashicorp/cap/saml/models/core" @@ -46,8 +49,8 @@ const meta = ` ` // From https://www.samltool.com/generic_sso_res.php -const responseSigned = ` - +const ResponseSigned = ` + http://idp.example.com/metadata.php @@ -118,8 +121,9 @@ func (s *SAMLResponsePostData) PostRequest(t *testing.T) *http.Request { // TestProvider is an identity provider that can be used for testing // SAML federeation and authentication flows. type TestProvider struct { - t *testing.T - server *httptest.Server + t *testing.T + server *httptest.Server + keystore dsig.X509KeyStore metadata *metadata.EntityDescriptorIDPSSO recorder *httptest.ResponseRecorder @@ -199,9 +203,18 @@ func StartTestProvider(t *testing.T) *TestProvider { err := xml.Unmarshal([]byte(meta), &m) r.NoError(err) + keystore := dsig.RandomKeyStoreForTest() + _, cert, err := keystore.GetKeyPair() + r.NoError(err) + + b64Cert := base64.StdEncoding.EncodeToString(cert) + + m.IDPSSODescriptor[0].RoleDescriptor.KeyDescriptor[0].KeyInfo.X509Data.X509Certificates[0].Data = b64Cert + provider := &TestProvider{ t: t, metadata: &m, + keystore: keystore, } provider.defaults() @@ -272,14 +285,13 @@ func (p *TestProvider) loginHandlerPost(w http.ResponseWriter, req *http.Request relayState := req.FormValue("RelayState") r.Equal(p.expectedRelayState, relayState, "relay state doesn't match") - http.Error(w, "not implemented", http.StatusNotImplemented) samlReq := p.parseRequestPost(rawReq) p.validateRequest(samlReq) samlResponseData := &SAMLResponsePostData{ - SAMLResponse: responseSigned, + SAMLResponse: ResponseSigned, RelayState: relayState, Destination: samlReq.AssertionConsumerServiceURL, } @@ -312,7 +324,7 @@ func (p *TestProvider) loginHandlerRedirect(w http.ResponseWriter, req *http.Req p.validateRequest(samlReq) samlResponseData := &SAMLResponsePostData{ - SAMLResponse: responseSigned, + SAMLResponse: ResponseSigned, RelayState: relayState, Destination: samlReq.AssertionConsumerServiceURL, } @@ -417,3 +429,132 @@ func (p *TestProvider) parseRequestPost(request string) *core.AuthnRequest { return &req } + +type responseOptions struct { + sign bool + expired bool +} + +type ResponseOption func(*responseOptions) + +func getResponseOptions(opts ...ResponseOption) *responseOptions { + defaults := defaultResponseOptions() + for _, o := range opts { + o(defaults) + } + + return defaults +} + +func defaultResponseOptions() *responseOptions { + return &responseOptions{} +} + +func WithResponseSigned() ResponseOption { + return func(o *responseOptions) { + o.sign = true + } +} + +func WithResponseExpired() ResponseOption { + return func(o *responseOptions) { + o.expired = true + } +} + +func (p *TestProvider) SamlResponse(t *testing.T, opts ...ResponseOption) string { + r := require.New(t) + + opt := getResponseOptions(opts...) + + notOnOrAfter := "2200-01-18T06:21:48Z" + + if opt.expired { + notOnOrAfter = "2001-01-18T06:21:48Z" + } + + response := &core.Response{ + Response: types.Response{ + Destination: "http://hashicorp-cap.test/saml/acs", + ID: "test-resp-id", + InResponseTo: "test-request-id", + IssueInstant: time.Now(), + Version: "2.0", + Issuer: &types.Issuer{ + Value: "http://test.idp", + }, + Status: &types.Status{ + StatusCode: &types.StatusCode{ + Value: string(core.StatusCodeSuccess), + }, + }, + Assertions: []types.Assertion{ + { + ID: "assertion-id", + Issuer: &types.Issuer{ + Value: "http://test.idp", + }, + Subject: &types.Subject{ + NameID: &types.NameID{ + Value: "name-id", + }, + SubjectConfirmation: &types.SubjectConfirmation{ + Method: "urn:oasis:names:tc:SAML:2.0:cm:bearer", + SubjectConfirmationData: &types.SubjectConfirmationData{ + InResponseTo: "test-request-id", + Recipient: "http://hashicorp-cap.test/saml/acs", + NotOnOrAfter: notOnOrAfter, + }, + }, + }, + Conditions: &types.Conditions{ + NotBefore: "2001-01-18T06:21:48Z", + NotOnOrAfter: notOnOrAfter, + AudienceRestrictions: []types.AudienceRestriction{ + { + Audiences: []types.Audience{ + {Value: "http://hashicorp-cap.test"}, + }, + }, + }, + }, + AttributeStatement: &types.AttributeStatement{ + Attributes: []types.Attribute{ + { + Name: "mail", + NameFormat: "urn:oasis:names:tc:SAML:2.0:attrname-format:basic", + Values: []types.AttributeValue{ + { + Type: "xs:string", + Value: "user@hashicorp-cap.test", + }, + }, + }, + }, + }, + }, + }, + }, + } + + resp, err := xml.Marshal(response) + r.NoError(err) + + doc := etree.NewDocument() + err = doc.ReadFromBytes(resp) + r.NoError(err) + + if opt.sign { + signCtx := dsig.NewDefaultSigningContext(p.keystore) + + signed, err := signCtx.SignEnveloped(doc.Root()) + r.NoError(err) + + doc.SetRoot(signed) + } + + result, err := doc.WriteToString() + r.NoError(err) + + return result +}