Skip to content

Commit

Permalink
Made the params for the GetClientUserSessions and GetClientOfflineSes…
Browse files Browse the repository at this point in the history
…sions variadic functions
  • Loading branch information
Dmitry Zakovyrin committed Jul 14, 2023
1 parent c716606 commit e32f4b7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ type GoCloak interface {
ClearUserCache(ctx context.Context, token, realm string) error
ClearKeysCache(ctx context.Context, token, realm string) error

GetClientUserSessions(ctx context.Context, token, realm, idOfClient string, params GetClientUserSessionsParams) ([]*UserSessionRepresentation, error)
GetClientOfflineSessions(ctx context.Context, token, realm, idOfClient string, params GetClientUserSessionsParams) ([]*UserSessionRepresentation, error)
GetClientUserSessions(ctx context.Context, token, realm, idOfClient string, params ...GetClientUserSessionsParams) ([]*UserSessionRepresentation, error)
GetClientOfflineSessions(ctx context.Context, token, realm, idOfClient string, params ...GetClientUserSessionsParams) ([]*UserSessionRepresentation, error)
GetUserSessions(ctx context.Context, token, realm, userID string) ([]*UserSessionRepresentation, error)
GetUserOfflineSessionsForClient(ctx context.Context, token, realm, userID, idOfClient string) ([]*UserSessionRepresentation, error)

Expand Down
26 changes: 18 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1451,13 +1451,18 @@ func (g *GoCloak) RegenerateClientSecret(ctx context.Context, token, realm, idOf
}

// GetClientOfflineSessions returns offline sessions associated with the client
func (g *GoCloak) GetClientOfflineSessions(ctx context.Context, token, realm, idOfClient string, params GetClientUserSessionsParams) ([]*UserSessionRepresentation, error) {
func (g *GoCloak) GetClientOfflineSessions(ctx context.Context, token, realm, idOfClient string, params ...GetClientUserSessionsParams) ([]*UserSessionRepresentation, error) {
const errMessage = "could not get client offline sessions"
var res []*UserSessionRepresentation

queryParams, err := GetQueryParams(params)
if err != nil {
return nil, errors.Wrap(err, errMessage)
queryParams := map[string]string{}
if params != nil && len(params) > 0 {
var err error

queryParams, err = GetQueryParams(params[0])
if err != nil {
return nil, errors.Wrap(err, errMessage)

Check warning on line 1464 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L1464

Added line #L1464 was not covered by tests
}
}

resp, err := g.GetRequestWithBearerAuth(ctx, token).
Expand All @@ -1473,13 +1478,18 @@ func (g *GoCloak) GetClientOfflineSessions(ctx context.Context, token, realm, id
}

// GetClientUserSessions returns user sessions associated with the client
func (g *GoCloak) GetClientUserSessions(ctx context.Context, token, realm, idOfClient string, params GetClientUserSessionsParams) ([]*UserSessionRepresentation, error) {
func (g *GoCloak) GetClientUserSessions(ctx context.Context, token, realm, idOfClient string, params ...GetClientUserSessionsParams) ([]*UserSessionRepresentation, error) {
const errMessage = "could not get client user sessions"
var res []*UserSessionRepresentation

queryParams, err := GetQueryParams(params)
if err != nil {
return nil, errors.Wrap(err, errMessage)
queryParams := map[string]string{}
if params != nil && len(params) > 0 {
var err error

queryParams, err = GetQueryParams(params[0])
if err != nil {
return nil, errors.Wrap(err, errMessage)

Check warning on line 1491 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L1491

Added line #L1491 was not covered by tests
}
}

resp, err := g.GetRequestWithBearerAuth(ctx, token).
Expand Down
22 changes: 22 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3719,6 +3719,15 @@ func Test_GetClientUserSessions(t *testing.T) {
)
require.NoError(t, err, "Login failed")
token := GetAdminToken(t, client)
allSessionsWithoutParams, err := client.GetClientUserSessions(
context.Background(),
token.AccessToken,
cfg.GoCloak.Realm,
gocloakClientID,
)
require.NoError(t, err, "GetClientUserSessions failed")
require.NotEmpty(t, allSessionsWithoutParams, "GetClientUserSessions returned an empty list")

allSessions, err := client.GetClientUserSessions(
context.Background(),
token.AccessToken,
Expand All @@ -3728,6 +3737,8 @@ func Test_GetClientUserSessions(t *testing.T) {
)
require.NoError(t, err, "GetClientUserSessions failed")
require.NotEmpty(t, allSessions, "GetClientUserSessions returned an empty list")
require.Equal(t, allSessionsWithoutParams, allSessions,
"GetClientUserSessions with and without params are not the same")

sessions, err := client.GetClientUserSessions(
context.Background(),
Expand Down Expand Up @@ -3896,6 +3907,17 @@ func Test_GetClientOfflineSessions(t *testing.T) {
)
require.NoError(t, err, "GetClientOfflineSessions failed")
require.NotEmpty(t, sessions, "GetClientOfflineSessions returned an empty list")

sessionsWithoutParams, err := client.GetClientOfflineSessions(
context.Background(),
token.AccessToken,
cfg.GoCloak.Realm,
gocloakClientID,
)
require.NoError(t, err, "GetClientOfflineSessions failed")
require.NotEmpty(t, sessions, "GetClientOfflineSessions returned an empty list")
require.Equal(t, sessions, sessionsWithoutParams,
"GetClientOfflineSessions with and without params are not the same")
}

func Test_ClientSecret(t *testing.T) {
Expand Down

0 comments on commit e32f4b7

Please sign in to comment.