datahub/datahub-frontend/app/security/AuthenticationManager.java

34 lines
1.2 KiB
Java
Raw Normal View History

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;
import javax.naming.AuthenticationException;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.security.UserPrincipal;
import org.eclipse.jetty.util.security.Credential;
2015-11-19 14:39:21 -08:00
public class AuthenticationManager {
private AuthenticationManager() {} // Prevent instantiation
2019-08-31 20:51:14 -07:00
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");
try {
// Create and configure credentials for authentication
UserPrincipal userPrincipal = new UserPrincipal(userName, Credential.getCredential(password));
2018-07-31 10:10:52 -07:00
// Verify credentials
if (!userPrincipal.authenticate(password)) {
throw new AuthenticationException("Invalid credentials for user: " + userName);
}
} catch (Exception e) {
AuthenticationException authenticationException =
new AuthenticationException("Authentication failed");
authenticationException.setRootCause(e);
throw authenticationException;
}
}
}