Skip to content

Commit

Permalink
P4ADEV-446 caching AccessToken String (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioT90 authored Jun 5, 2024
1 parent 122b7c5 commit 816d379
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package it.gov.pagopa.payhub.auth.service;

import it.gov.pagopa.payhub.model.generated.AccessToken;

import java.util.Map;

public interface TokenStoreService {
Map<String, String> save(AccessToken accessToken, Map<String, String> idTokenClaims);
Map<String, String> load(AccessToken accessToken);
void delete(AccessToken accessToken);
Map<String, String> save(String accessToken, Map<String, String> idTokenClaims);
Map<String, String> load(String accessToken);
void delete(String accessToken);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package it.gov.pagopa.payhub.auth.service;

import it.gov.pagopa.payhub.auth.configuration.RedisConfig;
import it.gov.pagopa.payhub.model.generated.AccessToken;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
Expand All @@ -15,19 +14,19 @@
class TokenStoreServiceImpl implements TokenStoreService{
@Override
@CachePut
public Map<String, String> save(AccessToken accessToken, Map<String, String> idTokenClaims) {
public Map<String, String> save(String accessToken, Map<String, String> idTokenClaims) {
return idTokenClaims;
}

@Override
@Cacheable
public Map<String, String> load(AccessToken accessToken) {
public Map<String, String> load(String accessToken) {
return null;
}

@Override
@CacheEvict
public void delete(AccessToken accessToken) {
public void delete(String accessToken) {
//Do Nothing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public AccessToken postToken(String clientId, String grantType, String subjectTo
clientId, subjectTokenType, subjectIssuer, grantType, scope);
Map<String, String> claims = validateExternalTokenService.validate(clientId, grantType, subjectToken, subjectIssuer, subjectTokenType, scope);
AccessToken accessToken = accessTokenBuilderService.build();
tokenStoreService.save(accessToken, claims);
tokenStoreService.save(accessToken.getAccessToken(), claims);
return accessToken;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.gov.pagopa.payhub.auth.service;

import it.gov.pagopa.payhub.model.generated.AccessToken;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -15,7 +14,7 @@ class TokenStoreServiceTest {
void givenClaimsWhenSaveThenReturnThem(){
// Given
HashMap<String, String> idTokenClaims = new HashMap<>();
AccessToken accessToken = new AccessToken();
String accessToken = "AccessToken";

// When
Map<String, String> result = service.save(accessToken, idTokenClaims);
Expand All @@ -27,7 +26,7 @@ void givenClaimsWhenSaveThenReturnThem(){
@Test
void givenAccessTokenWhenSaveThenNull(){
// Given
AccessToken accessToken = new AccessToken();
String accessToken = "AccessToken";

// When
Map<String, String> result = service.load(accessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void givenValidTokenWhenPostTokenThenSuccess(){
Mockito.when(validateExternalTokenServiceMock.validate(clientId, grantType, subjectToken, subjectIssuer, subjectTokenType, scope))
.thenReturn(expectedClaims);

AccessToken expectedAccessToken = new AccessToken();
AccessToken expectedAccessToken = AccessToken.builder().accessToken("accessToken").build();
Mockito.when(accessTokenBuilderServiceMock.build())
.thenReturn(expectedAccessToken);

Expand All @@ -62,6 +62,6 @@ void givenValidTokenWhenPostTokenThenSuccess(){

// Then
Assertions.assertSame(expectedAccessToken, result);
Mockito.verify(tokenStoreServiceMock).save(Mockito.same(expectedAccessToken), Mockito.same(expectedClaims));
Mockito.verify(tokenStoreServiceMock).save(Mockito.same(expectedAccessToken.getAccessToken()), Mockito.same(expectedClaims));
}
}

0 comments on commit 816d379

Please sign in to comment.