Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrofit Entur authorization service #340

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<java.version>21</java.version>
<entur.google.pubsub.emulator.download.skip>true</entur.google.pubsub.emulator.download.skip>

<entur.helpers.version>2.24</entur.helpers.version>
<entur.helpers.version>2.25</entur.helpers.version>

<entur-google-pubsub-version>${entur.helpers.version}</entur-google-pubsub-version>
<gcp-storage.version>${entur.helpers.version}</gcp-storage.version>
Expand Down
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.