diff --git a/.env.example b/.env.example index 2e7bc341..2b0764ba 100644 --- a/.env.example +++ b/.env.example @@ -19,7 +19,7 @@ DB_NAME=bma_ehr_organization # DB_NAME=bma_ehr_organization KC_URL=https://id.frappet.synology.me -KC_REALM=bma-ehr +KC_REALMS=bma-ehr KC_SERVICE_ACCOUNT_CLIENT_ID=bma-ehr-dev KC_SERVICE_ACCOUNT_SECRET=f2mp7Xj4nz6gbgITve9J7AHXZI8dRhOd API_URL=https://bma-ehr.frappet.synology.me/api/v1 diff --git a/src/controllers/CommandController.ts b/src/controllers/CommandController.ts index 3471339e..88d08f60 100644 --- a/src/controllers/CommandController.ts +++ b/src/controllers/CommandController.ts @@ -1255,7 +1255,7 @@ export class CommandController extends Controller { let _data: any = null; await Promise.all([ await new CallAPI() - .PostDataKeycloak(`/realms/${process.env.KC_REALM}/protocol/openid-connect/token`, data) + .PostDataKeycloak(`/realms/${process.env.KC_REALMS}/protocol/openid-connect/token`, data) .then(async (x) => { _data = x; }) diff --git a/src/controllers/LoginController.ts b/src/controllers/LoginController.ts index b2040bed..c20d688c 100644 --- a/src/controllers/LoginController.ts +++ b/src/controllers/LoginController.ts @@ -37,7 +37,7 @@ export class LoginController extends Controller { let _data: any = null; await Promise.all([ await new CallAPI() - .PostDataKeycloak(`/realms/${process.env.KC_REALM}/protocol/openid-connect/token`, data) + .PostDataKeycloak(`/realms/${process.env.KC_REALMS}/protocol/openid-connect/token`, data) .then(async (x) => { _data = x; }) diff --git a/src/keycloak/index.ts b/src/keycloak/index.ts index 0bc0b49f..16081ee3 100644 --- a/src/keycloak/index.ts +++ b/src/keycloak/index.ts @@ -1,7 +1,7 @@ import { DecodedJwt, createDecoder } from "fast-jwt"; const KC_URL = process.env.KC_URL; -const KC_REALM = process.env.KC_REALM; +const KC_REALMS = process.env.KC_REALMS; const KC_CLIENT_ID = process.env.KC_SERVICE_ACCOUNT_CLIENT_ID; const KC_SECRET = process.env.KC_SERVICE_ACCOUNT_SECRET; @@ -40,7 +40,7 @@ export async function getToken() { body.append("client_secret", KC_SECRET); body.append("grant_type", "client_credentials"); - const res = await fetch(`${KC_URL}/realms/${KC_REALM}/protocol/openid-connect/token`, { + const res = await fetch(`${KC_URL}/realms/${KC_REALMS}/protocol/openid-connect/token`, { method: "POST", body: body, }).catch((e) => console.error(e)); @@ -66,7 +66,7 @@ export async function getToken() { * @returns user uuid or true if success, false otherwise. */ export async function createUser(username: string, password: string, opts?: Record) { - const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/users`, { + const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users`, { // prettier-ignore headers: { "authorization": `Bearer ${await getToken()}`, @@ -75,7 +75,7 @@ export async function createUser(username: string, password: string, opts?: Reco method: "POST", body: JSON.stringify({ enabled: true, - credentials: [{ type: "password", value: password ,temporary: false,}], + credentials: [{ type: "password", value: password, temporary: false }], username, ...opts, }), @@ -100,7 +100,7 @@ export async function createUser(username: string, password: string, opts?: Reco * @returns user if success, false otherwise. */ export async function getUser(userId: string) { - const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/users/${userId}`, { + const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}`, { // prettier-ignore headers: { "authorization": `Bearer ${await getToken()}`, @@ -123,8 +123,8 @@ export async function getUser(userId: string) { */ export async function getUserList(first = "", max = "", search = "") { const res = await fetch( - // `${KC_URL}/admin/realms/${KC_REALM}/users`.concat(!!search ? `?search=${search}` : ""), - `${KC_URL}/admin/realms/${KC_REALM}/users?first=${first || "0"}&max=${max || "-1"}${search ? `&search=${search}` : ""}`, + // `${KC_URL}/admin/realms/${KC_REALMS}/users`.concat(!!search ? `?search=${search}` : ""), + `${KC_URL}/admin/realms/${KC_REALMS}/users?first=${first || "0"}&max=${max || "-1"}${search ? `&search=${search}` : ""}`, { // prettier-ignore headers: { @@ -151,7 +151,7 @@ export async function getUserList(first = "", max = "", search = "") { export async function getUserCount(first = "", max = "", search = "") { const res = await fetch( - `${KC_URL}/admin/realms/${KC_REALM}/users/count?first=${first || "0"}&max=${max || "-1"}${search ? `&search=${search}` : ""}`, + `${KC_URL}/admin/realms/${KC_REALMS}/users/count?first=${first || "0"}&max=${max || "-1"}${search ? `&search=${search}` : ""}`, { headers: { authorization: `Bearer ${await getToken()}`, @@ -178,8 +178,8 @@ export async function getUserCount(first = "", max = "", search = "") { export async function getUserListOrg(first = "", max = "", search = "", userIds: string[] = []) { const userIdsParam = userIds.join(","); const res = await fetch( - // `${KC_URL}/admin/realms/${KC_REALM}/users`.concat(!!search ? `?search=${search}` : ""), - `${KC_URL}/admin/realms/${KC_REALM}/users?first=${first || "0"}&max=${max || "-1"}${search ? `&search=${search}` : ""}${userIdsParam ? `&id=${userIdsParam}` : ""}`, + // `${KC_URL}/admin/realms/${KC_REALMS}/users`.concat(!!search ? `?search=${search}` : ""), + `${KC_URL}/admin/realms/${KC_REALMS}/users?first=${first || "0"}&max=${max || "-1"}${search ? `&search=${search}` : ""}${userIdsParam ? `&id=${userIdsParam}` : ""}`, { // prettier-ignore headers: { @@ -210,11 +210,11 @@ export async function getUserCountOrg(first = "", max = "", search = "", userIds console.log(userIdsParam); console.log("xxxxxxxxxxxxxxxxx"); console.log( - `${KC_URL}/admin/realms/${KC_REALM}/users/count?first=${first || "0"}&max=${max || "-1"}${search ? `&search=${search}` : ""}${userIdsParam && userIdsParam != "" ? `&id=${userIdsParam}` : ""}`, + `${KC_URL}/admin/realms/${KC_REALMS}/users/count?first=${first || "0"}&max=${max || "-1"}${search ? `&search=${search}` : ""}${userIdsParam && userIdsParam != "" ? `&id=${userIdsParam}` : ""}`, ); console.log("aaaaaaaaaaaaaaaaaa"); const res = await fetch( - `${KC_URL}/admin/realms/${KC_REALM}/users/count?first=${first || "0"}&max=${max || "-1"}${search ? `&search=${search}` : ""}${userIdsParam && userIdsParam != "" ? `&id=${userIdsParam}` : ""}`, + `${KC_URL}/admin/realms/${KC_REALMS}/users/count?first=${first || "0"}&max=${max || "-1"}${search ? `&search=${search}` : ""}${userIdsParam && userIdsParam != "" ? `&id=${userIdsParam}` : ""}`, { headers: { authorization: `Bearer ${await getToken()}`, @@ -241,7 +241,7 @@ export async function getUserCountOrg(first = "", max = "", search = "", userIds export async function editUser(userId: string, opts: Record) { const { password, ...rest } = opts; - const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/users/${userId}`, { + const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}`, { // prettier-ignore headers: { "authorization": `Bearer ${await getToken()}`, @@ -280,7 +280,7 @@ export async function updateName( ) { // const { password, ...rest } = opts; - const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/users/${userId}`, { + const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}`, { // prettier-ignore headers: { "authorization": `Bearer ${await getToken()}`, @@ -315,7 +315,7 @@ export async function updateName( * @returns user uuid or true if success, false otherwise. */ export async function enableStatus(userId: string, status: boolean) { - const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/users/${userId}`, { + const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}`, { headers: { authorization: `Bearer ${await getToken()}`, "content-type": `application/json`, @@ -344,7 +344,7 @@ export async function enableStatus(userId: string, status: boolean) { * @returns user true if success, false otherwise. */ export async function deleteUser(userId: string) { - const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/users/${userId}`, { + const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}`, { // prettier-ignore headers: { "authorization": `Bearer ${await getToken()}`, @@ -369,7 +369,7 @@ export async function deleteUser(userId: string) { */ export async function getRoleMappings(userId: string) { const res = await fetch( - `${KC_URL}/admin/realms/${KC_REALM}/users/${userId}/role-mappings/realm`, + `${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/role-mappings/realm`, { headers: { authorization: `Bearer ${await getToken()}`, @@ -394,7 +394,7 @@ export async function getRoleMappings(userId: string) { */ export async function getRoles(name?: string) { const res = await fetch( - `${KC_URL}/admin/realms/${KC_REALM}/roles`.concat((name && `/${name}`) || ""), + `${KC_URL}/admin/realms/${KC_REALMS}/roles`.concat((name && `/${name}`) || ""), { // prettier-ignore headers: { @@ -437,7 +437,7 @@ export async function getRoles(name?: string) { */ export async function getUserRoles(userId: string) { const res = await fetch( - `${KC_URL}/admin/realms/${KC_REALM}/users/${userId}/role-mappings/realm`, + `${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/role-mappings/realm`, { // prettier-ignore headers: { @@ -476,7 +476,7 @@ export async function getUserRoles(userId: string) { */ export async function addUserRoles(userId: string, roles: { id: string; name: string }[]) { const res = await fetch( - `${KC_URL}/admin/realms/${KC_REALM}/users/${userId}/role-mappings/realm`, + `${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/role-mappings/realm`, { // prettier-ignore headers: { @@ -504,7 +504,7 @@ export async function addUserRoles(userId: string, roles: { id: string; name: st */ export async function removeUserRoles(userId: string, roles: { id: string; name: string }[]) { const res = await fetch( - `${KC_URL}/admin/realms/${KC_REALM}/users/${userId}/role-mappings/realm`, + `${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/role-mappings/realm`, { // prettier-ignore headers: { @@ -532,7 +532,7 @@ export async function removeUserRoles(userId: string, roles: { id: string; name: */ export async function getGroups(id?: string) { const res = await fetch( - `${KC_URL}/admin/realms/${KC_REALM}/groups`.concat((id && `/${id}`) || ""), + `${KC_URL}/admin/realms/${KC_REALMS}/groups`.concat((id && `/${id}`) || ""), { // prettier-ignore headers: { @@ -570,7 +570,7 @@ export async function getGroups(id?: string) { * @returns true if success, false otherwise. */ export async function createGroup(name: string) { - const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/groups`, { + const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/groups`, { // prettier-ignore headers: { "authorization": `Bearer ${await getToken()}`, @@ -596,7 +596,7 @@ export async function createGroup(name: string) { * @returns true if success, false otherwise. */ export async function editGroup(id: string, name: string) { - const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/groups/${id}`, { + const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/groups/${id}`, { // prettier-ignore headers: { "authorization": `Bearer ${await getToken()}`, @@ -622,7 +622,7 @@ export async function editGroup(id: string, name: string) { * @returns true if success, false otherwise. */ export async function deleteGroup(id: string) { - const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/groups/${id}`, { + const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/groups/${id}`, { // prettier-ignore headers: { "authorization": `Bearer ${await getToken()}`, @@ -646,7 +646,7 @@ export async function deleteGroup(id: string) { * @returns group's info (array if not specify name) if success, null if not found, false otherwise. */ export async function getUserGroups(userId?: string) { - const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/users/${userId}/groups`, { + const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/groups`, { // prettier-ignore headers: { "authorization": `Bearer ${await getToken()}`, @@ -682,7 +682,7 @@ export async function getUserGroups(userId?: string) { * @returns true if success, false otherwise. */ export async function addUserGroup(userId: string, groupId: string) { - const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/users/${userId}/groups/${groupId}`, { + const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/groups/${groupId}`, { // prettier-ignore headers: { "authorization": `Bearer ${await getToken()}`, @@ -706,7 +706,7 @@ export async function addUserGroup(userId: string, groupId: string) { * @returns true if success, false otherwise. */ export async function removeUserGroup(userId: string, groupId: string) { - const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/users/${userId}/groups/${groupId}`, { + const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/groups/${groupId}`, { // prettier-ignore headers: { "authorization": `Bearer ${await getToken()}`,