2016-12-21 06:54:17 +05:30
|
|
|
package saml
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509"
|
2017-01-26 23:35:40 +05:30
|
|
|
"encoding/base64"
|
2016-12-21 06:54:17 +05:30
|
|
|
"encoding/pem"
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
2017-04-04 13:11:12 +05:30
|
|
|
"sort"
|
2016-12-21 06:54:17 +05:30
|
|
|
"testing"
|
2017-01-26 23:35:40 +05:30
|
|
|
"time"
|
2016-12-21 06:54:17 +05:30
|
|
|
|
2017-04-04 13:11:12 +05:30
|
|
|
"github.com/kylelemons/godebug/pretty"
|
2017-01-26 23:35:40 +05:30
|
|
|
dsig "github.com/russellhaering/goxmldsig"
|
2017-07-26 02:15:17 +05:30
|
|
|
"github.com/sirupsen/logrus"
|
2017-01-26 23:35:40 +05:30
|
|
|
|
|
|
|
"github.com/coreos/dex/connector"
|
|
|
|
)
|
|
|
|
|
2017-04-04 13:11:12 +05:30
|
|
|
// responseTest maps a SAML 2.0 response object to a set of expected values.
|
|
|
|
//
|
|
|
|
// Tests are defined in the "testdata" directory and are self-signed using xmlsec1.
|
|
|
|
//
|
|
|
|
// To add a new test, define a new, unsigned SAML 2.0 response that exercises some
|
|
|
|
// case, then sign it using the "testdata/gen.sh" script.
|
|
|
|
//
|
|
|
|
// cp testdata/good-resp.tmpl testdata/( testname ).tmpl
|
|
|
|
// vim ( testname ).tmpl # Modify your template for your test case.
|
|
|
|
// vim testdata/gen.sh # Add a xmlsec1 command to the generation script.
|
|
|
|
// ./testdata/gen.sh # Sign your template.
|
|
|
|
//
|
|
|
|
// To install xmlsec1 on Fedora run:
|
|
|
|
//
|
|
|
|
// sudo dnf install xmlsec1 xmlsec1-openssl
|
|
|
|
//
|
|
|
|
// On mac:
|
|
|
|
//
|
|
|
|
// brew install Libxmlsec1
|
|
|
|
//
|
|
|
|
type responseTest struct {
|
|
|
|
// CA file and XML file of the response.
|
|
|
|
caFile string
|
|
|
|
respFile string
|
2017-03-22 01:46:42 +05:30
|
|
|
|
2017-04-04 13:11:12 +05:30
|
|
|
// Values that should be used to validate the signature.
|
|
|
|
now string
|
|
|
|
inResponseTo string
|
|
|
|
redirectURI string
|
2017-04-07 05:41:28 +05:30
|
|
|
entityIssuer string
|
2017-04-04 13:11:12 +05:30
|
|
|
|
|
|
|
// Attribute customization.
|
|
|
|
usernameAttr string
|
|
|
|
emailAttr string
|
|
|
|
groupsAttr string
|
|
|
|
|
|
|
|
// Expected outcome of the test.
|
|
|
|
wantErr bool
|
|
|
|
wantIdent connector.Identity
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGoodResponse(t *testing.T) {
|
|
|
|
test := responseTest{
|
|
|
|
caFile: "testdata/ca.crt",
|
|
|
|
respFile: "testdata/good-resp.xml",
|
|
|
|
now: "2017-04-04T04:34:59.330Z",
|
|
|
|
usernameAttr: "Name",
|
|
|
|
emailAttr: "email",
|
|
|
|
inResponseTo: "6zmm5mguyebwvajyf2sdwwcw6m",
|
|
|
|
redirectURI: "http://127.0.0.1:5556/dex/callback",
|
|
|
|
wantIdent: connector.Identity{
|
|
|
|
UserID: "eric.chiang+okta@coreos.com",
|
|
|
|
Username: "Eric",
|
|
|
|
Email: "eric.chiang+okta@coreos.com",
|
|
|
|
EmailVerified: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
test.run(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGroups(t *testing.T) {
|
|
|
|
test := responseTest{
|
|
|
|
caFile: "testdata/ca.crt",
|
|
|
|
respFile: "testdata/good-resp.xml",
|
|
|
|
now: "2017-04-04T04:34:59.330Z",
|
|
|
|
usernameAttr: "Name",
|
|
|
|
emailAttr: "email",
|
|
|
|
groupsAttr: "groups",
|
|
|
|
inResponseTo: "6zmm5mguyebwvajyf2sdwwcw6m",
|
|
|
|
redirectURI: "http://127.0.0.1:5556/dex/callback",
|
|
|
|
wantIdent: connector.Identity{
|
|
|
|
UserID: "eric.chiang+okta@coreos.com",
|
|
|
|
Username: "Eric",
|
|
|
|
Email: "eric.chiang+okta@coreos.com",
|
|
|
|
EmailVerified: true,
|
|
|
|
Groups: []string{"Admins", "Everyone"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
test.run(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestOkta tests against an actual response from Okta.
|
|
|
|
func TestOkta(t *testing.T) {
|
|
|
|
test := responseTest{
|
|
|
|
caFile: "testdata/okta-ca.pem",
|
|
|
|
respFile: "testdata/okta-resp.xml",
|
|
|
|
now: "2017-04-04T04:34:59.330Z",
|
|
|
|
usernameAttr: "Name",
|
|
|
|
emailAttr: "email",
|
|
|
|
inResponseTo: "6zmm5mguyebwvajyf2sdwwcw6m",
|
|
|
|
redirectURI: "http://127.0.0.1:5556/dex/callback",
|
|
|
|
wantIdent: connector.Identity{
|
|
|
|
UserID: "eric.chiang+okta@coreos.com",
|
|
|
|
Username: "Eric",
|
|
|
|
Email: "eric.chiang+okta@coreos.com",
|
|
|
|
EmailVerified: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
test.run(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadStatus(t *testing.T) {
|
|
|
|
test := responseTest{
|
|
|
|
caFile: "testdata/ca.crt",
|
|
|
|
respFile: "testdata/bad-status.xml",
|
|
|
|
now: "2017-04-04T04:34:59.330Z",
|
|
|
|
usernameAttr: "Name",
|
|
|
|
emailAttr: "email",
|
|
|
|
inResponseTo: "6zmm5mguyebwvajyf2sdwwcw6m",
|
|
|
|
redirectURI: "http://127.0.0.1:5556/dex/callback",
|
|
|
|
wantErr: true,
|
|
|
|
}
|
|
|
|
test.run(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidCA(t *testing.T) {
|
|
|
|
test := responseTest{
|
|
|
|
caFile: "testdata/bad-ca.crt", // Not the CA that signed this response.
|
|
|
|
respFile: "testdata/good-resp.xml",
|
|
|
|
now: "2017-04-04T04:34:59.330Z",
|
|
|
|
usernameAttr: "Name",
|
|
|
|
emailAttr: "email",
|
|
|
|
inResponseTo: "6zmm5mguyebwvajyf2sdwwcw6m",
|
|
|
|
redirectURI: "http://127.0.0.1:5556/dex/callback",
|
|
|
|
wantErr: true,
|
|
|
|
}
|
|
|
|
test.run(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnsignedResponse(t *testing.T) {
|
|
|
|
test := responseTest{
|
|
|
|
caFile: "testdata/ca.crt",
|
|
|
|
respFile: "testdata/good-resp.tmpl", // Use the unsigned template, not the signed document.
|
|
|
|
now: "2017-04-04T04:34:59.330Z",
|
|
|
|
usernameAttr: "Name",
|
|
|
|
emailAttr: "email",
|
|
|
|
inResponseTo: "6zmm5mguyebwvajyf2sdwwcw6m",
|
|
|
|
redirectURI: "http://127.0.0.1:5556/dex/callback",
|
|
|
|
wantErr: true,
|
|
|
|
}
|
|
|
|
test.run(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExpiredAssertion(t *testing.T) {
|
|
|
|
test := responseTest{
|
|
|
|
caFile: "testdata/ca.crt",
|
|
|
|
respFile: "testdata/assertion-signed.xml",
|
|
|
|
now: "2020-04-04T04:34:59.330Z", // Assertion has expired.
|
|
|
|
usernameAttr: "Name",
|
|
|
|
emailAttr: "email",
|
|
|
|
inResponseTo: "6zmm5mguyebwvajyf2sdwwcw6m",
|
|
|
|
redirectURI: "http://127.0.0.1:5556/dex/callback",
|
|
|
|
wantErr: true,
|
|
|
|
}
|
|
|
|
test.run(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestAssertionSignedNotResponse ensures the connector validates SAML 2.0
|
|
|
|
// responses where the assertion is signed but the root element, the
|
|
|
|
// response, isn't.
|
|
|
|
func TestAssertionSignedNotResponse(t *testing.T) {
|
|
|
|
test := responseTest{
|
|
|
|
caFile: "testdata/ca.crt",
|
|
|
|
respFile: "testdata/assertion-signed.xml",
|
|
|
|
now: "2017-04-04T04:34:59.330Z",
|
|
|
|
usernameAttr: "Name",
|
|
|
|
emailAttr: "email",
|
|
|
|
inResponseTo: "6zmm5mguyebwvajyf2sdwwcw6m",
|
|
|
|
redirectURI: "http://127.0.0.1:5556/dex/callback",
|
|
|
|
wantIdent: connector.Identity{
|
|
|
|
UserID: "eric.chiang+okta@coreos.com",
|
|
|
|
Username: "Eric",
|
|
|
|
Email: "eric.chiang+okta@coreos.com",
|
|
|
|
EmailVerified: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
test.run(t)
|
|
|
|
}
|
|
|
|
|
2017-04-07 05:41:28 +05:30
|
|
|
func TestInvalidSubjectInResponseTo(t *testing.T) {
|
|
|
|
test := responseTest{
|
|
|
|
caFile: "testdata/ca.crt",
|
|
|
|
respFile: "testdata/assertion-signed.xml",
|
|
|
|
now: "2017-04-04T04:34:59.330Z",
|
|
|
|
usernameAttr: "Name",
|
|
|
|
emailAttr: "email",
|
|
|
|
inResponseTo: "invalid-id", // Bad InResponseTo value.
|
|
|
|
redirectURI: "http://127.0.0.1:5556/dex/callback",
|
|
|
|
wantErr: true,
|
|
|
|
}
|
|
|
|
test.run(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidSubjectRecipient(t *testing.T) {
|
|
|
|
test := responseTest{
|
|
|
|
caFile: "testdata/ca.crt",
|
|
|
|
respFile: "testdata/assertion-signed.xml",
|
|
|
|
now: "2017-04-04T04:34:59.330Z",
|
|
|
|
usernameAttr: "Name",
|
|
|
|
emailAttr: "email",
|
|
|
|
inResponseTo: "6zmm5mguyebwvajyf2sdwwcw6m",
|
|
|
|
redirectURI: "http://bad.com/dex/callback", // Doesn't match Recipient value.
|
|
|
|
wantErr: true,
|
|
|
|
}
|
|
|
|
test.run(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidAssertionAudience(t *testing.T) {
|
|
|
|
test := responseTest{
|
|
|
|
caFile: "testdata/ca.crt",
|
|
|
|
respFile: "testdata/assertion-signed.xml",
|
|
|
|
now: "2017-04-04T04:34:59.330Z",
|
|
|
|
usernameAttr: "Name",
|
|
|
|
emailAttr: "email",
|
|
|
|
inResponseTo: "6zmm5mguyebwvajyf2sdwwcw6m",
|
|
|
|
redirectURI: "http://127.0.0.1:5556/dex/callback",
|
|
|
|
// EntityIssuer overrides RedirectURI when determining the expected
|
|
|
|
// audience. In this case, ensure the audience is invalid.
|
|
|
|
entityIssuer: "http://localhost:5556/dex/callback",
|
|
|
|
wantErr: true,
|
|
|
|
}
|
|
|
|
test.run(t)
|
|
|
|
}
|
|
|
|
|
2017-04-04 13:11:12 +05:30
|
|
|
// TestTwoAssertionFirstSigned tries to catch an edge case where an attacker
|
|
|
|
// provides a second assertion that's not signed.
|
|
|
|
func TestTwoAssertionFirstSigned(t *testing.T) {
|
|
|
|
test := responseTest{
|
|
|
|
caFile: "testdata/ca.crt",
|
|
|
|
respFile: "testdata/two-assertions-first-signed.xml",
|
|
|
|
now: "2017-04-04T04:34:59.330Z",
|
|
|
|
usernameAttr: "Name",
|
|
|
|
emailAttr: "email",
|
|
|
|
inResponseTo: "6zmm5mguyebwvajyf2sdwwcw6m",
|
|
|
|
redirectURI: "http://127.0.0.1:5556/dex/callback",
|
|
|
|
wantIdent: connector.Identity{
|
|
|
|
UserID: "eric.chiang+okta@coreos.com",
|
|
|
|
Username: "Eric",
|
|
|
|
Email: "eric.chiang+okta@coreos.com",
|
|
|
|
EmailVerified: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
test.run(t)
|
|
|
|
}
|
2016-12-21 06:54:17 +05:30
|
|
|
|
2018-02-28 13:12:17 +05:30
|
|
|
func TestTamperedResponseNameID(t *testing.T) {
|
|
|
|
test := responseTest{
|
|
|
|
caFile: "testdata/ca.crt",
|
|
|
|
respFile: "testdata/tampered-resp.xml",
|
|
|
|
now: "2017-04-04T04:34:59.330Z",
|
|
|
|
usernameAttr: "Name",
|
|
|
|
emailAttr: "email",
|
|
|
|
inResponseTo: "6zmm5mguyebwvajyf2sdwwcw6m",
|
|
|
|
redirectURI: "http://127.0.0.1:5556/dex/callback",
|
|
|
|
wantErr: true,
|
|
|
|
}
|
|
|
|
test.run(t)
|
|
|
|
}
|
|
|
|
|
2016-12-21 06:54:17 +05:30
|
|
|
func loadCert(ca string) (*x509.Certificate, error) {
|
|
|
|
data, err := ioutil.ReadFile(ca)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
block, _ := pem.Decode(data)
|
|
|
|
if block == nil {
|
|
|
|
return nil, errors.New("ca file didn't contain any PEM data")
|
|
|
|
}
|
|
|
|
return x509.ParseCertificate(block.Bytes)
|
|
|
|
}
|
|
|
|
|
2017-04-04 13:11:12 +05:30
|
|
|
func (r responseTest) run(t *testing.T) {
|
|
|
|
c := Config{
|
|
|
|
CA: r.caFile,
|
|
|
|
UsernameAttr: r.usernameAttr,
|
|
|
|
EmailAttr: r.emailAttr,
|
|
|
|
GroupsAttr: r.groupsAttr,
|
|
|
|
RedirectURI: r.redirectURI,
|
2017-04-07 05:41:28 +05:30
|
|
|
EntityIssuer: r.entityIssuer,
|
2017-04-04 13:11:12 +05:30
|
|
|
// Never logging in, don't need this.
|
|
|
|
SSOURL: "http://foo.bar/",
|
|
|
|
}
|
|
|
|
now, err := time.Parse(timeFormat, r.now)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("parse test time: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := c.openConnector(logrus.New())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
conn.now = func() time.Time { return now }
|
|
|
|
resp, err := ioutil.ReadFile(r.respFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
samlResp := base64.StdEncoding.EncodeToString(resp)
|
|
|
|
|
|
|
|
scopes := connector.Scopes{
|
|
|
|
OfflineAccess: false,
|
|
|
|
Groups: true,
|
|
|
|
}
|
|
|
|
ident, err := conn.HandlePOST(scopes, samlResp, r.inResponseTo)
|
|
|
|
if err != nil {
|
|
|
|
if !r.wantErr {
|
|
|
|
t.Fatalf("handle response: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.wantErr {
|
|
|
|
t.Fatalf("wanted error")
|
|
|
|
}
|
|
|
|
sort.Strings(ident.Groups)
|
|
|
|
sort.Strings(r.wantIdent.Groups)
|
|
|
|
if diff := pretty.Compare(ident, r.wantIdent); diff != "" {
|
|
|
|
t.Error(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2017-03-29 06:36:49 +05:30
|
|
|
defaultSSOIssuer = "http://www.okta.com/exk91cb99lKkKSYoy0h7"
|
2017-04-04 13:11:12 +05:30
|
|
|
defaultRedirectURI = "http://localhost:5556/dex/callback"
|
|
|
|
|
|
|
|
// Response ID embedded in our testdata.
|
|
|
|
testDataResponseID = "_fd1b3ef9-ec09-44a7-a66b-0d39c250f6a0"
|
|
|
|
)
|
|
|
|
|
2017-03-29 06:36:49 +05:30
|
|
|
// Deprecated: Use testing framework established above.
|
2017-01-26 23:35:40 +05:30
|
|
|
func runVerify(t *testing.T, ca string, resp string, shouldSucceed bool) {
|
|
|
|
cert, err := loadCert(ca)
|
2016-12-21 06:54:17 +05:30
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s := certStore{[]*x509.Certificate{cert}}
|
|
|
|
|
2017-01-26 23:35:40 +05:30
|
|
|
validator := dsig.NewDefaultValidationContext(s)
|
2016-12-21 06:54:17 +05:30
|
|
|
|
2017-01-26 23:35:40 +05:30
|
|
|
data, err := ioutil.ReadFile(resp)
|
2016-12-21 06:54:17 +05:30
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-04-04 13:11:12 +05:30
|
|
|
if _, _, err := verifyResponseSig(validator, data); err != nil {
|
2017-01-26 23:35:40 +05:30
|
|
|
if shouldSucceed {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !shouldSucceed {
|
|
|
|
t.Fatalf("expected an invalid signatrue but verification has been successful")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 06:36:49 +05:30
|
|
|
// Deprecated: Use testing framework established above.
|
|
|
|
func newProvider(ssoIssuer string, redirectURI string) *provider {
|
|
|
|
if ssoIssuer == "" {
|
|
|
|
ssoIssuer = defaultSSOIssuer
|
2017-01-26 23:35:40 +05:30
|
|
|
}
|
|
|
|
if redirectURI == "" {
|
|
|
|
redirectURI = defaultRedirectURI
|
|
|
|
}
|
|
|
|
now, _ := time.Parse(time.RFC3339, "2017-01-24T20:48:41Z")
|
|
|
|
timeFunc := func() time.Time { return now }
|
|
|
|
return &provider{
|
2017-03-29 06:36:49 +05:30
|
|
|
ssoIssuer: ssoIssuer,
|
2017-01-26 23:35:40 +05:30
|
|
|
ssoURL: "http://idp.org/saml/sso",
|
|
|
|
now: timeFunc,
|
|
|
|
usernameAttr: "user",
|
|
|
|
emailAttr: "email",
|
|
|
|
redirectURI: redirectURI,
|
|
|
|
logger: logrus.New(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerify(t *testing.T) {
|
|
|
|
runVerify(t, "testdata/okta-ca.pem", "testdata/okta-resp.xml", true)
|
|
|
|
}
|
|
|
|
|
2017-03-23 05:26:25 +05:30
|
|
|
func TestVerifyUnsignedMessageAndSignedAssertionWithRootXmlNs(t *testing.T) {
|
|
|
|
runVerify(t, "testdata/oam-ca.pem", "testdata/oam-resp.xml", true)
|
|
|
|
}
|
|
|
|
|
2017-01-26 23:35:40 +05:30
|
|
|
func TestVerifySignedMessageAndUnsignedAssertion(t *testing.T) {
|
|
|
|
runVerify(t, "testdata/idp-cert.pem", "testdata/idp-resp-signed-message.xml", true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerifyUnsignedMessageAndSignedAssertion(t *testing.T) {
|
|
|
|
runVerify(t, "testdata/idp-cert.pem", "testdata/idp-resp-signed-assertion.xml", true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerifySignedMessageAndSignedAssertion(t *testing.T) {
|
|
|
|
runVerify(t, "testdata/idp-cert.pem", "testdata/idp-resp-signed-message-and-assertion.xml", true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerifyUnsignedMessageAndUnsignedAssertion(t *testing.T) {
|
|
|
|
runVerify(t, "testdata/idp-cert.pem", "testdata/idp-resp.xml", false)
|
|
|
|
}
|