-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: P4PU-329 save access token #43
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c00eea3
[P4PU-329] added feature for saving tokens
oleksiybozhykntt b09c628
Merge branch 'develop' of https://github.com/pagopa/arc-be into P4PU-…
oleksiybozhykntt 6c60fd7
[P4PU-329] removed missleading annotation
oleksiybozhykntt 4d5880a
[P4PU-329] added optional for getUserInfo, changed token name followi…
oleksiybozhykntt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package it.gov.pagopa.arc.dto; | ||
|
||
import java.util.Map; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter @Setter | ||
@AllArgsConstructor | ||
@RequiredArgsConstructor | ||
public class IamUserInfoDTO { | ||
|
||
|
||
private String userId; | ||
private String fiscalCode; | ||
private String familyName; | ||
private String name; | ||
private String email; | ||
private String issuer; | ||
|
||
public static IamUserInfoDTO map2IamUserInfoDTO(Map<String, Object> attributes) { | ||
return new IamUserInfoDTO( | ||
getStringFromMap(attributes, "sub"), | ||
getStringFromMap(attributes, "fiscalNumber"), | ||
getStringFromMap(attributes, "familyName"), | ||
getStringFromMap(attributes, "name"), | ||
getStringFromMap(attributes, "email"), | ||
getStringFromMap(attributes, "iss") | ||
); | ||
} | ||
|
||
private static String getStringFromMap(Map<String, Object> map, String key) { | ||
return map.containsKey(key) ? map.get(key).toString() : null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/it/gov/pagopa/arc/service/TokenStoreService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package it.gov.pagopa.arc.service; | ||
|
||
import it.gov.pagopa.arc.dto.IamUserInfoDTO; | ||
|
||
|
||
public interface TokenStoreService { | ||
|
||
void save(String accessToken, IamUserInfoDTO userInfo); | ||
|
||
IamUserInfoDTO getUserInfo(String accessToken); | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/it/gov/pagopa/arc/service/TokenStoreServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package it.gov.pagopa.arc.service; | ||
|
||
import it.gov.pagopa.arc.dto.IamUserInfoDTO; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class TokenStoreServiceImpl implements TokenStoreService{ | ||
|
||
private Map<String, IamUserInfoDTO> tokens = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public void save(String accessToken, IamUserInfoDTO idTokenClaims) { | ||
tokens.put(accessToken,idTokenClaims); | ||
} | ||
|
||
@Override | ||
public IamUserInfoDTO getUserInfo(String accessToken) { | ||
return tokens.getOrDefault(accessToken, null); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/it/gov/pagopa/arc/dto/IamUserInfoDTOTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package it.gov.pagopa.arc.dto; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class IamUserInfoDTOTest { | ||
|
||
@Test | ||
public void testMap2IamUserInfoDTO() { | ||
Map<String, Object> attributes = Map.of( | ||
"sub", "123456", | ||
"fiscalNumber", "789012", | ||
"familyName", "Polo", | ||
"name", "Marco", | ||
"email", "marco.polo@example.com", | ||
"iss", "issuer" | ||
); | ||
|
||
IamUserInfoDTO userInfo = IamUserInfoDTO.map2IamUserInfoDTO(attributes); | ||
|
||
assertEquals("123456", userInfo.getUserId()); | ||
assertEquals("789012", userInfo.getFiscalCode()); | ||
assertEquals("Polo", userInfo.getFamilyName()); | ||
assertEquals("Marco", userInfo.getName()); | ||
assertEquals("marco.polo@example.com", userInfo.getEmail()); | ||
assertEquals("issuer", userInfo.getIssuer()); | ||
} | ||
|
||
@Test | ||
public void testMap2IamUserInfoDTOWithMissingAttributes() { | ||
Map<String, Object> attributes = Map.of( | ||
"sub", "123456" | ||
); | ||
|
||
IamUserInfoDTO userInfo = IamUserInfoDTO.map2IamUserInfoDTO(attributes); | ||
|
||
assertEquals("123456", userInfo.getUserId()); | ||
assertNull(userInfo.getFiscalCode()); | ||
assertNull(userInfo.getFamilyName()); | ||
assertNull(userInfo.getName()); | ||
assertNull(userInfo.getEmail()); | ||
assertNull(userInfo.getIssuer()); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,38 +8,68 @@ | |
import it.gov.pagopa.arc.config.JWTSampleConfiguration; | ||
import it.gov.pagopa.arc.model.generated.TokenResponse; | ||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.springframework.security.core.Authentication; | ||
|
||
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | ||
import org.springframework.security.oauth2.core.oidc.OidcIdToken; | ||
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
@ExtendWith(MockitoExtension.class) | ||
public class CustomAuthenticationSuccessHandlerTest { | ||
|
||
@Mock | ||
private AccessTokenBuilderService accessTokenBuilderService; | ||
|
||
@Mock | ||
private CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler; | ||
|
||
@Mock | ||
oleksiybozhykntt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private Authentication authentication; | ||
private TokenStoreService tokenStoreService; | ||
|
||
@BeforeEach | ||
void setUp(){ | ||
tokenStoreService = new TokenStoreServiceImpl(); | ||
JWTConfiguration jwtConfiguration = JWTSampleConfiguration.getCorrectConfiguration(); | ||
accessTokenBuilderService = new AccessTokenBuilderService(jwtConfiguration); | ||
customAuthenticationSuccessHandler = new CustomAuthenticationSuccessHandler(accessTokenBuilderService,new ObjectMapper(),jwtConfiguration); | ||
customAuthenticationSuccessHandler = new CustomAuthenticationSuccessHandler(new AccessTokenBuilderService(jwtConfiguration),new ObjectMapper(),jwtConfiguration,tokenStoreService); | ||
} | ||
@Test | ||
void givenAuthenticationRequestThenInResponseGetCustomTokenResponse() throws IOException { | ||
String sample_id_token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ik1hcmNvIiwiaWF0IjoxNTE2MjM5MDIyLCJmaXNjYWxOdW1iZXIiOiI3ODkwMTIiLCJmYW1pbHlOYW1lIjoiUG9sbyIsImVtYWlsIjoibWFyY28ucG9sb0BleGFtcGxlLmNvbSIsImlzcyI6Imlzc3VlciJ9.AErwXpbHrT_0WvN86QuQ0nvnZndVxt8jnmiD1lfj1_A"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sonar points out the naming of |
||
Consumer<Map<String, Object>> attributesConsumer = attributes -> { | ||
attributes.putAll(Map.of( | ||
"sub", "123456", | ||
"fiscalNumber", "789012", | ||
"familyName", "Polo", | ||
"name", "Marco", | ||
"email", "marco.polo@example.com", | ||
"iss", "issuer" | ||
)); | ||
}; | ||
Instant issuedAt = Instant.now(); | ||
Instant expiresAt = issuedAt.plusSeconds(3600); | ||
|
||
OidcIdToken idToken = OidcIdToken.withTokenValue(sample_id_token) | ||
.issuedAt(issuedAt) | ||
.expiresAt(expiresAt) | ||
.claims(attributesConsumer) | ||
.build(); | ||
OAuth2User principal = new DefaultOidcUser(null,idToken); | ||
OAuth2AuthenticationToken oAuth2AuthenticationToken = new OAuth2AuthenticationToken(principal,null,"test-client"); | ||
|
||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
customAuthenticationSuccessHandler.onAuthenticationSuccess(request,response,authentication); | ||
|
||
customAuthenticationSuccessHandler.onAuthenticationSuccess(request,response,oAuth2AuthenticationToken); | ||
|
||
assertEquals(response.getHeader("Content-Type"),"application/json"); | ||
|
||
TokenResponse token = new ObjectMapper().readValue(response.getContentAsString(),TokenResponse.class); | ||
assertNotNull(token.getAccessToken()); | ||
assertNotNull(tokenStoreService.getUserInfo(token.getAccessToken())); | ||
assertNotNull(token.getTokenType()); | ||
assertNotNull(token.getExpiresIn()); | ||
} | ||
|
44 changes: 44 additions & 0 deletions
44
src/test/java/it/gov/pagopa/arc/service/TokenStoreServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package it.gov.pagopa.arc.service; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import it.gov.pagopa.arc.dto.IamUserInfoDTO; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
|
||
class TokenStoreServiceImplTest { | ||
|
||
@Mock | ||
TokenStoreServiceImpl tokenStoreService; | ||
|
||
@BeforeEach | ||
void setUp(){ | ||
tokenStoreService = new TokenStoreServiceImpl(); | ||
} | ||
@Test | ||
void givenAccessTokenAndUserInfoThenSaveAndRetrieveTheSameData() { | ||
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; | ||
Map<String, Object> attributes = Map.of( | ||
"sub", "123456", | ||
"fiscalNumber", "789012", | ||
"familyName", "Polo", | ||
"name", "Marco", | ||
"email", "marco.polo@example.com", | ||
"iss", "issuer" | ||
); | ||
IamUserInfoDTO userInfo = IamUserInfoDTO.map2IamUserInfoDTO(attributes); | ||
tokenStoreService.save(token,userInfo); | ||
|
||
assertNotNull(tokenStoreService.getUserInfo(token)); | ||
|
||
assertEquals(attributes.get("sub"),tokenStoreService.getUserInfo(token).getUserId()); | ||
assertEquals(attributes.get("fiscalNumber"),tokenStoreService.getUserInfo(token).getFiscalCode()); | ||
assertEquals(attributes.get("familyName"),tokenStoreService.getUserInfo(token).getFamilyName()); | ||
assertEquals(attributes.get("name"),tokenStoreService.getUserInfo(token).getName()); | ||
assertEquals(attributes.get("email"),tokenStoreService.getUserInfo(token).getEmail()); | ||
assertEquals(attributes.get("iss"),tokenStoreService.getUserInfo(token).getIssuer()); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider the use of
Optional
as return param