Skip to content

Commit

Permalink
Merge pull request Nerzal#164 from FridaFino/master
Browse files Browse the repository at this point in the history
add groups count
  • Loading branch information
SVilgelm authored May 4, 2020
2 parents 2cf3e2a + 819472c commit 33311f5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ type GoCloak interface {
GetUserGroups(accessToken string, realm string, userID string) ([]*UserGroup, error)
GetComponents(accessToken string, realm string) ([]*Component, error)
GetGroups(accessToken string, realm string, params GetGroupsParams) ([]*Group, error)
GetGroupsCount(token string, realm string) (int, error)
GetGroup(accessToken string, realm, groupID string) (*Group, error)
GetDefaultGroups(accessToken string, realm string) ([]*Group, error)
AddDefaultGroup(accessToken string, realm string, groupID string) error
Expand Down
30 changes: 24 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ const (
urlSeparator string = "/"
)

var authAdminRealms = makeURL("auth", "admin", "realms")
var authRealms = makeURL("auth", "realms")
var tokenEndpoint = makeURL("protocol", "openid-connect", "token")
var logoutEndpoint = makeURL("protocol", "openid-connect", "logout")
var openIDConnect = makeURL("protocol", "openid-connect")
var (
authAdminRealms = makeURL("auth", "admin", "realms")
authRealms = makeURL("auth", "realms")
tokenEndpoint = makeURL("protocol", "openid-connect", "token")
logoutEndpoint = makeURL("protocol", "openid-connect", "logout")
openIDConnect = makeURL("protocol", "openid-connect")
)

func makeURL(path ...string) string {
return strings.Join(path, urlSeparator)
Expand Down Expand Up @@ -146,7 +148,7 @@ func (client *gocloak) getAdminRealmURL(realm string, path ...string) string {
}

func (client *gocloak) GetServerInfo(accessToken string) (*ServerInfoRepesentation, error) {
var errMessage = "could not get server info"
errMessage := "could not get server info"
var result ServerInfoRepesentation

resp, err := client.getRequestWithBearerAuth(accessToken).
Expand Down Expand Up @@ -1015,6 +1017,22 @@ func (client *gocloak) GetGroups(token string, realm string, params GetGroupsPar
return result, nil
}

// GetGroupsCount gets the groups count in the realm
func (client *gocloak) GetGroupsCount(token string, realm string) (int, error) {
const errMessage = "could not get groups count"

var result GroupsCount
resp, err := client.getRequestWithBearerAuth(token).
SetResult(&result).
Get(client.getAdminRealmURL(realm, "groups", "count"))

if err := checkForError(resp, err, errMessage); err != nil {
return -1, errors.Wrap(err, errMessage)
}

return result.Count, nil
}

// GetGroupMembers get a list of users of group with id in realm
func (client *gocloak) GetGroupMembers(token string, realm string, groupID string, params GetGroupsParams) ([]*User, error) {
const errMessage = "could not get group members"
Expand Down
14 changes: 13 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,6 @@ func TestGocloak_ExecuteActionsEmail_UpdatePassword(t *testing.T) {
token.AccessToken,
cfg.GoCloak.Realm,
params)

if err != nil {
if err.Error() == "500 Internal Server Error: Failed to send execute actions email" {
return
Expand Down Expand Up @@ -1934,6 +1933,19 @@ func TestGocloak_GetUserCount(t *testing.T) {
assert.NoError(t, err, "GetUserCount failed")
}

func TestGocloak_GetGroupsCount(t *testing.T) {
t.Parallel()
cfg := GetConfig(t)
client := NewClientWithDebug(t)
token := GetAdminToken(t, client)

count, err := client.GetGroupsCount(
token.AccessToken,
cfg.GoCloak.Realm)
t.Logf("Groups in Realm: %d", count)
assert.NoError(t, err, "GetGroupsCount failed")
}

func TestGocloak_AddUserToGroup(t *testing.T) {
t.Parallel()
cfg := GetConfig(t)
Expand Down
2 changes: 2 additions & 0 deletions gocloak.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ type GoCloak interface {
RemoveDefaultGroup(accessToken string, realm string, groupID string) error
// GetGroups gets all groups of the given realm
GetGroups(accessToken string, realm string, params GetGroupsParams) ([]*Group, error)
// GetGroupsCount gets groups count of the given realm
GetGroupsCount(token string, realm string) (int, error)
// GetGroup gets the given group
GetGroup(accessToken string, realm, groupID string) (*Group, error)
// GetGroupMembers get a list of users of group with id in realm
Expand Down
9 changes: 7 additions & 2 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ type Group struct {
RealmRoles []string `json:"realmRoles,omitempty"`
}

// GroupsCount represents the groups count response from keycloak
type GroupsCount struct {
Count int `json:"count"`
}

// GetGroupsParams represents the optional parameters for getting groups
type GetGroupsParams struct {
First *int `json:"first,string,omitempty"`
Expand Down Expand Up @@ -493,10 +498,10 @@ type GroupDefinition struct {

// ResourceRepresentation is a representation of a Resource
type ResourceRepresentation struct {
ID *string `json:"_id,omitempty"` //TODO: is marked "_optional" in template, input error or deliberate?
ID *string `json:"_id,omitempty"` // TODO: is marked "_optional" in template, input error or deliberate?
Attributes map[string][]string `json:"attributes,omitempty"`
DisplayName *string `json:"displayName,omitempty"`
IconURI *string `json:"icon_uri,omitempty"` //TODO: With "_" because that's how it's written down in the template
IconURI *string `json:"icon_uri,omitempty"` // TODO: With "_" because that's how it's written down in the template
Name *string `json:"name,omitempty"`
Owner *ResourceOwnerRepresentation `json:"owner"`
OwnerManagedAccess *bool `json:"ownerManagedAccess"`
Expand Down

0 comments on commit 33311f5

Please sign in to comment.