Skip to content

Commit

Permalink
fix attribute importer and user template mapper for Facebook/Google (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Photonios authored Apr 9, 2021
1 parent 60db2f1 commit 483fa37
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions keycloak/identity_provider_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

type IdentityProviderMapperConfig struct {
UserAttribute string `json:"user.attribute,omitempty"`
UserAttributeName string `json:"userAttribute,omitempty"`
Claim string `json:"claim,omitempty"`
ClaimValue string `json:"claim.value,omitempty"`
HardcodedAttribute string `json:"attribute,omitempty"`
Expand All @@ -19,6 +20,7 @@ type IdentityProviderMapperConfig struct {
AttributeFriendlyName string `json:"attribute.friendly.name,omitempty"`
Template string `json:"template,omitempty"`
Role string `json:"role,omitempty"`
JsonField string `json:"jsonField,omitEmpty"`
ExtraConfig map[string]interface{} `json:"-"`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ func getAttributeImporterIdentityProviderMapperFromData(data *schema.ResourceDat
if _, ok := data.GetOk("claim_name"); !ok {
return nil, fmt.Errorf(`provider.keycloak: keycloak_attribute_importer_identity_provider_mapper: %s: "claim_name": should be set for %s identity provider`, data.Get("name").(string), identityProvider.ProviderId)
}

rec.Config.Claim = data.Get("claim_name").(string)
} else if identityProvider.ProviderId == "facebook" || identityProvider.ProviderId == "google" {
rec.IdentityProviderMapper = fmt.Sprintf("%s-user-attribute-mapper", identityProvider.ProviderId)
rec.Config.JsonField = data.Get("claim_name").(string)
rec.Config.UserAttributeName = data.Get("user_attribute").(string)
} else {
return nil, fmt.Errorf(`provider.keycloak: keycloak_attribute_importer_identity_provider_mapper: %s: "%s" identity provider is not supported yet`, data.Get("name").(string), identityProvider.ProviderId)
}
Expand All @@ -79,10 +84,16 @@ func getAttributeImporterIdentityProviderMapperFromData(data *schema.ResourceDat

func setAttributeImporterIdentityProviderMapperData(data *schema.ResourceData, identityProviderMapper *keycloak.IdentityProviderMapper) error {
setIdentityProviderMapperData(data, identityProviderMapper)

claimName := identityProviderMapper.Config.Claim
if claimName == "" {
claimName = identityProviderMapper.Config.JsonField
}

data.Set("attribute_name", identityProviderMapper.Config.Attribute)
data.Set("user_attribute", identityProviderMapper.Config.UserAttribute)
data.Set("attribute_friendly_name", identityProviderMapper.Config.AttributeFriendlyName)
data.Set("claim_name", identityProviderMapper.Config.Claim)
data.Set("claim_name", claimName)
data.Set("extra_config", identityProviderMapper.Config.ExtraConfig)
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ func getUserTemplateImporterIdentityProviderMapperFromData(data *schema.Resource
if err != nil {
return nil, handleNotFoundError(err, data)
}
rec.IdentityProviderMapper = fmt.Sprintf("%s-username-idp-mapper", identityProvider.ProviderId)

if identityProvider.ProviderId == "facebook" || identityProvider.ProviderId == "google" {
rec.IdentityProviderMapper = "oidc-username-idp-mapper"
} else {
rec.IdentityProviderMapper = fmt.Sprintf("%s-username-idp-mapper", identityProvider.ProviderId)
}

rec.Config = &keycloak.IdentityProviderMapperConfig{
Template: data.Get("template").(string),
ExtraConfig: extraConfig,
Expand Down

0 comments on commit 483fa37

Please sign in to comment.