use of javax.naming.AuthenticationException in project tomee by apache.
the class InitContextFactory method getInitialContext.
@SuppressWarnings("unchecked")
@Override
public Context getInitialContext(final Hashtable env) throws javax.naming.NamingException {
if (!OpenEJB.isInitialized()) {
initializeOpenEJB(env);
}
final String user = (String) env.get(Context.SECURITY_PRINCIPAL);
final String pass = (String) env.get(Context.SECURITY_CREDENTIALS);
final String realmName = (String) env.get("openejb.authentication.realmName");
if (user != null && pass != null) {
try {
final SecurityService securityService = SystemInstance.get().getComponent(SecurityService.class);
final Object identity;
if (realmName == null) {
identity = securityService.login(user, pass);
} else {
identity = securityService.login(realmName, user, pass);
}
securityService.associate(identity);
} catch (final LoginException e) {
throw (AuthenticationException) new AuthenticationException("User could not be authenticated: " + user).initCause(e);
}
}
final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
Context context = containerSystem.getJNDIContext();
context = (Context) context.lookup("openejb/local");
return context;
}
use of javax.naming.AuthenticationException in project tomee by apache.
the class LocalInitialContext method login.
private void login() throws AuthenticationException {
final String user = (String) properties.get(Context.SECURITY_PRINCIPAL);
final String pass = (String) properties.get(Context.SECURITY_CREDENTIALS);
final String realmName = (String) properties.get("openejb.authentication.realmName");
if (user != null && pass != null) {
try {
if (logger.isDebugEnabled()) {
logger.debug("Logging in: " + user);
}
final SecurityService securityService = SystemInstance.get().getComponent(SecurityService.class);
if (realmName == null) {
clientIdentity = securityService.login(user, pass);
} else {
clientIdentity = securityService.login(realmName, user, pass);
}
ClientSecurity.setIdentity(clientIdentity);
} catch (final LoginException e) {
throw (AuthenticationException) new AuthenticationException("User could not be authenticated: " + user).initCause(e);
}
}
}
use of javax.naming.AuthenticationException in project tomee by apache.
the class JNDIContext method logout.
private void logout() throws AuthenticationException {
final LogoutRequest request = new LogoutRequest(client.getClientIdentity());
final LogoutResponse response;
try {
response = LogoutResponse.class.cast(Client.request(request, new LogoutResponse(), server));
} catch (final RemoteException e) {
throw new AuthenticationException(e.getLocalizedMessage());
}
switch(response.getResponseCode()) {
case ResponseCodes.AUTH_DENIED:
throw AuthenticationException.class.cast(new AuthenticationException("Can't logout").initCause(response.getDeniedCause()));
case ResponseCodes.LOGOUT_SUCCESS:
default:
}
}
use of javax.naming.AuthenticationException in project tomee by apache.
the class JNDIContext method authenticate.
public void authenticate(final String userID, final String psswrd) throws AuthenticationException {
final AuthenticationRequest req = new AuthenticationRequest(String.class.cast(env.get("openejb.authentication.realmName")), userID, psswrd);
final AuthenticationResponse res;
try {
res = requestAuthorization(req);
} catch (RemoteException e) {
throw new AuthenticationException(e.getLocalizedMessage());
}
switch(res.getResponseCode()) {
case ResponseCodes.AUTH_GRANTED:
client = res.getIdentity();
break;
case ResponseCodes.AUTH_REDIRECT:
client = res.getIdentity();
server = res.getServer();
break;
case ResponseCodes.AUTH_DENIED:
throw (AuthenticationException) new AuthenticationException("This principle is not authorized.").initCause(res.getDeniedCause());
}
}
use of javax.naming.AuthenticationException in project zeppelin by apache.
the class LdapRealm method getUserDn.
/**
* Returns the LDAP User Distinguished Name (DN) to use when acquiring an
* {@link javax.naming.ldap.LdapContext LdapContext} from the
* {@link LdapContextFactory}.
* <p/>
* If the the {@link #getUserDnTemplate() userDnTemplate} property has been
* set, this implementation will construct the User DN by substituting the
* specified {@code principal} into the configured template. If the
* {@link #getUserDnTemplate() userDnTemplate} has not been set, the method
* argument will be returned directly (indicating that the submitted
* authentication token principal <em>is</em> the User DN).
*
* @param principal
* the principal to substitute into the configured
* {@link #getUserDnTemplate() userDnTemplate}.
* @return the constructed User DN to use at runtime when acquiring an
* {@link javax.naming.ldap.LdapContext}.
* @throws IllegalArgumentException
* if the method argument is null or empty
* @throws IllegalStateException
* if the {@link #getUserDnTemplate userDnTemplate} has not been
* set.
* @see LdapContextFactory#getLdapContext(Object, Object)
*/
@Override
protected String getUserDn(final String principal) throws IllegalArgumentException, IllegalStateException {
String userDn;
Matcher matchedPrincipal = matchPrincipal(principal);
String userSearchBase = getUserSearchBase();
String userSearchAttributeName = getUserSearchAttributeName();
// If not searching use the userDnTemplate and return.
if ((userSearchBase == null || userSearchBase.isEmpty()) || (userSearchAttributeName == null && userSearchFilter == null && !"object".equalsIgnoreCase(userSearchScope))) {
userDn = expandTemplate(userDnTemplate, matchedPrincipal);
if (log.isDebugEnabled()) {
log.debug("LDAP UserDN and Principal: " + userDn + "," + principal);
}
return userDn;
}
// Create the searchBase and searchFilter from config.
String searchBase = expandTemplate(getUserSearchBase(), matchedPrincipal);
String searchFilter = null;
if (userSearchFilter == null) {
if (userSearchAttributeName == null) {
searchFilter = String.format("(objectclass=%1$s)", getUserObjectClass());
} else {
searchFilter = String.format("(&(objectclass=%1$s)(%2$s=%3$s))", getUserObjectClass(), userSearchAttributeName, expandTemplate(getUserSearchAttributeTemplate(), matchedPrincipal));
}
} else {
searchFilter = expandTemplate(userSearchFilter, matchedPrincipal);
}
SearchControls searchControls = getUserSearchControls();
// Search for userDn and return.
LdapContext systemLdapCtx = null;
NamingEnumeration<SearchResult> searchResultEnum = null;
try {
systemLdapCtx = getContextFactory().getSystemLdapContext();
if (log.isDebugEnabled()) {
log.debug("SearchBase,SearchFilter,UserSearchScope: " + searchBase + "," + searchFilter + "," + userSearchScope);
}
searchResultEnum = systemLdapCtx.search(searchBase, searchFilter, searchControls);
// SearchResults contains all the entries in search scope
if (searchResultEnum.hasMore()) {
SearchResult searchResult = searchResultEnum.next();
userDn = searchResult.getNameInNamespace();
if (log.isDebugEnabled()) {
log.debug("UserDN Returned,Principal: " + userDn + "," + principal);
}
return userDn;
} else {
throw new IllegalArgumentException("Illegal principal name: " + principal);
}
} catch (AuthenticationException ne) {
ne.printStackTrace();
throw new IllegalArgumentException("Illegal principal name: " + principal);
} catch (NamingException ne) {
throw new IllegalArgumentException("Hit NamingException: " + ne.getMessage());
} finally {
try {
if (searchResultEnum != null) {
searchResultEnum.close();
}
} catch (NamingException ne) {
// Ignore exception on close.
} finally {
LdapUtils.closeContext(systemLdapCtx);
}
}
}
Aggregations