use of io.helidon.security.EndpointConfig in project helidon by oracle.
the class RoleValidatorTest method testDenyAll.
@Test
void testDenyAll() {
RoleValidator validator = RoleValidator.create();
DenyAll annot = mock(DenyAll.class);
SecurityLevel appSecurityLevel = mock(SecurityLevel.class);
SecurityLevel classSecurityLevel = mock(SecurityLevel.class);
List<SecurityLevel> securityLevels = new ArrayList<>();
securityLevels.add(appSecurityLevel);
securityLevels.add(classSecurityLevel);
EndpointConfig ep = mock(EndpointConfig.class);
when(ep.securityLevels()).thenReturn(securityLevels);
when(classSecurityLevel.filterAnnotations(DenyAll.class, EndpointConfig.AnnotationScope.METHOD)).thenReturn(List.of(annot));
RoleValidator.RoleConfig rConfig = validator.fromAnnotations(ep);
Errors.Collector collector = Errors.collector();
ProviderRequest request = mock(ProviderRequest.class);
when(request.subject()).thenReturn(Optional.of(Subject.builder().principal(Principal.create("myAdmin")).addGrant(Role.create("user")).build()));
when(request.service()).thenReturn(Optional.empty());
validator.validate(rConfig, collector, request);
if (collector.collect().isValid()) {
fail("DenyAll is set on this method, this should have failed");
}
}
use of io.helidon.security.EndpointConfig in project helidon by oracle.
the class RoleValidatorTest method testServiceRolesDeny.
@Test
void testServiceRolesDeny() {
RoleValidator validator = RoleValidator.create();
RoleValidator.Roles annot = mock(RoleValidator.Roles.class);
String[] roleArray = new String[] { "admin" };
when(annot.value()).thenReturn(roleArray);
when(annot.subjectType()).thenReturn(SubjectType.SERVICE);
SecurityLevel appSecurityLevel = mock(SecurityLevel.class);
SecurityLevel classSecurityLevel = mock(SecurityLevel.class);
List<SecurityLevel> securityLevels = new ArrayList<>();
securityLevels.add(appSecurityLevel);
securityLevels.add(classSecurityLevel);
EndpointConfig ep = mock(EndpointConfig.class);
when(ep.securityLevels()).thenReturn(securityLevels);
when(classSecurityLevel.filterAnnotations(RoleValidator.Roles.class, EndpointConfig.AnnotationScope.METHOD)).thenReturn(List.of(annot));
RoleValidator.RoleConfig rConfig = validator.fromAnnotations(ep);
Errors.Collector collector = Errors.collector();
ProviderRequest request = mock(ProviderRequest.class);
when(request.subject()).thenReturn(Optional.of(Subject.builder().principal(Principal.create("myAdmin")).addGrant(Role.create("admin")).build()));
when(request.service()).thenReturn(Optional.of(Subject.builder().principal(Principal.create("service")).addGrant(Role.create("user")).build()));
validator.validate(rConfig, collector, request);
if (collector.collect().isValid()) {
fail("Service is not in admin role, should have failed");
}
}
use of io.helidon.security.EndpointConfig in project helidon by oracle.
the class ScopeValidatorTest method testScopesAndDeny.
@Test
public void testScopesAndDeny() {
ScopeValidator validator = ScopeValidator.create();
ScopeValidator.Scope annot = mock(ScopeValidator.Scope.class);
when(annot.value()).thenReturn("calendar_get");
ScopeValidator.Scope annotTwo = mock(ScopeValidator.Scope.class);
when(annotTwo.value()).thenReturn("calendar_update");
ScopeValidator.Scopes scopes = mock(ScopeValidator.Scopes.class);
when(scopes.value()).thenReturn(new ScopeValidator.Scope[] { annot, annotTwo });
SecurityLevel appSecurityLevel = mock(SecurityLevel.class);
SecurityLevel classSecurityLevel = mock(SecurityLevel.class);
List<SecurityLevel> securityLevels = new ArrayList<>();
securityLevels.add(appSecurityLevel);
securityLevels.add(classSecurityLevel);
EndpointConfig ep = mock(EndpointConfig.class);
when(ep.securityLevels()).thenReturn(securityLevels);
when(classSecurityLevel.filterAnnotations(ScopeValidator.Scopes.class, EndpointConfig.AnnotationScope.METHOD)).thenReturn(List.of(scopes));
ScopeValidator.ScopesConfig sConfig = validator.fromAnnotations(ep);
Errors.Collector collector = Errors.collector();
ProviderRequest request = mock(ProviderRequest.class);
when(request.subject()).thenReturn(Optional.of(Subject.builder().principal(Principal.create("myAdmin")).addGrant(Grant.builder().type("scope").name("calendar_get").build()).build()));
when(request.service()).thenReturn(Optional.empty());
validator.validate(sConfig, collector, request);
if (collector.collect().isValid()) {
fail("User does not have calendar_update scope, so this should have failed");
}
}
use of io.helidon.security.EndpointConfig in project helidon by oracle.
the class TimeValidatorTest method initClass.
@BeforeAll
public static void initClass() {
validator = TimeValidator.create();
EndpointConfig ep = mock(EndpointConfig.class);
TimeValidator.TimeOfDay tod = mock(TimeValidator.TimeOfDay.class);
when(tod.from()).thenReturn("08:15:00");
when(tod.to()).thenReturn("12:00");
annotations.add(tod);
TimeValidator.TimeOfDay tod2 = mock(TimeValidator.TimeOfDay.class);
when(tod2.from()).thenReturn("12:30:00");
when(tod2.to()).thenReturn("17:30");
annotations.add(tod2);
SecurityLevel appSecurityLevel = mock(SecurityLevel.class);
SecurityLevel classSecurityLevel = mock(SecurityLevel.class);
List<SecurityLevel> securityLevels = new ArrayList<>();
securityLevels.add(appSecurityLevel);
securityLevels.add(classSecurityLevel);
when(ep.securityLevels()).thenReturn(securityLevels);
when(classSecurityLevel.filterAnnotations(TimeValidator.TimeOfDay.class, EndpointConfig.AnnotationScope.CLASS)).thenReturn(List.of(tod, tod2));
TimeValidator.DaysOfWeek dow = mock(TimeValidator.DaysOfWeek.class);
when(dow.value()).thenReturn(new DayOfWeek[] { DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY, DayOfWeek.THURSDAY, DayOfWeek.FRIDAY });
annotations.add(dow);
when(classSecurityLevel.filterAnnotations(TimeValidator.DaysOfWeek.class, EndpointConfig.AnnotationScope.CLASS)).thenReturn(List.of(dow));
timeConfig = validator.fromAnnotations(ep);
}
Aggregations