Skip to content

Commit

Permalink
feat: add requiredActionProviderRepresentation (#339)
Browse files Browse the repository at this point in the history
This commit adds the requiredActionProviderRepresentation and a function for updating required actions
  • Loading branch information
shibumi authored Feb 17, 2022
1 parent 76b1bd1 commit 7fa5389
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
14 changes: 14 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
18 changes: 18 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
8 changes: 8 additions & 0 deletions gocloak.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 13 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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) }

0 comments on commit 7fa5389

Please sign in to comment.