Search in sources :

Example 16 with ProviderRequest

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

the class RoleValidatorTest method testUserRoles.

@Test
void testUserRoles() {
    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.USER);
    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.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) EndpointConfig(io.helidon.security.EndpointConfig) ProviderRequest(io.helidon.security.ProviderRequest) Test(org.junit.jupiter.api.Test)

Example 17 with ProviderRequest

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

the class RoleValidatorTest method testRolesAllowedPermit.

@Test
void testRolesAllowedPermit() {
    RoleValidator validator = RoleValidator.create();
    RolesAllowed annot = mock(RolesAllowed.class);
    String[] roleArray = new String[] { "admin" };
    when(annot.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(RolesAllowed.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.empty());
    validator.validate(rConfig, collector, request);
    collector.collect().checkValid();
}
Also used : Errors(io.helidon.common.Errors) RolesAllowed(jakarta.annotation.security.RolesAllowed) 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 18 with ProviderRequest

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

the class RoleValidatorTest method testAllAccessModificationsOnTheSameLevelFromConfig.

@Test
void testAllAccessModificationsOnTheSameLevelFromConfig() {
    RoleValidator roleValidator = RoleValidator.create();
    RoleValidator.RoleConfig roleConfig = roleValidator.fromConfig(config.get("test4"));
    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("test")).build()));
    roleValidator.validate(roleConfig, collector, request);
    if (collector.collect().isValid()) {
        fail("DenyAll is set on this method, this should have failed");
    }
}
Also used : Errors(io.helidon.common.Errors) ProviderRequest(io.helidon.security.ProviderRequest) Test(org.junit.jupiter.api.Test)

Example 19 with ProviderRequest

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

the class RoleValidatorTest method testRolesFromConfig.

@Test
void testRolesFromConfig() {
    RoleValidator roleValidator = RoleValidator.create();
    RoleValidator.RoleConfig roleConfig = roleValidator.fromConfig(config.get("test1"));
    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()));
    roleValidator.validate(roleConfig, collector, request);
    collector.collect().checkValid();
}
Also used : Errors(io.helidon.common.Errors) ProviderRequest(io.helidon.security.ProviderRequest) Test(org.junit.jupiter.api.Test)

Example 20 with ProviderRequest

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

the class JakartaElPolicyExecutorTest method testSimpleExpression.

@Test
public void testSimpleExpression() {
    JavaxElPolicyExecutor ex = JavaxElPolicyExecutor.create();
    SecurityEnvironment env = SecurityEnvironment.create();
    Subject user = Subject.builder().principal(Principal.create("unit-test-user")).addGrant(Role.create("unit-test-user-role")).build();
    Subject service = Subject.builder().principal(Principal.create("unit-test-service")).addGrant(Role.create("unit-test-service-role")).build();
    MyResource object = new MyResource("unit-test-user");
    ProviderRequest request = mock(ProviderRequest.class);
    when(request.service()).thenReturn(Optional.of(service));
    when(request.subject()).thenReturn(Optional.of(user));
    when(request.env()).thenReturn(env);
    when(request.getObject()).thenReturn(Optional.of(object));
    Errors.Collector collector = Errors.collector();
    ex.executePolicy("${user.principal.id == object.owner}", collector, request);
    collector.collect().checkValid();
    collector = Errors.collector();
    ex.executePolicy("${user.principal.id == object.owner}", collector, request);
    collector.collect().checkValid();
    collector = Errors.collector();
    ex.executePolicy("${inRole(user, 'unit-test-user-role') && inRole(service, 'unit-test-service-role')}", collector, request);
    collector.collect().checkValid();
    collector = Errors.collector();
    ex.executePolicy("${service.principal.id == object.owner}", collector, request);
    if (collector.collect().isValid()) {
        fail("Should have failed, as service is not the owner of the object");
    }
    collector = Errors.collector();
    ex.executePolicy("${env.time.year >= 2017}", collector, request);
    collector.collect().checkValid();
}
Also used : Errors(io.helidon.common.Errors) SecurityEnvironment(io.helidon.security.SecurityEnvironment) Subject(io.helidon.security.Subject) 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