4
4
5
5
import { Zodios , ZodiosResponseByAlias } from "@zodios/core" ;
6
6
import { isAxiosError } from "axios" ;
7
+ import { authConfig } from "common-helpers" ;
7
8
import NodeCache from "node-cache" ;
8
9
import { keycloakAPI , type ClientCredentialsToken } from "validation-helpers" ;
9
10
import { CustomError } from "./custom_error" ;
10
11
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 ) ;
15
13
16
14
const cache = new NodeCache ( { stdTTL : 5 * 60 , checkperiod : 60 } ) ;
17
15
@@ -34,16 +32,16 @@ export const getAccessToken = async (): Promise<ClientCredentialsToken> => {
34
32
try {
35
33
const accessToken = ( await kcClient . PostToken (
36
34
{
37
- client_id : process . env . KEYCLOAK_CLIENT_ID_API ! ,
35
+ client_id : authConfig . clientIdAPI ,
38
36
grant_type : "client_credentials" ,
39
- client_secret : process . env . KEYCLOAK_CLIENT_SECRET_API ! ,
37
+ client_secret : authConfig . clientSecretAPI ,
40
38
} ,
41
39
{
42
40
headers : {
43
41
"Content-Type" : "application/x-www-form-urlencoded" ,
44
42
} ,
45
43
params : {
46
- realm : process . env . KEYCLOAK_REALM ! ,
44
+ realm : authConfig . realm ,
47
45
} ,
48
46
} ,
49
47
) ) 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 (
100
98
const token = await getAccessToken ( ) ;
101
99
await kcClient . LogoutUser ( undefined , {
102
100
params : {
103
- realm : process . env . KEYCLOAK_REALM ! ,
101
+ realm : authConfig . realm ,
104
102
id : userId ,
105
103
} ,
106
104
headers : {
@@ -173,7 +171,7 @@ export const createUser = async (data: {
173
171
const token = await getAccessToken ( ) ;
174
172
await kcClient . CreateUser ( data , {
175
173
params : {
176
- realm : process . env . KEYCLOAK_REALM ! ,
174
+ realm : authConfig . realm ,
177
175
} ,
178
176
headers : {
179
177
Authorization : "Bearer " + token . access_token ,
@@ -239,7 +237,7 @@ export const deleteUser = async (userId: string): Promise<boolean> => {
239
237
const token = await getAccessToken ( ) ;
240
238
await kcClient . DeleteUser ( undefined , {
241
239
params : {
242
- realm : process . env . KEYCLOAK_REALM ! ,
240
+ realm : authConfig . realm ,
243
241
id : userId ,
244
242
} ,
245
243
headers : {
@@ -294,7 +292,7 @@ export const getRealmRoles = async (): Promise<RealmRole[]> => {
294
292
const token = await getAccessToken ( ) ;
295
293
roles = await kcClient . GetRealmRoles ( {
296
294
params : {
297
- realm : process . env . KEYCLOAK_REALM ! ,
295
+ realm : authConfig . realm ,
298
296
} ,
299
297
headers : {
300
298
Authorization : "Bearer " + token . access_token ,
@@ -360,7 +358,7 @@ export const addRealmRolesToUser = async (
360
358
361
359
await kcClient . AddRealmRoleToUser ( roles , {
362
360
params : {
363
- realm : process . env . KEYCLOAK_REALM ! ,
361
+ realm : authConfig . realm ,
364
362
id : userId ,
365
363
} ,
366
364
headers : {
@@ -420,7 +418,7 @@ export const getUsers = async (
420
418
const token = await getAccessToken ( ) ;
421
419
users = await kcClient . GetUsers ( {
422
420
params : {
423
- realm : process . env . KEYCLOAK_REALM ! ,
421
+ realm : authConfig . realm ,
424
422
} ,
425
423
queries : {
426
424
username : username ,
@@ -496,7 +494,7 @@ export const updateUser = async (
496
494
const token = await getAccessToken ( ) ;
497
495
await kcClient . UpdateUser ( data , {
498
496
params : {
499
- realm : process . env . KEYCLOAK_REALM ! ,
497
+ realm : authConfig . realm ,
500
498
id : userId ,
501
499
} ,
502
500
headers : {
0 commit comments