-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
antonio.torre
committed
Jun 5, 2024
1 parent
816d379
commit 51f3f32
Showing
15 changed files
with
293 additions
and
52 deletions.
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
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
6 changes: 3 additions & 3 deletions
6
src/main/java/it/gov/pagopa/payhub/auth/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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package it.gov.pagopa.payhub.auth.service; | ||
|
||
import java.util.Map; | ||
import it.gov.pagopa.payhub.model.generated.UserInfo; | ||
|
||
public interface TokenStoreService { | ||
Map<String, String> save(String accessToken, Map<String, String> idTokenClaims); | ||
Map<String, String> load(String accessToken); | ||
UserInfo save(String accessToken, UserInfo userInfo); | ||
UserInfo load(String accessToken); | ||
void delete(String accessToken); | ||
} |
7 changes: 3 additions & 4 deletions
7
src/main/java/it/gov/pagopa/payhub/auth/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
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
61 changes: 61 additions & 0 deletions
61
src/main/java/it/gov/pagopa/payhub/auth/service/exchange/IDTokenClaims2UserInfoMapper.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,61 @@ | ||
package it.gov.pagopa.payhub.auth.service.exchange; | ||
|
||
import com.auth0.jwt.interfaces.Claim; | ||
import io.jsonwebtoken.Claims; | ||
import it.gov.pagopa.payhub.auth.exception.custom.InvalidTokenException; | ||
import it.gov.pagopa.payhub.model.generated.UserInfo; | ||
import it.gov.pagopa.payhub.model.generated.UserOrganizationRoles; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
|
||
@Service | ||
public class IDTokenClaims2UserInfoMapper implements Function<Map<String, Claim>, UserInfo> { | ||
@Override | ||
public UserInfo apply(Map<String, Claim> claims) { | ||
try { | ||
return UserInfo.builder() | ||
.issuer(claims.get(Claims.ISSUER).asString()) | ||
.userId(claims.get("uid").asString()) | ||
.name(claims.get("name").asString()) | ||
.familyName(claims.get("family_name").asString()) | ||
.fiscalCode(claims.get("fiscal_number").asString()) | ||
.organization(buildUserOrganizationRoles(claims)) | ||
.build(); | ||
} catch (Exception e){ | ||
throw new InvalidTokenException("Unexpected IDToken structure", e); | ||
} | ||
} | ||
|
||
private UserOrganizationRoles buildUserOrganizationRoles(Map<String, Claim> claims) { | ||
Map<String, Object> organizationClaim = claims.get("organization").asMap(); | ||
|
||
List<String> roles = readUserOrganizationRoles(organizationClaim); | ||
return UserOrganizationRoles.builder() | ||
.id((String)organizationClaim.get("id")) | ||
.name((String)organizationClaim.get("name")) | ||
.fiscalCode((String)organizationClaim.get("fiscal_code")) | ||
.ipaCode((String)organizationClaim.get("ipaCode")) | ||
.roles(roles) | ||
.build(); | ||
} | ||
|
||
private static List<String> readUserOrganizationRoles(Map<String, Object> organizationClaim) { | ||
List<String> out; | ||
if(organizationClaim.get("roles") instanceof List<?> roles){ | ||
out = roles.stream().map(o -> (o instanceof Map<?, ?> orgMap) ? (String) orgMap.get("role") : null) | ||
.filter(Objects::nonNull) | ||
.toList(); | ||
} else { | ||
out = List.of(); | ||
} | ||
if(out.isEmpty()){ | ||
throw new InvalidTokenException("No organization roles provided"); | ||
} else { | ||
return out; | ||
} | ||
} | ||
} |
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
15 changes: 14 additions & 1 deletion
15
src/main/java/it/gov/pagopa/payhub/auth/service/user/UserServiceImpl.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 |
---|---|---|
@@ -1,12 +1,25 @@ | ||
package it.gov.pagopa.payhub.auth.service.user; | ||
|
||
import it.gov.pagopa.payhub.auth.exception.custom.InvalidAccessTokenException; | ||
import it.gov.pagopa.payhub.auth.service.TokenStoreService; | ||
import it.gov.pagopa.payhub.model.generated.UserInfo; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class UserServiceImpl implements UserService{ | ||
|
||
private final TokenStoreService tokenStoreService; | ||
public UserServiceImpl(TokenStoreService tokenStoreService) { | ||
this.tokenStoreService = tokenStoreService; | ||
} | ||
|
||
@Override | ||
public UserInfo getUserInfo(String accessToken) { | ||
return null; //TODO | ||
UserInfo userInfo = tokenStoreService.load(accessToken); | ||
if(userInfo==null){ | ||
throw new InvalidAccessTokenException("AccessToken not found"); | ||
} else { | ||
return userInfo; | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.