1 10 11 package org.mule.extras.jaas; 12 13 import java.io.IOException ; 14 import java.security.Security ; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 18 import javax.security.auth.login.AppConfigurationEntry ; 19 import javax.security.auth.login.Configuration ; 20 import javax.security.auth.login.LoginContext ; 21 import javax.security.auth.login.LoginException ; 22 23 import org.mule.config.i18n.Messages; 24 import org.mule.impl.security.MuleAuthentication; 25 import org.mule.umo.lifecycle.InitialisationException; 26 import org.mule.umo.security.UMOAuthentication; 27 import org.mule.umo.security.UMOSecurityContext; 28 import org.mule.umo.security.UMOSecurityContextFactory; 29 import org.mule.umo.security.UMOSecurityProvider; 30 import org.mule.umo.security.UnauthorisedException; 31 import org.mule.umo.security.UnknownAuthenticationTypeException; 32 33 36 public class JaasSimpleAuthenticationProvider implements UMOSecurityProvider 37 { 38 39 private String loginConfig; 40 private String loginContextName; 41 private String credentials; 42 private String loginModule; 43 private String defaultModule = "org.mule.extras.jaas.loginmodule.DefaultLoginModule"; 44 private String name; 45 private UMOSecurityContextFactory factory; 46 47 50 55 public final void setLoginConfig(String loginConfig) 56 { 57 this.loginConfig = loginConfig; 58 } 59 60 65 public final String getLoginConfig() 66 { 67 return loginConfig; 68 } 69 70 75 public final void setLoginContextName(String loginContextName) 76 { 77 this.loginContextName = loginContextName; 78 } 79 80 85 public final String getLoginContextName() 86 { 87 return loginContextName; 88 } 89 90 95 public final String getCredentials() 96 { 97 return credentials; 98 } 99 100 105 public final void setCredentials(String credentials) 106 { 107 this.credentials = credentials; 108 } 109 110 115 public final String getLoginModule() 116 { 117 return loginModule; 118 } 119 120 125 public final void setLoginModule(String loginModule) 126 { 127 this.loginModule = loginModule; 128 } 129 130 133 public final String getName() 134 { 135 return name; 136 } 137 138 141 public final void setName(String name) 142 { 143 this.name = name; 144 } 145 146 148 153 private void configureJaas() throws IOException 154 { 155 156 String loginConfigUrl = "file://" 157 + org.mule.util.FileUtils.getResourcePath(loginConfig, 158 JaasSimpleAuthenticationProvider.class); 159 160 boolean alreadySet = false; 161 162 int n = 1; 163 String prefix = "login.config.url."; 164 String existing = null; 165 166 while ((existing = Security.getProperty(prefix + n)) != null) 167 { 168 alreadySet = existing.equals(loginConfigUrl); 169 170 if (alreadySet) 171 { 172 break; 173 } 174 n++; 175 } 176 177 if (!alreadySet) 178 { 179 String key = prefix + n; 180 Security.setProperty(key, loginConfigUrl); 181 } 182 } 183 184 193 public final UMOAuthentication authenticate(UMOAuthentication authentication) 194 throws org.mule.umo.security.SecurityException 195 { 196 197 LoginContext loginContext; 198 MuleAuthentication auth = (MuleAuthentication)authentication; 199 200 MuleCallbackHandler cbh = new MuleCallbackHandler(auth); 202 203 try 205 { 206 loginContext = new LoginContext (loginContextName, cbh); 207 } 208 catch (LoginException e) 209 { 210 throw new org.mule.umo.security.UnauthorisedException(new org.mule.config.i18n.Message( 211 org.mule.config.i18n.Messages.CANT_LOAD_X_FROM_CLASSPATH_FILE, loginContextName)); 212 } 213 214 try 216 { 217 loginContext.login(); 218 } 219 catch (LoginException le) 220 { 221 throw new UnauthorisedException(new org.mule.config.i18n.Message( 222 org.mule.config.i18n.Messages.AUTH_FAILED_FOR_USER_X, auth.getPrincipal())); 223 } 224 225 auth.setAuthenticated(true); 226 227 return auth; 228 } 229 230 236 public final boolean supports(Class aClass) 237 { 238 return UMOAuthentication.class.isAssignableFrom(aClass); 239 } 240 241 246 public final UMOSecurityContext createSecurityContext(UMOAuthentication auth) 247 throws UnknownAuthenticationTypeException 248 { 249 return factory.create(auth); 250 } 251 252 261 public final void initialise() throws InitialisationException 262 { 263 if (loginConfig == null) 266 { 267 try 268 { 269 AppConfigurationEntry entry = null; 270 JaasConfig.init(); 271 272 HashMap options = new HashMap (); 273 options.put("credentials", credentials); 274 275 if (loginModule != null) 278 { 279 entry = new AppConfigurationEntry (loginModule, 280 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options); 281 } 282 else 283 { 284 entry = new AppConfigurationEntry (defaultModule, 285 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options); 286 } 287 288 JaasConfig.addApplicationConfigEntry(loginContextName, entry); 289 } 290 catch (Exception e) 291 { 292 throw new InitialisationException(e, this); 293 } 294 } 295 else 296 { 297 try 299 { 300 configureJaas(); 301 } 302 catch (IOException e) 303 { 304 throw new InitialisationException(e, this); 305 } 306 } 307 308 try 310 { 311 factory = new JaasSecurityContextFactory(); 312 } 313 catch (Exception e) 314 { 315 throw new InitialisationException(new org.mule.config.i18n.Message(Messages.FAILED_TO_CREATE_X, 316 "JaasProvider"), e); 317 } 318 } 319 320 324 public static class JaasConfig extends Configuration 325 { 326 327 private static Map appConfigEntries = new HashMap (); 328 private static JaasConfig jaasConfig; 329 330 333 public static void init() 334 { 335 jaasConfig = new JaasConfig(); 336 Configuration.setConfiguration(jaasConfig); 337 } 338 339 344 public static JaasConfig getJaasConfig() 345 { 346 return jaasConfig; 347 } 348 349 355 public static void addApplicationConfigEntry(String name, AppConfigurationEntry entry) 356 { 357 appConfigEntries.put(name, entry); 358 } 359 360 366 public final AppConfigurationEntry [] getAppConfigurationEntry(String applicationName) 367 { 368 369 if (applicationName == null) 370 { 371 throw new NullPointerException ("applicationName passed in was null."); 372 } 373 374 AppConfigurationEntry entry = (AppConfigurationEntry )appConfigEntries.get(applicationName); 375 if (entry == null) 376 { 377 return new AppConfigurationEntry []{}; 378 } 379 else 380 { 381 AppConfigurationEntry e[] = new AppConfigurationEntry [1]; 382 e[0] = entry; 383 return e; 384 } 385 } 386 387 public void refresh() 388 { 389 } 391 } 392 } 393 | Popular Tags |