Skip to content

Commit

Permalink
feat: Add aggregate attributes flag to keycloak_saml_user_attribute_p…
Browse files Browse the repository at this point in the history
…rotocol_mapper (keycloak#942)

Signed-off-by: Sven-Torben Janus <[email protected]>
  • Loading branch information
sventorben committed Mar 26, 2024
1 parent 3f6b75b commit ee72032
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
1 change: 1 addition & 0 deletions docs/resources/saml_user_attribute_protocol_mapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ resource "keycloak_saml_user_attribute_protocol_mapper" "saml_user_attribute_map
- `client_id` - (Optional) The client this protocol mapper should be attached to. Conflicts with `client_scope_id`. One of `client_id` or `client_scope_id` must be specified.
- `client_scope_id` - (Optional) The client scope this protocol mapper should be attached to. Conflicts with `client_id`. One of `client_id` or `client_scope_id` must be specified.
- `friendly_name` - (Optional) An optional human-friendly name for this attribute.
- `aggregate_attributes`- (Optional) Indicates whether this attribute is a single value or an array of values. Defaults to `false`.

## Import

Expand Down
39 changes: 24 additions & 15 deletions keycloak/saml_user_attribute_protocol_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keycloak
import (
"context"
"fmt"
"strconv"
)

type SamlUserAttributeProtocolMapper struct {
Expand All @@ -12,10 +13,11 @@ type SamlUserAttributeProtocolMapper struct {
ClientId string
ClientScopeId string

UserAttribute string
FriendlyName string
SamlAttributeName string
SamlAttributeNameFormat string
UserAttribute string
FriendlyName string
SamlAttributeName string
SamlAttributeNameFormat string
AggregateAttributeValues bool
}

func (mapper *SamlUserAttributeProtocolMapper) convertToGenericProtocolMapper() *protocolMapper {
Expand All @@ -25,27 +27,34 @@ func (mapper *SamlUserAttributeProtocolMapper) convertToGenericProtocolMapper()
Protocol: "saml",
ProtocolMapper: "saml-user-attribute-mapper",
Config: map[string]string{
attributeNameField: mapper.SamlAttributeName,
attributeNameFormatField: mapper.SamlAttributeNameFormat,
friendlyNameField: mapper.FriendlyName,
userAttributeField: mapper.UserAttribute,
attributeNameField: mapper.SamlAttributeName,
attributeNameFormatField: mapper.SamlAttributeNameFormat,
friendlyNameField: mapper.FriendlyName,
userAttributeField: mapper.UserAttribute,
aggregateAttributeValuesField: strconv.FormatBool(mapper.AggregateAttributeValues),
},
}
}

func (protocolMapper *protocolMapper) convertToSamlUserAttributeProtocolMapper(realmId, clientId, clientScopeId string) *SamlUserAttributeProtocolMapper {
func (protocolMapper *protocolMapper) convertToSamlUserAttributeProtocolMapper(realmId, clientId, clientScopeId string) (*SamlUserAttributeProtocolMapper, error) {
aggregateAttributeValues, err := parseBoolAndTreatEmptyStringAsFalse(protocolMapper.Config[addToAccessTokenField])
if err != nil {
return nil, err
}

return &SamlUserAttributeProtocolMapper{
Id: protocolMapper.Id,
Name: protocolMapper.Name,
RealmId: realmId,
ClientId: clientId,
ClientScopeId: clientScopeId,

UserAttribute: protocolMapper.Config[userAttributeField],
FriendlyName: protocolMapper.Config[friendlyNameField],
SamlAttributeName: protocolMapper.Config[attributeNameField],
SamlAttributeNameFormat: protocolMapper.Config[attributeNameFormatField],
}
UserAttribute: protocolMapper.Config[userAttributeField],
FriendlyName: protocolMapper.Config[friendlyNameField],
SamlAttributeName: protocolMapper.Config[attributeNameField],
SamlAttributeNameFormat: protocolMapper.Config[attributeNameFormatField],
AggregateAttributeValues: aggregateAttributeValues,
}, nil
}

func (keycloakClient *KeycloakClient) GetSamlUserAttributeProtocolMapper(ctx context.Context, realmId, clientId, clientScopeId, mapperId string) (*SamlUserAttributeProtocolMapper, error) {
Expand All @@ -56,7 +65,7 @@ func (keycloakClient *KeycloakClient) GetSamlUserAttributeProtocolMapper(ctx con
return nil, err
}

return protocolMapper.convertToSamlUserAttributeProtocolMapper(realmId, clientId, clientScopeId), nil
return protocolMapper.convertToSamlUserAttributeProtocolMapper(realmId, clientId, clientScopeId)
}

func (keycloakClient *KeycloakClient) DeleteSamlUserAttributeProtocolMapper(ctx context.Context, realmId, clientId, clientScopeId, mapperId string) error {
Expand Down
16 changes: 12 additions & 4 deletions provider/resource_keycloak_saml_user_attribute_protocol_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func resourceKeycloakSamlUserAttributeProtocolMapper() *schema.Resource {
Required: true,
ValidateFunc: validation.StringInSlice(keycloakSamlUserAttributeProtocolMapperNameFormats, false),
},
"aggregate_attributes": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Indicates if attribute values should be aggregated within the group attributes",
},
},
}
}
Expand All @@ -74,10 +80,11 @@ func mapFromDataToSamlUserAttributeProtocolMapper(data *schema.ResourceData) *ke
ClientId: data.Get("client_id").(string),
ClientScopeId: data.Get("client_scope_id").(string),

UserAttribute: data.Get("user_attribute").(string),
FriendlyName: data.Get("friendly_name").(string),
SamlAttributeName: data.Get("saml_attribute_name").(string),
SamlAttributeNameFormat: data.Get("saml_attribute_name_format").(string),
UserAttribute: data.Get("user_attribute").(string),
FriendlyName: data.Get("friendly_name").(string),
SamlAttributeName: data.Get("saml_attribute_name").(string),
SamlAttributeNameFormat: data.Get("saml_attribute_name_format").(string),
AggregateAttributeValues: data.Get("aggregate_attributes").(bool),
}
}

Expand All @@ -96,6 +103,7 @@ func mapFromSamlUserAttributeMapperToData(mapper *keycloak.SamlUserAttributeProt
data.Set("friendly_name", mapper.FriendlyName)
data.Set("saml_attribute_name", mapper.SamlAttributeName)
data.Set("saml_attribute_name_format", mapper.SamlAttributeNameFormat)
data.Set("aggregate_attributes", mapper.AggregateAttributeValues)
}

func resourceKeycloakSamlUserAttributeProtocolMapperCreate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down

0 comments on commit ee72032

Please sign in to comment.