2021-08-26 08:39:35 +00:00
|
|
|
import KeycloakAdminClient from "@keycloak/keycloak-admin-client";
|
|
|
|
import type UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation";
|
|
|
|
import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation";
|
2021-12-10 12:24:26 +00:00
|
|
|
import type ClientScopeRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientScopeRepresentation";
|
2022-01-07 12:56:27 +00:00
|
|
|
import type RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
|
|
|
|
import type UserProfileConfig from "@keycloak/keycloak-admin-client/lib/defs/userProfileConfig";
|
2022-02-02 10:33:57 +00:00
|
|
|
import { merge } from "lodash-es";
|
2021-02-09 12:32:41 +00:00
|
|
|
|
|
|
|
export default class AdminClient {
|
|
|
|
private client: KeycloakAdminClient;
|
|
|
|
constructor() {
|
|
|
|
this.client = new KeycloakAdminClient({
|
2021-07-30 18:12:32 +00:00
|
|
|
baseUrl: `${Cypress.env("KEYCLOAK_SERVER")}/auth`,
|
2021-02-09 12:32:41 +00:00
|
|
|
realmName: "master",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private async login() {
|
|
|
|
await this.client.auth({
|
|
|
|
username: "admin",
|
|
|
|
password: "admin",
|
|
|
|
grantType: "password",
|
|
|
|
clientId: "admin-cli",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-07 12:56:27 +00:00
|
|
|
async createRealm(realm: string, payload?: RealmRepresentation) {
|
2021-05-06 05:31:40 +00:00
|
|
|
await this.login();
|
2022-01-07 12:56:27 +00:00
|
|
|
await this.client.realms.create({ realm, ...payload });
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateRealm(realm: string, payload: RealmRepresentation) {
|
|
|
|
await this.login();
|
|
|
|
await this.client.realms.update({ realm }, payload);
|
2021-05-06 05:31:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 12:32:41 +00:00
|
|
|
async deleteRealm(realm: string) {
|
|
|
|
await this.login();
|
|
|
|
await this.client.realms.del({ realm });
|
|
|
|
}
|
2021-02-28 20:02:31 +00:00
|
|
|
|
2021-04-01 14:14:19 +00:00
|
|
|
async createClient(client: ClientRepresentation) {
|
|
|
|
await this.login();
|
|
|
|
await this.client.clients.create(client);
|
|
|
|
}
|
2021-05-06 05:31:40 +00:00
|
|
|
|
2021-02-28 20:02:31 +00:00
|
|
|
async deleteClient(clientName: string) {
|
|
|
|
await this.login();
|
|
|
|
const client = (
|
|
|
|
await this.client.clients.find({ clientId: clientName })
|
|
|
|
)[0];
|
|
|
|
await this.client.clients.del({ id: client.id! });
|
|
|
|
}
|
2021-03-16 12:37:57 +00:00
|
|
|
|
2022-02-02 11:44:52 +00:00
|
|
|
async createGroup(groupName: string) {
|
|
|
|
await this.login();
|
|
|
|
return await this.client.groups.create({ name: groupName });
|
|
|
|
}
|
|
|
|
|
2021-03-16 12:37:57 +00:00
|
|
|
async createSubGroups(groups: string[]) {
|
|
|
|
await this.login();
|
|
|
|
let parentGroup = undefined;
|
|
|
|
const createdGroups = [];
|
|
|
|
for (const group of groups) {
|
|
|
|
if (!parentGroup) {
|
|
|
|
parentGroup = await this.client.groups.create({ name: group });
|
|
|
|
} else {
|
|
|
|
parentGroup = await this.client.groups.setOrCreateChild(
|
|
|
|
{ id: parentGroup.id },
|
|
|
|
{ name: group }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
createdGroups.push(parentGroup);
|
|
|
|
}
|
|
|
|
return createdGroups;
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteGroups() {
|
|
|
|
await this.login();
|
|
|
|
const groups = await this.client.groups.find();
|
|
|
|
for (const group of groups) {
|
|
|
|
await this.client.groups.del({ id: group.id! });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 14:07:49 +00:00
|
|
|
async createUser(user: UserRepresentation) {
|
|
|
|
await this.login();
|
|
|
|
return await this.client.users.create(user);
|
|
|
|
}
|
|
|
|
|
2021-03-16 12:37:57 +00:00
|
|
|
async createUserInGroup(username: string, groupId: string) {
|
|
|
|
await this.login();
|
2021-03-24 14:07:49 +00:00
|
|
|
const user = await this.createUser({ username, enabled: true });
|
2021-03-16 12:37:57 +00:00
|
|
|
await this.client.users.addToGroup({ id: user.id!, groupId });
|
|
|
|
}
|
2021-05-06 05:31:40 +00:00
|
|
|
|
|
|
|
async deleteUser(username: string) {
|
|
|
|
await this.login();
|
|
|
|
const user = await this.client.users.find({ username });
|
|
|
|
await this.client.users.del({ id: user[0].id! });
|
|
|
|
}
|
2021-12-10 12:24:26 +00:00
|
|
|
|
|
|
|
async createClientScope(scope: ClientScopeRepresentation) {
|
|
|
|
await this.login();
|
|
|
|
return await this.client.clientScopes.create(scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteClientScope(clientScopeName: string) {
|
|
|
|
await this.login();
|
|
|
|
const clientScope = await this.client.clientScopes.findOneByName({
|
|
|
|
name: clientScopeName,
|
|
|
|
});
|
|
|
|
return await this.client.clientScopes.del({ id: clientScope?.id! });
|
|
|
|
}
|
|
|
|
|
|
|
|
async addDefaultClientScopeInClient(
|
|
|
|
clientScopeName: string,
|
|
|
|
clientId: string
|
|
|
|
) {
|
|
|
|
await this.login();
|
|
|
|
const scope = await this.client.clientScopes.findOneByName({
|
|
|
|
name: clientScopeName,
|
|
|
|
});
|
|
|
|
const client = await this.client.clients.find({ clientId: clientId });
|
|
|
|
return await this.client.clients.addDefaultClientScope({
|
|
|
|
id: client[0]?.id!,
|
|
|
|
clientScopeId: scope?.id!,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeDefaultClientScopeInClient(
|
|
|
|
clientScopeName: string,
|
|
|
|
clientId: string
|
|
|
|
) {
|
|
|
|
await this.login();
|
|
|
|
const scope = await this.client.clientScopes.findOneByName({
|
|
|
|
name: clientScopeName,
|
|
|
|
});
|
|
|
|
const client = await this.client.clients.find({ clientId: clientId });
|
|
|
|
return await this.client.clients.delDefaultClientScope({
|
|
|
|
id: client[0]?.id!,
|
|
|
|
clientScopeId: scope?.id!,
|
|
|
|
});
|
|
|
|
}
|
2022-01-07 12:56:27 +00:00
|
|
|
|
|
|
|
async patchUserProfile(realm: string, payload: UserProfileConfig) {
|
|
|
|
await this.login();
|
|
|
|
|
|
|
|
const currentProfile = await this.client.users.getProfile({ realm });
|
|
|
|
|
|
|
|
await this.client.users.updateProfile(
|
|
|
|
merge(currentProfile, payload, { realm })
|
|
|
|
);
|
|
|
|
}
|
2021-02-09 12:32:41 +00:00
|
|
|
}
|