use of javax.security.auth.login.Configuration in project felix by apache.
the class TCCLDemoServlet method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// Demonstrates the JAAS authentication
// In following case the client code would have to
// 1. Manage the thread's context classloader
// 2. Add a DynamicImport for org.apache.felix.jaas.boot
// 3. Fetch the config using the Configuration.getInstance API and pass that on
PrintWriter pw = resp.getWriter();
CallbackHandler handler = new ServletRequestCallbackHandler(req);
Subject subject = new Subject();
final ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
Configuration config = Configuration.getInstance("JavaLoginConfig", null, "FelixJaasProvider");
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
LoginContext lc = new LoginContext("sample", subject, handler, config);
lc.login();
pw.println("Principal authentication successful");
pw.println(subject);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (NoSuchProviderException e) {
throw new RuntimeException(e);
} catch (LoginException e) {
handleAuthenticationFailure(e, pw);
} finally {
Thread.currentThread().setContextClassLoader(cl);
}
}
use of javax.security.auth.login.Configuration in project felix by apache.
the class ITJaasWithBootClasspath method testJaasWithBoot.
/**
* Creates the scenario where jaas-boot jar is placed in bootclasspath. With this the client
* code need not switch the TCCL
*/
@Test
public void testJaasWithBoot() throws Exception {
String realmName = name.getMethodName();
createLoginModuleConfig(realmName);
delay();
CallbackHandler handler = new SimpleCallbackHandler("foo", "foo");
Configuration config = Configuration.getInstance("JavaLoginConfig", null, "FelixJaasProvider");
Subject s = new Subject();
LoginContext lc = new LoginContext(realmName, s, handler, config);
lc.login();
assertFalse(s.getPrincipals().isEmpty());
}
use of javax.security.auth.login.Configuration in project felix by apache.
the class ITJaasWithConfigBasedLoginModule method testJaasConfigOrderedViaRanking.
@Test
public void testJaasConfigOrderedViaRanking() throws Exception {
String realmName = name.getMethodName();
List<Integer> ranks = Arrays.asList(1, 2, 3, 4, 5, 6);
Collections.shuffle(ranks);
// 1. Create LoginModule config with random rankings
for (Integer i : ranks) {
org.osgi.service.cm.Configuration config = ca.createFactoryConfiguration("org.apache.felix.jaas.Configuration.factory", null);
Dictionary<String, Object> p = new Hashtable<String, Object>();
p.put("jaas.classname", "org.apache.felix.jaas.integration.sample1.ConfigLoginModule");
p.put("jaas.realmName", realmName);
p.put("jaas.ranking", i);
p.put("order", i);
config.update(p);
}
delay();
Configuration jaasConfig = Configuration.getInstance("JavaLoginConfig", null, "FelixJaasProvider");
AppConfigurationEntry[] entries = jaasConfig.getAppConfigurationEntry(realmName);
assertEquals("No of entries does not match the no of created", ranks.size(), entries.length);
// Entries would be sorted via ranking. Higher ranking comes first
int ranking = 6;
for (AppConfigurationEntry e : entries) {
Integer order = (Integer) e.getOptions().get("order");
assertEquals(ranking--, order.intValue());
}
}
use of javax.security.auth.login.Configuration in project felix by apache.
the class ITJaasWithConfigBasedLoginModule method testJaasWithTCCL.
@Test
public void testJaasWithTCCL() throws Exception {
String realmName = name.getMethodName();
createLoginModuleConfig(realmName);
delay();
CallbackHandler handler = new SimpleCallbackHandler("foo", "foo");
Configuration config = Configuration.getInstance("JavaLoginConfig", null, "FelixJaasProvider");
Subject s = new Subject();
final ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
LoginContext lc = new LoginContext(realmName, s, handler, config);
lc.login();
} finally {
Thread.currentThread().setContextClassLoader(cl);
}
assertFalse(s.getPrincipals().isEmpty());
}
use of javax.security.auth.login.Configuration in project tomcat70 by apache.
the class JAASRealm method authenticate.
// -------------------------------------------------------- Package Methods
// ------------------------------------------------------ Protected Methods
/**
* Perform the actual JAAS authentication
*/
protected Principal authenticate(String username, CallbackHandler callbackHandler) {
// Establish a LoginContext to use for authentication
try {
LoginContext loginContext = null;
if (appName == null)
appName = "Tomcat";
if (log.isDebugEnabled())
log.debug(sm.getString("jaasRealm.beginLogin", username, appName));
// What if the LoginModule is in the container class loader ?
ClassLoader ocl = null;
if (!isUseContextClassLoader()) {
ocl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
}
try {
Configuration config = getConfig();
loginContext = new LoginContext(appName, null, callbackHandler, config);
} catch (Throwable e) {
ExceptionUtils.handleThrowable(e);
log.error(sm.getString("jaasRealm.unexpectedError"), e);
return (null);
} finally {
if (!isUseContextClassLoader()) {
Thread.currentThread().setContextClassLoader(ocl);
}
}
if (log.isDebugEnabled())
log.debug("Login context created " + username);
// Negotiate a login via this LoginContext
Subject subject = null;
try {
loginContext.login();
subject = loginContext.getSubject();
if (subject == null) {
if (log.isDebugEnabled())
log.debug(sm.getString("jaasRealm.failedLogin", username));
return (null);
}
} catch (AccountExpiredException e) {
if (log.isDebugEnabled())
log.debug(sm.getString("jaasRealm.accountExpired", username));
return (null);
} catch (CredentialExpiredException e) {
if (log.isDebugEnabled())
log.debug(sm.getString("jaasRealm.credentialExpired", username));
return (null);
} catch (FailedLoginException e) {
if (log.isDebugEnabled())
log.debug(sm.getString("jaasRealm.failedLogin", username));
return (null);
} catch (LoginException e) {
log.warn(sm.getString("jaasRealm.loginException", username), e);
return (null);
} catch (Throwable e) {
ExceptionUtils.handleThrowable(e);
log.error(sm.getString("jaasRealm.unexpectedError"), e);
return (null);
}
if (log.isDebugEnabled())
log.debug(sm.getString("jaasRealm.loginContextCreated", username));
// Return the appropriate Principal for this authenticated Subject
Principal principal = createPrincipal(username, subject, loginContext);
if (principal == null) {
log.debug(sm.getString("jaasRealm.authenticateFailure", username));
return (null);
}
if (log.isDebugEnabled()) {
log.debug(sm.getString("jaasRealm.authenticateSuccess", username));
}
return (principal);
} catch (Throwable t) {
log.error("error ", t);
return null;
}
}
Aggregations