Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PA-22319 - Make go-saml handle signatures on response XML (Georgia Pacific) #3

Merged
merged 1 commit into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion authnresponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (r *Response) Validate(s *ServiceProviderSettings) error {
return errors.New("no Assertions")
}

if len(r.Signature.SignatureValue.Value) == 0 {
if len(r.Signature.SignatureValue.Value) == 0 && len(r.Assertion.Signature.SignatureValue.Value) == 0 && len(r.EncryptedAssertion.Assertion.Signature.SignatureValue.Value) == 0 {
return errors.New("no signature")
}

Expand Down
113 changes: 113 additions & 0 deletions authnresponse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package saml

import (
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestResponseSignatureOnResponse(t *testing.T) {
assertion := assert.New(t)
sp := ServiceProviderSettings{
PublicCertPath: "./default.crt",
PrivateKeyPath: "./default.key",
IDPSSOURL: "http://www.onelogin.net",
IDPSSODescriptorURL: "http://www.onelogin.net",
IDPPublicCertPath: "./default.crt",
AssertionConsumerServiceURL: "http://localhost:8000/auth/saml/name",
SPSignRequest: true,
}

err := sp.Init()
assertion.NoError(err)

// Construct a SignedResponse
response := NewSignedResponse()

var sURI strings.Builder
sURI.WriteString("#")
sURI.WriteString(response.ID)

response.Signature.SignedInfo.SamlsigReference.URI = sURI.String()

sXml, err := response.String()
assertion.NoError(err)

fmt.Println("Response (XML as String) : ", sXml)

signedXml, err := SignResponse(sXml, "./default.key")
assertion.NoError(err)

fmt.Println("Signed Response (XML as String) : ", sXml)

signedResponse, err := ParseDecodedResponse([]byte(signedXml))
assertion.NoError(err)

err = signedResponse.VerifySignature("./default.crt")
assertion.NoError(err)
}

func TestResponseSignatureOnAssertion(t *testing.T) {
assertion := assert.New(t)
sp := ServiceProviderSettings{
PublicCertPath: "./default.crt",
PrivateKeyPath: "./default.key",
IDPSSOURL: "http://www.onelogin.net",
IDPSSODescriptorURL: "http://www.onelogin.net",
IDPPublicCertPath: "./default.crt",
AssertionConsumerServiceURL: "http://localhost:8000/auth/saml/name",
SPSignRequest: true,
}

err := sp.Init()
assertion.NoError(err)

// Construct a SignedResponse
response := NewSignedResponse()

var sURI strings.Builder
sURI.WriteString("#")
sURI.WriteString(response.ID)

response.Assertion.Signature.SignedInfo.SamlsigReference.URI = sURI.String()

sXml, err := response.String()
assertion.NoError(err)

fmt.Println("Response (XML as String) : ", sXml)

signedXml, err := SignResponse(sXml, "./default.key")
assertion.NoError(err)

fmt.Println("Signed Response (XML as String) : ", sXml)

signedResponse, err := ParseDecodedResponse([]byte(signedXml))
assertion.NoError(err)

err = signedResponse.VerifySignature("./default.crt")
assertion.NoError(err)
}

func TestLoadedXmlResponse(t *testing.T) {
assertion := assert.New(t)
sp := ServiceProviderSettings{
PublicCertPath: "./default.crt",
PrivateKeyPath: "./default.key",
IDPSSOURL: "http://www.onelogin.net",
IDPSSODescriptorURL: "http://www.onelogin.net",
IDPPublicCertPath: "./default.crt",
AssertionConsumerServiceURL: "http://localhost:8000/auth/saml/name",
SPSignRequest: true,
}

err := sp.Init()
assertion.NoError(err)

gpXMLResponse, err := LoadXml("./samlresponse.xml") // Feel free to change the Path to whatever your XML Response is
assertion.NoError(err)

err = VerifyResponseSignature(gpXMLResponse, sp.PublicCertPath, "")
assertion.NoError(err)
}
1 change: 1 addition & 0 deletions samlresponse.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<samlp:Response Version="2.0" ID="fso3J3gur11OlRb3iygthyi90rg" IssueInstant="2019-08-09T21:01:47.444Z" InResponseTo="WI_ed8fb9e4874049d5a8dabff4977f4c85" Destination="https://api.parsable.net/sso/saml/reply" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"><saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">https://auth.kochid.com</saml:Issuer><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/><ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/><ds:Reference URI="#fso3J3gur11OlRb3iygthyi90rg"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>sXsSLWX4wRQbU8WEoWLHDli+L1dlKJYTUmqiREYfT/I=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>fb2K++ys6Se5t9ZcL0K3qfy356rOY26LqQbUTv6VQGMsKQ/RScwkpE5vbFgJaiyrw9xNvD8ZK2FwieB1gMqJUsnVRPQiuYIulrKZfNQn1oFwSFhDrRD/wUdtdia7+afSspYlid6Gc5qieTiVdl2OI8rpfNeF9T399N4HB2ZR9pcjuGvTBspQVRPHAsGVQhK80LStSD8Gtk7xRKILKSl+J/+wtEpRTVE0BtzP+oRuaFvEn65ACUWOVW12liIJUawjGq9t6rPaq/bzVYrdA4Zu/yB10wa/3ZH1COyAF9h1U/W6vu70F8SoARW957F2chFrJVuMzTPNkdGQOlb/RNv0VQ==</ds:SignatureValue></ds:Signature><samlp:Status><samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/></samlp:Status><saml:Assertion ID="qSUZy6lG8Nk7nDQYsLuC04aHueb" IssueInstant="2019-08-09T21:01:47.628Z" Version="2.0" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"><saml:Issuer>https://auth.kochid.com</saml:Issuer><saml:Subject><saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified">COLIN.DAWSON@GAPAC.COM</saml:NameID><saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"><saml:SubjectConfirmationData Recipient="https://api.parsable.net/sso/saml/reply" NotOnOrAfter="2019-08-09T21:06:47.628Z" InResponseTo="WI_ed8fb9e4874049d5a8dabff4977f4c85"/></saml:SubjectConfirmation></saml:Subject><saml:Conditions NotBefore="2019-08-09T20:56:47.628Z" NotOnOrAfter="2019-08-09T21:06:47.628Z"><saml:AudienceRestriction><saml:Audience>https://go.parsable.com</saml:Audience></saml:AudienceRestriction></saml:Conditions><saml:AuthnStatement SessionIndex="qSUZy6lG8Nk7nDQYsLuC04aHueb" AuthnInstant="2019-08-09T21:01:47.530Z"><saml:AuthnContext><saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:Telephony</saml:AuthnContextClassRef></saml:AuthnContext></saml:AuthnStatement><saml:AttributeStatement><saml:Attribute Name="firstName" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">COLIN</saml:AttributeValue></saml:Attribute><saml:Attribute Name="lastName" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">DAWSON</saml:AttributeValue></saml:Attribute><saml:Attribute Name="email" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">COLIN.DAWSON@GAPAC.COM</saml:AttributeValue></saml:Attribute></saml:AttributeStatement></saml:Assertion></samlp:Response>
14 changes: 11 additions & 3 deletions xmlsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

const (
xmlResponseID = "urn:oasis:names:tc:SAML:2.0:protocol:Response"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was moved by golangci-lint

xmlRequestID = "urn:oasis:names:tc:SAML:2.0:protocol:AuthnRequest"
xmlLogoutRequestID = "urn:oasis:names:tc:SAML:2.0:protocol:LogoutRequest"
xmlResponseID = "urn:oasis:names:tc:SAML:2.0:protocol:Response"
xmlRequestID = "urn:oasis:names:tc:SAML:2.0:protocol:AuthnRequest"
xmlLogoutRequestID = "urn:oasis:names:tc:SAML:2.0:protocol:LogoutRequest"
)

// SignRequest sign a SAML 2.0 AuthnRequest
Expand Down Expand Up @@ -134,3 +134,11 @@ func Decrypt(xml string, privateKeyPath string) ([]byte, error) {
func deleteTempFile(filename string) {
_ = os.Remove(filename)
}

func LoadXml(certPath string) (string, error) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this function in case if we have a Request/Response and we need to test/validate somethig with it

bXML, err := ioutil.ReadFile(certPath)
if err != nil {
return "", err
}
return string(bXML), nil
}
2 changes: 1 addition & 1 deletion xmlsec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ func TestResponse(t *testing.T) {
assert.NoError(err)
assert.NotEmpty(signedXml)

err = VerifyRequestSignature(signedXml, "./default.crt")
err = VerifyResponseSignature(signedXml, "./default.crt", "")
assert.NoError(err)
}