Skip to content

Commit

Permalink
Merge 02b967a into f5511a0
Browse files Browse the repository at this point in the history
  • Loading branch information
bsoniam committed Apr 17, 2019
2 parents f5511a0 + 02b967a commit 91df8e9
Show file tree
Hide file tree
Showing 7 changed files with 400 additions and 105 deletions.
330 changes: 225 additions & 105 deletions cmd/keycloakb/keycloak_bridge.go

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions pkg/management/component.go
Expand Up @@ -25,6 +25,7 @@ type KeycloakClient interface {
GetRealmLevelRoleMappings(accessToken string, realmName, userID string) ([]kc.RoleRepresentation, error)
ResetPassword(accessToken string, realmName string, userID string, cred kc.CredentialRepresentation) error
SendVerifyEmail(accessToken string, realmName string, userID string, paramKV ...string) error
ExecuteActionsEmail(accessToken string, realmName string, userID string, actions []string, paramKV ...string) error
GetCredentialsForUser(accessToken string, realmReq, realmName string, userID string) ([]kc.CredentialRepresentation, error)
DeleteCredentialsForUser(accessToken string, realmReq, realmName string, userID string, credentialID string) error
GetRoles(accessToken string, realmName string) ([]kc.RoleRepresentation, error)
Expand All @@ -50,6 +51,7 @@ type Component interface {
GetRealmRolesForUser(ctx context.Context, realmName, userID string) ([]api.RoleRepresentation, error)
ResetPassword(ctx context.Context, realmName string, userID string, password api.PasswordRepresentation) error
SendVerifyEmail(ctx context.Context, realmName string, userID string, paramKV ...string) error
ExecuteActionsEmail(ctx context.Context, realmName string, userID string, actions []string, paramKV ...string) error
GetCredentialsForUser(ctx context.Context, realmName string, userID string) ([]api.CredentialRepresentation, error)
DeleteCredentialsForUser(ctx context.Context, realmName string, userID string, credentialID string) error
GetRoles(ctx context.Context, realmName string) ([]api.RoleRepresentation, error)
Expand Down Expand Up @@ -568,6 +570,12 @@ func (c *component) SendVerifyEmail(ctx context.Context, realmName string, userI
return c.keycloakClient.SendVerifyEmail(accessToken, realmName, userID, paramKV...)
}

func (c *component) ExecuteActionsEmail(ctx context.Context, realmName string, userID string, actions []string, paramKV ...string) error {
var accessToken = ctx.Value("access_token").(string)

return c.keycloakClient.ExecuteActionsEmail(accessToken, realmName, userID, actions, paramKV...)
}

func (c *component) GetCredentialsForUser(ctx context.Context, realmName string, userID string) ([]api.CredentialRepresentation, error) {
var accessToken = ctx.Value("access_token").(string)
var ctxRealm = ctx.Value("realm").(string)
Expand Down
42 changes: 42 additions & 0 deletions pkg/management/component_test.go
Expand Up @@ -1074,6 +1074,48 @@ func TestSendVerifyEmail(t *testing.T) {
}
}

func TestExecuteActionsEmail(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
var mockKeycloakClient = mock.NewKeycloakClient(mockCtrl)
var mockEventDBModule = mock.NewEventsDBModule(mockCtrl)

var managementComponent = NewComponent(mockKeycloakClient, mockEventDBModule)

var accessToken = "TOKEN=="
var realmName = "master"
var userID = "1245-7854-8963"
var actions = []string{"action1", "action2"}

var key1 = "key1"
var value1 = "value1"
var key2 = "key2"
var value2 = "value2"

// Send email actions
{

mockKeycloakClient.EXPECT().ExecuteActionsEmail(accessToken, realmName, userID, actions, key1, value1, key2, value2).Return(nil).Times(1)

var ctx = context.WithValue(context.Background(), "access_token", accessToken)

err := managementComponent.ExecuteActionsEmail(ctx, "master", userID, actions, key1, value1, key2, value2)

assert.Nil(t, err)
}

// Error
{
mockKeycloakClient.EXPECT().ExecuteActionsEmail(accessToken, realmName, userID, actions).Return(fmt.Errorf("Invalid input")).Times(1)

var ctx = context.WithValue(context.Background(), "access_token", accessToken)

err := managementComponent.ExecuteActionsEmail(ctx, "master", userID, actions)

assert.NotNil(t, err)
}
}

func TestGetCredentialsForUser(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
Expand Down
25 changes: 25 additions & 0 deletions pkg/management/endpoint.go
Expand Up @@ -26,6 +26,7 @@ type Endpoints struct {
GetRealmRoleForUser endpoint.Endpoint
ResetPassword endpoint.Endpoint
SendVerifyEmail endpoint.Endpoint
ExecuteActionsEmail endpoint.Endpoint
GetCredentialsForUser endpoint.Endpoint
DeleteCredentialsForUser endpoint.Endpoint
GetRoles endpoint.Endpoint
Expand All @@ -51,6 +52,7 @@ type ManagementComponent interface {
GetRealmRolesForUser(ctx context.Context, realmName, userID string) ([]api.RoleRepresentation, error)
ResetPassword(ctx context.Context, realmName string, userID string, password api.PasswordRepresentation) error
SendVerifyEmail(ctx context.Context, realmName string, userID string, paramKV ...string) error
ExecuteActionsEmail(ctx context.Context, realmName string, userID string, actions []string, paramKV ...string) error
GetCredentialsForUser(ctx context.Context, realmName string, userID string) ([]api.CredentialRepresentation, error)
DeleteCredentialsForUser(ctx context.Context, realmName string, userID string, credentialID string) error
GetRoles(ctx context.Context, realmName string) ([]api.RoleRepresentation, error)
Expand Down Expand Up @@ -244,6 +246,29 @@ func MakeSendVerifyEmailEndpoint(managementComponent ManagementComponent) endpoi
}
}

func MakeExecuteActionsEmailEndpoint(managementComponent ManagementComponent) endpoint.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var m = req.(map[string]string)

var paramKV []string
for _, key := range []string{"client_id", "redirect_uri", "lifespan"} {
if m[key] != "" {
paramKV = append(paramKV, key, m[key])
}
}

//extract the actions
var actions []string
err := json.Unmarshal([]byte(m["actions"]), &actions)

if err != nil {
return nil, err
}

return nil, managementComponent.ExecuteActionsEmail(ctx, m["realm"], m["userID"], actions, paramKV...)
}
}

func MakeGetCredentialsForUserEndpoint(managementComponent ManagementComponent) endpoint.Endpoint {
return func(ctx context.Context, req interface{}) (interface{}, error) {
var m = req.(map[string]string)
Expand Down
66 changes: 66 additions & 0 deletions pkg/management/endpoint_test.go
Expand Up @@ -485,6 +485,72 @@ func TestSendVerifyEmailEndpoint(t *testing.T) {
}
}

func TestExecuteActionsEmailEndpoint(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()

var mockManagementComponent = mock.NewManagementComponent(mockCtrl)

var e = MakeExecuteActionsEmailEndpoint(mockManagementComponent)

// No error - Without param
{
var realm = "master"
var userID = "123-456-789"
var actions = []string{"action1", "action2"}
var ctx = context.Background()
var req = make(map[string]string)
req["realm"] = realm
req["userID"] = userID
actionsJSON, _ := json.Marshal(actions)
req["actions"] = string(actionsJSON)

mockManagementComponent.EXPECT().ExecuteActionsEmail(ctx, realm, userID, actions).Return(nil).Times(1)
var res, err = e(ctx, req)
assert.Nil(t, err)
assert.Nil(t, res)
}

// No error - With params
{
var realm = "master"
var userID = "123-456-789"
var actions = []string{"action1", "action2"}
var ctx = context.Background()
var req = make(map[string]string)
req["realm"] = realm
req["userID"] = userID
req["client_id"] = "123789"
req["redirect_uri"] = "http://redirect.com"
req["toto"] = "tutu" // Check this param is not transmitted
actionsJSON, _ := json.Marshal(actions)
req["actions"] = string(actionsJSON)

mockManagementComponent.EXPECT().ExecuteActionsEmail(ctx, realm, userID, actions, "client_id", req["client_id"], "redirect_uri", req["redirect_uri"]).Return(nil).Times(1)
var res, err = e(ctx, req)
assert.Nil(t, err)
assert.Nil(t, res)
}

// Error - Unmarshalling error
{

var realm = "master"
var userID = "123-456-789"
var ctx = context.Background()
var req = make(map[string]string)
req["realm"] = realm
req["userID"] = userID
req["client_id"] = "123789"
req["redirect_uri"] = "http://redirect.com"
req["actions"] = string("actions")

var res, err = e(ctx, req)
assert.NotNil(t, err)
assert.Nil(t, res)
}
}

func TestGetCredentialsForUserEndpoint(t *testing.T) {
var mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
Expand Down
17 changes: 17 additions & 0 deletions pkg/management/mock/component.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pkg/management/mock/keycloak_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 91df8e9

Please sign in to comment.