1 28 package net.sf.jguard.ext.util; 29 30 import java.security.Principal ; 31 import java.util.Collection ; 32 import java.util.HashSet ; 33 import java.util.Iterator ; 34 import java.util.Set ; 35 import java.util.logging.Logger ; 36 37 import javax.security.auth.Subject ; 38 39 import net.sf.jguard.core.authentication.credentials.JGuardCredential; 40 import net.sf.jguard.core.authorization.permissions.PermissionUtils; 41 import net.sf.jguard.core.principals.RolePrincipal; 42 import net.sf.jguard.core.principals.UserPrincipal; 43 import net.sf.jguard.ext.authentication.AuthenticationException; 44 import net.sf.jguard.ext.registration.SubjectTemplate; 45 46 51 public class SubjectUtils { 52 53 private static final Logger logger = Logger.getLogger(SubjectUtils.class.getName()); 54 55 62 public static Collection getCredentialValues(Subject subject,String credentialId){ 63 Collection valuesFound = new HashSet (); 64 valuesFound.addAll(getCredentialValues(subject,true,credentialId)); 65 valuesFound.addAll(getCredentialValues(subject,false,credentialId)); 66 return valuesFound; 67 } 68 69 78 public static Collection getCredentialValues(Subject subject,boolean publicVisibility ,String credentialId){ 79 Collection valuesFound = new HashSet (); 80 Collection credentials = null; 81 if(publicVisibility==true){ 82 credentials = subject.getPublicCredentials(JGuardCredential.class); 83 }else{ 84 try{ 85 credentials = subject.getPrivateCredentials(JGuardCredential.class); 86 }catch(SecurityException sex){ 87 logger.finest(" you don't have the permission to grab private credentials "); 88 return valuesFound; 89 } 90 } 91 Iterator it = credentials.iterator(); 92 while(it.hasNext()){ 93 Object credential = (JGuardCredential)it.next(); 94 if(credential instanceof JGuardCredential == false){ 96 continue; 97 } 98 JGuardCredential cred = (JGuardCredential)credential; 99 if(cred.getId().equals(credentialId)){ 100 valuesFound.add(cred.getValue()); 101 } 102 } 103 104 return valuesFound; 105 } 106 107 117 public static String getCredentialValueAsString(Subject subject, boolean publicVisibility, String credentialId){ 118 String valueFound = ""; 119 Collection credentials = null; 120 if(publicVisibility==true){ 121 credentials = subject.getPublicCredentials(JGuardCredential.class); 122 }else{ 123 try{ 124 credentials = subject.getPrivateCredentials(JGuardCredential.class); 125 }catch(SecurityException sex){ 126 logger.finest(" you don't have the permission to grab private credentials "); 127 return valueFound; 128 } 129 } 130 Iterator it = credentials.iterator(); 131 while(it.hasNext()){ 132 JGuardCredential cred = (JGuardCredential)it.next(); 133 if(cred.getId().equals(credentialId)){ 134 valueFound = (String )cred.getValue(); 135 } 136 } 137 138 return valueFound; 139 } 140 141 151 public static void setCredentialValue(Subject subject, boolean publicVisibility, String credentialId, Object credentialValue, boolean isIdentity) { 152 Set credentials = null; 153 boolean credFound = false; 154 if(publicVisibility){ 155 credentials = subject.getPublicCredentials(); 156 }else{ 157 try{ 158 credentials = subject.getPrivateCredentials(); 159 }catch(SecurityException sex){ 160 logger.finest(" you don't have the permission to grab private credentials "); 161 return; 162 } 163 } 164 Iterator it = credentials.iterator(); 165 JGuardCredential jCred = null; 166 while(it.hasNext()){ 167 Object credential = it.next(); 168 if(!(credential instanceof JGuardCredential)){ 169 continue; 170 }else{ 171 jCred = (JGuardCredential)credential; 172 } 173 174 if(jCred.getId().equals(credentialId)){ 175 jCred.setValue(credentialValue); 176 credFound = true; 177 break; 178 } 179 } 180 if(!credFound) { 181 jCred = new JGuardCredential(); 182 jCred.setId(credentialId); 183 jCred.setValue(credentialValue); 184 jCred.setIdentity(isIdentity); 185 credentials.add(jCred); 186 } 187 } 188 189 196 public static void addCredentialValue(Subject subject, boolean publicVisibility, String credentialId, Object credentialValue) { 197 Set credentials = null; 198 boolean credAlreadyExists = false; 199 if(publicVisibility){ 200 credentials = subject.getPublicCredentials(); 201 }else{ 202 try{ 203 credentials = subject.getPrivateCredentials(); 204 }catch(SecurityException sex){ 205 logger.finest(" you don't have the permission to grab private credentials "); 206 return; 207 } 208 } 209 Iterator it = credentials.iterator(); 210 JGuardCredential jCred = null; 211 while(it.hasNext()){ 212 Object credential = it.next(); 213 if(!(credential instanceof JGuardCredential)){ 214 continue; 215 }else{ 216 jCred = (JGuardCredential)credential; 217 } 218 219 if(jCred.getId().equals(credentialId) && jCred.getValue().equals(credentialValue)){ 220 credAlreadyExists = true; 221 break; 222 } 223 } 224 if(!credAlreadyExists) { 225 jCred = new JGuardCredential(); 226 jCred.setId(credentialId); 227 jCred.setValue(credentialValue); 228 credentials.add(jCred); 229 } 230 } 231 232 239 public static JGuardCredential getIdentityCredential(Subject subject ,SubjectTemplate template) throws AuthenticationException{ 240 241 if(subject == null){ 242 throw new IllegalArgumentException ("'subject' parameter is null"); 243 } 244 245 JGuardCredential idCredential = new JGuardCredential(); 246 idCredential.setId(template.getIdentityCredential().getId()); 247 idCredential.setIdentity(true); 248 Collection values = getCredentialValues(subject,idCredential.getId()); 249 Iterator it = values.iterator(); 250 if(it.hasNext()){ 251 idCredential.setValue(it.next()); 252 } 253 return idCredential; 254 } 255 256 263 public static void removeCredentialValue(Subject subject, boolean publicVisibility, String credentialId, Object credentialValue) { 264 Set credentials = null; 265 boolean credentialExists = false; 266 if(publicVisibility){ 267 credentials = subject.getPublicCredentials(); 268 }else{ 269 try{ 270 credentials = subject.getPrivateCredentials(); 271 }catch(SecurityException sex){ 272 logger.finest(" you don't have the permission to grab private credentials "); 273 return; 274 } 275 } 276 Iterator it = credentials.iterator(); 277 JGuardCredential jCred = null; 278 while(it.hasNext()){ 279 Object credential = it.next(); 280 if(!(credential instanceof JGuardCredential)){ 281 continue; 282 }else{ 283 jCred = (JGuardCredential)credential; 284 } 285 286 if(jCred.getId().equals(credentialId) && jCred.getValue().equals(credentialValue)){ 287 credentialExists = true; 288 break; 289 } 290 } 291 if(credentialExists) { 292 credentials.remove(jCred); 293 } 294 } 295 296 public static Set getEnabledPrincipals(Set userPrincipals) { 297 Set enabledPrincipals = new HashSet (); 298 UserPrincipal userPrincipal = null; 300 Iterator userPrincipalsIt = userPrincipals.iterator(); 301 while(userPrincipalsIt.hasNext()){ 302 Principal ppal = (Principal )userPrincipalsIt.next(); 303 if(ppal instanceof UserPrincipal) { 304 userPrincipal = (UserPrincipal)ppal; 305 break; 306 } 307 } 308 userPrincipalsIt = userPrincipals.iterator(); 309 while(userPrincipalsIt.hasNext()) { 311 Principal ppal = (Principal )userPrincipalsIt.next(); 312 if(ppal instanceof RolePrincipal) { 313 RolePrincipal tempUserPrincipal = (RolePrincipal) ppal; 314 if(!"userPrincipal".equals(tempUserPrincipal.getLocalName())) { 315 if(PermissionUtils.evaluatePrincipal(tempUserPrincipal, userPrincipal)) { 316 enabledPrincipals.add(tempUserPrincipal); 317 } 318 } 319 } 320 } 321 322 return enabledPrincipals; 323 } 324 325 } 326 | Popular Tags |