44
55import { Zodios , ZodiosResponseByAlias } from "@zodios/core" ;
66import { isAxiosError } from "axios" ;
7+ import { authConfig } from "common-helpers" ;
78import NodeCache from "node-cache" ;
89import { keycloakAPI , type ClientCredentialsToken } from "validation-helpers" ;
910import { CustomError } from "./custom_error" ;
1011
11- const kcClient = new Zodios (
12- process . env . KEYCLOAK_URL || "https://auth.dev.doubleopen.io/" ,
13- keycloakAPI ,
14- ) ;
12+ const kcClient = new Zodios ( authConfig . url , keycloakAPI ) ;
1513
1614const cache = new NodeCache ( { stdTTL : 5 * 60 , checkperiod : 60 } ) ;
1715
@@ -34,16 +32,16 @@ export const getAccessToken = async (): Promise<ClientCredentialsToken> => {
3432 try {
3533 const accessToken = ( await kcClient . PostToken (
3634 {
37- client_id : process . env . KEYCLOAK_CLIENT_ID_API ! ,
35+ client_id : authConfig . clientIdAPI ,
3836 grant_type : "client_credentials" ,
39- client_secret : process . env . KEYCLOAK_CLIENT_SECRET_API ! ,
37+ client_secret : authConfig . clientSecretAPI ,
4038 } ,
4139 {
4240 headers : {
4341 "Content-Type" : "application/x-www-form-urlencoded" ,
4442 } ,
4543 params : {
46- realm : process . env . KEYCLOAK_REALM ! ,
44+ realm : authConfig . realm ,
4745 } ,
4846 } ,
4947 ) ) as ClientCredentialsToken ; // The endpoint provides a union type, but we know it's a ClientCredentialsToken with this type of request
@@ -100,7 +98,7 @@ export const logoutUser = async (
10098 const token = await getAccessToken ( ) ;
10199 await kcClient . LogoutUser ( undefined , {
102100 params : {
103- realm : process . env . KEYCLOAK_REALM ! ,
101+ realm : authConfig . realm ,
104102 id : userId ,
105103 } ,
106104 headers : {
@@ -173,7 +171,7 @@ export const createUser = async (data: {
173171 const token = await getAccessToken ( ) ;
174172 await kcClient . CreateUser ( data , {
175173 params : {
176- realm : process . env . KEYCLOAK_REALM ! ,
174+ realm : authConfig . realm ,
177175 } ,
178176 headers : {
179177 Authorization : "Bearer " + token . access_token ,
@@ -239,7 +237,7 @@ export const deleteUser = async (userId: string): Promise<boolean> => {
239237 const token = await getAccessToken ( ) ;
240238 await kcClient . DeleteUser ( undefined , {
241239 params : {
242- realm : process . env . KEYCLOAK_REALM ! ,
240+ realm : authConfig . realm ,
243241 id : userId ,
244242 } ,
245243 headers : {
@@ -294,7 +292,7 @@ export const getRealmRoles = async (): Promise<RealmRole[]> => {
294292 const token = await getAccessToken ( ) ;
295293 roles = await kcClient . GetRealmRoles ( {
296294 params : {
297- realm : process . env . KEYCLOAK_REALM ! ,
295+ realm : authConfig . realm ,
298296 } ,
299297 headers : {
300298 Authorization : "Bearer " + token . access_token ,
@@ -360,7 +358,7 @@ export const addRealmRolesToUser = async (
360358
361359 await kcClient . AddRealmRoleToUser ( roles , {
362360 params : {
363- realm : process . env . KEYCLOAK_REALM ! ,
361+ realm : authConfig . realm ,
364362 id : userId ,
365363 } ,
366364 headers : {
@@ -420,7 +418,7 @@ export const getUsers = async (
420418 const token = await getAccessToken ( ) ;
421419 users = await kcClient . GetUsers ( {
422420 params : {
423- realm : process . env . KEYCLOAK_REALM ! ,
421+ realm : authConfig . realm ,
424422 } ,
425423 queries : {
426424 username : username ,
@@ -496,7 +494,7 @@ export const updateUser = async (
496494 const token = await getAccessToken ( ) ;
497495 await kcClient . UpdateUser ( data , {
498496 params : {
499- realm : process . env . KEYCLOAK_REALM ! ,
497+ realm : authConfig . realm ,
500498 id : userId ,
501499 } ,
502500 headers : {
0 commit comments