Skip to content

Commit bd15a1d

Browse files
committed
add: add ClearClientAccessToken method to KeycloakJWTReceiverCachedInMemory
1 parent 395b9ea commit bd15a1d

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

client/keycloakJWTReceiverCachedInMemory.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ func NewKeycloakJWTReceiverCachedInMemory(keycloakRepository IKeycloakRepository
2525
}
2626
}
2727

28-
func isTokenValid(token *gocloak.JWT) bool {
29-
if token == nil {
28+
func (k *KeycloakJWTReceiverCachedInMemory) isTokenValid() bool {
29+
if k.cachedToken == nil {
3030
return false
3131
}
3232

3333
parser := jwt.NewParser()
3434
claims := &jwt.MapClaims{}
3535

36-
_, _, err := parser.ParseUnverified(token.AccessToken, claims)
36+
_, _, err := parser.ParseUnverified(k.cachedToken.AccessToken, claims)
3737
if err != nil {
3838
log.Error().Msgf("couldn't parse JWT access token: %v", err)
3939
return false
@@ -55,7 +55,7 @@ func (k *KeycloakJWTReceiverCachedInMemory) getClientToken(clientName, clientSec
5555
k.mutex.Lock()
5656
defer k.mutex.Unlock()
5757

58-
if k.cachedToken == nil || !isTokenValid(k.cachedToken) {
58+
if !k.isTokenValid() {
5959
token, err := k.keycloakRepository.getClientToken(clientName, clientSecret)
6060
if err != nil {
6161
return nil, fmt.Errorf("couldn't fetch JWT access token: %w", err)
@@ -74,3 +74,10 @@ func (k *KeycloakJWTReceiverCachedInMemory) GetClientAccessToken(clientName, cli
7474

7575
return token.AccessToken, nil
7676
}
77+
78+
func (k *KeycloakJWTReceiverCachedInMemory) ClearClientAccessToken() {
79+
k.mutex.Lock()
80+
defer k.mutex.Unlock()
81+
82+
k.cachedToken = nil
83+
}

0 commit comments

Comments
 (0)