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

saml refinement: NameIDFormat #217

Merged
merged 2 commits into from
May 6, 2021
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
29 changes: 24 additions & 5 deletions eval/lib/saml.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
cs "github.com/avenga/couper/config/saml"
)

const FnSamlSsoUrl = "saml_sso_url"
const (
FnSamlSsoUrl = "saml_sso_url"
NameIdFormatUnspecified = "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified"
)

func NewSamlSsoUrlFunction(samlConfigs []*cs.SAML) function.Function {
samls := make(map[string]*cs.SAML)
Expand Down Expand Up @@ -55,10 +58,7 @@ func NewSamlSsoUrlFunction(samlConfigs []*cs.SAML) function.Function {
}
}

nameIDFormat := ""
if len(metadata.IDPSSODescriptor.NameIDFormats) > 0 {
nameIDFormat = metadata.IDPSSODescriptor.NameIDFormats[0].Value
}
nameIDFormat := getNameIDFormat(metadata.IDPSSODescriptor.NameIDFormats)

sp := &saml2.SAMLServiceProvider{
AssertionConsumerServiceURL: saml.SpAcsUrl,
Expand All @@ -79,3 +79,22 @@ func NewSamlSsoUrlFunction(samlConfigs []*cs.SAML) function.Function {
},
})
}

func getNameIDFormat(supportedNameIDFormats []types.NameIDFormat) string {
nameIDFormat := ""
if isSupportedNameIDFormat(supportedNameIDFormats, NameIdFormatUnspecified) {
nameIDFormat = NameIdFormatUnspecified
} else if len(supportedNameIDFormats) > 0 {
nameIDFormat = supportedNameIDFormats[0].Value
}
return nameIDFormat
}

func isSupportedNameIDFormat(supportedNameIDFormats []types.NameIDFormat, nameIDFormat string) bool {
for _, n := range supportedNameIDFormats {
if n.Value == nameIDFormat {
return true
}
}
return false
}
58 changes: 58 additions & 0 deletions eval/lib/saml_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package lib

import (
"testing"

"github.com/russellhaering/gosaml2/types"
)

func Test_getNameIDFormat(t *testing.T) {
tests := []struct {
name string
supportedFormats []types.NameIDFormat
wantFormat string
}{
{
"only unspecified",
[]types.NameIDFormat{
{Value: "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified"},
},
"urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified",
},
{
"unspecified 1st",
[]types.NameIDFormat{
{Value: "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified"},
{Value: "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"},
{Value: "urn:oasis:names:tc:SAML:2.0:nameid-format:persistent"},
},
"urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified",
},
{
"unspecified 2nd",
[]types.NameIDFormat{
{Value: "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"},
{Value: "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified"},
{Value: "urn:oasis:names:tc:SAML:2.0:nameid-format:persistent"},
},
"urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified",
},
{
"no unspecified",
[]types.NameIDFormat{
{Value: "urn:oasis:names:tc:SAML:2.0:nameid-format:transient"},
{Value: "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"},
{Value: "urn:oasis:names:tc:SAML:2.0:nameid-format:persistent"},
},
"urn:oasis:names:tc:SAML:2.0:nameid-format:transient",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
format := getNameIDFormat(tt.supportedFormats)
if format != tt.wantFormat {
t.Errorf("Expected format %q, got: %#v", tt.wantFormat, format)
}
})
}
}