2015-11-19 14:39:21 -08:00
|
|
|
package security;
|
|
|
|
|
2018-07-31 13:22:18 -07:00
|
|
|
import com.google.common.base.Preconditions;
|
2019-08-31 20:51:14 -07:00
|
|
|
import javax.annotation.Nonnull;
|
2017-04-06 22:01:29 -07:00
|
|
|
import javax.naming.AuthenticationException;
|
2025-03-12 05:25:35 -05:00
|
|
|
import javax.security.auth.callback.Callback;
|
|
|
|
import javax.security.auth.callback.CallbackHandler;
|
|
|
|
import javax.security.auth.callback.NameCallback;
|
|
|
|
import javax.security.auth.callback.PasswordCallback;
|
|
|
|
import javax.security.auth.login.LoginContext;
|
|
|
|
import javax.security.auth.login.LoginException;
|
2022-05-06 16:18:20 -05:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2025-03-12 05:25:35 -05:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2022-05-06 16:18:20 -05:00
|
|
|
|
2015-11-19 14:39:21 -08:00
|
|
|
public class AuthenticationManager {
|
2025-03-12 05:25:35 -05:00
|
|
|
private static final Logger log = LoggerFactory.getLogger(AuthenticationManager.class);
|
|
|
|
|
2025-01-23 10:45:40 -06:00
|
|
|
private AuthenticationManager() {} // Prevent instantiation
|
2019-08-31 20:51:14 -07:00
|
|
|
|
2023-12-06 11:02:42 +05:30
|
|
|
public static void authenticateJaasUser(@Nonnull String userName, @Nonnull String password)
|
|
|
|
throws Exception {
|
2019-08-31 20:51:14 -07:00
|
|
|
Preconditions.checkArgument(!StringUtils.isAnyEmpty(userName), "Username cannot be empty");
|
2016-10-13 14:27:02 -07:00
|
|
|
|
2025-01-23 10:45:40 -06:00
|
|
|
try {
|
2025-03-12 05:25:35 -05:00
|
|
|
// Create a login context with our custom callback handler
|
|
|
|
LoginContext loginContext =
|
|
|
|
new LoginContext("WHZ-Authentication", new WHZCallbackHandler(userName, password));
|
2018-07-31 10:10:52 -07:00
|
|
|
|
2025-03-12 05:25:35 -05:00
|
|
|
// Attempt login
|
|
|
|
loginContext.login();
|
|
|
|
|
|
|
|
// If we get here, authentication succeeded
|
|
|
|
log.debug("Authentication succeeded for user: {}", userName);
|
2025-01-23 10:45:40 -06:00
|
|
|
|
2025-03-12 05:25:35 -05:00
|
|
|
} catch (LoginException le) {
|
|
|
|
log.info("Authentication failed for user {}: {}", userName, le.getMessage());
|
2025-01-23 10:45:40 -06:00
|
|
|
AuthenticationException authenticationException =
|
2025-03-12 05:25:35 -05:00
|
|
|
new AuthenticationException(le.getMessage());
|
|
|
|
authenticationException.setRootCause(le);
|
2025-01-23 10:45:40 -06:00
|
|
|
throw authenticationException;
|
2016-03-10 16:32:37 -08:00
|
|
|
}
|
2016-10-13 14:27:02 -07:00
|
|
|
}
|
2025-03-12 05:25:35 -05:00
|
|
|
|
|
|
|
private static class WHZCallbackHandler implements CallbackHandler {
|
|
|
|
private final String password;
|
|
|
|
private final String username;
|
|
|
|
|
|
|
|
private WHZCallbackHandler(@Nonnull String username, @Nonnull String password) {
|
|
|
|
this.username = username;
|
|
|
|
this.password = password;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void handle(@Nonnull Callback[] callbacks) {
|
|
|
|
for (Callback callback : callbacks) {
|
|
|
|
if (callback instanceof NameCallback) {
|
|
|
|
NameCallback nc = (NameCallback) callback;
|
|
|
|
nc.setName(username);
|
|
|
|
} else if (callback instanceof PasswordCallback) {
|
|
|
|
PasswordCallback pc = (PasswordCallback) callback;
|
|
|
|
pc.setPassword(password.toCharArray());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-13 14:27:02 -07:00
|
|
|
}
|