001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/portal/standard/security/control/ClientHelper.java $ 002 /*---------------- FILE HEADER ------------------------------------------ 003 004 This file is part of deegree. 005 Copyright (C) 2001-2008 by: 006 EXSE, Department of Geography, University of Bonn 007 http://www.giub.uni-bonn.de/deegree/ 008 lat/lon GmbH 009 http://www.lat-lon.de 010 011 This library is free software; you can redistribute it and/or 012 modify it under the terms of the GNU Lesser General Public 013 License as published by the Free Software Foundation; either 014 version 2.1 of the License, or (at your option) any later version. 015 016 This library is distributed in the hope that it will be useful, 017 but WITHOUT ANY WARRANTY; without even the implied warranty of 018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 019 Lesser General Public License for more details. 020 021 You should have received a copy of the GNU Lesser General Public 022 License along with this library; if not, write to the Free Software 023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 024 025 Contact: 026 027 Andreas Poth 028 lat/lon GmbH 029 Aennchenstr. 19 030 53177 Bonn 031 Germany 032 E-Mail: poth@lat-lon.de 033 034 Prof. Dr. Klaus Greve 035 Department of Geography 036 University of Bonn 037 Meckenheimer Allee 166 038 53115 Bonn 039 Germany 040 E-Mail: greve@giub.uni-bonn.de 041 042 ---------------------------------------------------------------------------*/ 043 package org.deegree.portal.standard.security.control; 044 045 import java.util.HashSet; 046 import java.util.Iterator; 047 import java.util.Set; 048 049 import javax.servlet.http.HttpServletRequest; 050 import javax.servlet.http.HttpSession; 051 052 import org.deegree.enterprise.control.AbstractListener; 053 import org.deegree.i18n.Messages; 054 import org.deegree.security.GeneralSecurityException; 055 import org.deegree.security.UnauthorizedException; 056 import org.deegree.security.drm.SecurityAccess; 057 import org.deegree.security.drm.SecurityAccessManager; 058 import org.deegree.security.drm.SecurityTransaction; 059 import org.deegree.security.drm.model.RightType; 060 import org.deegree.security.drm.model.Role; 061 import org.deegree.security.drm.model.User; 062 063 /** 064 * Helper class that performs common security access tasks and checks used in the 065 * <code>Listener</code> classes. 066 * 067 * @author <a href="mschneider@lat-lon.de">Markus Schneider </a> 068 * @author last edited by: $Author: apoth $ 069 * 070 * @version $Revision: 9346 $, $Date: 2007-12-27 17:39:07 +0100 (Do, 27 Dez 2007) $ 071 */ 072 public class ClientHelper { 073 074 public static final String KEY_USERNAME = "USERNAME"; 075 076 public static final String KEY_PASSWORD = "PASSWORD"; 077 078 public static final String TYPE_LAYER = "Layer"; 079 080 public static final String TYPE_FEATURETYPE = "Featuretype"; 081 082 public static final String TYPE_METADATASCHEMA = "MetadataSchema"; 083 084 /** 085 * Tries to acquire a <code>SecurityAccess</code> for the credentials (username, password) 086 * stored in the associated <code>HttpSesssion</code> of the given 087 * <code>AbstractListener</code>. 088 * 089 * @param listener 090 * @throws GeneralSecurityException 091 * @return SecurityAccess 092 */ 093 public static SecurityAccess acquireAccess( AbstractListener listener ) 094 throws GeneralSecurityException { 095 // get USERNAME and PASSWORD from HttpSession 096 HttpSession session = ( (HttpServletRequest) listener.getRequest() ).getSession( false ); 097 if ( session == null ) { 098 throw new UnauthorizedException( Messages.getMessage( "IGEO_STD_SEC_ERROR_UNAUTHORIZED_ACCESS" ) ); 099 } 100 String userName = (String) session.getAttribute( KEY_USERNAME ); 101 String password = (String) session.getAttribute( KEY_PASSWORD ); 102 103 // perform access check 104 SecurityAccessManager manager = SecurityAccessManager.getInstance(); 105 User user = manager.getUserByName( userName ); 106 user.authenticate( password ); 107 return manager.acquireAccess( user ); 108 } 109 110 /** 111 * Tries to acquire a <code>SecurityTransaction</code> for the credentials (username, 112 * password) stored in the associated <code>HttpSesssion</code>. 113 * 114 * @param listener 115 * @throws GeneralSecurityException 116 * @return SecurityTransaction 117 */ 118 public static SecurityTransaction acquireTransaction( AbstractListener listener ) 119 throws GeneralSecurityException { 120 // get USERNAME and PASSWORD from HttpSession 121 HttpSession session = ( (HttpServletRequest) listener.getRequest() ).getSession( false ); 122 String userName = (String) session.getAttribute( KEY_USERNAME ); 123 String password = (String) session.getAttribute( KEY_PASSWORD ); 124 125 // perform access check 126 SecurityAccessManager manager = SecurityAccessManager.getInstance(); 127 User user = manager.getUserByName( userName ); 128 user.authenticate( password ); 129 return manager.acquireTransaction( user ); 130 } 131 132 /** 133 * Returns the administrator (the 'Administrator'- or a 'SUBADMIN:'-role) for the given role. 134 * 135 * @param access 136 * @param role 137 * @throws GeneralSecurityException 138 * @return Role 139 */ 140 public static Role findAdminForRole( SecurityAccess access, Role role ) 141 throws GeneralSecurityException { 142 Role[] allRoles = access.getAllRoles(); 143 Role admin = access.getRoleById( Role.ID_SEC_ADMIN ); 144 for ( int i = 0; i < allRoles.length; i++ ) { 145 if ( allRoles[i].getName().startsWith( "SUBADMIN:" ) ) { 146 // if a subadmin-role has the update right, it is 147 // considered to be administrative for the role 148 if ( allRoles[i].hasRight( access, RightType.UPDATE, role ) ) { 149 admin = allRoles[i]; 150 } 151 } 152 } 153 return admin; 154 } 155 156 /** 157 * Returns the associated 'Administrator'- or 'SUBADMIN:'-role of the token holder. 158 * 159 * @param access 160 * @throws GeneralSecurityException 161 * @return Role 162 */ 163 public static Role checkForAdminOrSubadminRole( SecurityAccess access ) 164 throws GeneralSecurityException { 165 Role adminOrSubadminRole = null; 166 Role[] roles = access.getUser().getRoles( access ); 167 for ( int i = 0; i < roles.length; i++ ) { 168 if ( roles[i].getID() == Role.ID_SEC_ADMIN 169 || roles[i].getName().startsWith( "SUBADMIN:" ) ) { 170 if ( adminOrSubadminRole == null ) { 171 adminOrSubadminRole = roles[i]; 172 } else { 173 throw new GeneralSecurityException( Messages.getMessage( "IGEO_STD_SEC_WRONG_ROLE", 174 access.getUser().getTitle(), 175 adminOrSubadminRole.getTitle(), 176 roles[i].getTitle() ) ); 177 } 178 } 179 } 180 if ( adminOrSubadminRole == null ) { 181 throw new UnauthorizedException( Messages.getMessage( "IGEO_STD_SEC_MISSING_SUBADMIN_ROLE" ) ); 182 } 183 return adminOrSubadminRole; 184 } 185 186 /** 187 * Tests if the given token is associated with the 'Administrator'-role. 188 * 189 * @param access 190 * @throws GeneralSecurityException, 191 * this is an UnauthorizedException if the user does not have the 192 * 'Administrator'-role 193 */ 194 public static void checkForAdminRole( SecurityAccess access ) 195 throws GeneralSecurityException { 196 Role[] roles = access.getUser().getRoles( access ); 197 for ( int i = 0; i < roles.length; i++ ) { 198 if ( roles[i].getID() == Role.ID_SEC_ADMIN ) { 199 return; 200 } 201 } 202 throw new UnauthorizedException( Messages.getMessage( "IGEO_STD_SEC_MISSING_ADMIN_ROLE" ) ); 203 } 204 205 /** 206 * Tests if the 'SUBADMIN:' and 'Administrator'-roles are all disjoint (so that there are no 207 * users that have more than 1 role). 208 * 209 * @param access 210 * @throws GeneralSecurityException 211 * if there is a user with more than one role 212 */ 213 public static void checkSubadminRoleValidity( SecurityAccess access ) 214 throws GeneralSecurityException { 215 216 Role[] subadminRoles = access.getRolesByNS( "SUBADMIN" ); 217 Set<User>[] rolesAndUsers = new Set[subadminRoles.length + 1]; 218 219 String[] roleNames = new String[subadminRoles.length + 1]; 220 221 // admin role 222 User[] users = access.getRoleById( Role.ID_SEC_ADMIN ).getAllUsers( access ); 223 rolesAndUsers[0] = new HashSet<User>(); 224 roleNames[0] = "Administrator"; 225 for ( int i = 0; i < users.length; i++ ) { 226 rolesAndUsers[0].add( users[i] ); 227 } 228 229 // subadmin roles 230 for ( int i = 1; i < rolesAndUsers.length; i++ ) { 231 users = subadminRoles[i - 1].getAllUsers( access ); 232 rolesAndUsers[i] = new HashSet<User>(); 233 roleNames[i] = subadminRoles[i - 1].getTitle(); 234 for ( int j = 0; j < users.length; j++ ) { 235 rolesAndUsers[i].add( users[j] ); 236 } 237 } 238 239 // now check if all usersets are disjoint 240 for ( int i = 0; i < rolesAndUsers.length - 1; i++ ) { 241 Set userSet1 = rolesAndUsers[i]; 242 for ( int j = i + 1; j < rolesAndUsers.length; j++ ) { 243 Set userSet2 = rolesAndUsers[j]; 244 Iterator it = userSet2.iterator(); 245 while ( it.hasNext() ) { 246 User user = (User) it.next(); 247 if ( userSet1.contains( user ) ) { 248 throw new GeneralSecurityException( Messages.getMessage( "IGEO_STD_SEC_INVALID_SUBADMIN_ROLE", 249 user.getTitle(), 250 roleNames[i], 251 roleNames[j] ) ); 252 } 253 } 254 } 255 } 256 } 257 }