2021-08-26 08:39:35 +00:00
|
|
|
import KeycloakAdminClient from "@keycloak/keycloak-admin-client";
|
|
|
|
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";
|
2022-02-23 11:05:38 +00:00
|
|
|
import type RoleRepresentation from "@keycloak/keycloak-admin-client/lib/defs/roleRepresentation";
|
2022-11-25 21:11:15 +00:00
|
|
|
import type { RoleMappingPayload } from "@keycloak/keycloak-admin-client/lib/defs/roleRepresentation";
|
2023-11-14 11:04:55 +00:00
|
|
|
import type UserProfileConfig from "@keycloak/keycloak-admin-client/lib/defs/userProfileMetadata";
|
2023-10-06 13:15:39 +00:00
|
|
|
import type UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation";
|
2022-02-02 10:33:57 +00:00
|
|
|
import { merge } from "lodash-es";
|
2021-02-09 12:32:41 +00:00
|
|
|
|
2022-02-24 10:31:46 +00:00
|
|
|
class AdminClient {
|
2023-10-23 18:12:55 +00:00
|
|
|
readonly #client = new KeycloakAdminClient({
|
2022-03-06 15:25:37 +00:00
|
|
|
baseUrl: Cypress.env("KEYCLOAK_SERVER"),
|
2022-02-24 10:31:46 +00:00
|
|
|
realmName: "master",
|
|
|
|
});
|
|
|
|
|
2023-10-23 18:12:55 +00:00
|
|
|
#login() {
|
|
|
|
return this.#client.auth({
|
2021-02-09 12:32:41 +00:00
|
|
|
username: "admin",
|
|
|
|
password: "admin",
|
|
|
|
grantType: "password",
|
|
|
|
clientId: "admin-cli",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-19 13:43:48 +00:00
|
|
|
async loginUser(username: string, password: string, clientId: string) {
|
2023-10-23 18:12:55 +00:00
|
|
|
return this.#client.auth({
|
2022-05-19 13:43:48 +00:00
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
grantType: "password",
|
|
|
|
clientId: clientId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-07 12:56:27 +00:00
|
|
|
async createRealm(realm: string, payload?: RealmRepresentation) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
await this.#client.realms.create({ realm, ...payload });
|
2022-01-07 12:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async updateRealm(realm: string, payload: RealmRepresentation) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
await this.#client.realms.update({ realm }, payload);
|
2021-05-06 05:31:40 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 21:11:15 +00:00
|
|
|
async getRealm(realm: string) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
return await this.#client.realms.findOne({ realm });
|
2022-11-25 21:11:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 12:32:41 +00:00
|
|
|
async deleteRealm(realm: string) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
await this.#client.realms.del({ realm });
|
2021-02-09 12:32:41 +00:00
|
|
|
}
|
2021-02-28 20:02:31 +00:00
|
|
|
|
2023-11-28 13:07:11 +00:00
|
|
|
async createClient(
|
|
|
|
client: ClientRepresentation & {
|
|
|
|
realm?: string;
|
|
|
|
},
|
|
|
|
) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
await this.#client.clients.create(client);
|
2021-04-01 14:14:19 +00:00
|
|
|
}
|
2021-05-06 05:31:40 +00:00
|
|
|
|
2021-02-28 20:02:31 +00:00
|
|
|
async deleteClient(clientName: string) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
2021-02-28 20:02:31 +00:00
|
|
|
const client = (
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#client.clients.find({ clientId: clientName })
|
2021-02-28 20:02:31 +00:00
|
|
|
)[0];
|
2022-08-23 14:30:52 +00:00
|
|
|
|
|
|
|
if (client) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#client.clients.del({ id: client.id! });
|
2022-08-23 14:30:52 +00:00
|
|
|
}
|
2021-02-28 20:02:31 +00:00
|
|
|
}
|
2021-03-16 12:37:57 +00:00
|
|
|
|
2023-11-28 13:07:11 +00:00
|
|
|
async getClient(clientName: string) {
|
|
|
|
await this.#login();
|
|
|
|
return (await this.#client.clients.find({ clientId: clientName }))[0];
|
|
|
|
}
|
|
|
|
|
2022-02-02 11:44:52 +00:00
|
|
|
async createGroup(groupName: string) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
return await this.#client.groups.create({ name: groupName });
|
2022-02-02 11:44:52 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 12:37:57 +00:00
|
|
|
async createSubGroups(groups: string[]) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
2021-03-16 12:37:57 +00:00
|
|
|
let parentGroup = undefined;
|
|
|
|
const createdGroups = [];
|
|
|
|
for (const group of groups) {
|
|
|
|
if (!parentGroup) {
|
2023-10-23 18:12:55 +00:00
|
|
|
parentGroup = await this.#client.groups.create({ name: group });
|
2021-03-16 12:37:57 +00:00
|
|
|
} else {
|
2023-10-23 18:12:55 +00:00
|
|
|
parentGroup = await this.#client.groups.createChildGroup(
|
2021-03-16 12:37:57 +00:00
|
|
|
{ id: parentGroup.id },
|
2023-07-11 14:03:21 +00:00
|
|
|
{ name: group },
|
2021-03-16 12:37:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
createdGroups.push(parentGroup);
|
|
|
|
}
|
|
|
|
return createdGroups;
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteGroups() {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
const groups = await this.#client.groups.find();
|
2021-03-16 12:37:57 +00:00
|
|
|
for (const group of groups) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#client.groups.del({ id: group.id! });
|
2021-03-16 12:37:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 14:07:49 +00:00
|
|
|
async createUser(user: UserRepresentation) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
2023-04-13 13:55:50 +00:00
|
|
|
|
2023-10-23 18:12:55 +00:00
|
|
|
const { id } = await this.#client.users.create(user);
|
|
|
|
const createdUser = await this.#client.users.findOne({ id });
|
2023-04-13 13:55:50 +00:00
|
|
|
|
|
|
|
if (!createdUser) {
|
|
|
|
throw new Error(
|
2023-07-11 14:03:21 +00:00
|
|
|
"Unable to create user, created user could not be found.",
|
2023-04-13 13:55:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return createdUser;
|
2021-03-24 14:07:49 +00:00
|
|
|
}
|
|
|
|
|
2022-10-24 11:43:46 +00:00
|
|
|
async updateUser(id: string, payload: UserRepresentation) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
return this.#client.users.update({ id }, payload);
|
2022-10-24 11:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async getAdminUser() {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
const [user] = await this.#client.users.find({ username: "admin" });
|
2022-10-24 11:43:46 +00:00
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2022-06-27 08:47:41 +00:00
|
|
|
async addUserToGroup(userId: string, groupId: string) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
await this.#client.users.addToGroup({ id: userId, groupId });
|
2022-06-27 08:47:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 12:37:57 +00:00
|
|
|
async createUserInGroup(username: string, groupId: string) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
2021-03-24 14:07:49 +00:00
|
|
|
const user = await this.createUser({ username, enabled: true });
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#client.users.addToGroup({ id: user.id!, groupId });
|
2021-03-16 12:37:57 +00:00
|
|
|
}
|
2021-05-06 05:31:40 +00:00
|
|
|
|
2022-11-25 21:11:15 +00:00
|
|
|
async addRealmRoleToUser(userId: string, roleName: string) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
2022-11-25 21:11:15 +00:00
|
|
|
|
2023-10-23 18:12:55 +00:00
|
|
|
const realmRole = await this.#client.roles.findOneByName({
|
|
|
|
name: roleName,
|
|
|
|
});
|
2022-11-25 21:11:15 +00:00
|
|
|
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#client.users.addRealmRoleMappings({
|
2022-11-25 21:11:15 +00:00
|
|
|
id: userId,
|
|
|
|
roles: [realmRole as RoleMappingPayload],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-28 13:07:11 +00:00
|
|
|
async addClientRoleToUser(
|
|
|
|
userId: string,
|
|
|
|
clientId: string,
|
|
|
|
roleNames: string[],
|
|
|
|
) {
|
|
|
|
await this.#login();
|
|
|
|
|
|
|
|
const client = await this.#client.clients.find({ clientId });
|
|
|
|
const clientRoles = await Promise.all(
|
|
|
|
roleNames.map(
|
|
|
|
async (roleName) =>
|
|
|
|
(await this.#client.clients.findRole({
|
|
|
|
id: client[0].id!,
|
|
|
|
roleName: roleName,
|
|
|
|
})) as RoleMappingPayload,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
await this.#client.users.addClientRoleMappings({
|
|
|
|
id: userId,
|
|
|
|
clientUniqueId: client[0].id!,
|
|
|
|
roles: clientRoles,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-06 05:31:40 +00:00
|
|
|
async deleteUser(username: string) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
const user = await this.#client.users.find({ username });
|
|
|
|
await this.#client.users.del({ id: user[0].id! });
|
2021-05-06 05:31:40 +00:00
|
|
|
}
|
2021-12-10 12:24:26 +00:00
|
|
|
|
|
|
|
async createClientScope(scope: ClientScopeRepresentation) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
return await this.#client.clientScopes.create(scope);
|
2021-12-10 12:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async deleteClientScope(clientScopeName: string) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
const clientScope = await this.#client.clientScopes.findOneByName({
|
2021-12-10 12:24:26 +00:00
|
|
|
name: clientScopeName,
|
|
|
|
});
|
2023-10-23 18:12:55 +00:00
|
|
|
return await this.#client.clientScopes.del({ id: clientScope?.id! });
|
2021-12-10 12:24:26 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 12:46:49 +00:00
|
|
|
async existsClientScope(clientScopeName: string) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
return (await this.#client.clientScopes.findOneByName({
|
2022-02-22 12:46:49 +00:00
|
|
|
name: clientScopeName,
|
|
|
|
})) == undefined
|
|
|
|
? false
|
|
|
|
: true;
|
|
|
|
}
|
|
|
|
|
2021-12-10 12:24:26 +00:00
|
|
|
async addDefaultClientScopeInClient(
|
|
|
|
clientScopeName: string,
|
2023-07-11 14:03:21 +00:00
|
|
|
clientId: string,
|
2021-12-10 12:24:26 +00:00
|
|
|
) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
const scope = await this.#client.clientScopes.findOneByName({
|
2021-12-10 12:24:26 +00:00
|
|
|
name: clientScopeName,
|
|
|
|
});
|
2023-10-23 18:12:55 +00:00
|
|
|
const client = await this.#client.clients.find({ clientId: clientId });
|
|
|
|
return await this.#client.clients.addDefaultClientScope({
|
2021-12-10 12:24:26 +00:00
|
|
|
id: client[0]?.id!,
|
|
|
|
clientScopeId: scope?.id!,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeDefaultClientScopeInClient(
|
|
|
|
clientScopeName: string,
|
2023-07-11 14:03:21 +00:00
|
|
|
clientId: string,
|
2021-12-10 12:24:26 +00:00
|
|
|
) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
const scope = await this.#client.clientScopes.findOneByName({
|
2021-12-10 12:24:26 +00:00
|
|
|
name: clientScopeName,
|
|
|
|
});
|
2023-10-23 18:12:55 +00:00
|
|
|
const client = await this.#client.clients.find({ clientId: clientId });
|
|
|
|
return await this.#client.clients.delDefaultClientScope({
|
2021-12-10 12:24:26 +00:00
|
|
|
id: client[0]?.id!,
|
|
|
|
clientScopeId: scope?.id!,
|
|
|
|
});
|
|
|
|
}
|
2022-01-07 12:56:27 +00:00
|
|
|
|
|
|
|
async patchUserProfile(realm: string, payload: UserProfileConfig) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
2022-01-07 12:56:27 +00:00
|
|
|
|
2023-10-23 18:12:55 +00:00
|
|
|
const currentProfile = await this.#client.users.getProfile({ realm });
|
2022-01-07 12:56:27 +00:00
|
|
|
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#client.users.updateProfile(
|
2023-07-11 14:03:21 +00:00
|
|
|
merge(currentProfile, payload, { realm }),
|
2022-01-07 12:56:27 +00:00
|
|
|
);
|
|
|
|
}
|
2022-02-23 11:05:38 +00:00
|
|
|
|
|
|
|
async createRealmRole(payload: RoleRepresentation) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
2022-02-23 11:05:38 +00:00
|
|
|
|
2023-10-23 18:12:55 +00:00
|
|
|
return await this.#client.roles.create(payload);
|
2022-02-23 11:05:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async deleteRealmRole(name: string) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
return await this.#client.roles.delByName({ name });
|
2022-02-23 11:05:38 +00:00
|
|
|
}
|
2022-08-22 16:05:57 +00:00
|
|
|
|
|
|
|
async createIdentityProvider(idpDisplayName: string, alias: string) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
2022-08-22 16:05:57 +00:00
|
|
|
const identityProviders =
|
2023-10-23 18:12:55 +00:00
|
|
|
(await this.#client.serverInfo.find()).identityProviders || [];
|
2022-08-22 16:05:57 +00:00
|
|
|
const idp = identityProviders.find(({ name }) => name === idpDisplayName);
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#client.identityProviders.create({
|
2022-08-22 16:05:57 +00:00
|
|
|
providerId: idp?.id!,
|
|
|
|
displayName: idpDisplayName,
|
|
|
|
alias: alias,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-25 15:46:38 +00:00
|
|
|
async deleteIdentityProvider(idpAlias: string) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
await this.#client.identityProviders.del({
|
2022-08-25 15:46:38 +00:00
|
|
|
alias: idpAlias,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-22 16:05:57 +00:00
|
|
|
async unlinkAccountIdentityProvider(
|
|
|
|
username: string,
|
2023-07-11 14:03:21 +00:00
|
|
|
idpDisplayName: string,
|
2022-08-22 16:05:57 +00:00
|
|
|
) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
const user = await this.#client.users.find({ username });
|
2022-08-22 16:05:57 +00:00
|
|
|
const identityProviders =
|
2023-10-23 18:12:55 +00:00
|
|
|
(await this.#client.serverInfo.find()).identityProviders || [];
|
2022-08-22 16:05:57 +00:00
|
|
|
const idp = identityProviders.find(({ name }) => name === idpDisplayName);
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#client.users.delFromFederatedIdentity({
|
2022-08-22 16:05:57 +00:00
|
|
|
id: user[0].id!,
|
|
|
|
federatedIdentityId: idp?.id!,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async linkAccountIdentityProvider(username: string, idpDisplayName: string) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
const user = await this.#client.users.find({ username });
|
2022-08-22 16:05:57 +00:00
|
|
|
const identityProviders =
|
2023-10-23 18:12:55 +00:00
|
|
|
(await this.#client.serverInfo.find()).identityProviders || [];
|
2022-08-22 16:05:57 +00:00
|
|
|
const idp = identityProviders.find(({ name }) => name === idpDisplayName);
|
|
|
|
const fedIdentity = {
|
|
|
|
identityProvider: idp?.id,
|
|
|
|
userId: "testUserIdApi",
|
|
|
|
userName: "testUserNameApi",
|
|
|
|
};
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#client.users.addToFederatedIdentity({
|
2022-08-22 16:05:57 +00:00
|
|
|
id: user[0].id!,
|
|
|
|
federatedIdentityId: idp?.id!,
|
|
|
|
federatedIdentity: fedIdentity,
|
|
|
|
});
|
|
|
|
}
|
2022-11-25 21:11:15 +00:00
|
|
|
|
|
|
|
async addLocalizationText(locale: string, key: string, value: string) {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
await this.#client.realms.addLocalization(
|
|
|
|
{ realm: this.#client.realmName, selectedLocale: locale, key: key },
|
2023-07-11 14:03:21 +00:00
|
|
|
value,
|
2022-11-25 21:11:15 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeAllLocalizationTexts() {
|
2023-10-23 18:12:55 +00:00
|
|
|
await this.#login();
|
|
|
|
const localesWithTexts = await this.#client.realms.getRealmSpecificLocales({
|
|
|
|
realm: this.#client.realmName,
|
2022-11-25 21:11:15 +00:00
|
|
|
});
|
|
|
|
await Promise.all(
|
|
|
|
localesWithTexts.map((locale) =>
|
2023-10-23 18:12:55 +00:00
|
|
|
this.#client.realms.deleteRealmLocalizationTexts({
|
|
|
|
realm: this.#client.realmName,
|
2022-11-25 21:11:15 +00:00
|
|
|
selectedLocale: locale,
|
2023-07-11 14:03:21 +00:00
|
|
|
}),
|
|
|
|
),
|
2022-11-25 21:11:15 +00:00
|
|
|
);
|
|
|
|
}
|
2021-02-09 12:32:41 +00:00
|
|
|
}
|
2022-02-24 10:31:46 +00:00
|
|
|
|
|
|
|
const adminClient = new AdminClient();
|
|
|
|
|
|
|
|
export default adminClient;
|