1 17 18 package org.apache.geronimo.security.util; 19 20 import java.lang.reflect.Constructor ; 21 import java.security.AccessController ; 22 import java.security.PrivilegedActionException ; 23 import java.security.PrivilegedExceptionAction ; 24 import java.util.Set ; 25 import javax.security.auth.Subject ; 26 import javax.security.auth.x500.X500Principal ; 27 import javax.security.jacc.PolicyContext ; 28 import javax.security.jacc.PolicyContextException ; 29 import javax.security.jacc.PolicyContextHandler ; 30 31 import org.apache.geronimo.common.DeploymentException; 32 import org.apache.geronimo.common.GeronimoSecurityException; 33 import org.apache.geronimo.security.DomainPrincipal; 34 import org.apache.geronimo.security.PrimaryDomainPrincipal; 35 import org.apache.geronimo.security.PrimaryPrincipal; 36 import org.apache.geronimo.security.PrimaryRealmPrincipal; 37 import org.apache.geronimo.security.RealmPrincipal; 38 import org.apache.geronimo.security.deploy.DefaultDomainPrincipal; 39 import org.apache.geronimo.security.deploy.DefaultPrincipal; 40 import org.apache.geronimo.security.deploy.DefaultRealmPrincipal; 41 import org.apache.geronimo.security.deploy.PrincipalInfo; 42 43 44 52 public class ConfigurationUtil { 53 54 60 public static X500Principal generateX500Principal(String name) { 61 return new X500Principal (name); 62 } 63 64 71 public static java.security.Principal generatePrincipal(final PrincipalInfo principalInfo, ClassLoader classLoader) { 72 return generatePrincipal(principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader); 73 } 74 75 public static java.security.Principal generatePrincipal(final String className, final String principalName, final ClassLoader classLoader) { 76 try { 77 return (java.security.Principal ) AccessController.doPrivileged(new PrivilegedExceptionAction () { 78 public Object run() throws Exception { 79 Class clazz = classLoader.loadClass(className); 80 Constructor constructor = clazz.getDeclaredConstructor(new Class []{String .class}); 81 return (java.security.Principal ) constructor.newInstance(new Object []{principalName}); 82 } 83 }); 84 } catch (PrivilegedActionException e) { 85 e.printStackTrace(); 86 if (e.getException() != null) { 87 e.getException().printStackTrace(); 88 } 89 return null; 90 } 91 } 92 93 100 public static RealmPrincipal generateRealmPrincipal(final String realm, final String loginDomain, final PrincipalInfo principalInfo, ClassLoader classLoader) { 101 return generateRealmPrincipal(realm, loginDomain, principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader); 102 } 103 104 public static RealmPrincipal generateRealmPrincipal(final String realm, final String loginDomain, final String className, final String principalName, 105 ClassLoader classLoader) 106 { 107 return new RealmPrincipal(realm, loginDomain, generatePrincipal(className, principalName, classLoader)); 108 } 109 110 117 public static DomainPrincipal generateDomainPrincipal(final String loginDomain, final PrincipalInfo principalInfo, ClassLoader classLoader) { 118 return generateDomainPrincipal(loginDomain, principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader); 119 } 120 121 public static DomainPrincipal generateDomainPrincipal(final String loginDomain, final String className, final String principalName, ClassLoader classLoader) { 122 return new DomainPrincipal(loginDomain, generatePrincipal(className, principalName, classLoader)); 123 } 124 125 132 public static PrimaryRealmPrincipal generatePrimaryRealmPrincipal(final String realm, final String domain, final PrincipalInfo principalInfo, ClassLoader classLoader) throws DeploymentException { 133 return generatePrimaryRealmPrincipal(realm, domain, principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader); 134 } 135 136 public static PrimaryRealmPrincipal generatePrimaryRealmPrincipal(final String realm, final String domain, final String className, final String principalName, 137 final ClassLoader classLoader) throws DeploymentException 138 { 139 try { 140 return (PrimaryRealmPrincipal) AccessController.doPrivileged(new PrivilegedExceptionAction () { 141 public Object run() throws Exception { 142 java.security.Principal p = null; 143 Class clazz = classLoader.loadClass(className); 144 Constructor constructor = clazz.getDeclaredConstructor(new Class []{String .class}); 145 p = (java.security.Principal ) constructor.newInstance(new Object []{principalName}); 146 147 return new PrimaryRealmPrincipal(realm, domain, p); 148 } 149 }); 150 } catch (PrivilegedActionException pae) { 151 throw new DeploymentException("Unable to create realm principal", pae.getException()); 152 } 153 } 154 155 162 public static PrimaryDomainPrincipal generatePrimaryDomainPrincipal(final String domain, final PrincipalInfo principalInfo, ClassLoader classLoader) throws DeploymentException { 163 return generatePrimaryDomainPrincipal(domain, principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader); 164 } 165 166 public static PrimaryDomainPrincipal generatePrimaryDomainPrincipal(final String domain, final String className, final String principalName, 167 final ClassLoader classLoader) throws DeploymentException 168 { 169 try { 170 return (PrimaryDomainPrincipal) AccessController.doPrivileged(new PrivilegedExceptionAction () { 171 public Object run() throws Exception { 172 java.security.Principal p = null; 173 Class clazz = classLoader.loadClass(className); 174 Constructor constructor = clazz.getDeclaredConstructor(new Class []{String .class}); 175 p = (java.security.Principal ) constructor.newInstance(new Object []{principalName}); 176 177 return new PrimaryDomainPrincipal(domain, p); 178 } 179 }); 180 } catch (PrivilegedActionException pae) { 181 throw new DeploymentException("Unable to create domain principal", pae.getException()); 182 } 183 } 184 185 192 public static PrimaryPrincipal generatePrimaryPrincipal(final PrincipalInfo principalInfo, ClassLoader classLoader) throws DeploymentException { 193 return generatePrimaryPrincipal(principalInfo.getClassName(), principalInfo.getPrincipalName(), classLoader); 194 } 195 196 public static PrimaryPrincipal generatePrimaryPrincipal(final String className, final String principalName, final ClassLoader classLoader) throws DeploymentException { 197 try { 198 return (PrimaryPrincipal) AccessController.doPrivileged(new PrivilegedExceptionAction () { 199 public Object run() throws Exception { 200 java.security.Principal p = null; 201 Class clazz = classLoader.loadClass(className); 202 Constructor constructor = clazz.getDeclaredConstructor(new Class []{String .class}); 203 p = (java.security.Principal ) constructor.newInstance(new Object []{principalName}); 204 205 return new PrimaryPrincipal(p); 206 } 207 }); 208 } catch (PrivilegedActionException pae) { 209 throw new DeploymentException("Unable to create principal", pae.getException()); 210 } 211 } 212 213 220 public static Subject generateDefaultSubject(DefaultPrincipal defaultPrincipal, ClassLoader classLoader) throws DeploymentException { 221 if (defaultPrincipal == null) { 222 throw new GeronimoSecurityException("No DefaultPrincipal configuration supplied"); 223 } 224 Subject defaultSubject = new Subject (); 225 java.security.Principal principal; 226 java.security.Principal primaryPrincipal; 227 228 if (defaultPrincipal instanceof DefaultRealmPrincipal) { 229 DefaultRealmPrincipal defaultRealmPrincipal = (DefaultRealmPrincipal) defaultPrincipal; 230 principal = generateRealmPrincipal(defaultRealmPrincipal.getRealm(), defaultRealmPrincipal.getDomain(), defaultRealmPrincipal.getPrincipal(), classLoader); 231 primaryPrincipal = generatePrimaryRealmPrincipal(defaultRealmPrincipal.getRealm(), defaultRealmPrincipal.getDomain(), defaultRealmPrincipal.getPrincipal(), classLoader); 232 } else if (defaultPrincipal instanceof DefaultDomainPrincipal) { 233 DefaultDomainPrincipal defaultDomainPrincipal = (DefaultDomainPrincipal) defaultPrincipal; 234 principal = generateDomainPrincipal(defaultDomainPrincipal.getDomain(), defaultDomainPrincipal.getPrincipal(), classLoader); 235 primaryPrincipal = generatePrimaryDomainPrincipal(defaultDomainPrincipal.getDomain(), defaultDomainPrincipal.getPrincipal(), classLoader); 236 } else { 237 principal = generatePrincipal(defaultPrincipal.getPrincipal(), classLoader); 238 primaryPrincipal = generatePrimaryPrincipal(defaultPrincipal.getPrincipal(), classLoader); 239 240 } 241 defaultSubject.getPrincipals().add(principal); 242 defaultSubject.getPrincipals().add(primaryPrincipal); 243 244 Set namedUserPasswordCredentials = defaultPrincipal.getNamedUserPasswordCredentials(); 245 if (namedUserPasswordCredentials != null) { 246 defaultSubject.getPrivateCredentials().addAll(namedUserPasswordCredentials); 247 } 248 249 return defaultSubject; 250 } 251 252 253 265 public static void registerPolicyContextHandler(PolicyContextHandler handler, boolean replace) throws PolicyContextException { 266 String [] keys = handler.getKeys(); 267 268 for (int i = 0; i < keys.length; i++) { 269 PolicyContext.registerHandler(keys[i], handler, replace); 270 } 271 } 272 273 274 } 275 | Popular Tags |