diff --git a/src/client.ts b/src/client.ts index ab8e82fc..5e4ee8df 100644 --- a/src/client.ts +++ b/src/client.ts @@ -19,6 +19,7 @@ import Keycloak, { KeycloakInitOptions, KeycloakInstance, } from 'keycloak-js'; +import {Sessions} from './resources/sessions'; export interface ConnectionConfig { baseUrl?: string; @@ -39,6 +40,7 @@ export class KeycloakAdminClient { public serverInfo: ServerInfo; public whoAmI: WhoAmI; public attackDetection: AttackDetection; + public sessions: Sessions; public authenticationManagement: AuthenticationManagement; // Members @@ -69,6 +71,7 @@ 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); } diff --git a/src/resources/sessions.ts b/src/resources/sessions.ts new file mode 100644 index 00000000..6dceb640 --- /dev/null +++ b/src/resources/sessions.ts @@ -0,0 +1,19 @@ +import Resource from './resource'; +import KeycloakAdminClient from '..'; + +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/test/sessions.spec.ts b/test/sessions.spec.ts new file mode 100644 index 00000000..b0042ca1 --- /dev/null +++ b/test/sessions.spec.ts @@ -0,0 +1,23 @@ +// tslint:disable:no-unused-expression +import * as chai from 'chai'; +import {KeycloakAdminClient} from '../src/client'; +import {credentials} from './constants'; + +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'); + }); + +});