@@ -63,9 +63,8 @@ func (c *KeycloakClient) GetToken(ctx context.Context) (string, error) {
6363 defer c .tokenMutex .RUnlock ()
6464 if time .Now ().Before (c .tokenInfo .ExpiresAt .Add (- tokenRefreshMargin )) {
6565 return c .tokenInfo .AccessToken , true
66- } else {
67- return "" , false
6866 }
67+ return "" , false
6968 }
7069
7170 token , ok := getCachedToken ()
@@ -82,6 +81,22 @@ func (c *KeycloakClient) GetToken(ctx context.Context) (string, error) {
8281 return c .tokenInfo .AccessToken , nil
8382 }
8483
84+ authResponse , err := c .requestToken (ctx )
85+ if err != nil {
86+ return "" , fmt .Errorf ("failed to request token: %w" , err )
87+ }
88+
89+ c .tokenInfo = tokenInfo {
90+ AccessToken : authResponse .AccessToken ,
91+ ExpiresAt : time .Now ().Add (time .Duration (authResponse .ExpiresIn ) * time .Second ),
92+ }
93+
94+ return authResponse .AccessToken , nil
95+ }
96+
97+ func (c * KeycloakClient ) requestToken (ctx context.Context ) (authResponse , error ) {
98+ var empty authResponse
99+
85100 data := url.Values {}
86101 data .Set ("client_id" , c .cfg .ClientID )
87102 data .Set ("password" , c .cfg .Password )
@@ -91,35 +106,30 @@ func (c *KeycloakClient) GetToken(ctx context.Context) (string, error) {
91106 authenticationURL := fmt .Sprintf ("%s/realms/%s/protocol/openid-connect/token" ,
92107 c .cfg .AuthURL , c .cfg .KeycloakRealm )
93108
94- req , err := http .NewRequest ( http .MethodPost , authenticationURL , strings .NewReader (data .Encode ()))
109+ req , err := http .NewRequestWithContext ( ctx , http .MethodPost , authenticationURL , strings .NewReader (data .Encode ()))
95110 if err != nil {
96- return "" , fmt .Errorf ("failed to create authentication request: %w" , err )
111+ return empty , fmt .Errorf ("failed to create authentication request: %w" , err )
97112 }
98113 req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
99114
100115 resp , err := c .httpClient .Do (req )
101116 if err != nil {
102- return "" , fmt .Errorf ("failed to execute authentication request with retry: %w" , err )
117+ return empty , fmt .Errorf ("failed to execute authentication request with retry: %w" , err )
103118 }
104- defer resp .Body .Close ()
119+ defer func () { _ = resp .Body .Close () } ()
105120
106121 if resp .StatusCode != http .StatusOK {
107122 respBody , err := io .ReadAll (resp .Body )
108123 if err != nil {
109124 respBody = []byte ("failed to read response body: " + err .Error ())
110125 }
111- return "" , fmt .Errorf ("authentication request failed with status: %s: %s" , resp .Status , string (respBody ))
126+ return empty , fmt .Errorf ("authentication request failed with status: %s: %s" , resp .Status , string (respBody ))
112127 }
113128
114129 var authResponse authResponse
115130 if err := json .NewDecoder (resp .Body ).Decode (& authResponse ); err != nil {
116- return "" , fmt .Errorf ("failed to parse authentication response: %w" , err )
131+ return empty , fmt .Errorf ("failed to parse authentication response: %w" , err )
117132 }
118133
119- c .tokenInfo = tokenInfo {
120- AccessToken : authResponse .AccessToken ,
121- ExpiresAt : time .Now ().Add (time .Duration (authResponse .ExpiresIn ) * time .Second ),
122- }
123-
124- return authResponse .AccessToken , nil
134+ return authResponse , nil
125135}
0 commit comments