1 17 18 package org.apache.geronimo.jetty6; 19 20 import java.io.BufferedReader ; 21 import java.io.IOException ; 22 import java.io.InputStreamReader ; 23 import java.net.HttpURLConnection ; 24 import java.net.URL ; 25 import java.security.PermissionCollection ; 26 import java.security.Permissions ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Set ; 32 import javax.security.jacc.WebResourcePermission ; 33 import javax.security.jacc.WebUserDataPermission ; 34 35 import org.apache.geronimo.security.deploy.DefaultPrincipal; 36 import org.apache.geronimo.security.deploy.PrincipalInfo; 37 import org.apache.geronimo.security.deploy.Role; 38 import org.apache.geronimo.security.deploy.Security; 39 import org.apache.geronimo.security.deployment.GeronimoSecurityBuilderImpl; 40 import org.apache.geronimo.security.jacc.ComponentPermissions; 41 42 43 48 public class SecurityTest extends AbstractWebModuleTest { 49 54 public void testExplicitMapping() throws Exception { 55 Security securityConfig = new Security(); 56 securityConfig.setUseContextHandler(false); 57 58 DefaultPrincipal defaultPrincipal = new DefaultPrincipal(); 59 PrincipalInfo principalInfo = new PrincipalInfo("org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal", "izumi", false); 60 defaultPrincipal.setPrincipal(principalInfo); 61 62 securityConfig.setDefaultPrincipal(defaultPrincipal); 63 64 Role role = new Role(); 65 role.setRoleName("content-administrator"); 66 principalInfo = new PrincipalInfo("org.apache.geronimo.security.realm.providers.GeronimoGroupPrincipal", "it", false); 67 role.getPrincipals().add(principalInfo); 68 69 securityConfig.getRoleMappings().put(role.getRoleName(), role); 70 71 Map roleDesignates = new HashMap (); 72 Map principalRoleMap = new HashMap (); 73 buildPrincipalRoleMap(securityConfig, roleDesignates, principalRoleMap); 74 75 PermissionCollection uncheckedPermissions = new Permissions (); 76 77 PermissionCollection excludedPermissions = new Permissions (); 78 excludedPermissions.add(new WebResourcePermission ("/auth/login.html", "")); 79 excludedPermissions.add(new WebUserDataPermission ("/auth/login.html", "")); 80 81 Map rolePermissions = new HashMap (); 82 PermissionCollection permissions = new Permissions (); 83 permissions.add(new WebUserDataPermission ("/protected/*", "")); 84 permissions.add(new WebResourcePermission ("/protected/*", "")); 85 rolePermissions.put("content-administrator", permissions); 86 rolePermissions.put("auto-administrator", permissions); 87 88 Set securityRoles = new HashSet (); 89 securityRoles.add("content-administrator"); 90 securityRoles.add("auto-administrator"); 91 92 ComponentPermissions componentPermissions = new ComponentPermissions(excludedPermissions, uncheckedPermissions, rolePermissions); 93 94 startWebApp(roleDesignates, principalRoleMap, componentPermissions, defaultPrincipal, permissions, securityRoles); 95 96 HttpURLConnection connection = (HttpURLConnection ) new URL ("http://localhost:5678/test/protected/hello.txt").openConnection(); 97 connection.setInstanceFollowRedirects(false); 98 assertEquals(HttpURLConnection.HTTP_MOVED_TEMP, connection.getResponseCode()); 99 100 String cookie = connection.getHeaderField("Set-Cookie"); 101 cookie = cookie == null? "": cookie.substring(0, cookie.lastIndexOf(';')); 102 String location = connection.getHeaderField("Location"); 103 104 connection = (HttpURLConnection ) new URL (location).openConnection(); 105 connection.setInstanceFollowRedirects(false); 106 assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode()); 107 108 location = location.substring(0, location.lastIndexOf('/')) + "/j_security_check?j_username=alan&j_password=starcraft"; 109 110 connection = (HttpURLConnection ) new URL (location).openConnection(); 111 connection.setRequestMethod("POST"); 112 connection.setRequestProperty("Cookie", cookie); 113 connection.setInstanceFollowRedirects(false); 114 assertEquals(HttpURLConnection.HTTP_MOVED_TEMP, connection.getResponseCode()); 115 116 connection = (HttpURLConnection ) new URL ("http://localhost:5678/test/protected/hello.txt").openConnection(); 117 connection.setRequestProperty("Cookie", cookie); 118 connection.setInstanceFollowRedirects(false); 119 BufferedReader reader = new BufferedReader (new InputStreamReader (connection.getInputStream())); 120 121 assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode()); 122 assertEquals("Hello World", reader.readLine()); 123 connection.disconnect(); 124 125 connection = (HttpURLConnection ) new URL ("http://localhost:5678/test/protected/hello.txt").openConnection(); 126 connection.setInstanceFollowRedirects(false); 127 assertEquals(HttpURLConnection.HTTP_MOVED_TEMP, connection.getResponseCode()); 128 129 cookie = connection.getHeaderField("Set-Cookie"); 130 cookie = cookie.substring(0, cookie.lastIndexOf(';')); 131 location = connection.getHeaderField("Location"); 132 133 connection = (HttpURLConnection ) new URL (location).openConnection(); 134 connection.setInstanceFollowRedirects(false); 135 assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode()); 136 137 location = location.substring(0, location.lastIndexOf('/')) + "/j_security_check?j_username=izumi&j_password=violin"; 138 139 connection = (HttpURLConnection ) new URL (location).openConnection(); 140 connection.setRequestMethod("POST"); 141 connection.setRequestProperty("Cookie", cookie); 142 connection.setInstanceFollowRedirects(false); 143 assertEquals(HttpURLConnection.HTTP_MOVED_TEMP, connection.getResponseCode()); 144 145 try { 146 connection = (HttpURLConnection ) new URL ("http://localhost:5678/test/protected/hello.txt").openConnection(); 147 connection.setRequestProperty("Cookie", cookie); 148 connection.setInstanceFollowRedirects(false); 149 reader = new BufferedReader (new InputStreamReader (connection.getInputStream())); 150 151 fail("Should throw an IOException for HTTP 403 response"); 152 } catch (IOException e) { 153 } 154 155 assertEquals(HttpURLConnection.HTTP_FORBIDDEN, connection.getResponseCode()); 156 connection.disconnect(); 157 158 stopWebApp(); 159 } 160 161 protected void startWebApp(Map roleDesignates, Map principalRoleMap, ComponentPermissions componentPermissions, DefaultPrincipal defaultPrincipal, PermissionCollection checked, Set securityRoles) throws Exception { 162 JettyWebAppContext app = setUpSecureAppContext(roleDesignates, principalRoleMap, componentPermissions, defaultPrincipal, checked, securityRoles); 163 setUpStaticContentServlet(app); 164 } 166 167 protected void stopWebApp() throws Exception { 168 } 170 171 protected void setUp() throws Exception { 172 super.setUp(); 173 setUpSecurity(); 174 } 175 176 protected void tearDown() throws Exception { 177 tearDownSecurity(); 178 super.tearDown(); 179 } 180 181 public void buildPrincipalRoleMap(Security security, Map roleDesignates, Map principalRoleMap) { 183 Map roleToPrincipalMap = new HashMap (); 184 GeronimoSecurityBuilderImpl.buildRolePrincipalMap(security, roleDesignates, roleToPrincipalMap, getClass().getClassLoader()); 185 invertMap(roleToPrincipalMap, principalRoleMap); 186 } 187 188 private static Map invertMap(Map roleToPrincipalMap, Map principalRoleMapping) { 189 for (Iterator roles = roleToPrincipalMap.entrySet().iterator(); roles.hasNext();) { 190 Map.Entry entry = (Map.Entry ) roles.next(); 191 String role = (String ) entry.getKey(); 192 Set principals = (Set ) entry.getValue(); 193 for (Iterator iter = principals.iterator(); iter.hasNext();) { 194 java.security.Principal principal = (java.security.Principal ) iter.next(); 195 196 HashSet roleSet = (HashSet ) principalRoleMapping.get(principal); 197 if (roleSet == null) { 198 roleSet = new HashSet (); 199 principalRoleMapping.put(principal, roleSet); 200 } 201 roleSet.add(role); 202 } 203 } 204 return principalRoleMapping; 205 } 206 } 207 | Popular Tags |