From d30f00d2161fcd4991417a19cf29eb0f801cb490 Mon Sep 17 00:00:00 2001 From: Hugo Renard Date: Wed, 26 Jan 2022 19:44:05 +0100 Subject: [PATCH] feat: add GetAdminEvents --- client.go | 22 ++++++++++++++++++++++ models.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/client.go b/client.go index 9f06b215..42c9cf2e 100644 --- a/client.go +++ b/client.go @@ -4090,6 +4090,28 @@ func (g *GoCloak) GetEvents(ctx context.Context, token string, realm string, par return result, nil } +// GetAdminEvents returns admin events +func (g *GoCloak) GetAdminEvents(ctx context.Context, token string, realm string, params GetAdminEventsParams) ([]*AdminEventRepresentation, error) { + const errMessage = "could not get admin events" + + queryParams, err := GetQueryParams(params) + if err != nil { + return nil, errors.Wrap(err, errMessage) + } + + var result []*AdminEventRepresentation + resp, err := g.GetRequestWithBearerAuth(ctx, token). + SetResult(&result). + SetQueryParams(queryParams). + Get(g.getAdminRealmURL(realm, "admin-events")) + + if err := checkForError(resp, err, errMessage); err != nil { + return nil, err + } + + return result, nil +} + // GetClientScopesScopeMappingsRealmRolesAvailable returns realm-level roles that are available to attach to this client scope func (g *GoCloak) GetClientScopesScopeMappingsRealmRolesAvailable(ctx context.Context, token, realm, clientScopeID string) ([]*Role, error) { const errMessage = "could not get available realm-level roles with the client-scope" diff --git a/models.go b/models.go index ee051217..89923774 100644 --- a/models.go +++ b/models.go @@ -1359,6 +1359,40 @@ type EventRepresentation struct { Details map[string]string `json:"details,omitempty"` } +// GetAdminEventsParams represents the optional parameters for getting events +type GetAdminEventsParams struct { + AuthClient *string `json:"authClient,omitempty"` + AuthIpAddress *string `json:"authIpAddress,omitempty"` + AuthRealm *string `json:"authRealm,omitempty"` + AuthUser *string `json:"authUser,omitempty"` + DateFrom *string `json:"dateFrom,omitempty"` + DateTo *string `json:"dateTo,omitempty"` + First *int32 `json:"first,omitempty"` + Max *int32 `json:"max,omitempty"` + OperationTypes []string `json:"operationTypes,omitempty"` + ResourcePath *string `json:"resourcePath,omitempty"` + ResourceTypes []string `json:"resourceTypes,omitempty"` +} + +// AdminEventRepresentation is a representation of an Admin Event +type AdminEventRepresentation struct { + Time int64 `json:"time,omitempty"` + OperationType *string `json:"operationType,omitempty"` + RealmID *string `json:"realmId,omitempty"` + ResourceType *string `json:"resourceType,omitempty"` + ResourcePath *string `json:"resourcePath,omitempty"` + AuthDetails *AdminEventAuthDetailsRepresentation `json:"authDetails,omitempty"` + Representation *string `json:"representation,omitempty"` +} + +// AdminEventAuthDetailsRepresentation is a representation of an Admin Event Details +type AdminEventAuthDetailsRepresentation struct { + RealmID *string `json:"realmId,omitempty"` + ClientID *string `json:"clientId,omitempty"` + UserID *string `json:"userId,omitempty"` + IPAddress *string `json:"ipAddress,omitempty"` +} + // CredentialRepresentation is a representations of the credentials // v7: https://www.keycloak.org/docs-api/7.0/rest-api/index.html#_credentialrepresentation // v8: https://www.keycloak.org/docs-api/8.0/rest-api/index.html#_credentialrepresentation