@@ -13,66 +13,43 @@ import UserRepresentation from '@keycloak/keycloak-admin-client/lib/defs/userRep
13
13
const logger = Logger ( 'kc.group' ) ;
14
14
15
15
export class KeycloakGroupService {
16
+ private static instanceCount = 0 ; // Track total instances created
17
+ private instanceId : string ;
18
+ private createdAt : Date ;
16
19
private allGroups : any = undefined ;
17
20
private kcAdminClient : KeycloakAdminClient ;
18
- private clientId : string ;
19
- private clientSecret : string ;
20
- private lastAuthTime : number = 0 ;
21
- private readonly AUTH_TIMEOUT = 280 * 1000 ; // 280 seconds (slightly less than typical 5 min token lifetime)
22
21
23
22
constructor ( issuerUrl : string ) {
23
+ this . instanceId = `kc-group-${ ++ KeycloakGroupService . instanceCount } ` ;
24
+ this . createdAt = new Date ( ) ;
25
+ logger . info ( '[Instance Created] id=%s, created=%s' , this . instanceId , this . createdAt ) ;
26
+
24
27
const baseUrl = issuerUrl . substr ( 0 , issuerUrl . indexOf ( '/realms' ) ) ;
25
28
const realmName = issuerUrl . substr ( issuerUrl . lastIndexOf ( '/' ) + 1 ) ;
26
29
logger . debug ( '%s %s' , baseUrl , realmName ) ;
27
30
this . kcAdminClient = new KcAdminClient ( { baseUrl, realmName } ) ;
28
31
}
29
32
30
- private async ensureAuthenticated ( ) : Promise < void > {
31
- if ( this . clientId && ( Date . now ( ) - this . lastAuthTime > this . AUTH_TIMEOUT ) ) {
32
- logger . debug ( '[ensureAuthenticated] Re-authenticating due to timeout' ) ;
33
- await this . login ( this . clientId , this . clientSecret ) ;
34
- }
35
- }
36
-
37
33
public async cacheGroups ( ) {
38
34
this . allGroups = await this . getAllGroups ( ) ;
39
35
}
40
36
41
37
public async login (
42
38
clientId : string ,
43
- clientSecret : string ,
44
- retryAttempts : number = 3
39
+ clientSecret : string
45
40
) : Promise < KeycloakGroupService > {
46
- this . clientId = clientId ;
47
- this . clientSecret = clientSecret ;
48
-
49
- const result = await this . _login ( retryAttempts ) ;
50
- this . lastAuthTime = Date . now ( ) ;
51
- return result ;
52
- }
53
-
54
- private async _login ( retryAttempts : number ) : Promise < KeycloakGroupService > {
55
- logger . debug ( '[login] %s' , this . clientId ) ;
41
+ logger . debug ( '[login] %s' , clientId ) ;
56
42
57
- for ( let attempt = 1 ; attempt <= retryAttempts ; attempt ++ ) {
58
- try {
59
- await this . kcAdminClient
60
- . auth ( {
61
- grantType : 'client_credentials' ,
62
- clientId : this . clientId ,
63
- clientSecret : this . clientSecret ,
64
- } ) ;
65
- return this ;
66
- } catch ( err : any ) {
67
- if ( attempt === retryAttempts ) {
68
- logger . error ( '[login] Login failed after %d attempts: %s' , retryAttempts , err ) ;
69
- throw err ;
70
- }
71
- logger . warn ( '[login] Attempt %d failed, retrying: %s' , attempt , err ) ;
72
- // Add exponential backoff
73
- await new Promise ( resolve => setTimeout ( resolve , Math . pow ( 2 , attempt ) * 100 ) ) ;
74
- }
75
- }
43
+ await this . kcAdminClient
44
+ . auth ( {
45
+ grantType : 'client_credentials' ,
46
+ clientId : clientId ,
47
+ clientSecret : clientSecret ,
48
+ } )
49
+ . catch ( ( err : any ) => {
50
+ logger . error ( '[login] Login failed %s' , err ) ;
51
+ throw err ;
52
+ } ) ;
76
53
return this ;
77
54
}
78
55
@@ -100,7 +77,6 @@ export class KeycloakGroupService {
100
77
}
101
78
102
79
public async updateGroup ( group : GroupRepresentation ) : Promise < void > {
103
- await this . ensureAuthenticated ( ) ;
104
80
logger . debug ( '[updateGroup] %j' , group ) ;
105
81
await this . kcAdminClient . groups . update ( { id : group . id } , group ) ;
106
82
}
0 commit comments