Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
create endpoint for user storage provider (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
edewit authored Feb 15, 2021
1 parent a9902e5 commit cccb2fa
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Keycloak, {
KeycloakInstance,
} from 'keycloak-js';
import {Sessions} from './resources/sessions';
import {UserStorageProvider} from './resources/userStorageProvider';

export interface ConnectionConfig {
baseUrl?: string;
Expand All @@ -30,6 +31,7 @@ export interface ConnectionConfig {
export class KeycloakAdminClient {
// Resources
public users: Users;
public userStorageProvider: UserStorageProvider;
public groups: Groups;
public roles: Roles;
public clients: Clients;
Expand Down Expand Up @@ -61,6 +63,7 @@ export class KeycloakAdminClient {

// Initialize resources
this.users = new Users(this);
this.userStorageProvider = new UserStorageProvider(this);
this.groups = new Groups(this);
this.roles = new Roles(this);
this.clients = new Clients(this);
Expand Down
12 changes: 12 additions & 0 deletions src/defs/synchronizationResultRepresentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* https://www.keycloak.org/docs-api/11.0/rest-api/index.html#_synchronizationresult
*/

export default interface SynchronizationResultRepresentation {
ignored?: boolean;
added?: number;
updated?: number;
removed?: number;
failed?: number;
status?: string;
}
60 changes: 60 additions & 0 deletions src/resources/userStorageProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {KeycloakAdminClient} from '../client';
import SynchronizationResultRepresentation from '../defs/synchronizationResultRepresentation';
import Resource from './resource';

type ActionType = 'triggerFullSync' | 'triggerChangedUsersSync';
type DirectionType = 'fedToKeycloak' | 'keycloakToFed';
type NameResponse = {
id: string;
name: string;
};

export class UserStorageProvider extends Resource<{realm?: string}> {
public name = this.makeRequest<{id: string}, NameResponse>({
method: 'GET',
path: '/{id}/name',
urlParamKeys: ['id'],
});

public removeImportedUsers = this.makeRequest<{id: string}, void>({
method: 'POST',
path: '/{id}/remove-imported-users',
urlParamKeys: ['id'],
});

public sync = this.makeRequest<
{id: string; action?: ActionType},
SynchronizationResultRepresentation
>({
method: 'POST',
path: '/{id}/sync',
urlParamKeys: ['id'],
queryParamKeys: ['action'],
});

public unlinkUsers = this.makeRequest<{id: string}, void>({
method: 'POST',
path: '/{id}/unlink-users',
urlParamKeys: ['id'],
});

public mappersSync = this.makeRequest<
{id: string; parentId: string; direction?: DirectionType},
SynchronizationResultRepresentation
>({
method: 'POST',
path: '/{parentId}/mappers/{id}/sync',
urlParamKeys: ['id', 'parentId'],
queryParamKeys: ['direction'],
});

constructor(client: KeycloakAdminClient) {
super(client, {
path: '/admin/realms/{realm}/user-storage',
getUrlParams: () => ({
realm: client.realmName,
}),
getBaseUrl: () => client.baseUrl,
});
}
}
52 changes: 52 additions & 0 deletions test/userStorageProvider.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// tslint:disable:no-unused-expression
import * as chai from 'chai';
import {KeycloakAdminClient} from '../src/client';
import {credentials} from './constants';
import faker from 'faker';

import ComponentRepresentation from '../src/defs/componentRepresentation';

const expect = chai.expect;

describe('Users federation provider', () => {
let kcAdminClient: KeycloakAdminClient;
let currentUserFed: ComponentRepresentation;

before(async () => {
kcAdminClient = new KeycloakAdminClient();
await kcAdminClient.auth(credentials);

const name = faker.internet.userName();
currentUserFed = await kcAdminClient.components.create({
name,
parentId: 'master',
providerId: 'ldap',
providerType: 'org.keycloak.storage.UserStorageProvider',
});
});

after(async () => {
await kcAdminClient.components.del({
id: currentUserFed.id,
});
});

it('list storage provider', async () => {
const name = await kcAdminClient.userStorageProvider.name({
id: currentUserFed.id,
});
expect(name).to.be.ok;
});

it('remove imported users', async () => {
await kcAdminClient.userStorageProvider.removeImportedUsers({
id: currentUserFed.id,
});
});

it('unlink users', async () => {
await kcAdminClient.userStorageProvider.unlinkUsers({
id: currentUserFed.id,
});
});
});

0 comments on commit cccb2fa

Please sign in to comment.