|
| 1 | +/* |
| 2 | + * JBoss, Home of Professional Open Source. |
| 3 | + * Copyright 2021 Red Hat, Inc., and individual contributors |
| 4 | + * as indicated by the @author tags. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package io.undertow.servlet.test.listener.servletcontext; |
| 20 | + |
| 21 | +import static io.undertow.util.Headers.AUTHORIZATION; |
| 22 | +import static io.undertow.util.Headers.BASIC; |
| 23 | +import static org.junit.Assert.assertEquals; |
| 24 | + |
| 25 | +import java.nio.charset.Charset; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +import javax.servlet.ServletException; |
| 31 | + |
| 32 | +import org.apache.http.HttpResponse; |
| 33 | +import org.apache.http.client.methods.HttpGet; |
| 34 | +import org.junit.BeforeClass; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | + |
| 38 | +import io.undertow.server.handlers.PathHandler; |
| 39 | +import io.undertow.servlet.api.AuthMethodConfig; |
| 40 | +import io.undertow.servlet.api.DeploymentInfo; |
| 41 | +import io.undertow.servlet.api.DeploymentManager; |
| 42 | +import io.undertow.servlet.api.ListenerInfo; |
| 43 | +import io.undertow.servlet.api.LoginConfig; |
| 44 | +import io.undertow.servlet.api.SecurityConstraint; |
| 45 | +import io.undertow.servlet.api.SecurityInfo; |
| 46 | +import io.undertow.servlet.api.ServletContainer; |
| 47 | +import io.undertow.servlet.api.ServletInfo; |
| 48 | +import io.undertow.servlet.api.WebResourceCollection; |
| 49 | +import io.undertow.servlet.test.security.constraint.ServletIdentityManager; |
| 50 | +import io.undertow.servlet.test.util.TestClassIntrospector; |
| 51 | +import io.undertow.testutils.DefaultServer; |
| 52 | +import io.undertow.testutils.HttpClientUtils; |
| 53 | +import io.undertow.testutils.TestHttpClient; |
| 54 | +import io.undertow.util.FlexBase64; |
| 55 | +import io.undertow.util.Headers; |
| 56 | + |
| 57 | +/** |
| 58 | + * @author baranowb |
| 59 | + */ |
| 60 | +@RunWith(DefaultServer.class) |
| 61 | +public class ServletContextRolesTestCase { |
| 62 | + private static final String REALM_NAME = "Servlet_Realm-1"; |
| 63 | + static DeploymentManager manager; |
| 64 | + |
| 65 | + @BeforeClass |
| 66 | + public static void setup() throws ServletException { |
| 67 | + |
| 68 | + final PathHandler root = new PathHandler(); |
| 69 | + final ServletContainer container = ServletContainer.Factory.newInstance(); |
| 70 | + final ServletIdentityManager identityManager = new ServletIdentityManager(); |
| 71 | + identityManager.addUser("user1", "password1", "unspecified-role"); |
| 72 | + |
| 73 | + LoginConfig loginConfig = new LoginConfig(REALM_NAME); |
| 74 | + Map<String, String> props = new HashMap<>(); |
| 75 | + props.put("charset", "ISO_8859_1"); |
| 76 | + props.put("user-agent-charsets", "Chrome,UTF-8,OPR,UTF-8"); |
| 77 | + loginConfig.addFirstAuthMethod(new AuthMethodConfig("BASIC", props)); |
| 78 | + |
| 79 | + DeploymentInfo builder = new DeploymentInfo() |
| 80 | + .setClassLoader(ServletContextRolesTestCase.class.getClassLoader()) |
| 81 | + .setContextPath("/servletContext") |
| 82 | + .setClassIntrospecter(TestClassIntrospector.INSTANCE) |
| 83 | + .setDeploymentName("servletContext.war") |
| 84 | + .addServlet( |
| 85 | + new ServletInfo("servlet", CheckRolesServlet.class) |
| 86 | + .addMapping("/aa") |
| 87 | + ) |
| 88 | + .addListener(new ListenerInfo(DeclareRolesServletContextListener.class)) |
| 89 | + .setIdentityManager(identityManager) |
| 90 | + .setLoginConfig(loginConfig); |
| 91 | + |
| 92 | + builder.addPrincipalVsRoleMappings("user1", DeclareRolesServletContextListener.ROLES); |
| 93 | + builder.addSecurityConstraint(new SecurityConstraint() |
| 94 | + .addWebResourceCollection(new WebResourceCollection() |
| 95 | + .addUrlPattern("/*")) |
| 96 | + .addRolesAllowed(DeclareRolesServletContextListener.ROLES) |
| 97 | + .setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.DENY)); |
| 98 | + |
| 99 | + manager = container.addDeployment(builder); |
| 100 | + manager.deploy(); |
| 101 | + root.addPrefixPath(builder.getContextPath(), manager.start()); |
| 102 | + |
| 103 | + DefaultServer.setRootHandler(root); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testRoles() throws Exception { |
| 108 | + final StringBuilder sb = new StringBuilder(40); |
| 109 | + sb.append("dobby:true\n") |
| 110 | + .append("was:true\n") |
| 111 | + .append("here:true\n"); |
| 112 | + testCall(sb.toString(), StandardCharsets.UTF_8, "Chrome", "user1", "password1", 200); |
| 113 | + } |
| 114 | + |
| 115 | + public void testCall( final String expectedResponse, Charset charset, String userAgent, String user, String password, int expect) throws Exception { |
| 116 | + TestHttpClient client = new TestHttpClient(); |
| 117 | + try { |
| 118 | + String url = DefaultServer.getDefaultServerURL() + "/servletContext/aa"; |
| 119 | + HttpGet get = new HttpGet(url); |
| 120 | + get = new HttpGet(url); |
| 121 | + get.addHeader(Headers.USER_AGENT_STRING, userAgent); |
| 122 | + get.addHeader(AUTHORIZATION.toString(), BASIC + " " + FlexBase64.encodeString((user + ":" + password).getBytes(charset), false)); |
| 123 | + HttpResponse result = client.execute(get); |
| 124 | + assertEquals(expect, result.getStatusLine().getStatusCode()); |
| 125 | + |
| 126 | + final String response = HttpClientUtils.readResponse(result); |
| 127 | + if(expect == 200) { |
| 128 | + assertEquals(expectedResponse, response); |
| 129 | + } |
| 130 | + } finally { |
| 131 | + client.getConnectionManager().shutdown(); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments