use of javax.security.auth.login.AccountException in project karaf by apache.
the class JaasSecurityProvider method doAuthenticate.
public Subject doAuthenticate(final String address, final String username, final String password) {
try {
Subject subject = new Subject();
subject.getPrincipals().add(new ClientPrincipal("webconsole", address));
LoginContext loginContext = new LoginContext(realm, subject, callbacks -> {
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
((NameCallback) callback).setName(username);
} else if (callback instanceof PasswordCallback) {
((PasswordCallback) callback).setPassword(password.toCharArray());
} else {
throw new UnsupportedCallbackException(callback);
}
}
});
loginContext.login();
if (role != null && role.length() > 0) {
String clazz = "org.apache.karaf.jaas.boot.principal.RolePrincipal";
String name = role;
int idx = role.indexOf(':');
if (idx > 0) {
clazz = role.substring(0, idx);
name = role.substring(idx + 1);
}
boolean found = false;
for (Principal p : subject.getPrincipals()) {
if (p.getClass().getName().equals(clazz) && p.getName().equals(name)) {
found = true;
break;
}
}
if (!found) {
throw new FailedLoginException("User does not have the required role " + role);
}
}
return subject;
} catch (FailedLoginException e) {
LOG.debug("Login failed", e);
return null;
} catch (AccountException e) {
LOG.warn("Account failure", e);
return null;
} catch (GeneralSecurityException e) {
LOG.error("General Security Exception", e);
return null;
}
}
use of javax.security.auth.login.AccountException in project fabric8 by jboss-fuse.
the class HttpBasicServer method doAuthenticate.
private Subject doAuthenticate(final String username, final String password) {
try {
Subject subject = new Subject();
LoginContext loginContext = new LoginContext(realm, subject, new CallbackHandler() {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof NameCallback) {
((NameCallback) callbacks[i]).setName(username);
} else if (callbacks[i] instanceof PasswordCallback) {
((PasswordCallback) callbacks[i]).setPassword(password.toCharArray());
} else {
throw new UnsupportedCallbackException(callbacks[i]);
}
}
}
});
loginContext.login();
logger.debug("Login successful: {}", subject.toString());
boolean found = false;
for (String role : roles) {
if (role != null && role.length() > 0 && !found) {
String roleName = role.trim();
int idx = roleName.indexOf(':');
if (idx > 0) {
roleName = roleName.substring(idx + 1);
}
for (Principal p : subject.getPrincipals()) {
logger.debug("Principal found in real: {}", p.getName());
if (p.getName().equals(roleName)) {
found = true;
break;
}
}
}
}
if (!found) {
throw new FailedLoginException("User does not have the required role " + Arrays.asList(roles));
}
return subject;
} catch (AccountException e) {
logger.warn("Account failure {}", e.getMessage());
return null;
} catch (LoginException e) {
logger.debug("Login failed {}", e.getMessage());
return null;
}
}
use of javax.security.auth.login.AccountException in project fabric8 by jboss-fuse.
the class MavenSecureHttpContext method doAuthenticate.
public Subject doAuthenticate(final String username, final String password) {
try {
Subject subject = new Subject();
LoginContext loginContext = new LoginContext(realm, subject, new CallbackHandler() {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof NameCallback) {
((NameCallback) callbacks[i]).setName(username);
} else if (callbacks[i] instanceof PasswordCallback) {
((PasswordCallback) callbacks[i]).setPassword(password.toCharArray());
} else {
throw new UnsupportedCallbackException(callbacks[i]);
}
}
}
});
loginContext.login();
boolean found = false;
for (String role : roles) {
if (role != null && role.length() > 0) {
String clazz = "org.apache.karaf.jaas.boot.principal.RolePrincipal";
String name = role;
int idx = role.indexOf(':');
if (idx > 0) {
clazz = role.substring(0, idx);
name = role.substring(idx + 1);
}
for (Principal p : subject.getPrincipals()) {
if (p.getClass().getName().equals(clazz) && p.getName().equals(name)) {
found = true;
break;
}
}
}
}
if (!found) {
throw new FailedLoginException("User does not have the required role " + roles);
}
return subject;
} catch (AccountException e) {
LOGGER.warn("Account failure", e);
return null;
} catch (LoginException e) {
LOGGER.debug("Login failed", e);
return null;
} catch (GeneralSecurityException e) {
LOGGER.error("General Security Exception", e);
return null;
}
}
use of javax.security.auth.login.AccountException in project kylo by Teradata.
the class LdapLoginModule method doLogin.
/* (non-Javadoc)
* @see com.thinkbiganalytics.auth.jaas.AbstractLoginModule#doLogin()
*/
@Override
protected boolean doLogin() throws Exception {
final NameCallback nameCallback = new NameCallback("Username: ");
final PasswordCallback passwordCallback = new PasswordCallback("Password: ", false);
handle(nameCallback, passwordCallback);
if (nameCallback.getName() == null) {
throw new AccountException("No username provided for authentication");
}
Principal userPrincipal = new UsernamePrincipal(nameCallback.getName());
String password = new String(passwordCallback.getPassword());
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userPrincipal, password);
try {
log.debug("Authenticating: {}", userPrincipal);
DirContextOperations dirContext = this.authenticator.authenticate(authentication);
log.debug("Successfully Authenticated: {}", userPrincipal);
setUserPrincipal(userPrincipal);
for (GrantedAuthority grant : this.authoritiesPopulator.getGrantedAuthorities(dirContext, nameCallback.getName())) {
String groupName = grant.getAuthority();
log.debug("Found group for {}: {}", userPrincipal, groupName);
if (groupName != null) {
addNewGroupPrincipal(groupName);
}
}
return true;
} catch (BadCredentialsException e) {
throw new CredentialException(e.getMessage());
}
}
use of javax.security.auth.login.AccountException in project cxf by apache.
the class ServiceListJAASAuthenticator method doAuthenticate.
public Subject doAuthenticate(final String username, final String password) {
try {
Subject subject = new Subject();
LoginContext loginContext = new LoginContext(realm, subject, new CallbackHandler() {
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof NameCallback) {
((NameCallback) callbacks[i]).setName(username);
} else if (callbacks[i] instanceof PasswordCallback) {
((PasswordCallback) callbacks[i]).setPassword(password == null ? null : password.toCharArray());
} else {
throw new UnsupportedCallbackException(callbacks[i]);
}
}
}
});
loginContext.login();
return subject;
} catch (FailedLoginException e) {
LOG.log(Level.FINE, "Login failed ", e);
return null;
} catch (AccountException e) {
LOG.log(Level.WARNING, "Account failure ", e);
return null;
} catch (GeneralSecurityException e) {
LOG.log(Level.SEVERE, "General Security Exception ", e);
return null;
}
}
Aggregations