Skip to content

Commit

Permalink
Retrofit Entur authorization service (#340)
Browse files Browse the repository at this point in the history
* Retrofit Entur authorization service

* Code cleanup
  • Loading branch information
vpaturet authored Jun 10, 2024
1 parent 3fd0983 commit 8791537
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import no.entur.uttu.model.Provider;
import no.entur.uttu.repository.ProviderRepository;
import org.entur.oauth2.JwtRoleAssignmentExtractor;
import org.entur.oauth2.user.JwtUserInfoExtractor;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -30,7 +31,8 @@ void setUp() {
subject =
new EnturUserContextService(
mockProviderRepository,
new JwtRoleAssignmentExtractor()
new JwtRoleAssignmentExtractor(),
new JwtUserInfoExtractor()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import org.entur.oauth2.JwtRoleAssignmentExtractor;
import org.entur.oauth2.RorAuthenticationConverter;
import org.entur.oauth2.multiissuer.MultiIssuerAuthenticationManagerResolverBuilder;
import org.entur.oauth2.user.JwtUserInfoExtractor;
import org.rutebanken.helper.organisation.RoleAssignmentExtractor;
import org.rutebanken.helper.organisation.user.UserInfoExtractor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -24,16 +26,26 @@ public JwtAuthenticationConverter customJwtAuthenticationConverter() {
}

@Bean
public UserContextService userContextService(
ProviderRepository providerRepository,
RoleAssignmentExtractor roleAssignmentExtractor
) {
return new EnturUserContextService(providerRepository, roleAssignmentExtractor);
public RoleAssignmentExtractor roleAssignmentExtractor() {
return new JwtRoleAssignmentExtractor();
}

@Bean
public RoleAssignmentExtractor roleAssignmentExtractor() {
return new JwtRoleAssignmentExtractor();
public UserInfoExtractor userInfoExtractor() {
return new JwtUserInfoExtractor();
}

@Bean
public UserContextService userContextService(
ProviderRepository providerRepository,
RoleAssignmentExtractor roleAssignmentExtractor,
UserInfoExtractor userInfoExtractor
) {
return new EnturUserContextService(
providerRepository,
roleAssignmentExtractor,
userInfoExtractor
);
}

@Bean
Expand Down
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();
}
}

This file was deleted.

57 changes: 0 additions & 57 deletions src/test/java/no/entur/uttu/config/RoleAssignmentListBuilder.java

This file was deleted.

0 comments on commit 8791537

Please sign in to comment.