Skip to content

Commit

Permalink
Use common credential class
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Apr 27, 2020
1 parent 9c08506 commit 9afb92b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.prestosql.plugin.password.file;
package io.prestosql.plugin.password;

import java.util.Objects;

import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;

public final class Credential
{
private final String user;
private final String password;

public Credential(String username, String password)
public Credential(String user, String password)
{
this.user = requireNonNull(username, "username is null");
this.user = requireNonNull(user, "user is null");
this.password = requireNonNull(password, "password is null");
}

Expand Down Expand Up @@ -57,4 +58,12 @@ public int hashCode()
{
return Objects.hash(user, password);
}

@Override
public String toString()
{
return toStringHelper(this)
.add("user", user)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import io.prestosql.plugin.password.Credential;
import io.prestosql.spi.PrestoException;

import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.UncheckedExecutionException;
import io.airlift.log.Logger;
import io.prestosql.plugin.password.Credential;
import io.prestosql.spi.security.AccessDeniedException;
import io.prestosql.spi.security.BasicPrincipal;
import io.prestosql.spi.security.PasswordAuthenticator;
Expand All @@ -36,10 +37,8 @@

import java.security.Principal;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Throwables.throwIfInstanceOf;
import static io.prestosql.plugin.password.jndi.JndiUtils.createDirContext;
Expand Down Expand Up @@ -67,7 +66,7 @@ public class LdapAuthenticator
private final Optional<String> bindPassword;
private final boolean ignoreReferrals;
private final Map<String, String> basicEnvironment;
private final LoadingCache<Credentials, Principal> authenticationCache;
private final LoadingCache<Credential, Principal> authenticationCache;

@Inject
public LdapAuthenticator(LdapConfig ldapConfig)
Expand Down Expand Up @@ -117,28 +116,28 @@ public LdapAuthenticator(LdapConfig ldapConfig)
public Principal createAuthenticatedPrincipal(String user, String password)
{
try {
return authenticationCache.getUnchecked(new Credentials(user, password));
return authenticationCache.getUnchecked(new Credential(user, password));
}
catch (UncheckedExecutionException e) {
throwIfInstanceOf(e.getCause(), AccessDeniedException.class);
throw e;
}
}

private Principal authenticateWithUserBind(Credentials credentials)
private Principal authenticateWithUserBind(Credential credential)
{
String user = credentials.getUser();
String user = credential.getUser();
if (containsSpecialCharacters(user)) {
throw new AccessDeniedException("Username contains a special LDAP character");
}
try {
String userDistinguishedName = createUserDistinguishedName(user);
if (groupAuthorizationSearchPattern.isPresent()) {
// user password is also validated as user DN and password is used for querying LDAP
checkGroupMembership(user, userDistinguishedName, credentials.getPassword());
checkGroupMembership(user, userDistinguishedName, credential.getPassword());
}
else {
validatePassword(userDistinguishedName, credentials.getPassword());
validatePassword(userDistinguishedName, credential.getPassword());
}
log.debug("Authentication successful for user [%s]", user);
}
Expand All @@ -149,22 +148,22 @@ private Principal authenticateWithUserBind(Credentials credentials)
return new BasicPrincipal(user);
}

private Principal authenticateWithBindDistinguishedName(Credentials credentials)
private Principal authenticateWithBindDistinguishedName(Credential credential)
{
String user = credentials.getUser();
String user = credential.getUser();
if (containsSpecialCharacters(user)) {
throw new AccessDeniedException("Username contains a special LDAP character");
}
try {
String userDistinguishedName = validateGroupMembership(user, bindDistinguishedName.get(), bindPassword.get());
validatePassword(userDistinguishedName, credentials.getPassword());
validatePassword(userDistinguishedName, credential.getPassword());
log.debug("Authentication successful for user [%s]", user);
}
catch (NamingException e) {
log.debug(e, "Authentication failed for user [%s], %s", user, e.getMessage());
throw new RuntimeException("Authentication error");
}
return new BasicPrincipal(credentials.getUser());
return new BasicPrincipal(credential.getUser());
}

private String createUserDistinguishedName(String user)
Expand Down Expand Up @@ -298,57 +297,4 @@ private static String replaceUser(String pattern, String user)
{
return pattern.replace("${USER}", user);
}

private static class Credentials
{
private final String user;
private final String password;

private Credentials(String user, String password)
{
this.user = requireNonNull(user);
this.password = requireNonNull(password);
}

public String getUser()
{
return user;
}

public String getPassword()
{
return password;
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

Credentials that = (Credentials) o;

return Objects.equals(this.user, that.user) &&
Objects.equals(this.password, that.password);
}

@Override
public int hashCode()
{
return Objects.hash(user, password);
}

@Override
public String toString()
{
return toStringHelper(this)
.add("user", user)
.add("password", password)
.toString();
}
}
}

0 comments on commit 9afb92b

Please sign in to comment.