Skip to content

Commit

Permalink
feat: add CreateAuthenticationExecutionFlow (#327)
Browse files Browse the repository at this point in the history
* feat: add CreateAuthenticationExecutionFlow

* test: add test for GetAuthenticationExecutions, DeleteAuthenticationExecution and CreateAuthenticationExecutionFlow

* test: improve authExecs check

Co-authored-by: 黄维啸 <huangweixiao@megvii.com>
  • Loading branch information
weixiao-huang and 黄维啸 authored Feb 17, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 4716d38 commit 08df6c5
Showing 4 changed files with 79 additions and 9 deletions.
9 changes: 9 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -2204,6 +2204,15 @@ func (client *gocloak) DeleteAuthenticationExecution(ctx context.Context, token,
return checkForError(resp, err, errMessage)
}

//CreateAuthenticationExecutionFlow creates a new execution for the given flow name in the given realm
func (client *gocloak) CreateAuthenticationExecutionFlow(ctx context.Context, token, realm, flow string, executionFlow CreateAuthenticationExecutionFlowRepresentation) error {
const errMessage = "could not create authentication execution flow"
resp, err := client.getRequestWithBearerAuth(ctx, token).SetBody(executionFlow).
Post(client.getAdminRealmURL(realm, "authentication", "flows", flow, "executions", "flow"))

return checkForError(resp, err, errMessage)
}

// -----
// Users
// -----
68 changes: 59 additions & 9 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -6149,7 +6149,7 @@ func TestGocloak_GetAuthenticationFlows(t *testing.T) {
require.Error(t, err)
}

func TestGocloak_CreateAuthenticationFlowsAndCreateAuthenticationExecution(t *testing.T) {
func TestGocloak_CreateAuthenticationFlowsAndCreateAuthenticationExecutionAndFlow(t *testing.T) {
t.Parallel()
cfg := GetConfig(t)
client := NewClientWithDebug(t)
@@ -6166,6 +6166,13 @@ func TestGocloak_CreateAuthenticationFlowsAndCreateAuthenticationExecution(t *te
ProviderID: gocloak.StringP("basic-flow"),
}

authExecFlow := gocloak.CreateAuthenticationExecutionFlowRepresentation{
Alias: gocloak.StringP("testauthexecflow"),
Description: gocloak.StringP("test"),
Provider: gocloak.StringP("basic-flow"),
Type: gocloak.StringP("basic-flow"),
}

err := client.CreateAuthenticationFlow(
context.Background(),
token.AccessToken,
@@ -6184,25 +6191,40 @@ func TestGocloak_CreateAuthenticationFlowsAndCreateAuthenticationExecution(t *te
)
require.NoError(t, err, "Failed to create authentication execution")

authExecs, err := client.GetAuthenticationExecutions(
err = client.CreateAuthenticationExecutionFlow(
context.Background(),
token.AccessToken,
cfg.GoCloak.Realm,
*authFlow.Alias,
authExecFlow,
)
require.NoError(t, err, "Failed to create authentication execution flow")

t.Logf("authentication executions: %+v", authExecs)
require.NoError(t, err, "Failed to get authentication executions")

authExecs[0].Requirement = gocloak.StringP("ALTERNATIVE")
err = client.UpdateAuthenticationExecution(
authExecs, err := client.GetAuthenticationExecutions(
context.Background(),
token.AccessToken,
cfg.GoCloak.Realm,
*authFlow.Alias,
*authExecs[0],
)
require.NoError(t, err, "Failed to update authentication executions")

t.Logf("authentication executions: %+v", authExecs)
require.NoError(t, err, "Failed to get authentication executions")

// UpdateAuthenticationExecution
for _, execution := range authExecs {
if execution.ProviderID != nil && *execution.ProviderID == *authExec.Provider {
execution.Requirement = gocloak.StringP("ALTERNATIVE")
err = client.UpdateAuthenticationExecution(
context.Background(),
token.AccessToken,
cfg.GoCloak.Realm,
*authFlow.Alias,
*execution,
)
require.NoError(t, err, fmt.Sprintf("Failed to update authentication executions, realm: %+v, flow: %+v, execution: %+v", cfg.GoCloak.Realm, *authFlow.Alias, *execution.ProviderID))
break
}
}
authExecs, err = client.GetAuthenticationExecutions(
context.Background(),
token.AccessToken,
@@ -6212,6 +6234,34 @@ func TestGocloak_CreateAuthenticationFlowsAndCreateAuthenticationExecution(t *te
require.NoError(t, err, "Failed to get authentication executions second time")
t.Logf("authentication executions after update: %+v", authExecs)

var (
execDeleted bool
execFlowFound bool
)
for _, execution := range authExecs {
if execution.DisplayName != nil && *execution.DisplayName == *authExecFlow.Alias {
execFlowFound = true
continue
}
if execution.ProviderID != nil && *execution.ProviderID == *authExec.Provider {
require.NotNil(t, execution.Requirement)
require.Equal(t, *execution.Requirement, "ALTERNATIVE")
err = client.DeleteAuthenticationExecution(
context.Background(),
token.AccessToken,
cfg.GoCloak.Realm,
*execution.ID,
)
require.NoError(t, err, "Failed to delete authentication execution")
execDeleted = true
}
if execDeleted && execFlowFound {
break
}
}
require.True(t, execDeleted, "Failed to delete authentication execution, no execution was deleted")
require.True(t, execFlowFound, "Failed to find authentication execution flow")

flows, err := client.GetAuthenticationFlows(context.Background(), token.AccessToken, cfg.GoCloak.Realm)
require.NoError(t, err, "Failed to get authentication flows")
deleted := false
3 changes: 3 additions & 0 deletions gocloak.go
Original file line number Diff line number Diff line change
@@ -316,6 +316,9 @@ type GoCloak interface {
// DeleteAuthenticationExecution delete a single execution with the given ID
DeleteAuthenticationExecution(ctx context.Context, token, realm, executionID string) error

//CreateAuthenticationExecutionFlow creates a new flow execution for the given flow name in the given realm
CreateAuthenticationExecutionFlow(ctx context.Context, token, realm, flow string, execution CreateAuthenticationExecutionFlowRepresentation) error

// *** Users ***
// CreateUser creates a new user
CreateUser(ctx context.Context, token, realm string, user User) (string, error)
8 changes: 8 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
@@ -793,6 +793,14 @@ type CreateAuthenticationExecutionRepresentation struct {
Provider *string `json:"provider,omitempty"`
}

// CreateAuthenticationExecutionFlowRepresentation contains the provider to be used for a new authentication representation
type CreateAuthenticationExecutionFlowRepresentation struct {
Alias *string `json:"alias,omitempty"`
Description *string `json:"description,omitempty"`
Provider *string `json:"provider,omitempty"`
Type *string `json:"type,omitempty"`
}

// ModifyAuthenticationExecutionRepresentation is the payload for updating an execution representation
type ModifyAuthenticationExecutionRepresentation struct {
ID *string `json:"id,omitempty"`

0 comments on commit 08df6c5

Please sign in to comment.