Skip to content

Commit

Permalink
SAMLProvider constant and add configurable name and email claims
Browse files Browse the repository at this point in the history
  • Loading branch information
joshblakeley committed May 25, 2020
1 parent cd6b930 commit 437eb19
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
8 changes: 4 additions & 4 deletions constants/constants.go
Expand Up @@ -7,8 +7,8 @@ const (

//providers
const (
SocialProvider = "SocialProvider"
ADProvider = "ADProvider"
ProxyProvider = "ProxyProvider"
SocialProvider = "SocialProvider"
ADProvider = "ADProvider"
ProxyProvider = "ProxyProvider"
SAMLProvider = "SAMLProvider"
)

27 changes: 23 additions & 4 deletions providers/saml.go
Expand Up @@ -43,6 +43,9 @@ type SAMLConfig struct {
SAMLBaseURL string
ForceAuthentication bool
SAMLBinding string
SAMLEmailClaim string
SAMLForenameClaim string
SAMLSurnameClaim string
}

func (s *SAMLProvider) Init(handler tap.IdentityHandler, profile tap.Profile, config []byte) error {
Expand Down Expand Up @@ -248,16 +251,32 @@ func (s *SAMLProvider) HandleCallback(w http.ResponseWriter, r *http.Request, on
}

//this is going to be a nightmare of slight differences between IDPs
// so lets make it configurable with a sensible backup
var email string
name := rawData["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname"].(string) + " " +
rawData["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname"].(string)
emailClaim := s.config.SAMLEmailClaim
if emailClaim == "" {
emailClaim = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"
}

if _, ok := rawData["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"]; ok {
email = rawData["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"].(string)
if _, ok := rawData[emailClaim]; ok {
email = rawData[emailClaim].(string)
} else if _, ok := rawData["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/"]; ok {
email = rawData["http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"].(string)
}

givenNameClaim := s.config.SAMLForenameClaim
surnameClaim := s.config.SAMLSurnameClaim

if givenNameClaim == "" {
givenNameClaim = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname"
}

if surnameClaim == "" {
surnameClaim = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname"
}
name := rawData[givenNameClaim].(string) + " " +
rawData[surnameClaim].(string)

thisUser := goth.User{
UserID: name,
Email: email,
Expand Down
2 changes: 1 addition & 1 deletion providers/tapProvider.go
Expand Up @@ -22,7 +22,7 @@ func GetTAProvider(conf tap.Profile, handler tyk.TykAPI, identityKeyStore tap.Au
thisProvider = &ADProvider{}
case constants.ProxyProvider:
thisProvider = &ProxyProvider{}
case "SAMLProvider":
case constants.SAMLProvider:
thisProvider = &SAMLProvider{}
default:
return nil, errors.New("invalid provider name")
Expand Down

0 comments on commit 437eb19

Please sign in to comment.