diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/common/user/CurrentUserHelper.java b/alfa-service/src/main/java/de/ozgcloud/alfa/common/user/CurrentUserHelper.java index c3752745433b597fec75c1eea29995c7efda3075..82ab8a80f3e2ce5b9ad86fa268ab764ce5b8d34a 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/common/user/CurrentUserHelper.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/common/user/CurrentUserHelper.java @@ -34,6 +34,7 @@ import org.springframework.security.authentication.AuthenticationTrustResolverIm import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.oauth2.jwt.Jwt; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -41,20 +42,13 @@ import lombok.NoArgsConstructor; @NoArgsConstructor(access = AccessLevel.PRIVATE) public class CurrentUserHelper { static final String ROLE_PREFIX = "ROLE_"; + private static final String SUB_CLAIM_KEY = "sub"; public static final Predicate<String> HAS_ROLE = CurrentUserHelper::hasRole; private static final AuthenticationTrustResolver TRUST_RESOLVER = new AuthenticationTrustResolverImpl(); private static final Predicate<Authentication> TRUSTED = auth -> !TRUST_RESOLVER.isAnonymous(auth); - public static Authentication getAuthentication() { - return findAuthentication().orElseThrow(() -> new IllegalStateException("No authenticated User found")); - } - - public static Optional<Authentication> findAuthentication() { - return Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication()).filter(TRUSTED); - } - public static boolean hasRole(String role) { var auth = getAuthentication(); @@ -82,7 +76,15 @@ public class CurrentUserHelper { return containsRole(roleToCheck, authorities); } - public static String prepareRoleForCheck(String role) { + static boolean containsRole(String role, Collection<? extends GrantedAuthority> authorities) { + return authorities.stream().anyMatch(a -> isAuthorityEquals(role, a.getAuthority())); + } + + private static boolean isAuthorityEquals(String role, String authority) { + return StringUtils.equalsIgnoreCase(role, authority) || StringUtils.equalsIgnoreCase(prepareRoleForCheck(role), authority); + } + + static String prepareRoleForCheck(String role) { if ((Objects.nonNull(role)) && (!role.startsWith(ROLE_PREFIX))) { return ROLE_PREFIX + role; } else { @@ -90,17 +92,19 @@ public class CurrentUserHelper { } } - public static boolean containsRole(String role, Collection<? extends GrantedAuthority> authorities) { - return authorities.stream().anyMatch(a -> isAuthorityEquals(role, a.getAuthority())); + public static UserId getCurrentUserId() { + return UserId.from(getSubClaim()); } - private static boolean isAuthorityEquals(String role, String authority) { - String roleToCheck = prepareRoleForCheck(role); - return StringUtils.equalsIgnoreCase(role, authority) || StringUtils.equalsIgnoreCase(roleToCheck, authority); + private static String getSubClaim() { + return ((Jwt) getAuthentication().getPrincipal()).getClaim(SUB_CLAIM_KEY); } - static UserId getCurrentUserId() { - return UserId.from(getAuthentication().getName()); + public static Authentication getAuthentication() { + return findAuthentication().orElseThrow(() -> new IllegalStateException("No authenticated User found")); } + private static Optional<Authentication> findAuthentication() { + return Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication()).filter(TRUSTED); + } }