Skip to content

Commit

Permalink
fix missing session note for keycloak_openid_user_session_note_protoc…
Browse files Browse the repository at this point in the history
…ol_mapper, deprecate session_note_label attribute (keycloak#365)
  • Loading branch information
hcl31415 committed Oct 12, 2020
1 parent eea39e5 commit 4b6c523
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ resource "keycloak_openid_user_session_note_protocol_mapper" "user_session_note_
client_id = keycloak_openid_client.openid_client.id
claim_name = "foo"
claim_value_type = "String"
session_note_label = "bar"
session_note = "bar"
add_to_id_token = true
add_to_access_token = false
}
Expand All @@ -54,7 +54,7 @@ resource "keycloak_openid_user_session_note_protocol_mapper" "user_session_note_
client_scope_id = keycloak_openid_client_scope.client_scope.id
claim_name = "foo"
claim_value_type = "String"
session_note_label = "bar"
session_note = "bar"
add_to_id_token = true
add_to_access_token = false
}
Expand All @@ -70,7 +70,8 @@ The following arguments are supported:
- `name` - (Required) The display name of this protocol mapper in the GUI.
- `claim_name` - (Required) The name of the claim to insert into a token.
- `claim_value_type` - (Optional) The claim type used when serializing JSON tokens. Can be one of `String`, `JSON`, `long`, `int`, or `boolean`. Defaults to `String`.
- `session_note_label` - (Optional) String value being the name of stored user session note within the UserSessionModel.note map.
- `session_note` - (Optional) String value being the name of stored user session note within the UserSessionModel.note map.
- `session_note_label` - (Optional) **Deprecated** Use `session_note` instead.
- `add_to_id_token` - (Optional) Indicates if the property should be added as a claim to the id token. Defaults to `true`.
- `add_to_access_token` - (Optional) Indicates if the property should be added as a claim to the access token. Defaults to `true`.

Expand Down
7 changes: 4 additions & 3 deletions docs/resources/openid_user_session_note_protocol_mapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ resource "keycloak_openid_user_session_note_protocol_mapper" "user_session_note_
claim_name = "foo"
claim_value_type = "String"
session_note_label = "bar"
session_note = "bar"
}
```

Expand All @@ -63,7 +63,7 @@ resource "keycloak_openid_user_session_note_protocol_mapper" "user_session_note_
claim_name = "foo"
claim_value_type = "String"
session_note_label = "bar"
session_note = "bar"
}
```

Expand All @@ -75,7 +75,8 @@ resource "keycloak_openid_user_session_note_protocol_mapper" "user_session_note_
- `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.
- `claim_value_type` - (Optional) The claim type used when serializing JSON tokens. Can be one of `String`, `JSON`, `long`, `int`, or `boolean`. Defaults to `String`.
- `session_note_label` - (Optional) String value being the name of stored user session note within the UserSessionModel.note map.
- `session_note` - (Optional) String value being the name of stored user session note within the UserSessionModel.note map.
- `session_note_label` - (Optional) **Deprecated** Use `session_note` instead.
- `add_to_id_token` - (Optional) Indicates if the property should be added as a claim to the id token. Defaults to `true`.
- `add_to_access_token` - (Optional) Indicates if the property should be added as a claim to the access token. Defaults to `true`.

Expand Down
4 changes: 2 additions & 2 deletions example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ resource "keycloak_openid_user_session_note_protocol_mapper" "user_session_note_

claim_name = "foo"
claim_value_type = "String"
session_note_label = "bar"
session_note = "bar"

add_to_id_token = true
add_to_access_token = false
Expand All @@ -482,7 +482,7 @@ resource "keycloak_openid_user_session_note_protocol_mapper" "user_session_note_

claim_name = "foo2"
claim_value_type = "String"
session_note_label = "bar2"
session_note = "bar2"

add_to_id_token = true
add_to_access_token = false
Expand Down
22 changes: 11 additions & 11 deletions keycloak/openid_user_session_note_protocol_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ type OpenIdUserSessionNoteProtocolMapper struct {
AddToIdToken bool
AddToAccessToken bool

ClaimName string
ClaimValueType string
UserSessionNoteLabel string
ClaimName string
ClaimValueType string
UserSessionNote string
}

func (mapper *OpenIdUserSessionNoteProtocolMapper) convertToGenericProtocolMapper() *protocolMapper {
Expand All @@ -27,11 +27,11 @@ func (mapper *OpenIdUserSessionNoteProtocolMapper) convertToGenericProtocolMappe
Protocol: "openid-connect",
ProtocolMapper: "oidc-usersessionmodel-note-mapper",
Config: map[string]string{
addToIdTokenField: strconv.FormatBool(mapper.AddToIdToken),
addToAccessTokenField: strconv.FormatBool(mapper.AddToAccessToken),
claimNameField: mapper.ClaimName,
claimValueTypeField: mapper.ClaimValueType,
userSessionModelNoteLabelField: mapper.UserSessionNoteLabel,
addToIdTokenField: strconv.FormatBool(mapper.AddToIdToken),
addToAccessTokenField: strconv.FormatBool(mapper.AddToAccessToken),
claimNameField: mapper.ClaimName,
claimValueTypeField: mapper.ClaimValueType,
userSessionNoteField: mapper.UserSessionNote,
},
}
}
Expand All @@ -57,9 +57,9 @@ func (protocolMapper *protocolMapper) convertToOpenIdUserSessionNoteProtocolMapp
AddToIdToken: addToIdToken,
AddToAccessToken: addToAccessToken,

ClaimName: protocolMapper.Config[claimNameField],
ClaimValueType: protocolMapper.Config[claimValueTypeField],
UserSessionNoteLabel: protocolMapper.Config[userSessionModelNoteLabelField],
ClaimName: protocolMapper.Config[claimNameField],
ClaimValueType: protocolMapper.Config[claimValueTypeField],
UserSessionNote: protocolMapper.Config[userSessionNoteField],
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion keycloak/protocol_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var (
userRealmRoleMappingRolePrefixField = "usermodel.realmRoleMapping.rolePrefix"
userClientRoleMappingClientIdField = "usermodel.clientRoleMapping.clientId"
userClientRoleMappingRolePrefixField = "usermodel.clientRoleMapping.rolePrefix"
userSessionModelNoteLabelField = "userSession.modelNote.label"
userSessionNoteField = "user.session.note"
aggregateAttributeValuesField = "aggregate.attrs"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,30 @@ func resourceKeycloakOpenIdUserSessionNoteProtocolMapper() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{"JSON", "String", "long", "int", "boolean"}, true),
},
"session_note_label": {
Type: schema.TypeString,
Optional: true,
Description: "String value being the name of stored user session note within the UserSessionModel.note map.",
Type: schema.TypeString,
Optional: true,
Deprecated: "use session_note instead",
ConflictsWith: []string{"session_note"},
Description: "String value being the name of stored user session note within the UserSessionModel.note map.",
},
"session_note": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"session_note_label"},
Description: "String value being the name of stored user session note within the UserSessionModel.note map.",
},
},
}
}

func mapFromDataToOpenIdUserSessionNoteProtocolMapper(data *schema.ResourceData) *keycloak.OpenIdUserSessionNoteProtocolMapper {
var sessionNote string
if s, ok := data.GetOk("session_note_label"); ok {
sessionNote = s.(string)
} else {
sessionNote = data.Get("session_note").(string)
}

return &keycloak.OpenIdUserSessionNoteProtocolMapper{
Id: data.Id(),
Name: data.Get("name").(string),
Expand All @@ -87,9 +102,9 @@ func mapFromDataToOpenIdUserSessionNoteProtocolMapper(data *schema.ResourceData)
AddToIdToken: data.Get("add_to_id_token").(bool),
AddToAccessToken: data.Get("add_to_access_token").(bool),

ClaimName: data.Get("claim_name").(string),
ClaimValueType: data.Get("claim_value_type").(string),
UserSessionNoteLabel: data.Get("session_note_label").(string),
ClaimName: data.Get("claim_name").(string),
ClaimValueType: data.Get("claim_value_type").(string),
UserSessionNote: sessionNote,
}
}

Expand All @@ -108,7 +123,12 @@ func mapFromOpenIdUserSessionNoteMapperToData(mapper *keycloak.OpenIdUserSession
data.Set("add_to_access_token", mapper.AddToAccessToken)
data.Set("claim_name", mapper.ClaimName)
data.Set("claim_value_type", mapper.ClaimValueType)
data.Set("session_note_label", mapper.UserSessionNoteLabel)

if _, ok := data.GetOk("session_note_label"); ok {
data.Set("session_note_label", mapper.UserSessionNote)
} else {
data.Set("session_note", mapper.UserSessionNote)
}
}

func resourceKeycloakOpenIdUserSessionNoteProtocolMapperCreate(data *schema.ResourceData, meta interface{}) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ func TestAccKeycloakOpenIdUserSessionNoteProtocolMapper_updateClaim(t *testing.T
})
}

func TestAccKeycloakOpenIdUserSessionNoteProtocolMapper_updateLabel(t *testing.T) {
func TestAccKeycloakOpenIdUserSessionNoteProtocolMapper_updateNote(t *testing.T) {
realmName := "terraform-realm-" + acctest.RandString(10)
clientId := "terraform-client-" + acctest.RandString(10)
mapperName := "terraform-openid-connect-user-session-note-mapper-" + acctest.RandString(5)

labelName := "session-note-label-" + acctest.RandString(10)
updatedLabelName := "session-note-label-update-" + acctest.RandString(10)
noteName := "session-note-" + acctest.RandString(10)
updatedNoteName := "session-note-update-" + acctest.RandString(10)

resourceName := "keycloak_openid_user_session_note_protocol_mapper.user_session_note_mapper"

Expand All @@ -131,11 +131,11 @@ func TestAccKeycloakOpenIdUserSessionNoteProtocolMapper_updateLabel(t *testing.T
CheckDestroy: testAccKeycloakOpenIdUserSessionNoteProtocolMapperDestroy(),
Steps: []resource.TestStep{
{
Config: testKeycloakOpenIdUserSessionNoteProtocolMapper_label(realmName, clientId, mapperName, labelName),
Config: testKeycloakOpenIdUserSessionNoteProtocolMapper_note(realmName, clientId, mapperName, noteName),
Check: testKeycloakOpenIdUserSessionNoteProtocolMapperExists(resourceName),
},
{
Config: testKeycloakOpenIdUserSessionNoteProtocolMapper_label(realmName, clientId, mapperName, updatedLabelName),
Config: testKeycloakOpenIdUserSessionNoteProtocolMapper_note(realmName, clientId, mapperName, updatedNoteName),
Check: testKeycloakOpenIdUserSessionNoteProtocolMapperExists(resourceName),
},
},
Expand Down Expand Up @@ -347,7 +347,7 @@ resource "keycloak_openid_user_session_note_protocol_mapper" "user_session_note_
client_id = "${keycloak_openid_client.openid_client.id}"
claim_name = "foo"
claim_value_type = "String"
session_note_label = "bar"
session_note = "bar"
}`, realmName, clientId, mapperName)
}

Expand All @@ -366,7 +366,7 @@ resource "keycloak_openid_user_session_note_protocol_mapper" "user_session_note_
client_scope_id = "${keycloak_openid_client_scope.client_scope.id}"
claim_name = "foo"
claim_value_type = "String"
session_note_label = "bar"
session_note = "bar"
}`, realmName, clientScopeId, mapperName)
}

Expand All @@ -389,7 +389,7 @@ resource "keycloak_openid_user_session_note_protocol_mapper" "user_session_note_
}`, realmName, clientId, mapperName, claimName)
}

func testKeycloakOpenIdUserSessionNoteProtocolMapper_label(realmName, clientId, mapperName, labelName string) string {
func testKeycloakOpenIdUserSessionNoteProtocolMapper_note(realmName, clientId, mapperName, noteName string) string {
return fmt.Sprintf(`
resource "keycloak_realm" "realm" {
realm = "%s"
Expand All @@ -405,8 +405,8 @@ resource "keycloak_openid_user_session_note_protocol_mapper" "user_session_note_
client_id = "${keycloak_openid_client.openid_client.id}"
claim_name = "foo"
claim_value_type = "String"
session_note_label = "%s"
}`, realmName, clientId, mapperName, labelName)
session_note = "%s"
}`, realmName, clientId, mapperName, noteName)
}

func testKeycloakOpenIdUserSessionNoteProtocolMapper_import(realmName, clientId, clientScopeId, mapperName string) string {
Expand All @@ -425,7 +425,7 @@ resource "keycloak_openid_user_session_note_protocol_mapper" "user_session_note_
client_id = "${keycloak_openid_client.openid_client.id}"
claim_name = "foo"
claim_value_type = "String"
session_note_label = "bar"
session_note = "bar"
}
resource "keycloak_openid_client_scope" "client_scope" {
name = "%s"
Expand All @@ -437,7 +437,7 @@ resource "keycloak_openid_user_session_note_protocol_mapper" "user_session_note_
client_scope_id = "${keycloak_openid_client_scope.client_scope.id}"
claim_name = "foo"
claim_value_type = "String"
session_note_label = "bar"
session_note = "bar"
}`, realmName, clientId, mapperName, clientScopeId, mapperName)
}

Expand All @@ -457,6 +457,6 @@ resource "keycloak_openid_user_session_note_protocol_mapper" "user_session_note_
client_id = "${keycloak_openid_client.openid_client.id}"
claim_name = "foo"
claim_value_type = "%s"
session_note_label = "bar"
session_note = "bar"
}`, realmName, mapperName, claimValueType)
}

0 comments on commit 4b6c523

Please sign in to comment.