Search in sources :

Example 76 with ProviderRequest

use of io.helidon.security.ProviderRequest in project helidon by oracle.

the class RoleValidatorTest method testPermitAll.

@Test
void testPermitAll() {
    RoleValidator validator = RoleValidator.create();
    PermitAll annot = mock(PermitAll.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(PermitAll.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);
    collector.collect().checkValid();
}
Also used : Errors(io.helidon.common.Errors) SecurityLevel(io.helidon.security.SecurityLevel) ArrayList(java.util.ArrayList) PermitAll(jakarta.annotation.security.PermitAll) EndpointConfig(io.helidon.security.EndpointConfig) ProviderRequest(io.helidon.security.ProviderRequest) Test(org.junit.jupiter.api.Test)

Example 77 with ProviderRequest

use of io.helidon.security.ProviderRequest in project helidon by oracle.

the class RoleValidatorTest method testPermitAllAndRolesAndDenyAll.

@Test
void testPermitAllAndRolesAndDenyAll() {
    RoleValidator validator = RoleValidator.create();
    PermitAll permitAll = mock(PermitAll.class);
    DenyAll denyAll = mock(DenyAll.class);
    RolesAllowed rolesAllowed = mock(RolesAllowed.class);
    String[] roleArray = new String[] { "admin" };
    when(rolesAllowed.value()).thenReturn(roleArray);
    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(PermitAll.class, EndpointConfig.AnnotationScope.CLASS)).thenReturn(List.of(permitAll));
    when(classSecurityLevel.filterAnnotations(DenyAll.class, EndpointConfig.AnnotationScope.METHOD)).thenReturn(List.of(denyAll));
    when(classSecurityLevel.filterAnnotations(RolesAllowed.class, EndpointConfig.AnnotationScope.METHOD)).thenReturn(List.of(rolesAllowed));
    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.empty());
    validator.validate(rConfig, collector, request);
    if (collector.collect().isValid()) {
        fail("DenyAll is set on this method, this should have failed");
    }
}
Also used : ArrayList(java.util.ArrayList) ProviderRequest(io.helidon.security.ProviderRequest) Errors(io.helidon.common.Errors) DenyAll(jakarta.annotation.security.DenyAll) RolesAllowed(jakarta.annotation.security.RolesAllowed) SecurityLevel(io.helidon.security.SecurityLevel) PermitAll(jakarta.annotation.security.PermitAll) EndpointConfig(io.helidon.security.EndpointConfig) Test(org.junit.jupiter.api.Test)

Example 78 with ProviderRequest

use of io.helidon.security.ProviderRequest 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");
    }
}
Also used : Errors(io.helidon.common.Errors) DenyAll(jakarta.annotation.security.DenyAll) SecurityLevel(io.helidon.security.SecurityLevel) ArrayList(java.util.ArrayList) EndpointConfig(io.helidon.security.EndpointConfig) ProviderRequest(io.helidon.security.ProviderRequest) Test(org.junit.jupiter.api.Test)

Example 79 with ProviderRequest

use of io.helidon.security.ProviderRequest 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");
    }
}
Also used : Errors(io.helidon.common.Errors) SecurityLevel(io.helidon.security.SecurityLevel) ArrayList(java.util.ArrayList) EndpointConfig(io.helidon.security.EndpointConfig) ProviderRequest(io.helidon.security.ProviderRequest) Test(org.junit.jupiter.api.Test)

Example 80 with ProviderRequest

use of io.helidon.security.ProviderRequest 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");
    }
}
Also used : Errors(io.helidon.common.Errors) SecurityLevel(io.helidon.security.SecurityLevel) ArrayList(java.util.ArrayList) EndpointConfig(io.helidon.security.EndpointConfig) ProviderRequest(io.helidon.security.ProviderRequest) Test(org.junit.jupiter.api.Test)

Aggregations

ProviderRequest (io.helidon.security.ProviderRequest)80 Test (org.junit.jupiter.api.Test)73 EndpointConfig (io.helidon.security.EndpointConfig)54 SecurityEnvironment (io.helidon.security.SecurityEnvironment)46 SecurityContext (io.helidon.security.SecurityContext)32 AuthenticationResponse (io.helidon.security.AuthenticationResponse)28 Errors (io.helidon.common.Errors)27 SecurityLevel (io.helidon.security.SecurityLevel)24 OutboundSecurityResponse (io.helidon.security.OutboundSecurityResponse)21 Subject (io.helidon.security.Subject)20 ArrayList (java.util.ArrayList)19 Principal (io.helidon.security.Principal)13 AuthorizationResponse (io.helidon.security.AuthorizationResponse)9 SignedJwt (io.helidon.security.jwt.SignedJwt)8 List (java.util.List)8 RolesAllowed (jakarta.annotation.security.RolesAllowed)7 Instant (java.time.Instant)7 Locale (java.util.Locale)7 Config (io.helidon.config.Config)6 Jwt (io.helidon.security.jwt.Jwt)6