Skip to content
Snippets Groups Projects
Commit 1063af07 authored by Lukas Malte Monnerjahn's avatar Lukas Malte Monnerjahn
Browse files

OZG-5176 PR Anmerkungen

parent 598c3aad
No related branches found
No related tags found
No related merge requests found
Showing
with 30 additions and 41 deletions
...@@ -56,7 +56,7 @@ public class RootModelAssembler implements RepresentationModelAssembler<Root, En ...@@ -56,7 +56,7 @@ public class RootModelAssembler implements RepresentationModelAssembler<Root, En
List<Link> links = new ArrayList<>(); List<Link> links = new ArrayList<>();
var rootLinkBuilder = WebMvcLinkBuilder.linkTo(RootController.class); var rootLinkBuilder = WebMvcLinkBuilder.linkTo(RootController.class);
links.add(rootLinkBuilder.withSelfRel()); links.add(rootLinkBuilder.withSelfRel());
if (currentUserService.hasRole(UserRole.ADMIN_USER)) { if (currentUserService.hasRole(UserRole.ADMIN_ADMIN)) {
links.add(buildConfigLink()); links.add(buildConfigLink());
} }
return links; return links;
......
...@@ -28,5 +28,5 @@ import lombok.NoArgsConstructor; ...@@ -28,5 +28,5 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor(access = AccessLevel.PRIVATE) @NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UserRole { public class UserRole {
public static final String ADMIN_USER = "ADMIN_ADMIN"; public static final String ADMIN_ADMIN = "ADMIN_ADMIN";
} }
...@@ -73,8 +73,8 @@ public class SecurityConfiguration { ...@@ -73,8 +73,8 @@ public class SecurityConfiguration {
http.authorizeHttpRequests(requests -> requests http.authorizeHttpRequests(requests -> requests
.requestMatchers(HttpMethod.GET, "/api/environment").permitAll() .requestMatchers(HttpMethod.GET, "/api/environment").permitAll()
.requestMatchers("/api/configuration").hasRole(UserRole.ADMIN_USER) .requestMatchers("/api/configuration").hasRole(UserRole.ADMIN_ADMIN)
.requestMatchers("/api/configuration/**").hasRole(UserRole.ADMIN_USER) .requestMatchers("/api/configuration/**").hasRole(UserRole.ADMIN_ADMIN)
.requestMatchers("/api").authenticated() .requestMatchers("/api").authenticated()
.requestMatchers("/api/**").authenticated() .requestMatchers("/api/**").authenticated()
.requestMatchers("/actuator").permitAll() .requestMatchers("/actuator").permitAll()
...@@ -95,7 +95,7 @@ public class SecurityConfiguration { ...@@ -95,7 +95,7 @@ public class SecurityConfiguration {
} }
Set<GrantedAuthority> convertJwtToGrantedAuthorities(Jwt jwt) { Set<GrantedAuthority> convertJwtToGrantedAuthorities(Jwt jwt) {
return getKeycloakRolesFromJwt(jwt) return getRolesFromJwt(jwt)
.stream() .stream()
.map(this::mapRoleStringToGrantedAuthority) .map(this::mapRoleStringToGrantedAuthority)
.collect(toSet()); .collect(toSet());
...@@ -105,7 +105,7 @@ public class SecurityConfiguration { ...@@ -105,7 +105,7 @@ public class SecurityConfiguration {
return new SimpleGrantedAuthority(SIMPLE_GRANT_AUTHORITY_PREFIX + role); return new SimpleGrantedAuthority(SIMPLE_GRANT_AUTHORITY_PREFIX + role);
} }
List<String> getKeycloakRolesFromJwt(Jwt jwt) { List<String> getRolesFromJwt(Jwt jwt) {
return Optional.ofNullable(jwt.getClaimAsMap(RESOURCE_ACCESS_KEY)) return Optional.ofNullable(jwt.getClaimAsMap(RESOURCE_ACCESS_KEY))
.flatMap(resourceAccessMap -> getMap(resourceAccessMap, oAuth2Properties.getResource())) .flatMap(resourceAccessMap -> getMap(resourceAccessMap, oAuth2Properties.getResource()))
.flatMap(adminClientMap -> getList(adminClientMap, ROLES_KEY)) .flatMap(adminClientMap -> getList(adminClientMap, ROLES_KEY))
......
...@@ -40,7 +40,7 @@ import lombok.SneakyThrows; ...@@ -40,7 +40,7 @@ import lombok.SneakyThrows;
@ITCase @ITCase
@AutoConfigureMockMvc @AutoConfigureMockMvc
@WithMockUser(roles = UserRole.ADMIN_USER) @WithMockUser(roles = UserRole.ADMIN_ADMIN)
class ApiRootITCase { class ApiRootITCase {
@Autowired @Autowired
......
...@@ -57,7 +57,7 @@ class RootModelAssemblerTest { ...@@ -57,7 +57,7 @@ class RootModelAssemblerTest {
class TestEntityModel { class TestEntityModel {
@BeforeEach @BeforeEach
void beforeEach() { void beforeEach() {
Mockito.when(currentUserService.hasRole(UserRole.ADMIN_USER)).thenReturn(true); Mockito.when(currentUserService.hasRole(UserRole.ADMIN_ADMIN)).thenReturn(true);
Mockito.when(restProperties.getBasePath()).thenReturn(BASE_PATH); Mockito.when(restProperties.getBasePath()).thenReturn(BASE_PATH);
} }
...@@ -90,7 +90,7 @@ class RootModelAssemblerTest { ...@@ -90,7 +90,7 @@ class RootModelAssemblerTest {
@Test @Test
void shouldHaveHrefToBasePathIfAuthorized() { void shouldHaveHrefToBasePathIfAuthorized() {
Mockito.when(restProperties.getBasePath()).thenReturn(BASE_PATH); Mockito.when(restProperties.getBasePath()).thenReturn(BASE_PATH);
Mockito.when(currentUserService.hasRole(UserRole.ADMIN_USER)).thenReturn(true); Mockito.when(currentUserService.hasRole(UserRole.ADMIN_ADMIN)).thenReturn(true);
List<Link> links = modelAssembler.buildRootModelLinks(); List<Link> links = modelAssembler.buildRootModelLinks();
...@@ -101,7 +101,7 @@ class RootModelAssemblerTest { ...@@ -101,7 +101,7 @@ class RootModelAssemblerTest {
@Test @Test
void shouldNotHaveHrefToBasePathIfUnauthorized() { void shouldNotHaveHrefToBasePathIfUnauthorized() {
Mockito.when(currentUserService.hasRole(UserRole.ADMIN_USER)).thenReturn(false); Mockito.when(currentUserService.hasRole(UserRole.ADMIN_ADMIN)).thenReturn(false);
List<Link> links = modelAssembler.buildRootModelLinks(); List<Link> links = modelAssembler.buildRootModelLinks();
......
...@@ -59,7 +59,7 @@ class CurrentUserHelperTest { ...@@ -59,7 +59,7 @@ class CurrentUserHelperTest {
Mockito.CALLS_REAL_METHODS)) { Mockito.CALLS_REAL_METHODS)) {
mockUserHelper.when(CurrentUserHelper::getAuthentication).thenReturn(null); mockUserHelper.when(CurrentUserHelper::getAuthentication).thenReturn(null);
boolean hasRole = CurrentUserHelper.hasRole(UserRole.ADMIN_USER); boolean hasRole = CurrentUserHelper.hasRole(UserRole.ADMIN_ADMIN);
assertThat(hasRole).isFalse(); assertThat(hasRole).isFalse();
} }
...@@ -73,7 +73,7 @@ class CurrentUserHelperTest { ...@@ -73,7 +73,7 @@ class CurrentUserHelperTest {
Mockito.CALLS_REAL_METHODS)) { Mockito.CALLS_REAL_METHODS)) {
mockUserHelper.when(CurrentUserHelper::getAuthentication).thenReturn(mockAuthentication); mockUserHelper.when(CurrentUserHelper::getAuthentication).thenReturn(mockAuthentication);
boolean hasRole = CurrentUserHelper.hasRole(UserRole.ADMIN_USER); boolean hasRole = CurrentUserHelper.hasRole(UserRole.ADMIN_ADMIN);
assertThat(hasRole).isFalse(); assertThat(hasRole).isFalse();
} }
...@@ -93,9 +93,9 @@ class CurrentUserHelperTest { ...@@ -93,9 +93,9 @@ class CurrentUserHelperTest {
mockUserHelper.when(() -> CurrentUserHelper.containsRole(Mockito.anyList(), Mockito.anyString())) mockUserHelper.when(() -> CurrentUserHelper.containsRole(Mockito.anyList(), Mockito.anyString()))
.thenReturn(containsRoleValue); .thenReturn(containsRoleValue);
boolean hasRole = CurrentUserHelper.hasRole(UserRole.ADMIN_USER); boolean hasRole = CurrentUserHelper.hasRole(UserRole.ADMIN_ADMIN);
mockUserHelper.verify(() -> CurrentUserHelper.containsRole(mockAuthentication.getAuthorities(), UserRole.ADMIN_USER)); mockUserHelper.verify(() -> CurrentUserHelper.containsRole(mockAuthentication.getAuthorities(), UserRole.ADMIN_ADMIN));
assertThat(hasRole).isEqualTo(containsRoleValue); assertThat(hasRole).isEqualTo(containsRoleValue);
} }
} }
...@@ -106,7 +106,7 @@ class CurrentUserHelperTest { ...@@ -106,7 +106,7 @@ class CurrentUserHelperTest {
class TestContainsRole { class TestContainsRole {
@Test @Test
void shouldNotContainRoleIfAuthoritiesIsNull() { void shouldNotContainRoleIfAuthoritiesIsNull() {
boolean containsRole = CurrentUserHelper.containsRole(null, UserRole.ADMIN_USER); boolean containsRole = CurrentUserHelper.containsRole(null, UserRole.ADMIN_ADMIN);
assertThat(containsRole).isFalse(); assertThat(containsRole).isFalse();
} }
...@@ -116,7 +116,7 @@ class CurrentUserHelperTest { ...@@ -116,7 +116,7 @@ class CurrentUserHelperTest {
List<GrantedAuthority> authorities = List.of( List<GrantedAuthority> authorities = List.of(
new SimpleGrantedAuthority(CurrentUserHelper.ROLE_PREFIX + "OTHER")); new SimpleGrantedAuthority(CurrentUserHelper.ROLE_PREFIX + "OTHER"));
boolean containsRole = CurrentUserHelper.containsRole(authorities, UserRole.ADMIN_USER); boolean containsRole = CurrentUserHelper.containsRole(authorities, UserRole.ADMIN_ADMIN);
assertThat(containsRole).isFalse(); assertThat(containsRole).isFalse();
} }
...@@ -124,9 +124,9 @@ class CurrentUserHelperTest { ...@@ -124,9 +124,9 @@ class CurrentUserHelperTest {
@Test @Test
void shouldContainRole() { void shouldContainRole() {
Collection<? extends GrantedAuthority> authorities = List.of( Collection<? extends GrantedAuthority> authorities = List.of(
new SimpleGrantedAuthority(CurrentUserHelper.ROLE_PREFIX + UserRole.ADMIN_USER)); new SimpleGrantedAuthority(CurrentUserHelper.ROLE_PREFIX + UserRole.ADMIN_ADMIN));
boolean containsRole = CurrentUserHelper.containsRole(authorities, UserRole.ADMIN_USER); boolean containsRole = CurrentUserHelper.containsRole(authorities, UserRole.ADMIN_ADMIN);
assertThat(containsRole).isTrue(); assertThat(containsRole).isTrue();
} }
...@@ -138,16 +138,16 @@ class CurrentUserHelperTest { ...@@ -138,16 +138,16 @@ class CurrentUserHelperTest {
@Test @Test
void shouldAddPrefixIfMissing() { void shouldAddPrefixIfMissing() {
var roleWithoutPrefix = UserRole.ADMIN_USER; var roleWithoutPrefix = UserRole.ADMIN_ADMIN;
var role = CurrentUserHelper.addRolePrefixIfMissing(roleWithoutPrefix); var role = CurrentUserHelper.addRolePrefixIfMissing(roleWithoutPrefix);
assertThat(role).isEqualTo(CurrentUserHelper.ROLE_PREFIX + UserRole.ADMIN_USER); assertThat(role).isEqualTo(CurrentUserHelper.ROLE_PREFIX + UserRole.ADMIN_ADMIN);
} }
@Test @Test
void shouldReturnRoleIfPrefixAlreadyExists() { void shouldReturnRoleIfPrefixAlreadyExists() {
var roleWithPrefix = CurrentUserHelper.ROLE_PREFIX + UserRole.ADMIN_USER; var roleWithPrefix = CurrentUserHelper.ROLE_PREFIX + UserRole.ADMIN_ADMIN;
var role = CurrentUserHelper.addRolePrefixIfMissing(roleWithPrefix); var role = CurrentUserHelper.addRolePrefixIfMissing(roleWithPrefix);
......
...@@ -47,9 +47,9 @@ class CurrentUserServiceTest { ...@@ -47,9 +47,9 @@ class CurrentUserServiceTest {
mockUserHelper.when(() -> CurrentUserHelper.hasRole(Mockito.anyString())) mockUserHelper.when(() -> CurrentUserHelper.hasRole(Mockito.anyString()))
.thenReturn(hasRoleValue); .thenReturn(hasRoleValue);
boolean hasRole = currentUserService.hasRole(UserRole.ADMIN_USER); boolean hasRole = currentUserService.hasRole(UserRole.ADMIN_ADMIN);
mockUserHelper.verify(() -> CurrentUserHelper.hasRole(UserRole.ADMIN_USER)); mockUserHelper.verify(() -> CurrentUserHelper.hasRole(UserRole.ADMIN_ADMIN));
assertThat(hasRole).isEqualTo(hasRoleValue); assertThat(hasRole).isEqualTo(hasRoleValue);
} }
} }
......
...@@ -44,17 +44,6 @@ public class JwtTestFactory { ...@@ -44,17 +44,6 @@ public class JwtTestFactory {
return createBuilder().claim(RESOURCE_ACCESS_KEY, Map.of(AUTH_RESOURCE, Map.of(ROLES_KEY, roles))); return createBuilder().claim(RESOURCE_ACCESS_KEY, Map.of(AUTH_RESOURCE, Map.of(ROLES_KEY, roles)));
} }
// private static Map<String, Object> readResourceAccessClaim() {
// var claimsJson = TestUtils.loadTextFile("jsonTemplates/security/resource_access.template.json");
// var mapper = new ObjectMapper();
// try {
// return mapper.readValue(claimsJson, new TypeReference<Map<String, Object>>() {
// });
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
// }
public static Jwt.Builder createBuilder() { public static Jwt.Builder createBuilder() {
return Jwt.withTokenValue("token-value").header("header-key", "header-value").claim("claim-key", "claim-value"); return Jwt.withTokenValue("token-value").header("header-key", "header-value").claim("claim-key", "claim-value");
} }
......
...@@ -109,7 +109,7 @@ class SecurityConfigurationTest { ...@@ -109,7 +109,7 @@ class SecurityConfigurationTest {
void mock() { void mock() {
var keycloakRoles = List.of(ROLE_1, JwtTestFactory.ROLE_2, JwtTestFactory.ROLE_3); var keycloakRoles = List.of(ROLE_1, JwtTestFactory.ROLE_2, JwtTestFactory.ROLE_3);
expectedSecurityRoleStrings = keycloakRoles.stream().map(role -> SIMPLE_GRANT_AUTHORITY_PREFIX + role).toList(); expectedSecurityRoleStrings = keycloakRoles.stream().map(role -> SIMPLE_GRANT_AUTHORITY_PREFIX + role).toList();
doReturn(keycloakRoles).when(securityConfiguration).getKeycloakRolesFromJwt(any()); doReturn(keycloakRoles).when(securityConfiguration).getRolesFromJwt(any());
} }
@DisplayName("should call get keycloak roles from jwt") @DisplayName("should call get keycloak roles from jwt")
...@@ -119,7 +119,7 @@ class SecurityConfigurationTest { ...@@ -119,7 +119,7 @@ class SecurityConfigurationTest {
securityConfiguration.convertJwtToGrantedAuthorities(jwt); securityConfiguration.convertJwtToGrantedAuthorities(jwt);
verify(securityConfiguration).getKeycloakRolesFromJwt(jwt); verify(securityConfiguration).getRolesFromJwt(jwt);
} }
@DisplayName("should return granted authorities with ROLE_ prefix") @DisplayName("should return granted authorities with ROLE_ prefix")
...@@ -136,9 +136,9 @@ class SecurityConfigurationTest { ...@@ -136,9 +136,9 @@ class SecurityConfigurationTest {
} }
} }
@DisplayName("get keycloak roles from jwt") @DisplayName("get roles from jwt")
@Nested @Nested
class TestGetKeycloakRolesFromJwt { class TestGetRolesFromJwt {
@BeforeEach @BeforeEach
void mock() { void mock() {
...@@ -149,7 +149,7 @@ class SecurityConfigurationTest { ...@@ -149,7 +149,7 @@ class SecurityConfigurationTest {
@ParameterizedTest @ParameterizedTest
@MethodSource("getIncompleteJwt") @MethodSource("getIncompleteJwt")
void shouldReturnEmptyListIfResourceAccessAdminRolesPathIsMissing(Jwt incompleteJwt) { void shouldReturnEmptyListIfResourceAccessAdminRolesPathIsMissing(Jwt incompleteJwt) {
var roleStrings = securityConfiguration.getKeycloakRolesFromJwt(incompleteJwt); var roleStrings = securityConfiguration.getRolesFromJwt(incompleteJwt);
assertThat(roleStrings).isEmpty(); assertThat(roleStrings).isEmpty();
} }
...@@ -168,7 +168,7 @@ class SecurityConfigurationTest { ...@@ -168,7 +168,7 @@ class SecurityConfigurationTest {
var expectedRoles = List.of(ROLE_1, JwtTestFactory.ROLE_2, JwtTestFactory.ROLE_3); var expectedRoles = List.of(ROLE_1, JwtTestFactory.ROLE_2, JwtTestFactory.ROLE_3);
var jwtWithRoles = JwtTestFactory.createWithRoles(expectedRoles).build(); var jwtWithRoles = JwtTestFactory.createWithRoles(expectedRoles).build();
var roleStrings = securityConfiguration.getKeycloakRolesFromJwt(jwtWithRoles); var roleStrings = securityConfiguration.getRolesFromJwt(jwtWithRoles);
assertThat(roleStrings).isEqualTo(expectedRoles); assertThat(roleStrings).isEqualTo(expectedRoles);
} }
......
...@@ -56,7 +56,7 @@ import lombok.SneakyThrows; ...@@ -56,7 +56,7 @@ import lombok.SneakyThrows;
@DataITCase @DataITCase
@AutoConfigureMockMvc @AutoConfigureMockMvc
@WithMockUser(roles = UserRole.ADMIN_USER) @WithMockUser(roles = UserRole.ADMIN_ADMIN)
class SettingITCase { class SettingITCase {
@Autowired @Autowired
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment