-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retrofit Entur authorization service (#340)
* Retrofit Entur authorization service * Code cleanup
- Loading branch information
Showing
5 changed files
with
41 additions
and
181 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
73 changes: 19 additions & 54 deletions
73
src/ext/java/no/entur/uttu/ext/entur/security/EnturUserContextService.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,85 +1,50 @@ | ||
package no.entur.uttu.ext.entur.security; | ||
|
||
import static org.rutebanken.helper.organisation.AuthorizationConstants.ROLE_ROUTE_DATA_ADMIN; | ||
import static org.rutebanken.helper.organisation.AuthorizationConstants.ROLE_ROUTE_DATA_EDIT; | ||
|
||
import java.util.List; | ||
import no.entur.uttu.model.Provider; | ||
import no.entur.uttu.repository.ProviderRepository; | ||
import no.entur.uttu.security.spi.UserContextService; | ||
import org.rutebanken.helper.organisation.RoleAssignment; | ||
import org.rutebanken.helper.organisation.RoleAssignmentExtractor; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; | ||
import org.rutebanken.helper.organisation.authorization.AuthorizationService; | ||
import org.rutebanken.helper.organisation.authorization.DefaultAuthorizationService; | ||
import org.rutebanken.helper.organisation.user.UserInfoExtractor; | ||
|
||
public class EnturUserContextService implements UserContextService { | ||
|
||
private final ProviderRepository providerRepository; | ||
|
||
private final RoleAssignmentExtractor roleAssignmentExtractor; | ||
private final AuthorizationService<String> authorizationService; | ||
private final UserInfoExtractor userInfoExtractor; | ||
|
||
public EnturUserContextService( | ||
ProviderRepository providerRepository, | ||
RoleAssignmentExtractor roleAssignmentExtractor | ||
RoleAssignmentExtractor roleAssignmentExtractor, | ||
UserInfoExtractor userInfoExtractor | ||
) { | ||
this.providerRepository = providerRepository; | ||
this.roleAssignmentExtractor = roleAssignmentExtractor; | ||
this.userInfoExtractor = userInfoExtractor; | ||
authorizationService = | ||
new DefaultAuthorizationService<>( | ||
this::getProviderCodespaceByProviderCode, | ||
roleAssignmentExtractor | ||
); | ||
} | ||
|
||
@Override | ||
public String getPreferredName() { | ||
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
JwtAuthenticationToken jwtAuthenticationToken = (JwtAuthenticationToken) auth; | ||
Jwt jwt = (Jwt) jwtAuthenticationToken.getPrincipal(); | ||
return jwt.getClaimAsString("https://ror.entur.io/preferred_name"); | ||
return userInfoExtractor.getPreferredName(); | ||
} | ||
|
||
@Override | ||
public boolean isAdmin() { | ||
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
List<RoleAssignment> roleAssignments = | ||
roleAssignmentExtractor.getRoleAssignmentsForUser(auth); | ||
|
||
return roleAssignments | ||
.stream() | ||
.anyMatch(roleAssignment -> | ||
roleAssignment.getRole().equals(ROLE_ROUTE_DATA_ADMIN) && | ||
roleAssignment.getOrganisation().equals("RB") | ||
); | ||
return authorizationService.isRouteDataAdmin(); | ||
} | ||
|
||
@Override | ||
public boolean hasAccessToProvider(String providerCode) { | ||
if (providerCode == null) { | ||
return false; | ||
} | ||
Provider provider = providerRepository.getOne(providerCode); | ||
if (provider == null) { | ||
return false; | ||
} | ||
|
||
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | ||
|
||
List<RoleAssignment> roleAssignments = | ||
roleAssignmentExtractor.getRoleAssignmentsForUser(auth); | ||
|
||
return roleAssignments | ||
.stream() | ||
.anyMatch(roleAssignment -> | ||
( | ||
roleAssignment.getRole().equals(ROLE_ROUTE_DATA_ADMIN) && | ||
roleAssignment.getOrganisation().equals("RB") | ||
) || | ||
match(roleAssignment, ROLE_ROUTE_DATA_EDIT, provider) | ||
); | ||
return authorizationService.canEditRouteData(providerCode); | ||
} | ||
|
||
private boolean match(RoleAssignment roleAssignment, String role, Provider provider) { | ||
return ( | ||
role.equals(roleAssignment.getRole()) && | ||
provider.getCodespace().getXmlns().equals(roleAssignment.getOrganisation()) | ||
); | ||
private String getProviderCodespaceByProviderCode(String providerCode) { | ||
Provider provider = providerRepository.getOne(providerCode); | ||
return provider == null ? null : provider.getCodespace().getXmlns(); | ||
} | ||
} |
62 changes: 0 additions & 62 deletions
62
src/test/java/no/entur/uttu/config/MockedRoleAssignmentExtractor.java
This file was deleted.
Oops, something went wrong.
57 changes: 0 additions & 57 deletions
57
src/test/java/no/entur/uttu/config/RoleAssignmentListBuilder.java
This file was deleted.
Oops, something went wrong.