Skip to content

Commit

Permalink
add: add ClearClientAccessToken method to KeycloakJWTReceiverCachedIn…
Browse files Browse the repository at this point in the history
…Memory
  • Loading branch information
larox11 committed Nov 3, 2024
1 parent 395b9ea commit bd15a1d
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions client/keycloakJWTReceiverCachedInMemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func NewKeycloakJWTReceiverCachedInMemory(keycloakRepository IKeycloakRepository
}
}

func isTokenValid(token *gocloak.JWT) bool {
if token == nil {
func (k *KeycloakJWTReceiverCachedInMemory) isTokenValid() bool {
if k.cachedToken == nil {
return false
}

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

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

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

return token.AccessToken, nil
}

func (k *KeycloakJWTReceiverCachedInMemory) ClearClientAccessToken() {
k.mutex.Lock()
defer k.mutex.Unlock()

k.cachedToken = nil
}

0 comments on commit bd15a1d

Please sign in to comment.