Search in sources :

Example 6 with ELProcessor

use of javax.el.ELProcessor in project tomee by apache.

the class TomEEELInvocationHandlerTest method canCreateInvocationHandler.

@Test
public void canCreateInvocationHandler() {
    final DatabaseIdentityStoreDefinition annotation = Color.class.getAnnotation(DatabaseIdentityStoreDefinition.class);
    final ELProcessor elProcessor = new ELProcessor();
    final ELResolver elResolver = bm().getELResolver();
    elProcessor.getELManager().addELResolver(elResolver);
    // small trick because of the @Vetoed bellow - OWB won't pick it up
    // so we will register one ourselves into the processor so it is resolved
    elProcessor.defineBean("color", new Color());
    final DatabaseIdentityStoreDefinition proxiedAnnotation = TomEEELInvocationHandler.of(DatabaseIdentityStoreDefinition.class, annotation, elProcessor);
    Assert.assertEquals("select password from caller where name = ?", proxiedAnnotation.callerQuery());
    Assert.assertEquals(90, proxiedAnnotation.priority());
    Assert.assertEquals("90", proxiedAnnotation.priorityExpression());
    Assert.assertArrayEquals(new IdentityStore.ValidationType[] { IdentityStore.ValidationType.VALIDATE }, proxiedAnnotation.useFor());
    Assert.assertEquals("select group_name from caller_groups where caller_name = ?", proxiedAnnotation.groupsQuery());
    final String[] hashAlgorithmParameters = proxiedAnnotation.hashAlgorithmParameters();
    Assert.assertArrayEquals(new String[] { "Pbkdf2PasswordHash.Iterations=3072", "${color.dyna}" }, hashAlgorithmParameters);
    final Set<String> evaluatedHashParameters = stream(hashAlgorithmParameters).flatMap(s -> toStream(eval(elProcessor, s, Object.class))).collect(toSet());
    System.out.println(evaluatedHashParameters);
    final Map<String, String> parametersMap = evaluatedHashParameters.stream().collect(toMap(s -> (String) s.substring(0, s.indexOf('=')), s -> (String) eval(elProcessor, s.substring(s.indexOf('=') + 1), String.class)));
    System.out.println(parametersMap);
}
Also used : TomEEDatabaseIdentityStore(org.apache.tomee.security.identitystore.TomEEDatabaseIdentityStore) Vetoed(javax.enterprise.inject.Vetoed) PasswordHash(javax.security.enterprise.identitystore.PasswordHash) CDI(javax.enterprise.inject.spi.CDI) IdentityStore(javax.security.enterprise.identitystore.IdentityStore) Set(java.util.Set) TomEEDatabaseIdentityStore.eval(org.apache.tomee.security.identitystore.TomEEDatabaseIdentityStore.eval) Test(org.junit.Test) ELProcessor(javax.el.ELProcessor) ELResolver(javax.el.ELResolver) Stream(java.util.stream.Stream) Collectors.toMap(java.util.stream.Collectors.toMap) DatabaseIdentityStoreDefinition(javax.security.enterprise.identitystore.DatabaseIdentityStoreDefinition) Map(java.util.Map) Named(javax.inject.Named) Assert(org.junit.Assert) Arrays.stream(java.util.Arrays.stream) BeanManager(javax.enterprise.inject.spi.BeanManager) Collectors.toSet(java.util.stream.Collectors.toSet) TomEEDatabaseIdentityStore.toStream(org.apache.tomee.security.identitystore.TomEEDatabaseIdentityStore.toStream) ELResolver(javax.el.ELResolver) ELProcessor(javax.el.ELProcessor) DatabaseIdentityStoreDefinition(javax.security.enterprise.identitystore.DatabaseIdentityStoreDefinition) TomEEDatabaseIdentityStore(org.apache.tomee.security.identitystore.TomEEDatabaseIdentityStore) IdentityStore(javax.security.enterprise.identitystore.IdentityStore) Test(org.junit.Test)

Example 7 with ELProcessor

use of javax.el.ELProcessor in project Payara by payara.

the class RolesPermittedInterceptor method checkAccessPermitted.

/**
 * Check that the roles allowed by the class or method match the roles
 * currently granted to the caller.
 *
 * @param roles The roles declared within the @Roles annotation.
 * @param invocationContext
 * @return True if access is allowed, false otherwise
 */
public boolean checkAccessPermitted(RolesPermitted roles, InvocationContext invocationContext) {
    authenticate(roles.value());
    ELProcessor eLProcessor = null;
    if (hasAnyELExpression(roles.value())) {
        eLProcessor = getElProcessor(invocationContext);
    }
    List<String> permittedRoles = asList(roles.value());
    final SecurityContext securityContext = lazyProperties.getSecurityContext();
    if (OR.equals(roles.semantics())) {
        for (String role : permittedRoles) {
            if (eLProcessor != null && hasAnyELExpression(role)) {
                role = evalELExpression(eLProcessor, role);
            }
            if (securityContext.isCallerInRole(role)) {
                return true;
            }
        }
    } else if (AND.equals(roles.semantics())) {
        for (String role : permittedRoles) {
            if (eLProcessor != null && hasAnyELExpression(role)) {
                role = evalELExpression(eLProcessor, role);
            }
            if (!securityContext.isCallerInRole(role)) {
                return false;
            }
        }
        return true;
    }
    return false;
}
Also used : ELProcessor(javax.el.ELProcessor) SecurityContext(javax.security.enterprise.SecurityContext)

Aggregations

ELProcessor (javax.el.ELProcessor)7 BeanManager (javax.enterprise.inject.spi.BeanManager)3 Named (javax.inject.Named)2 Parameter (java.lang.reflect.Parameter)1 Arrays.stream (java.util.Arrays.stream)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 Stream (java.util.stream.Stream)1 PostConstruct (javax.annotation.PostConstruct)1 ELResolver (javax.el.ELResolver)1 Vetoed (javax.enterprise.inject.Vetoed)1 CDI (javax.enterprise.inject.spi.CDI)1 SecurityContext (javax.security.enterprise.SecurityContext)1 DatabaseIdentityStoreDefinition (javax.security.enterprise.identitystore.DatabaseIdentityStoreDefinition)1 IdentityStore (javax.security.enterprise.identitystore.IdentityStore)1 PasswordHash (javax.security.enterprise.identitystore.PasswordHash)1 TomEEDatabaseIdentityStore (org.apache.tomee.security.identitystore.TomEEDatabaseIdentityStore)1 TomEEDatabaseIdentityStore.eval (org.apache.tomee.security.identitystore.TomEEDatabaseIdentityStore.eval)1