2023-07-07 14:34:54 +00:00
|
|
|
import KeycloakAdminClient from "@keycloak/keycloak-admin-client";
|
2024-02-27 06:15:23 +00:00
|
|
|
import ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation";
|
|
|
|
import IdentityProviderRepresentation from "@keycloak/keycloak-admin-client/lib/defs/identityProviderRepresentation";
|
2023-07-07 14:34:54 +00:00
|
|
|
import RealmRepresentation from "@keycloak/keycloak-admin-client/lib/defs/realmRepresentation";
|
2023-11-24 21:31:26 +00:00
|
|
|
import type { UserProfileConfig } from "@keycloak/keycloak-admin-client/lib/defs/userProfileMetadata";
|
2023-09-12 11:54:38 +00:00
|
|
|
import UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation";
|
2023-07-07 14:34:54 +00:00
|
|
|
|
2024-03-04 12:38:28 +00:00
|
|
|
import { DEFAULT_REALM } from "../src/constants";
|
2024-03-07 11:39:26 +00:00
|
|
|
import { getKeycloakServerUrl } from "./utils";
|
2024-03-04 12:38:28 +00:00
|
|
|
|
2023-07-07 14:34:54 +00:00
|
|
|
const adminClient = new KeycloakAdminClient({
|
2024-03-07 11:39:26 +00:00
|
|
|
baseUrl: getKeycloakServerUrl(),
|
2024-03-04 12:38:28 +00:00
|
|
|
realmName: DEFAULT_REALM,
|
2023-07-07 14:34:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await adminClient.auth({
|
|
|
|
username: "admin",
|
|
|
|
password: "admin",
|
|
|
|
grantType: "password",
|
|
|
|
clientId: "admin-cli",
|
|
|
|
});
|
|
|
|
|
2023-08-14 14:41:58 +00:00
|
|
|
export async function useTheme() {
|
2024-03-04 12:38:28 +00:00
|
|
|
const masterRealm = await adminClient.realms.findOne({
|
|
|
|
realm: DEFAULT_REALM,
|
|
|
|
});
|
|
|
|
|
2023-08-14 14:41:58 +00:00
|
|
|
await adminClient.realms.update(
|
2024-03-04 12:38:28 +00:00
|
|
|
{ realm: DEFAULT_REALM },
|
2023-08-14 14:41:58 +00:00
|
|
|
{ ...masterRealm, accountTheme: "keycloak.v3" },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-02-27 06:15:23 +00:00
|
|
|
export async function inRealm<T>(realm: string, fn: () => Promise<T>) {
|
|
|
|
const prevRealm = adminClient.realmName;
|
|
|
|
adminClient.realmName = realm;
|
|
|
|
try {
|
|
|
|
return await fn();
|
|
|
|
} finally {
|
|
|
|
adminClient.realmName = prevRealm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-07 14:34:54 +00:00
|
|
|
export async function importRealm(realm: RealmRepresentation) {
|
|
|
|
await adminClient.realms.create(realm);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteRealm(realm: string) {
|
|
|
|
await adminClient.realms.del({ realm });
|
|
|
|
}
|
2023-08-31 09:32:39 +00:00
|
|
|
|
2024-02-27 06:15:23 +00:00
|
|
|
export async function createClient(
|
|
|
|
client: ClientRepresentation,
|
|
|
|
): Promise<string> {
|
|
|
|
return adminClient.clients.create(client).then((client) => client.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function findClientByClientId(clientId: string) {
|
|
|
|
return adminClient.clients
|
|
|
|
.find({ clientId })
|
|
|
|
.then((clientArray) => clientArray[0]?.["id"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteClient(id: string) {
|
|
|
|
await adminClient.clients.del({ id });
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createIdentityProvider(
|
|
|
|
idp: IdentityProviderRepresentation,
|
|
|
|
): Promise<string> {
|
|
|
|
return adminClient.identityProviders.create(idp)["id"];
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteIdentityProvider(alias: string) {
|
|
|
|
await adminClient.identityProviders.del({ alias });
|
|
|
|
}
|
|
|
|
|
2023-08-31 09:32:39 +00:00
|
|
|
export async function importUserProfile(
|
|
|
|
userProfile: UserProfileConfig,
|
|
|
|
realm: string,
|
|
|
|
) {
|
|
|
|
await adminClient.users.updateProfile({ ...userProfile, realm });
|
|
|
|
}
|
2023-09-12 11:54:38 +00:00
|
|
|
|
2024-03-04 12:38:28 +00:00
|
|
|
export async function enableLocalization(realm = DEFAULT_REALM) {
|
2023-09-12 11:54:38 +00:00
|
|
|
const realmRepresentation = await adminClient.realms.findOne({ realm });
|
|
|
|
await adminClient.realms.update(
|
|
|
|
{ realm },
|
|
|
|
{
|
|
|
|
...realmRepresentation,
|
|
|
|
internationalizationEnabled: true,
|
|
|
|
supportedLocales: ["en", "nl", "de"],
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-02-27 06:15:23 +00:00
|
|
|
export async function createUser(user: UserRepresentation) {
|
2023-09-12 11:54:38 +00:00
|
|
|
try {
|
2024-02-27 06:15:23 +00:00
|
|
|
await adminClient.users.create(user);
|
2023-09-12 11:54:38 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 06:15:23 +00:00
|
|
|
export async function createRandomUserWithPassword(
|
|
|
|
username: string,
|
|
|
|
password: string,
|
|
|
|
props?: UserRepresentation,
|
|
|
|
): Promise<string> {
|
|
|
|
return createUser({
|
|
|
|
username: username,
|
|
|
|
enabled: true,
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
type: "password",
|
|
|
|
value: password,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
...props,
|
|
|
|
}).then(() => username);
|
|
|
|
}
|
|
|
|
|
2024-02-13 10:44:25 +00:00
|
|
|
export async function getUserByUsername(username: string, realm: string) {
|
|
|
|
const users = await adminClient.users.find({ username, realm, exact: true });
|
|
|
|
return users.length > 0 ? users[0] : undefined;
|
|
|
|
}
|
|
|
|
|
2024-02-27 06:15:23 +00:00
|
|
|
export async function deleteUser(username: string) {
|
2023-09-12 11:54:38 +00:00
|
|
|
try {
|
2024-03-04 12:38:28 +00:00
|
|
|
const users = await adminClient.users.find({ username });
|
2023-09-12 11:54:38 +00:00
|
|
|
const { id } = users[0];
|
2024-03-04 12:38:28 +00:00
|
|
|
await adminClient.users.del({ id: id! });
|
2023-09-12 11:54:38 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
2024-02-13 10:44:25 +00:00
|
|
|
|
|
|
|
export async function updateUser(user: UserRepresentation, realm: string) {
|
|
|
|
try {
|
|
|
|
await adminClient.users.update({ id: user.id!, realm }, user);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getCredentials(id: string, realm: string) {
|
|
|
|
try {
|
|
|
|
return await adminClient.users.getCredentials({ id, realm });
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteCredential(
|
|
|
|
id: string,
|
|
|
|
credentialId: string,
|
|
|
|
realm: string,
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
await adminClient.users.deleteCredential({ id, credentialId, realm });
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|