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

add Aggregate attribute values to User Attribute mapper #272

Merged
merged 5 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ The following arguments are supported:
- `add_to_id_token` - (Optional) Indicates if the attribute should be added as a claim to the id token. Defaults to `true`.
- `add_to_access_token` - (Optional) Indicates if the attribute should be added as a claim to the access token. Defaults to `true`.
- `add_to_userinfo` - (Optional) Indicates if the attribute should be added as a claim to the UserInfo response body. Defaults to `true`.

- `aggregate_attrs`- (Optional) Indicates whether this attribute is a single value or an array of values. Defaults to `false`.
### Import

Protocol mappers can be imported using one of the following formats:
Expand Down
32 changes: 20 additions & 12 deletions keycloak/openid_user_attribute_protocol_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ type OpenIdUserAttributeProtocolMapper struct {
ClaimName string
ClaimValueType string

Multivalued bool // indicates whether is this an array of attributes or a single attribute
Multivalued bool // indicates whether is this an array of attributes or a single attribute
AggregateAttributeValues bool
}

func (mapper *OpenIdUserAttributeProtocolMapper) convertToGenericProtocolMapper() *protocolMapper {
Expand All @@ -30,13 +31,14 @@ func (mapper *OpenIdUserAttributeProtocolMapper) convertToGenericProtocolMapper(
Protocol: "openid-connect",
ProtocolMapper: "oidc-usermodel-attribute-mapper",
Config: map[string]string{
addToIdTokenField: strconv.FormatBool(mapper.AddToIdToken),
addToAccessTokenField: strconv.FormatBool(mapper.AddToAccessToken),
addToUserInfoField: strconv.FormatBool(mapper.AddToUserInfo),
userAttributeField: mapper.UserAttribute,
claimNameField: mapper.ClaimName,
claimValueTypeField: mapper.ClaimValueType,
multivaluedField: strconv.FormatBool(mapper.Multivalued),
addToIdTokenField: strconv.FormatBool(mapper.AddToIdToken),
addToAccessTokenField: strconv.FormatBool(mapper.AddToAccessToken),
addToUserInfoField: strconv.FormatBool(mapper.AddToUserInfo),
userAttributeField: mapper.UserAttribute,
claimNameField: mapper.ClaimName,
claimValueTypeField: mapper.ClaimValueType,
multivaluedField: strconv.FormatBool(mapper.Multivalued),
aggregateAttributeValuesField: strconv.FormatBool(mapper.AggregateAttributeValues),
},
}
}
Expand All @@ -63,6 +65,11 @@ func (protocolMapper *protocolMapper) convertToOpenIdUserAttributeProtocolMapper
return nil, err
}

aggregateAttributeValues, err := strconv.ParseBool(protocolMapper.Config[aggregateAttributeValuesField])
if err != nil {
return nil, err
}

return &OpenIdUserAttributeProtocolMapper{
Id: protocolMapper.Id,
Name: protocolMapper.Name,
Expand All @@ -74,10 +81,11 @@ func (protocolMapper *protocolMapper) convertToOpenIdUserAttributeProtocolMapper
AddToAccessToken: addToAccessToken,
AddToUserInfo: addToUserInfo,

UserAttribute: protocolMapper.Config[userAttributeField],
ClaimName: protocolMapper.Config[claimNameField],
ClaimValueType: protocolMapper.Config[claimValueTypeField],
Multivalued: multivalued,
UserAttribute: protocolMapper.Config[userAttributeField],
ClaimName: protocolMapper.Config[claimNameField],
ClaimValueType: protocolMapper.Config[claimValueTypeField],
Multivalued: multivalued,
AggregateAttributeValues: aggregateAttributeValues,
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions keycloak/protocol_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
userAttributeField = "user.attribute"
userPropertyField = "user.attribute"
userRealmRoleMappingRolePrefixField = "usermodel.realmRoleMapping.rolePrefix"
aggregateAttributeValuesField = "aggregate.attrs"
)

func protocolMapperPath(realmId, clientId, clientScopeId string) string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ func resourceKeycloakOpenIdUserAttributeProtocolMapper() *schema.Resource {
Default: "String",
ValidateFunc: validation.StringInSlice([]string{"JSON", "String", "long", "int", "boolean"}, true),
},
"aggregate_attrs": {
arminfelder marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Indicates if attribute values should be aggregated within the group attributes",
},
},
}
}
Expand All @@ -99,10 +105,11 @@ func mapFromDataToOpenIdUserAttributeProtocolMapper(data *schema.ResourceData) *
AddToAccessToken: data.Get("add_to_access_token").(bool),
AddToUserInfo: data.Get("add_to_userinfo").(bool),

UserAttribute: data.Get("user_attribute").(string),
ClaimName: data.Get("claim_name").(string),
ClaimValueType: data.Get("claim_value_type").(string),
Multivalued: data.Get("multivalued").(bool),
UserAttribute: data.Get("user_attribute").(string),
ClaimName: data.Get("claim_name").(string),
ClaimValueType: data.Get("claim_value_type").(string),
Multivalued: data.Get("multivalued").(bool),
AggregateAttributeValues: data.Get("aggregate_attrs").(bool),
}
}

Expand All @@ -124,6 +131,7 @@ func mapFromOpenIdUserAttributeMapperToData(mapper *keycloak.OpenIdUserAttribute
data.Set("claim_name", mapper.ClaimName)
data.Set("claim_value_type", mapper.ClaimValueType)
data.Set("multivalued", mapper.Multivalued)
data.Set("aggregate_attrs", mapper.AggregateAttributeValues)
}

func resourceKeycloakOpenIdUserAttributeProtocolMapperCreate(data *schema.ResourceData, meta interface{}) error {
Expand Down