diff --git a/example/main.tf b/example/main.tf index b87c8d792..4da170839 100644 --- a/example/main.tf +++ b/example/main.tf @@ -107,6 +107,13 @@ resource "keycloak_required_action" "custom-configured_totp" { priority = keycloak_required_action.custom-terms-and-conditions.priority + 15 } +resource "keycloak_required_action" "required_action" { + realm_id = keycloak_realm.test.realm + alias = "webauthn-register" + enabled = true + name = "Webauthn Register" +} + resource "keycloak_group" "foo" { realm_id = keycloak_realm.test.id name = "foo" diff --git a/keycloak/required_action.go b/keycloak/required_action.go index faa7ff719..3f4ece2ba 100644 --- a/keycloak/required_action.go +++ b/keycloak/required_action.go @@ -7,6 +7,7 @@ type RequiredAction struct { RealmId string `json:"-"` Alias string `json:"alias"` Name string `json:"name"` + ProviderId string `json:"providerId"` Enabled bool `json:"enabled"` DefaultAction bool `json:"defaultAction"` Priority int `json:"priority"` @@ -42,6 +43,21 @@ func (keycloakClient *KeycloakClient) GetRequiredActions(realmId string) ([]*Req return requiredActions, nil } +func (keycloakClient *KeycloakClient) GetUnregisteredRequiredActions(realmId string) ([]*RequiredAction, error) { + var unregisteredRequiredActions []*RequiredAction + + err := keycloakClient.get(fmt.Sprintf("/realms/%s/authentication/unregistered-required-actions", realmId), &unregisteredRequiredActions, nil) + if err != nil { + return nil, err + } + + for _, unregisteredRequiredAction := range unregisteredRequiredActions { + unregisteredRequiredAction.RealmId = realmId + } + + return unregisteredRequiredActions, nil +} + func (keycloakClient *KeycloakClient) GetRequiredAction(realmId string, alias string) (*RequiredAction, error) { var requiredAction RequiredAction @@ -53,6 +69,11 @@ func (keycloakClient *KeycloakClient) GetRequiredAction(realmId string, alias st return &requiredAction, nil } +func (keycloakClient *KeycloakClient) RegisterRequiredAction(requiredAction *RequiredAction) error { + _, _, err := keycloakClient.post(fmt.Sprintf("/realms/%s/authentication/register-required-action", requiredAction.RealmId), requiredAction) + return err +} + func (keycloakClient *KeycloakClient) CreateRequiredAction(requiredAction *RequiredAction) error { requiredAction.Id = fmt.Sprintf("%s/%s", requiredAction.RealmId, requiredAction.Alias) return keycloakClient.UpdateRequiredAction(requiredAction) diff --git a/provider/resource_keycloak_required_action.go b/provider/resource_keycloak_required_action.go index f25136bd4..cd11e4ff7 100644 --- a/provider/resource_keycloak_required_action.go +++ b/provider/resource_keycloak_required_action.go @@ -81,6 +81,19 @@ func resourceKeycloakRequiredActionsCreate(data *schema.ResourceData, meta inter return err } + unregisteredRequiredActions, err := keycloakClient.GetUnregisteredRequiredActions(action.RealmId) + if err != nil { + return err + } + for _, unregisteredRequiredAction := range unregisteredRequiredActions { + if unregisteredRequiredAction.ProviderId == action.Alias { + if err := keycloakClient.RegisterRequiredAction(unregisteredRequiredAction); err != nil { + return err + } + break + } + } + err = keycloakClient.CreateRequiredAction(action) if err != nil { return err diff --git a/provider/resource_keycloak_required_action_test.go b/provider/resource_keycloak_required_action_test.go index c2c7d8e1b..bdabeefe8 100644 --- a/provider/resource_keycloak_required_action_test.go +++ b/provider/resource_keycloak_required_action_test.go @@ -26,6 +26,22 @@ func TestAccKeycloakRequiredAction_basic(t *testing.T) { }) } +func TestAccKeycloakRequiredAction_unregisteredAction(t *testing.T) { + realmName := "terraform-" + acctest.RandString(10) + requiredActionAlias := "webauthn-register" + + resource.Test(t, resource.TestCase{ + ProviderFactories: testAccProviderFactories, + PreCheck: func() { testAccPreCheck(t) }, + Steps: []resource.TestStep{ + { + Config: testKeycloakRequiredAction_basic(realmName, requiredActionAlias, 37), + Check: testAccCheckKeycloakRequiresActionExistsWithCorrectPriority(realmName, requiredActionAlias, 37), + }, + }, + }) +} + func TestAccKeycloakRequiredAction_invalidAlias(t *testing.T) { realmName := "terraform-" + acctest.RandString(10) randomReqActionAlias := "randomRequiredAction-" + acctest.RandString(10)