try {
SPNegoCredentials spnegoCredentials;
try {
spnegoCredentials = (SPNegoCredentials) credentials;
} catch (ClassCastException e) {
throw new InvalidCredentialsException(
"Credentials cannot be used for SPNego authentication: " + credentials.getClass().getName());
}
GSSContext gssContext = spnegoCredentials.getGSSContext();
byte[] clientToken = gssContext.initSecContext(serverToken, 0, serverToken.length);
if (gssContext.isEstablished()) {
complete = true;
LOGGER.log(Level.INFO, "GSS Context established");
LOGGER.log(Level.INFO, "Caller is " + gssContext.getSrcName());
LOGGER.log(Level.INFO, "Server is " + gssContext.getTargName());
if (gssContext.getMutualAuthState()) {
LOGGER.log(Level.INFO, "Mutually authenticated");
}
}
String encodedToken = new String(BASE64_CODEC.encode(clientToken));
return new StringBuffer(TOKEN_PREFIX).append(' ').append(encodedToken).toString();
} catch (GSSException e) {
complete = true;
switch (e.getMajor()) {
case GSSException.CREDENTIALS_EXPIRED:
throw new InvalidCredentialsException(e.getMessage(), e);
case GSSException.NO_CRED:
throw new CredentialsNotAvailableException(e.getMessage(), e);
default:
String errorMessage = "Caught GSSException in GSSContext.initSecContext()";
LOGGER.log(Level.SEVERE, errorMessage, e);