From 998b708c7867699335bd907a9460e5247a1227e6 Mon Sep 17 00:00:00 2001 From: Jon Koops Date: Mon, 13 Feb 2023 13:12:08 +0100 Subject: [PATCH] Implement client session statuses endpoint properly (#17033) --- js/libs/keycloak-admin-client/src/client.ts | 3 --- .../src/defs/clientSessionStat.ts | 6 +++++ .../src/resources/realms.ts | 10 +++++++++ .../src/resources/sessions.ts | 18 --------------- .../keycloak-admin-client/test/realms.spec.ts | 7 ++++++ .../test/sessions.spec.ts | 22 ------------------- 6 files changed, 23 insertions(+), 43 deletions(-) create mode 100644 js/libs/keycloak-admin-client/src/defs/clientSessionStat.ts delete mode 100644 js/libs/keycloak-admin-client/src/resources/sessions.ts delete mode 100644 js/libs/keycloak-admin-client/test/sessions.spec.ts diff --git a/js/libs/keycloak-admin-client/src/client.ts b/js/libs/keycloak-admin-client/src/client.ts index 655bee8e74..3256f48d10 100644 --- a/js/libs/keycloak-admin-client/src/client.ts +++ b/js/libs/keycloak-admin-client/src/client.ts @@ -11,7 +11,6 @@ import { IdentityProviders } from "./resources/identityProviders.js"; import { Realms } from "./resources/realms.js"; import { Roles } from "./resources/roles.js"; import { ServerInfo } from "./resources/serverInfo.js"; -import { Sessions } from "./resources/sessions.js"; import { Users } from "./resources/users.js"; import { UserStorageProvider } from "./resources/userStorageProvider.js"; import { WhoAmI } from "./resources/whoAmI.js"; @@ -44,7 +43,6 @@ export class KeycloakAdminClient { public serverInfo: ServerInfo; public whoAmI: WhoAmI; public attackDetection: AttackDetection; - public sessions: Sessions; public authenticationManagement: AuthenticationManagement; public cache: Cache; @@ -78,7 +76,6 @@ export class KeycloakAdminClient { this.authenticationManagement = new AuthenticationManagement(this); this.serverInfo = new ServerInfo(this); this.whoAmI = new WhoAmI(this); - this.sessions = new Sessions(this); this.attackDetection = new AttackDetection(this); this.cache = new Cache(this); } diff --git a/js/libs/keycloak-admin-client/src/defs/clientSessionStat.ts b/js/libs/keycloak-admin-client/src/defs/clientSessionStat.ts new file mode 100644 index 0000000000..b693614500 --- /dev/null +++ b/js/libs/keycloak-admin-client/src/defs/clientSessionStat.ts @@ -0,0 +1,6 @@ +export interface ClientSessionStat { + id: string; + clientId: string; + active: string; + offline: string; +} diff --git a/js/libs/keycloak-admin-client/src/resources/realms.ts b/js/libs/keycloak-admin-client/src/resources/realms.ts index 20e8f4fea4..7f86458c3e 100644 --- a/js/libs/keycloak-admin-client/src/resources/realms.ts +++ b/js/libs/keycloak-admin-client/src/resources/realms.ts @@ -17,6 +17,7 @@ import type GlobalRequestResult from "../defs/globalRequestResult.js"; import type GroupRepresentation from "../defs/groupRepresentation.js"; import type { ManagementPermissionReference } from "../defs/managementPermissionReference.js"; import type ComponentTypeRepresentation from "../defs/componentTypeRepresentation.js"; +import type { ClientSessionStat } from "../defs/clientSessionStat.js"; export class Realms extends Resource { /** @@ -294,6 +295,15 @@ export class Realms extends Resource { /** * Sessions */ + public getClientSessionStats = this.makeRequest< + { realm: string }, + ClientSessionStat[] + >({ + method: "GET", + path: "/{realm}/client-session-stats", + urlParamKeys: ["realm"], + }); + public logoutAll = this.makeRequest<{ realm: string }, void>({ method: "POST", path: "/{realm}/logout-all", diff --git a/js/libs/keycloak-admin-client/src/resources/sessions.ts b/js/libs/keycloak-admin-client/src/resources/sessions.ts deleted file mode 100644 index b9105411b6..0000000000 --- a/js/libs/keycloak-admin-client/src/resources/sessions.ts +++ /dev/null @@ -1,18 +0,0 @@ -import Resource from "./resource.js"; -import type KeycloakAdminClient from "../index.js"; - -export class Sessions extends Resource<{ realm?: string }> { - public find = this.makeRequest<{}, Record[]>({ - method: "GET", - }); - - constructor(client: KeycloakAdminClient) { - super(client, { - path: "/admin/realms/{realm}/client-session-stats", - getUrlParams: () => ({ - realm: client.realmName, - }), - getBaseUrl: () => client.baseUrl, - }); - } -} diff --git a/js/libs/keycloak-admin-client/test/realms.spec.ts b/js/libs/keycloak-admin-client/test/realms.spec.ts index 3a772d9b82..e333b89b87 100644 --- a/js/libs/keycloak-admin-client/test/realms.spec.ts +++ b/js/libs/keycloak-admin-client/test/realms.spec.ts @@ -374,6 +374,13 @@ describe("Realms", () => { currentRealmName = created.realmName; }); + it("gets client session stats", async () => { + const sessionStats = await kcAdminClient.realms.getClientSessionStats({ + realm: currentRealmName, + }); + expect(sessionStats).to.be.ok; + }); + it("push revocation", async () => { const push = await kcAdminClient.realms.pushRevocation({ realm: currentRealmName, diff --git a/js/libs/keycloak-admin-client/test/sessions.spec.ts b/js/libs/keycloak-admin-client/test/sessions.spec.ts deleted file mode 100644 index 762ca35e6a..0000000000 --- a/js/libs/keycloak-admin-client/test/sessions.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -// tslint:disable:no-unused-expression -import * as chai from "chai"; -import { KeycloakAdminClient } from "../src/client.js"; -import { credentials } from "./constants.js"; - -const expect = chai.expect; - -describe("Sessions", () => { - let client: KeycloakAdminClient; - - before(async () => { - client = new KeycloakAdminClient(); - await client.auth(credentials); - }); - - it("list sessions", async () => { - const sessions = await client.sessions.find(); - expect(sessions).to.be.ok; - expect(sessions.length).to.be.eq(1); - expect(sessions[0].clientId).to.be.eq("admin-cli"); - }); -});