Repository: Nerzal/gocloak
Pull request #339 (2022-02-15 10:37:25)

feat: add requiredActionProviderRepresentation
This commit adds the requiredActionProviderRepresentation and a function for updating required actions


shibumi: Code is tested and works for me
Nerzal: Hi, could you please also add a unit/integration test for that? :) 
codecov[bot]: # [Codecov](https://codecov.io/gh/Nerzal/gocloak/pull/339?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal) Report
> Merging [#339](https://codecov.io/gh/Nerzal/gocloak/pull/339?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal) (1dcc5c1) into [main](https://codecov.io/gh/Nerzal/gocloak/commit/76b1bd1e3b71ab7f376d5ec41650620c207404af?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal) (76b1bd1) will **decrease** coverage by `0.31%`.
> The diff coverage is `0.00%`.

[![Impacted file tree graph](https://codecov.io/gh/Nerzal/gocloak/pull/339/graphs/tree.svg?width=650&height=150&src=pr&token=gB52LSVNIV&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal)](https://codecov.io/gh/Nerzal/gocloak/pull/339?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal)

```diff
@@            Coverage Diff             @@
##             main     #339      +/-   ##
==========================================
- Coverage   76.60%   76.29%   -0.32%     
==========================================
  Files           4        4              
  Lines        1962     1970       +8     
==========================================
  Hits         1503     1503              
- Misses        319      327       +8     
  Partials      140      140              
```


| [Impacted Files](https://codecov.io/gh/Nerzal/gocloak/pull/339?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal) | Coverage Δ | |
|---|---|---|
| [client.go](https://codecov.io/gh/Nerzal/gocloak/pull/339/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal#diff-Y2xpZW50Lmdv) | `74.50% <0.00%> (-0.30%)` | :arrow_down: |
| [models.go](https://codecov.io/gh/Nerzal/gocloak/pull/339/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal#diff-bW9kZWxzLmdv) | `92.14% <0.00%> (-0.67%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Nerzal/gocloak/pull/339?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Nerzal/gocloak/pull/339?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal). Last update [76b1bd1...1dcc5c1](https://codecov.io/gh/Nerzal/gocloak/pull/339?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Nerzal).

shibumi: @Nerzal yeah sure. Let me have a look on this :D
Nerzal: Thank you for the contribution! 
I'll do now merge. It might take a day or 2 until i release a new tag, as there is also a little breaking change in the pipeline. 

I'll create a new Release + Tag until that one is also merged.

Patch:
diff --git a/client.go b/client.go
index 71c0319..918ed95 100644
--- a/client.go
+++ b/client.go
@@ -3683,3 +3683,17 @@ func (client *gocloak) CreateClientScopesScopeMappingsRealmRoles(ctx context.Con
 
 	return checkForError(resp, err, errMessage)
 }
+
+// UpdateRequiredAction updates a required action for a given realm
+func (client *gocloak) UpdateRequiredAction(ctx context.Context, token string, realm string, requiredAction RequiredActionProviderRepresentation) error {
+	const errMessage = "could not update required action"
+
+	if NilOrEmpty(requiredAction.ProviderID) {
+		return errors.New("providerId is required for updating a required action")
+	}
+	_, err := client.getRequestWithBearerAuth(ctx, token).
+		SetBody(requiredAction).
+		Put(client.getAdminRealmURL(realm, "authentication", "required-actions", *requiredAction.ProviderID))
+
+	return err
+}
diff --git a/gocloak.go b/gocloak.go
index d09bdf9..f840180 100644
--- a/gocloak.go
+++ b/gocloak.go
@@ -493,6 +493,14 @@ type GoCloak interface {
 	// ---------------
 	// Events API
 	// ---------------
+
 	// GetEvents returns events
 	GetEvents(ctx context.Context, token string, realm string, params GetEventsParams) ([]*EventRepresentation, error)
+
+	// -------------------
+	// RequiredActions API
+	// -------------------
+
+	// UpdateRequiredAction updates a required action for a given realm
+	UpdateRequiredAction(ctx context.Context, token string, realm string, requiredAction RequiredActionProviderRepresentation) error
 }
diff --git a/models.go b/models.go
index 045c040..089e7af 100644
--- a/models.go
+++ b/models.go
@@ -1236,6 +1236,18 @@ type CredentialRepresentation struct {
 	UserLabel      *string `json:"userLabel,omitempty"`
 }
 
+// RequiredActionProviderRepresentation is a representation of required actions
+// v15: https://www.keycloak.org/docs-api/15.0/rest-api/index.html#_requiredactionproviderrepresentation
+type RequiredActionProviderRepresentation struct {
+	Alias         *string            `json:"alias,omitempty"`
+	Config        *map[string]string `json:"config,omitempty"`
+	DefaultAction *bool              `json:"defaultAction,omitempty"`
+	Enabled       *bool              `json:"enabled,omitempty"`
+	Name          *string            `json:"name,omitempty"`
+	Priority      *int32             `json:"priority,omitempty"`
+	ProviderID    *string            `json:"providerId,omitempty"`
+}
+
 // prettyStringStruct returns struct formatted into pretty string
 func prettyStringStruct(t interface{}) string {
 
@@ -1328,3 +1340,4 @@ func (v *GetUserPermissionParams) String() string                   { return pre
 func (v *ResourcePolicyRepresentation) String() string              { return prettyStringStruct(v) }
 func (v *GetResourcePoliciesParams) String() string                 { return prettyStringStruct(v) }
 func (v *CredentialRepresentation) String() string                  { return prettyStringStruct(v) }
+func (v *RequiredActionProviderRepresentation) String() string      { return prettyStringStruct(v) }


Test patch:
diff --git a/client_test.go b/client_test.go
index a222279..a3d36b7 100644
--- a/client_test.go
+++ b/client_test.go
@@ -6230,3 +6230,21 @@ func TestGocloak_CreateAuthenticationFlowsAndCreateAuthenticationExecution(t *te
 	}
 	require.True(t, deleted, "Failed to delete authentication flow, no flow was deleted")
 }
+
+func TestGocloak_UpdateRequiredAction(t *testing.T) {
+	t.Parallel()
+	cfg := GetConfig(t)
+	client := NewClientWithDebug(t)
+	token := GetAdminToken(t, client)
+	requiredAction := gocloak.RequiredActionProviderRepresentation{
+		Alias:         gocloak.StringP("VERIFY_EMAIL"),
+		Config:        nil,
+		DefaultAction: gocloak.BoolP(false),
+		Enabled:       gocloak.BoolP(true),
+		Name:          gocloak.StringP("Verify Email"),
+		Priority:      gocloak.Int32P(50),
+		ProviderID:    gocloak.StringP("VERIFY_EMAIL"),
+	}
+	err := client.UpdateRequiredAction(context.Background(), token.AccessToken, cfg.GoCloak.Realm, requiredAction)
+	require.NoError(t, err, "Failed to update required action")
+}