Search in sources :

Example 6 with EJBAccessException

use of javax.ejb.EJBAccessException in project wildfly by wildfly.

the class AnnSBTest method testSingleMethodAnnotationsUser2Template.

/**
     * Test objective:
     * Check if default, @RolesAllowed, @PermitAll, @DenyAll and @RolesAllowed with multiple roles
     * works on method level with user1 logged in as described in EJB 3.1 spec.
     * user2 has "Users,Role2" roles.
     * The target session bean is given as parameter.
     * Expected results:
     * Test has to finish without any exception or error.
     * <p/>
     *
     * @throws Exception
     */
public void testSingleMethodAnnotationsUser2Template(final String MODULE, final Logger log, final Class SB_CLASS) throws Exception {
    final Context ctx = Util.createNamingContext();
    final AuthenticationContext authenticationContext = setupAuthenticationContext("user2", "password2");
    authenticationContext.runCallable(() -> {
        try {
            String echoValue = getBean(MODULE, log, SB_CLASS, ctx).defaultAccess("alohomora");
            Assert.assertEquals(echoValue, "alohomora");
        } catch (EJBAccessException e) {
            Assert.fail("EJBAccessException not expected");
        }
        try {
            String echoValue = getBean(MODULE, log, SB_CLASS, ctx).roleBasedAccessOne("alohomora");
            Assert.fail("Method cannot be successfully called with logged in user2");
        } catch (Exception e) {
            // expected
            Assert.assertTrue("Thrown exception must be EJBAccessException, but was different", e instanceof EJBAccessException);
        }
        try {
            String echoValue = getBean(MODULE, log, SB_CLASS, ctx).roleBasedAccessMore("alohomora");
            Assert.assertEquals(echoValue, "alohomora");
        } catch (EJBAccessException e) {
            Assert.fail("EJBAccessException not expected");
        }
        try {
            String echoValue = getBean(MODULE, log, SB_CLASS, ctx).permitAll("alohomora");
            Assert.assertEquals(echoValue, "alohomora");
        } catch (Exception e) {
            Assert.fail("@PermitAll annotation must allow all users and no users to call the method - principal.");
        }
        try {
            String echoValue = getBean(MODULE, log, SB_CLASS, ctx).denyAll("alohomora");
            Assert.fail("@DenyAll annotation must allow all users and no users to call the method");
        } catch (Exception e) {
            // expected
            Assert.assertTrue("Thrown exception must be EJBAccessException, but was different", e instanceof EJBAccessException);
        }
        return null;
    });
}
Also used : Context(javax.naming.Context) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) EJBAccessException(javax.ejb.EJBAccessException) NamingException(javax.naming.NamingException) EJBAccessException(javax.ejb.EJBAccessException)

Example 7 with EJBAccessException

use of javax.ejb.EJBAccessException in project wildfly by wildfly.

the class AnnSBTest method testSingleMethodAnnotationsNoUserTemplate.

/**
     * Test objective:
     * Check if default, @RolesAllowed, @PermitAll, @DenyAll and @RolesAllowed with multiple roles
     * works on method level without user logged in as described in EJB 3.1 spec.
     * The target session bean is given as parameter
     * Expected results:
     * Test has to finish without any exception or error.
     *
     * @throws Exception
     */
public void testSingleMethodAnnotationsNoUserTemplate(final String MODULE, final Logger log, final Class SB_CLASS) throws Exception {
    final Context ctx = Util.createNamingContext();
    final AuthenticationContext authenticationContext = setupAuthenticationContext("$local", null);
    authenticationContext.runCallable(() -> {
        String echoValue = getBean(MODULE, log, SB_CLASS, ctx).defaultAccess("alohomora");
        Assert.assertEquals(echoValue, "alohomora");
        try {
            echoValue = getBean(MODULE, log, SB_CLASS, ctx).roleBasedAccessOne("alohomora");
            Assert.fail("Method cannot be successfully called without logged in user");
        } catch (Exception e) {
            // expected
            Assert.assertTrue("Thrown exception must be EJBAccessException, but was " + e.getClass().getSimpleName(), e instanceof EJBAccessException);
        }
        try {
            echoValue = getBean(MODULE, log, SB_CLASS, ctx).roleBasedAccessMore("alohomora");
            Assert.fail("Method cannot be successfully called without logged in user");
        } catch (EJBAccessException e) {
        // expected
        }
        try {
            echoValue = getBean(MODULE, log, SB_CLASS, ctx).permitAll("alohomora");
            Assert.assertEquals(echoValue, "alohomora");
        } catch (Exception e) {
            Assert.fail("@PermitAll annotation must allow all users and no users to call the method");
        }
        try {
            echoValue = getBean(MODULE, log, SB_CLASS, ctx).denyAll("alohomora");
            Assert.fail("@DenyAll annotation must allow all users and no users to call the method");
        } catch (Exception e) {
            // expected
            Assert.assertTrue("Thrown exception must be EJBAccessException, but was " + e.getClass().getSimpleName(), e instanceof EJBAccessException);
        }
        return null;
    });
}
Also used : Context(javax.naming.Context) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) NamingException(javax.naming.NamingException) EJBAccessException(javax.ejb.EJBAccessException) EJBAccessException(javax.ejb.EJBAccessException)

Example 8 with EJBAccessException

use of javax.ejb.EJBAccessException in project wildfly by wildfly.

the class AnnSBTest method testSingleMethodAnnotationsUser1Template.

/**
     * Test objective:
     * Check if default, @RolesAllowed, @PermitAll, @DenyAll and @RolesAllowed with multiple roles
     * works on method level with user1 logged in as described in EJB 3.1 spec.
     * user1 has "Users,Role1" roles.
     * The target session bean is given as parameter.
     * Expected results:
     * Test has to finish without any exception or error.
     * <p/>
     *
     * @throws Exception
     */
public void testSingleMethodAnnotationsUser1Template(final String MODULE, final Logger log, final Class SB_CLASS) throws Exception {
    final Context ctx = Util.createNamingContext();
    final AuthenticationContext authenticationContext = setupAuthenticationContext("user1", "password1");
    authenticationContext.runCallable(() -> {
        try {
            String echoValue = getBean(MODULE, log, SB_CLASS, ctx).defaultAccess("alohomora");
            Assert.assertEquals(echoValue, "alohomora");
        } catch (EJBAccessException e) {
            Assert.fail("EJBAccessException not expected");
        }
        try {
            String echoValue = getBean(MODULE, log, SB_CLASS, ctx).roleBasedAccessOne("alohomora");
            Assert.assertEquals(echoValue, "alohomora");
        } catch (EJBAccessException e) {
            Assert.fail("EJBAccessException not expected");
        }
        try {
            String echoValue = getBean(MODULE, log, SB_CLASS, ctx).roleBasedAccessMore("alohomora");
            Assert.fail("Method cannot be successfully called with logged in principal.");
        } catch (Exception e) {
            // expected
            Assert.assertTrue("Thrown exception must be EJBAccessException, but was different", e instanceof EJBAccessException);
        }
        try {
            String echoValue = getBean(MODULE, log, SB_CLASS, ctx).permitAll("alohomora");
            Assert.assertEquals(echoValue, "alohomora");
        } catch (Exception e) {
            Assert.fail("@PermitAll annotation must allow all users and no users to call the method - principal.");
        }
        try {
            String echoValue = getBean(MODULE, log, SB_CLASS, ctx).denyAll("alohomora");
            Assert.fail("@DenyAll annotation must allow all users and no users to call the method");
        } catch (Exception e) {
            // expected
            Assert.assertTrue("Thrown exception must be EJBAccessException, but was different", e instanceof EJBAccessException);
        }
        try {
            String echoValue = getBean(MODULE, log, SB_CLASS, ctx).starRoleAllowed("alohomora");
            Assert.assertEquals(echoValue, "alohomora");
        } catch (Exception e) {
            Assert.fail("@RolesAllowed(\"**\") annotation must allow all authenticated users to the method.");
        }
        return null;
    });
}
Also used : Context(javax.naming.Context) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) AuthenticationContext(org.wildfly.security.auth.client.AuthenticationContext) EJBAccessException(javax.ejb.EJBAccessException) NamingException(javax.naming.NamingException) EJBAccessException(javax.ejb.EJBAccessException)

Example 9 with EJBAccessException

use of javax.ejb.EJBAccessException in project wildfly by wildfly.

the class AnnotationAuthorizationTestCase method testRolesAllowedOverriden_User2.

@Test
public void testRolesAllowedOverriden_User2() throws Exception {
    LoginContext lc = Util.getCLMLoginContext("user2", "password2");
    lc.login();
    try {
        try {
            rolesAllowedOverridenBean.defaultEcho("1");
            fail("Expected EJBAccessException not thrown");
        } catch (EJBAccessException ignored) {
        }
        try {
            rolesAllowedOverridenBean.denyAllEcho("2");
            fail("Expected EJBAccessException not thrown");
        } catch (EJBAccessException ignored) {
        }
        String response = rolesAllowedOverridenBean.permitAllEcho("3");
        assertEquals("3", response);
        response = rolesAllowedOverridenBean.role2Echo("4");
        assertEquals("4", response);
    } finally {
        lc.logout();
    }
}
Also used : LoginContext(javax.security.auth.login.LoginContext) EJBAccessException(javax.ejb.EJBAccessException) Test(org.junit.Test)

Example 10 with EJBAccessException

use of javax.ejb.EJBAccessException in project wildfly by wildfly.

the class AnnotationAuthorizationTestCase method testRolesAllowedOverriden_User1.

@Test
public void testRolesAllowedOverriden_User1() throws Exception {
    LoginContext lc = Util.getCLMLoginContext("user1", "password1");
    lc.login();
    try {
        String response = rolesAllowedOverridenBean.defaultEcho("1");
        assertEquals("1", response);
        try {
            rolesAllowedOverridenBean.denyAllEcho("2");
            fail("Expected EJBAccessException not thrown");
        } catch (EJBAccessException ignored) {
        }
        response = rolesAllowedOverridenBean.permitAllEcho("3");
        assertEquals("3", response);
        try {
            rolesAllowedOverridenBean.role2Echo("4");
            fail("Expected EJBAccessException not thrown");
        } catch (EJBAccessException ignored) {
        }
    } finally {
        lc.logout();
    }
}
Also used : LoginContext(javax.security.auth.login.LoginContext) EJBAccessException(javax.ejb.EJBAccessException) Test(org.junit.Test)

Aggregations

EJBAccessException (javax.ejb.EJBAccessException)42 Test (org.junit.Test)26 LoginContext (javax.security.auth.login.LoginContext)16 Context (javax.naming.Context)11 InitialContext (javax.naming.InitialContext)11 OpenEJBException (org.apache.openejb.OpenEJBException)5 Principal (java.security.Principal)4 NamingException (javax.naming.NamingException)4 ApplicationException (org.apache.openejb.ApplicationException)4 IOException (java.io.IOException)3 Method (java.lang.reflect.Method)3 Properties (java.util.Properties)3 EJBHome (javax.ejb.EJBHome)3 EJBLocalHome (javax.ejb.EJBLocalHome)3 EJBLocalObject (javax.ejb.EJBLocalObject)3 EJBObject (javax.ejb.EJBObject)3 LoginException (javax.security.auth.login.LoginException)3 ServletException (javax.servlet.ServletException)3 BeanContext (org.apache.openejb.BeanContext)3 ThreadContext (org.apache.openejb.core.ThreadContext)3