Skip to content

Commit 7f490d9

Browse files
mopo3ulaDmitry Zakovyrin
and
Dmitry Zakovyrin
authored
Added params for getting sessions associated with client (#436)
* Added params GetClientUserSessionsParams for GetClientUserSessions and GetClientOfflineSessions * Made the params for the GetClientUserSessions and GetClientOfflineSessions variadic functions --------- Co-authored-by: Dmitry Zakovyrin <[email protected]>
1 parent f08754e commit 7f490d9

File tree

5 files changed

+87
-7
lines changed

5 files changed

+87
-7
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ type GoCloak interface {
280280
ClearUserCache(ctx context.Context, token, realm string) error
281281
ClearKeysCache(ctx context.Context, token, realm string) error
282282

283-
GetClientUserSessions(ctx context.Context, token, realm, idOfClient string) ([]*UserSessionRepresentation, error)
284-
GetClientOfflineSessions(ctx context.Context, token, realm, idOfClient string) ([]*UserSessionRepresentation, error)
283+
GetClientUserSessions(ctx context.Context, token, realm, idOfClient string, params ...GetClientUserSessionsParams) ([]*UserSessionRepresentation, error)
284+
GetClientOfflineSessions(ctx context.Context, token, realm, idOfClient string, params ...GetClientUserSessionsParams) ([]*UserSessionRepresentation, error)
285285
GetUserSessions(ctx context.Context, token, realm, userID string) ([]*UserSessionRepresentation, error)
286286
GetUserOfflineSessionsForClient(ctx context.Context, token, realm, userID, idOfClient string) ([]*UserSessionRepresentation, error)
287287

client.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -1451,12 +1451,23 @@ func (g *GoCloak) RegenerateClientSecret(ctx context.Context, token, realm, idOf
14511451
}
14521452

14531453
// GetClientOfflineSessions returns offline sessions associated with the client
1454-
func (g *GoCloak) GetClientOfflineSessions(ctx context.Context, token, realm, idOfClient string) ([]*UserSessionRepresentation, error) {
1454+
func (g *GoCloak) GetClientOfflineSessions(ctx context.Context, token, realm, idOfClient string, params ...GetClientUserSessionsParams) ([]*UserSessionRepresentation, error) {
14551455
const errMessage = "could not get client offline sessions"
1456-
14571456
var res []*UserSessionRepresentation
1457+
1458+
queryParams := map[string]string{}
1459+
if params != nil && len(params) > 0 {
1460+
var err error
1461+
1462+
queryParams, err = GetQueryParams(params[0])
1463+
if err != nil {
1464+
return nil, errors.Wrap(err, errMessage)
1465+
}
1466+
}
1467+
14581468
resp, err := g.GetRequestWithBearerAuth(ctx, token).
14591469
SetResult(&res).
1470+
SetQueryParams(queryParams).
14601471
Get(g.getAdminRealmURL(realm, "clients", idOfClient, "offline-sessions"))
14611472

14621473
if err := checkForError(resp, err, errMessage); err != nil {
@@ -1467,12 +1478,23 @@ func (g *GoCloak) GetClientOfflineSessions(ctx context.Context, token, realm, id
14671478
}
14681479

14691480
// GetClientUserSessions returns user sessions associated with the client
1470-
func (g *GoCloak) GetClientUserSessions(ctx context.Context, token, realm, idOfClient string) ([]*UserSessionRepresentation, error) {
1481+
func (g *GoCloak) GetClientUserSessions(ctx context.Context, token, realm, idOfClient string, params ...GetClientUserSessionsParams) ([]*UserSessionRepresentation, error) {
14711482
const errMessage = "could not get client user sessions"
1472-
14731483
var res []*UserSessionRepresentation
1484+
1485+
queryParams := map[string]string{}
1486+
if params != nil && len(params) > 0 {
1487+
var err error
1488+
1489+
queryParams, err = GetQueryParams(params[0])
1490+
if err != nil {
1491+
return nil, errors.Wrap(err, errMessage)
1492+
}
1493+
}
1494+
14741495
resp, err := g.GetRequestWithBearerAuth(ctx, token).
14751496
SetResult(&res).
1497+
SetQueryParams(queryParams).
14761498
Get(g.getAdminRealmURL(realm, "clients", idOfClient, "user-sessions"))
14771499

14781500
if err := checkForError(resp, err, errMessage); err != nil {

client_test.go

+51-1
Original file line numberDiff line numberDiff line change
@@ -3719,14 +3719,52 @@ func Test_GetClientUserSessions(t *testing.T) {
37193719
)
37203720
require.NoError(t, err, "Login failed")
37213721
token := GetAdminToken(t, client)
3722+
allSessionsWithoutParams, err := client.GetClientUserSessions(
3723+
context.Background(),
3724+
token.AccessToken,
3725+
cfg.GoCloak.Realm,
3726+
gocloakClientID,
3727+
)
3728+
require.NoError(t, err, "GetClientUserSessions failed")
3729+
require.NotEmpty(t, allSessionsWithoutParams, "GetClientUserSessions returned an empty list")
3730+
3731+
allSessions, err := client.GetClientUserSessions(
3732+
context.Background(),
3733+
token.AccessToken,
3734+
cfg.GoCloak.Realm,
3735+
gocloakClientID,
3736+
gocloak.GetClientUserSessionsParams{},
3737+
)
3738+
require.NoError(t, err, "GetClientUserSessions failed")
3739+
require.NotEmpty(t, allSessions, "GetClientUserSessions returned an empty list")
3740+
require.Equal(t, allSessionsWithoutParams, allSessions,
3741+
"GetClientUserSessions with and without params are not the same")
3742+
37223743
sessions, err := client.GetClientUserSessions(
37233744
context.Background(),
37243745
token.AccessToken,
37253746
cfg.GoCloak.Realm,
37263747
gocloakClientID,
3748+
gocloak.GetClientUserSessionsParams{
3749+
Max: gocloak.IntP(1),
3750+
},
37273751
)
37283752
require.NoError(t, err, "GetClientUserSessions failed")
3729-
require.NotEmpty(t, sessions, "GetClientUserSessions returned an empty list")
3753+
require.Len(t, sessions, 1)
3754+
3755+
sessions, err = client.GetClientUserSessions(
3756+
context.Background(),
3757+
token.AccessToken,
3758+
cfg.GoCloak.Realm,
3759+
gocloakClientID,
3760+
gocloak.GetClientUserSessionsParams{
3761+
Max: gocloak.IntP(1),
3762+
First: gocloak.IntP(1),
3763+
},
3764+
)
3765+
require.NoError(t, err, "GetClientUserSessions failed")
3766+
require.Len(t, sessions, 1)
3767+
require.Equal(t, *allSessions[1].ID, *sessions[0].ID)
37303768
}
37313769

37323770
func findProtocolMapperByID(t *testing.T, client *gocloak.Client, id string) *gocloak.ProtocolMapperRepresentation {
@@ -3865,9 +3903,21 @@ func Test_GetClientOfflineSessions(t *testing.T) {
38653903
token.AccessToken,
38663904
cfg.GoCloak.Realm,
38673905
gocloakClientID,
3906+
gocloak.GetClientUserSessionsParams{},
3907+
)
3908+
require.NoError(t, err, "GetClientOfflineSessions failed")
3909+
require.NotEmpty(t, sessions, "GetClientOfflineSessions returned an empty list")
3910+
3911+
sessionsWithoutParams, err := client.GetClientOfflineSessions(
3912+
context.Background(),
3913+
token.AccessToken,
3914+
cfg.GoCloak.Realm,
3915+
gocloakClientID,
38683916
)
38693917
require.NoError(t, err, "GetClientOfflineSessions failed")
38703918
require.NotEmpty(t, sessions, "GetClientOfflineSessions returned an empty list")
3919+
require.Equal(t, sessions, sessionsWithoutParams,
3920+
"GetClientOfflineSessions with and without params are not the same")
38713921
}
38723922

38733923
func Test_ClientSecret(t *testing.T) {

model_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ func TestStringerOmitEmpty(t *testing.T) {
335335
&gocloak.GetClientsParams{},
336336
&gocloak.RequestingPartyTokenOptions{},
337337
&gocloak.RequestingPartyPermission{},
338+
&gocloak.GetClientUserSessionsParams{},
338339
}
339340

340341
for _, custom := range customs {

models.go

+7
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,12 @@ type ManagementPermissionRepresentation struct {
14161416
ScopePermissions *map[string]string `json:"scopePermissions,omitempty"`
14171417
}
14181418

1419+
// GetClientUserSessionsParams represents the optional parameters for getting user sessions associated with the client
1420+
type GetClientUserSessionsParams struct {
1421+
First *int `json:"first,string,omitempty"`
1422+
Max *int `json:"max,string,omitempty"`
1423+
}
1424+
14191425
// prettyStringStruct returns struct formatted into pretty string
14201426
func prettyStringStruct(t interface{}) string {
14211427
json, err := json.MarshalIndent(t, "", "\t")
@@ -1509,3 +1515,4 @@ func (v *GetResourcePoliciesParams) String() string { return pre
15091515
func (v *CredentialRepresentation) String() string { return prettyStringStruct(v) }
15101516
func (v *RequiredActionProviderRepresentation) String() string { return prettyStringStruct(v) }
15111517
func (v *BruteForceStatus) String() string { return prettyStringStruct(v) }
1518+
func (v *GetClientUserSessionsParams) String() string { return prettyStringStruct(v) }

0 commit comments

Comments
 (0)