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-10-06 13:15:39 +00:00
|
|
|
import type { UserProfileConfig } from "@keycloak/keycloak-admin-client/lib/defs/userProfileConfig";
|
|
|
|
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 {
|
|
|
|
private 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",
|
|
|
|
});
|
|
|
|
|
|
|
|
private 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) {
|
|
|
|
return this.client.auth({
|
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
grantType: "password",
|
|
|
|
clientId: clientId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-25 21:11:15 +00:00
|
|
|
async getRealm(realm: string) {
|
|
|
|
await this.login();
|
|
|
|
return await this.client.realms.findOne({ realm });
|
|
|
|
}
|
|
|
|
|
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];
|
2022-08-23 14:30:52 +00:00
|
|
|
|
|
|
|
if (client) {
|
|
|
|
await this.client.clients.del({ id: client.id! });
|
|
|
|
}
|
2021-02-28 20:02:31 +00:00
|
|
|
}
|
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 {
|
2023-06-23 19:42:13 +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() {
|
|
|
|
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();
|
2023-04-13 13:55:50 +00:00
|
|
|
|
|
|
|
const { id } = await this.client.users.create(user);
|
|
|
|
const createdUser = await this.client.users.findOne({ id });
|
|
|
|
|
|
|
|
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) {
|
|
|
|
await this.login();
|
|
|
|
return this.client.users.update({ id }, payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAdminUser() {
|
|
|
|
await this.login();
|
|
|
|
const [user] = await this.client.users.find({ username: "admin" });
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2022-06-27 08:47:41 +00:00
|
|
|
async addUserToGroup(userId: string, groupId: string) {
|
|
|
|
await this.login();
|
|
|
|
await this.client.users.addToGroup({ id: userId, groupId });
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-11-25 21:11:15 +00:00
|
|
|
async addRealmRoleToUser(userId: string, roleName: string) {
|
|
|
|
await this.login();
|
|
|
|
|
|
|
|
const realmRole = await this.client.roles.findOneByName({ name: roleName });
|
|
|
|
|
|
|
|
await this.client.users.addRealmRoleMappings({
|
|
|
|
id: userId,
|
|
|
|
roles: [realmRole as RoleMappingPayload],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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! });
|
|
|
|
}
|
|
|
|
|
2022-02-22 12:46:49 +00:00
|
|
|
async existsClientScope(clientScopeName: string) {
|
|
|
|
await this.login();
|
|
|
|
return (await this.client.clientScopes.findOneByName({
|
|
|
|
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
|
|
|
) {
|
|
|
|
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,
|
2023-07-11 14:03:21 +00:00
|
|
|
clientId: string,
|
2021-12-10 12:24:26 +00:00
|
|
|
) {
|
|
|
|
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(
|
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) {
|
|
|
|
await this.login();
|
|
|
|
|
|
|
|
return await this.client.roles.create(payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteRealmRole(name: string) {
|
|
|
|
await this.login();
|
|
|
|
return await this.client.roles.delByName({ name });
|
|
|
|
}
|
2022-08-22 16:05:57 +00:00
|
|
|
|
|
|
|
async createIdentityProvider(idpDisplayName: string, alias: string) {
|
|
|
|
await this.login();
|
|
|
|
const identityProviders =
|
|
|
|
(await this.client.serverInfo.find()).identityProviders || [];
|
|
|
|
const idp = identityProviders.find(({ name }) => name === idpDisplayName);
|
|
|
|
await this.client.identityProviders.create({
|
|
|
|
providerId: idp?.id!,
|
|
|
|
displayName: idpDisplayName,
|
|
|
|
alias: alias,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-25 15:46:38 +00:00
|
|
|
async deleteIdentityProvider(idpAlias: string) {
|
|
|
|
await this.login();
|
|
|
|
await this.client.identityProviders.del({
|
|
|
|
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
|
|
|
) {
|
|
|
|
await this.login();
|
|
|
|
const user = await this.client.users.find({ username });
|
|
|
|
const identityProviders =
|
|
|
|
(await this.client.serverInfo.find()).identityProviders || [];
|
|
|
|
const idp = identityProviders.find(({ name }) => name === idpDisplayName);
|
|
|
|
await this.client.users.delFromFederatedIdentity({
|
|
|
|
id: user[0].id!,
|
|
|
|
federatedIdentityId: idp?.id!,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async linkAccountIdentityProvider(username: string, idpDisplayName: string) {
|
|
|
|
await this.login();
|
|
|
|
const user = await this.client.users.find({ username });
|
|
|
|
const identityProviders =
|
|
|
|
(await this.client.serverInfo.find()).identityProviders || [];
|
|
|
|
const idp = identityProviders.find(({ name }) => name === idpDisplayName);
|
|
|
|
const fedIdentity = {
|
|
|
|
identityProvider: idp?.id,
|
|
|
|
userId: "testUserIdApi",
|
|
|
|
userName: "testUserNameApi",
|
|
|
|
};
|
|
|
|
await this.client.users.addToFederatedIdentity({
|
|
|
|
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) {
|
|
|
|
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() {
|
|
|
|
await this.login();
|
|
|
|
const localesWithTexts = await this.client.realms.getRealmSpecificLocales({
|
|
|
|
realm: this.client.realmName,
|
|
|
|
});
|
|
|
|
await Promise.all(
|
|
|
|
localesWithTexts.map((locale) =>
|
|
|
|
this.client.realms.deleteRealmLocalizationTexts({
|
|
|
|
realm: this.client.realmName,
|
|
|
|
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;
|