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;
|
2022-05-06 16:18:20 -05:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2025-01-23 10:45:40 -06:00
|
|
|
import org.eclipse.jetty.security.UserPrincipal;
|
|
|
|
import org.eclipse.jetty.util.security.Credential;
|
2022-05-06 16:18:20 -05:00
|
|
|
|
2015-11-19 14:39:21 -08:00
|
|
|
public class AuthenticationManager {
|
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 {
|
|
|
|
// Create and configure credentials for authentication
|
|
|
|
UserPrincipal userPrincipal = new UserPrincipal(userName, Credential.getCredential(password));
|
2018-07-31 10:10:52 -07:00
|
|
|
|
2025-01-23 10:45:40 -06:00
|
|
|
// Verify credentials
|
|
|
|
if (!userPrincipal.authenticate(password)) {
|
|
|
|
throw new AuthenticationException("Invalid credentials for user: " + userName);
|
2016-10-13 14:27:02 -07:00
|
|
|
}
|
2025-01-23 10:45:40 -06:00
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
AuthenticationException authenticationException =
|
|
|
|
new AuthenticationException("Authentication failed");
|
|
|
|
authenticationException.setRootCause(e);
|
|
|
|
throw authenticationException;
|
2016-03-10 16:32:37 -08:00
|
|
|
}
|
2016-10-13 14:27:02 -07:00
|
|
|
}
|
|
|
|
}
|