Skip to content

Commit

Permalink
Merge pull request #193 from greenbone/AT-2046-clear-cached-openid-to…
Browse files Browse the repository at this point in the history
…ken-if-request-unauthorized

support clearing cached openID token
  • Loading branch information
larox11 authored Nov 4, 2024
2 parents 395b9ea + bd15a1d commit d4992de
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 d4992de

Please sign in to comment.