Example usage for org.apache.hadoop.security.authentication.client Authenticator authenticate

List of usage examples for org.apache.hadoop.security.authentication.client Authenticator authenticate

Introduction

In this page you can find the example usage for org.apache.hadoop.security.authentication.client Authenticator authenticate.

Prototype

public void authenticate(URL url, AuthenticatedURL.Token token) throws IOException, AuthenticationException;

Source Link

Document

Authenticates against a URL and returns a AuthenticatedURL.Token to be used by subsequent requests.

Usage

From source file:com.bigstep.datalake.KerberosIdentityAuthenticator.java

License:Apache License

/**
 * Performs SPNEGO authentication against the specified URL.
 * <p>//from  w  w  w  .  j a  va 2  s  . co  m
 * If a token is given it does a NOP and returns the given token.
 * <p>
 * If no token is given, it will perform the SPNEGO authentication sequence using an
 * HTTP <code>OPTIONS</code> request.
 *
 * @param url   the URl to authenticate against.
 * @param token the authentication token being used for the user.
 * @throws IOException             if an IO error occurred.
 * @throws AuthenticationException if an authentication error occurred.
 */
@Override
public void authenticate(URL url, AuthenticatedURL.Token token) throws IOException, AuthenticationException {
    if (!token.isSet()) {
        this.url = url;
        base64 = new Base64(0);
        conn = (HttpURLConnection) url.openConnection();
        if (connConfigurator != null) {
            conn = connConfigurator.configure(conn);
        }
        conn.setRequestMethod(AUTH_HTTP_METHOD);
        conn.connect();

        boolean needFallback = false;
        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            LOG.debug("JDK performed authentication on our behalf.");
            // If the JDK already did the SPNEGO back-and-forth for
            // us, just pull out the token.
            AuthenticatedURL.extractToken(conn, token);
            if (isTokenKerberos(token)) {
                return;
            }
            needFallback = true;
        }
        if (!needFallback && isNegotiate()) {
            LOG.debug("Performing our own SPNEGO sequence.");
            doSpnegoSequence(token);
        } else {
            LOG.debug("Using fallback authenticator sequence.");
            Authenticator auth = getFallBackAuthenticator();
            // Make sure that the fall back authenticator have the same
            // ConnectionConfigurator, since the method might be overridden.
            // Otherwise the fall back authenticator might not have the information
            // to make the connection (e.g., SSL certificates)
            auth.setConnectionConfigurator(connConfigurator);
            auth.authenticate(url, token);
        }
    }
}