Skip to content

Commit

Permalink
FKS-364: Upgrade to latest authorization library to support multiple …
Browse files Browse the repository at this point in the history
…roles on a user.
  • Loading branch information
sourcecodeas committed Dec 3, 2023
1 parent 2ac0fc0 commit dd31788
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dependencies {
implementation 'no.fintlabs:fint-kafka:3.0.0-rc-1'
implementation 'no.fintlabs:fint-antlr:1.1.1'
implementation 'no.fintlabs:fint-resource-server-security:1.1.0'
implementation 'no.fintlabs:fint-kontroll-authorization:1.1.3'
implementation 'no.fintlabs:fint-kontroll-authorization:1.1.6'
implementation 'no.fint:fint-model-resource:0.4.1'
implementation 'io.projectreactor:reactor-core'
implementation 'org.apache.commons:commons-collections4:4.4'
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/no/fintlabs/user/User.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package no.fintlabs.user;

import lombok.*;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/no/fintlabs/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public List<SimpleUser> getSimpleUsers(

public List<String> getAllAutorizedOrgUnitIDs() {

List<Scope> scope = authorizationClient.getUserScopes();
List<Scope> scope = authorizationClient.getUserScopesList();
List<String> authorizedOrgIDs = scope.stream()
.filter(s -> s.getObjectType().equals("user"))
.map(Scope::getOrgUnits)
Expand Down
16 changes: 5 additions & 11 deletions src/test/java/no/fintlabs/user/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,40 @@ public void init(){
@Test
public void testGetAllAutorizedOrgUnitIDs() {
Scope scope1 = Scope.builder()
.id("1")
.objectType("user")
.orgUnits(List.of("198","2","3"))
.build();
Scope scope2 = Scope.builder()
.id("2")
.objectType("role")
.orgUnits(List.of("198","2","3"))
.build();
List<Scope> scopes= List.of(scope1,scope2);
List<String> authorizedOrgIDs = List.of("198","2","3");

when(authorizationClient.getUserScopes()).thenReturn(scopes);
when(authorizationClient.getUserScopesList()).thenReturn(scopes);

List<String> foundOrgIDs= userService.getAllAutorizedOrgUnitIDs();

assertEquals(authorizedOrgIDs,foundOrgIDs);

verify(authorizationClient, times(1)).getUserScopes();
verify(authorizationClient, times(1)).getUserScopesList();
}

@Test
void testCompareRequestedOrgUnitIDsWithOPA() {

Scope scope1 = Scope.builder()
.id("1")
.objectType("user")
.orgUnits(List.of("198","2","3"))
.build();
Scope scope2 = Scope.builder()
.id("2")
.objectType("role")
.orgUnits(List.of("198","2","3"))
.build();
List<Scope> scopes= List.of(scope1,scope2);
List<String> requestedOrgIDs = List.of("198","2","5");
List<String> authorizedOrgIDsForRequest = List.of("198","2");
when(authorizationClient.getUserScopes()).thenReturn(scopes);
when(authorizationClient.getUserScopesList()).thenReturn(scopes);

List<String> foundOrgIDs = userService.compareRequestedOrgUnitIDsWithOPA(requestedOrgIDs);

Expand All @@ -76,12 +72,10 @@ void testCompareRequestedOrgUnitIDsWithOPA() {
@Test
void testGetDetailedUserById_shouldPermitAccess(){
Scope scope1 = Scope.builder()
.id("1")
.objectType("user")
.orgUnits(List.of("198","2","3"))
.build();
Scope scope2 = Scope.builder()
.id("2")
.objectType("role")
.orgUnits(List.of("198","2","3"))
.build();
Expand All @@ -102,7 +96,7 @@ void testGetDetailedUserById_shouldPermitAccess(){

FintJwtEndUserPrincipal fintJwtEndUserPrincipal = new FintJwtEndUserPrincipal();
fintJwtEndUserPrincipal.setMail("titten@tei.no");
when(authorizationClient.getUserScopes()).thenReturn(scopes);
when(authorizationClient.getUserScopesList()).thenReturn(scopes);
when(userRepository.findById(1L)).thenReturn(Optional.of(requestedUser));
DetailedUser requestedDetailedUser = requestedUser.toDetailedUser();

Expand All @@ -113,4 +107,4 @@ void testGetDetailedUserById_shouldPermitAccess(){


}
}
}

0 comments on commit dd31788

Please sign in to comment.