Skip to content

Commit

Permalink
Add priority field support in authentication flow executions and subf…
Browse files Browse the repository at this point in the history
…lows

Fixes mrparkers#296

Signed-off-by: Andrejs Mivreniks <[email protected]>
  • Loading branch information
gim- committed Jun 1, 2024
1 parent 3f6b75b commit 9cc1a16
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
6 changes: 5 additions & 1 deletion docs/resources/authentication_execution.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Allows for creating and managing an authentication execution within Keycloak.
An authentication execution is an action that the user or service may or may not take when authenticating through an authentication
flow.

~> Due to limitations in the Keycloak API, the ordering of authentication executions within a flow must be specified using `depends_on`. Authentication executions that are created first will appear first within the flow.
~> Due to limitations in the Keycloak API, the ordering of authentication executions within a flow must be specified using `depends_on` in versions prior to Keycloak 25. Authentication executions that are created first will appear first within the flow.

## Example Usage

Expand All @@ -30,6 +30,7 @@ resource "keycloak_authentication_execution" "execution_one" {
parent_flow_alias = "${keycloak_authentication_flow.flow.alias}"
authenticator = "auth-cookie"
requirement = "ALTERNATIVE"
priority = 10 # Starting from Keycloak 25
}
# second execution
Expand All @@ -38,7 +39,9 @@ resource "keycloak_authentication_execution" "execution_two" {
parent_flow_alias = "${keycloak_authentication_flow.flow.alias}"
authenticator = "identity-provider-redirector"
requirement = "ALTERNATIVE"
priority = 20 # Starting from Keycloak 25
# Workaround for older Keycloak versions (Keycloak 24 and older)
depends_on = [
keycloak_authentication_execution.execution_one
]
Expand All @@ -51,6 +54,7 @@ resource "keycloak_authentication_execution" "execution_two" {
- `parent_flow_alias` - (Required) The alias of the flow this execution is attached to.
- `authenticator` - (Required) The name of the authenticator. This can be found by experimenting with the GUI and looking at HTTP requests within the network tab of your browser's development tools.
- `requirement`- (Optional) The requirement setting, which can be one of `REQUIRED`, `ALTERNATIVE`, `OPTIONAL`, `CONDITIONAL`, or `DISABLED`. Defaults to `DISABLED`.
- `priority`- (Optional) The authenticator priority, the lower the value the higher it will be placed in the parent flow. This option is supported only by Keycloak 25 and onwards.

## Import

Expand Down
2 changes: 2 additions & 0 deletions docs/resources/authentication_subflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ resource "keycloak_authentication_subflow" "subflow" {
parent_flow_alias = keycloak_authentication_flow.flow.alias
provider_id = "basic-flow"
requirement = "ALTERNATIVE"
priority = 10 # Starting from Keycloak 25
}
```

Expand All @@ -43,6 +44,7 @@ and `client-flow`. Defaults to `basic-flow`.
authenticators. In general this will remain empty.
- `requirement`- (Optional) The requirement setting, which can be one of `REQUIRED`, `ALTERNATIVE`, `OPTIONAL`, `CONDITIONAL`,
or `DISABLED`. Defaults to `DISABLED`.
- `priority`- (Optional) The subflow priority, the lower the value the higher it will be placed in the parent flow. This option is supported only by Keycloak 25 and onwards.

## Import

Expand Down
9 changes: 8 additions & 1 deletion keycloak/authentication_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
// POST /realms/${realmId}/authentication/flows/${flowAlias}/executions/execution
type authenticationExecutionCreate struct {
Provider string `json:"provider"` //authenticator of the execution
Priority int `json:"priority"`
}

type authenticationExecutionRequirementUpdate struct {
RealmId string `json:"-"`
ParentFlowAlias string `json:"-"`
Id string `json:"id"`
Requirement string `json:"requirement"`
Priority int `json:"priority"`
}

// this type is returned by GET /realms/${realmId}/authentication/flows/${flowAlias}/executions
Expand Down Expand Up @@ -47,6 +49,7 @@ type AuthenticationExecutionInfo struct {
Index int `json:"index"`
Level int `json:"level"`
ProviderId string `json:"providerId"`
Priority int `json:"priority"`
Requirement string `json:"requirement"`
}

Expand Down Expand Up @@ -119,7 +122,10 @@ func (keycloakClient *KeycloakClient) GetAuthenticationExecutionInfoFromProvider
}

func (keycloakClient *KeycloakClient) NewAuthenticationExecution(ctx context.Context, execution *AuthenticationExecution) error {
_, location, err := keycloakClient.post(ctx, fmt.Sprintf("/realms/%s/authentication/flows/%s/executions/execution", execution.RealmId, execution.ParentFlowAlias), &authenticationExecutionCreate{Provider: execution.Authenticator})
_, location, err := keycloakClient.post(ctx, fmt.Sprintf("/realms/%s/authentication/flows/%s/executions/execution", execution.RealmId, execution.ParentFlowAlias), &authenticationExecutionCreate{
Provider: execution.Authenticator,
Priority: execution.Priority,
})
if err != nil {
return err
}
Expand Down Expand Up @@ -154,6 +160,7 @@ func (keycloakClient *KeycloakClient) UpdateAuthenticationExecution(ctx context.
ParentFlowAlias: execution.ParentFlowAlias,
Id: execution.Id,
Requirement: execution.Requirement,
Priority: execution.Priority,
}
return keycloakClient.UpdateAuthenticationExecutionRequirement(ctx, authenticationExecutionUpdateRequirement)
}
Expand Down
30 changes: 2 additions & 28 deletions keycloak/authentication_subflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (keycloakClient *KeycloakClient) GetAuthenticationSubFlow(ctx context.Conte
}
authenticationSubFlow.Authenticator = subFlowExecution.Authenticator
authenticationSubFlow.Requirement = subFlowExecution.Requirement
authenticationSubFlow.Priority = subFlowExecution.Priority

return &authenticationSubFlow, nil
}
Expand Down Expand Up @@ -110,6 +111,7 @@ func (keycloakClient *KeycloakClient) UpdateAuthenticationSubFlow(ctx context.Co
ParentFlowAlias: authenticationSubFlow.ParentFlowAlias,
Id: executionId,
Requirement: authenticationSubFlow.Requirement,
Priority: authenticationSubFlow.Priority,
}
return keycloakClient.UpdateAuthenticationExecutionRequirement(ctx, authenticationExecutionUpdateRequirement)

Expand All @@ -128,31 +130,3 @@ func (keycloakClient *KeycloakClient) DeleteAuthenticationSubFlow(ctx context.Co

return keycloakClient.DeleteAuthenticationExecution(ctx, authenticationSubFlow.RealmId, executionId)
}

func (keycloakClient *KeycloakClient) RaiseAuthenticationSubFlowPriority(ctx context.Context, realmId, parentFlowAlias, id string) error {
authenticationSubFlow := AuthenticationSubFlow{
Id: id,
ParentFlowAlias: parentFlowAlias,
RealmId: realmId,
}
executionId, err := keycloakClient.getExecutionId(ctx, &authenticationSubFlow)
if err != nil {
return err
}

return keycloakClient.RaiseAuthenticationExecutionPriority(ctx, authenticationSubFlow.RealmId, executionId)
}

func (keycloakClient *KeycloakClient) LowerAuthenticationSubFlowPriority(ctx context.Context, realmId, parentFlowAlias, id string) error {
authenticationSubFlow := AuthenticationSubFlow{
Id: id,
ParentFlowAlias: parentFlowAlias,
RealmId: realmId,
}
executionId, err := keycloakClient.getExecutionId(ctx, &authenticationSubFlow)
if err != nil {
return err
}

return keycloakClient.LowerAuthenticationExecutionPriority(ctx, authenticationSubFlow.RealmId, executionId)
}
7 changes: 7 additions & 0 deletions provider/resource_keycloak_authentication_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func resourceKeycloakAuthenticationExecution() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{"REQUIRED", "ALTERNATIVE", "OPTIONAL", "CONDITIONAL", "DISABLED"}, false), //OPTIONAL is removed from 8.0.0 onwards
Default: "DISABLED",
},
"priority": {
Type: schema.TypeInt,
Optional: true,
},
},
}
}
Expand All @@ -54,6 +58,7 @@ func mapFromDataToAuthenticationExecution(data *schema.ResourceData) *keycloak.A
ParentFlowAlias: data.Get("parent_flow_alias").(string),
Authenticator: data.Get("authenticator").(string),
Requirement: data.Get("requirement").(string),
Priority: data.Get("priority").(int),
}

return authenticationExecution
Expand All @@ -66,13 +71,15 @@ func mapFromAuthenticationExecutionToData(data *schema.ResourceData, authenticat
data.Set("parent_flow_alias", authenticationExecution.ParentFlowAlias)
data.Set("authenticator", authenticationExecution.Authenticator)
data.Set("requirement", authenticationExecution.Requirement)
data.Set("priority", authenticationExecution.Priority)
}

func mapFromAuthenticationExecutionInfoToData(data *schema.ResourceData, authenticationExecutionInfo *keycloak.AuthenticationExecutionInfo) {
data.SetId(authenticationExecutionInfo.Id)

data.Set("realm_id", authenticationExecutionInfo.RealmId)
data.Set("parent_flow_alias", authenticationExecutionInfo.ParentFlowAlias)
data.Set("priority", authenticationExecutionInfo.Priority)
}

func resourceKeycloakAuthenticationExecutionCreate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
6 changes: 6 additions & 0 deletions provider/resource_keycloak_authentication_subflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func resourceKeycloakAuthenticationSubFlow() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{"REQUIRED", "ALTERNATIVE", "OPTIONAL", "CONDITIONAL", "DISABLED"}, false), //OPTIONAL is removed from 8.0.0 onwards
Default: "DISABLED",
},
"priority": {
Type: schema.TypeInt,
Optional: true,
},
},
}
}
Expand All @@ -73,6 +77,7 @@ func mapFromDataToAuthenticationSubFlow(data *schema.ResourceData) *keycloak.Aut
Description: data.Get("description").(string),
Authenticator: data.Get("authenticator").(string),
Requirement: data.Get("requirement").(string),
Priority: data.Get("priority").(int),
}

return authenticationSubFlow
Expand All @@ -87,6 +92,7 @@ func mapFromAuthenticationSubFlowToData(data *schema.ResourceData, authenticatio
data.Set("description", authenticationSubFlow.Description)
data.Set("authenticator", authenticationSubFlow.Authenticator)
data.Set("requirement", authenticationSubFlow.Requirement)
data.Set("priority", authenticationSubFlow.Priority)
}

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

0 comments on commit 9cc1a16

Please sign in to comment.