Search in sources :

Example 1 with TesterMapRealm

use of org.apache.catalina.startup.TesterMapRealm in project tomcat70 by apache.

the class TestRealmBase method doRoleTest.

private void doRoleTest(List<String> userRoles, List<String> constraintOneRoles, List<String> constraintTwoRoles, List<String> applicationRoles, boolean expected) throws IOException {
    TesterMapRealm mapRealm = new TesterMapRealm();
    // Configure the security constraints for the resource
    SecurityConstraint constraintOne = new SecurityConstraint();
    if (constraintOneRoles != null) {
        constraintOne.setAuthConstraint(true);
        for (String constraintRole : constraintOneRoles) {
            constraintOne.addAuthRole(constraintRole);
        }
    }
    SecurityConstraint constraintTwo = new SecurityConstraint();
    if (constraintTwoRoles != null) {
        constraintTwo.setAuthConstraint(true);
        for (String constraintRole : constraintTwoRoles) {
            constraintTwo.addAuthRole(constraintRole);
        }
    }
    SecurityConstraint[] constraints = new SecurityConstraint[] { constraintOne, constraintTwo };
    // Set up the mock request and response
    Request request = new Request();
    Response response = new TesterResponse();
    Context context = new TesterContext();
    for (String applicationRole : applicationRoles) {
        context.addSecurityRole(applicationRole);
    }
    request.setContext(context);
    // Configure the users in the Realm
    if (userRoles != null) {
        GenericPrincipal gp = new GenericPrincipal(USER1, PWD, userRoles);
        request.setUserPrincipal(gp);
    }
    // Check if user meets constraints
    boolean result = mapRealm.hasResourcePermission(request, response, constraints, null);
    Assert.assertEquals(Boolean.valueOf(expected), Boolean.valueOf(result));
}
Also used : TesterResponse(org.apache.tomcat.unittest.TesterResponse) Response(org.apache.catalina.connector.Response) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) TesterMapRealm(org.apache.catalina.startup.TesterMapRealm) Request(org.apache.catalina.connector.Request) TesterRequest(org.apache.tomcat.unittest.TesterRequest) TesterResponse(org.apache.tomcat.unittest.TesterResponse) TesterContext(org.apache.tomcat.unittest.TesterContext) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint)

Example 2 with TesterMapRealm

use of org.apache.catalina.startup.TesterMapRealm in project tomcat70 by apache.

the class TestRealmBase method doTestDigestDigestPasswords.

private void doTestDigestDigestPasswords(String password, String digest, String digestedPassword) throws Exception {
    Context context = new TesterContext();
    TesterMapRealm realm = new TesterMapRealm();
    realm.setContainer(context);
    realm.setDigest(digest);
    realm.start();
    realm.addUser(USER1, digestedPassword);
    Principal p = realm.authenticate(USER1, password);
    Assert.assertNotNull(p);
    Assert.assertEquals(USER1, p.getName());
}
Also used : Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) TesterMapRealm(org.apache.catalina.startup.TesterMapRealm) TesterContext(org.apache.tomcat.unittest.TesterContext) Principal(java.security.Principal)

Example 3 with TesterMapRealm

use of org.apache.catalina.startup.TesterMapRealm in project tomcat by apache.

the class TestAuthInfoResponseHeaders method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // Configure a context with digest auth and a single protected resource
    Tomcat tomcat = getTomcatInstance();
    tomcat.getHost().getPipeline().addValve(new RemoteIpValve());
    // No file system docBase required
    Context ctxt = tomcat.addContext(CONTEXT_PATH, null);
    // Add protected servlet
    Tomcat.addServlet(ctxt, "TesterServlet", new TesterServlet());
    ctxt.addServletMappingDecoded(URI, "TesterServlet");
    SecurityCollection collection = new SecurityCollection();
    collection.addPatternDecoded(URI);
    SecurityConstraint sc = new SecurityConstraint();
    sc.addAuthRole(ROLE);
    sc.addCollection(collection);
    ctxt.addConstraint(sc);
    // Configure the Realm
    TesterMapRealm realm = new TesterMapRealm();
    realm.addUser(USER, PWD);
    realm.addUserRole(USER, ROLE);
    ctxt.setRealm(realm);
    // Configure the authenticator
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod(HttpServletRequest.BASIC_AUTH);
    ctxt.setLoginConfig(lc);
    ctxt.getPipeline().addValve(new BasicAuthenticator());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) TesterMapRealm(org.apache.catalina.startup.TesterMapRealm) LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) TesterServlet(org.apache.catalina.startup.TesterServlet) RemoteIpValve(org.apache.catalina.valves.RemoteIpValve) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) SecurityCollection(org.apache.tomcat.util.descriptor.web.SecurityCollection)

Example 4 with TesterMapRealm

use of org.apache.catalina.startup.TesterMapRealm in project tomcat by apache.

the class TestDigestAuthenticator method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // Configure a context with digest auth and a single protected resource
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctxt = tomcat.addContext(CONTEXT_PATH, null);
    // Add protected servlet
    Tomcat.addServlet(ctxt, "TesterServlet", new TesterServlet());
    ctxt.addServletMappingDecoded(URI, "TesterServlet");
    SecurityCollection collection = new SecurityCollection();
    collection.addPatternDecoded(URI);
    SecurityConstraint sc = new SecurityConstraint();
    sc.addAuthRole(ROLE);
    sc.addCollection(collection);
    ctxt.addConstraint(sc);
    // Configure the Realm
    TesterMapRealm realm = new TesterMapRealm();
    realm.addUser(USER, PWD);
    realm.addUserRole(USER, ROLE);
    ctxt.setRealm(realm);
    // Configure the authenticator
    LoginConfig lc = new LoginConfig();
    lc.setAuthMethod("DIGEST");
    lc.setRealmName(REALM);
    ctxt.setLoginConfig(lc);
    ctxt.getPipeline().addValve(new DigestAuthenticator());
}
Also used : TesterServletContext(org.apache.tomcat.unittest.TesterServletContext) Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) Tomcat(org.apache.catalina.startup.Tomcat) TesterMapRealm(org.apache.catalina.startup.TesterMapRealm) LoginConfig(org.apache.tomcat.util.descriptor.web.LoginConfig) TesterServlet(org.apache.catalina.startup.TesterServlet) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) SecurityCollection(org.apache.tomcat.util.descriptor.web.SecurityCollection)

Example 5 with TesterMapRealm

use of org.apache.catalina.startup.TesterMapRealm in project tomcat by apache.

the class TestRealmBase method doTestDigestDigestPasswords.

private void doTestDigestDigestPasswords(String password, String digest, String digestedPassword) throws Exception {
    Context context = new TesterContext();
    TesterMapRealm realm = new TesterMapRealm();
    realm.setContainer(context);
    MessageDigestCredentialHandler ch = new MessageDigestCredentialHandler();
    ch.setAlgorithm(digest);
    realm.setCredentialHandler(ch);
    realm.start();
    realm.addUser(USER1, digestedPassword);
    Principal p = realm.authenticate(USER1, password);
    Assert.assertNotNull(p);
    Assert.assertEquals(USER1, p.getName());
}
Also used : Context(org.apache.catalina.Context) TesterContext(org.apache.tomcat.unittest.TesterContext) TesterMapRealm(org.apache.catalina.startup.TesterMapRealm) TesterContext(org.apache.tomcat.unittest.TesterContext) Principal(java.security.Principal)

Aggregations

Context (org.apache.catalina.Context)15 TesterMapRealm (org.apache.catalina.startup.TesterMapRealm)15 LoginConfig (org.apache.tomcat.util.descriptor.web.LoginConfig)9 Tomcat (org.apache.catalina.startup.Tomcat)7 TesterContext (org.apache.tomcat.unittest.TesterContext)7 BasicAuthenticator (org.apache.catalina.authenticator.BasicAuthenticator)5 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)5 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)5 ServletContext (jakarta.servlet.ServletContext)4 HttpConstraint (jakarta.servlet.annotation.HttpConstraint)4 HttpMethodConstraint (jakarta.servlet.annotation.HttpMethodConstraint)4 ArrayList (java.util.ArrayList)4 Response (org.apache.catalina.connector.Response)4 TesterRequest (org.apache.tomcat.unittest.TesterRequest)4 TesterResponse (org.apache.tomcat.unittest.TesterResponse)4 SecurityCollection (org.apache.tomcat.util.descriptor.web.SecurityCollection)4 Test (org.junit.Test)4 Principal (java.security.Principal)3 ServletContainerInitializer (jakarta.servlet.ServletContainerInitializer)2 List (java.util.List)2