org.postgresql.gss.MakeGSS Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mogdb-jdbc Show documentation
Show all versions of mogdb-jdbc Show documentation
Java JDBC driver for MogDB
/*
* Copyright (c) 2008, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/
package io.mogdb.gss;
import io.mogdb.core.PGStream;
import io.mogdb.util.GT;
import io.mogdb.util.PSQLException;
import io.mogdb.util.PSQLState;
import io.mogdb.log.Logger;
import io.mogdb.log.Log;
import org.ietf.jgss.GSSCredential;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.sql.SQLException;
import java.util.Set;
import javax.security.auth.Subject;
import javax.security.auth.login.LoginContext;
public class MakeGSS {
private static Log LOGGER = Logger.getLogger(MakeGSS.class.getName());
public static void authenticate(PGStream pgStream, String host, String user, String password,
String jaasApplicationName, String kerberosServerName, boolean useSpnego, boolean jaasLogin)
throws IOException, SQLException {
LOGGER.trace(" <=BE AuthenticationReqGSS");
if (jaasApplicationName == null) {
jaasApplicationName = "pgjdbc";
}
if (kerberosServerName == null) {
kerberosServerName = "postgres";
}
Exception result;
try {
boolean performAuthentication = jaasLogin;
GSSCredential gssCredential = null;
Subject sub = Subject.getSubject(AccessController.getContext());
if (sub != null) {
Set gssCreds = sub.getPrivateCredentials(GSSCredential.class);
if (gssCreds != null && !gssCreds.isEmpty()) {
gssCredential = gssCreds.iterator().next();
performAuthentication = false;
}
}
if (performAuthentication) {
LoginContext lc =
new LoginContext(jaasApplicationName, new GSSCallbackHandler(user, password));
lc.login();
sub = lc.getSubject();
}
PrivilegedAction action = new GssAction(pgStream, gssCredential, host, user,
kerberosServerName, useSpnego);
result = Subject.doAs(sub, action);
} catch (Exception e) {
throw new PSQLException(GT.tr("GSS Authentication failed"), PSQLState.CONNECTION_FAILURE, e);
}
if (result instanceof IOException) {
throw (IOException) result;
} else if (result instanceof SQLException) {
throw (SQLException) result;
} else if (result != null) {
throw new PSQLException(GT.tr("GSS Authentication failed"), PSQLState.CONNECTION_FAILURE,
result);
}
}
}