Example usage for org.apache.http.auth AuthState getAuthScheme

List of usage examples for org.apache.http.auth AuthState getAuthScheme

Introduction

In this page you can find the example usage for org.apache.http.auth AuthState getAuthScheme.

Prototype

public AuthScheme getAuthScheme() 

Source Link

Document

Returns actual AuthScheme .

Usage

From source file:dtu.ds.warnme.app.ws.client.https.ssl.PreemptiveAuthenticationRequestInterceptor.java

@Override
public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        Credentials creds = credsProvider.getCredentials(authScope);
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }/*w  w  w .ja  v  a  2 s  . com*/
    }
}

From source file:cn.com.loopj.android.http.PreemptiveAuthorizationHttpRequestInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        Credentials creds = credsProvider.getCredentials(authScope);
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }/*w w w.ja v a 2s  .  c o  m*/
    }
}

From source file:org.gbif.utils.PreemptiveAuthenticationInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    LOG.debug(request.getRequestLine().toString());

    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    // If not auth scheme has been initialized yet
    if (authState.getAuthScheme() == null && credsProvider != null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        // Obtain credentials matching the target host
        Credentials creds = credsProvider.getCredentials(authScope);
        // If found, generate BasicScheme preemptively
        if (creds != null) {
            LOG.debug("Authentication used for scope " + authScope.getHost());
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }/*www. j  a  va2s .c  o  m*/
    }
}

From source file:org.orbeon.oxf.resources.handler.PreemptiveAuthHttpRequestInterceptor.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    CredentialsProvider credentialsProvider = (CredentialsProvider) context
            .getAttribute(ClientContext.CREDS_PROVIDER);
    HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

    // If not auth scheme has been initialized yet
    if (authState.getAuthScheme() == null) {
        AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        // Obtain credentials matching the target host
        Credentials credentials = credentialsProvider.getCredentials(authScope);
        // If found, generate BasicScheme preemptively
        if (credentials != null) {
            authState.setAuthScheme(credentials instanceof NTCredentials ? new NTLMScheme(new JCIFSEngine())
                    : new BasicScheme());
            authState.setCredentials(credentials);
        }//ww  w .j a  va  2 s . co  m
    }
}

From source file:com.olacabs.fabric.processors.httpwriter.HttpWriter.java

private void setAuth(AuthConfiguration authConfiguration, HttpClientBuilder builder)
        throws InitializationException {
    if (!Strings.isNullOrEmpty(authConfiguration.getUsername())) {
        Credentials credentials = new UsernamePasswordCredentials(authConfiguration.getUsername(),
                authConfiguration.getPassword());
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), credentials);

        builder.addInterceptorFirst((HttpRequestInterceptor) (request, context) -> {
            AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
            if (authState.getAuthScheme() == null) {
                //log.debug("SETTING CREDS");
                //log.info("Preemptive AuthState: {}", authState);
                authState.update(new BasicScheme(), credentials);
            }/*from  w w w . ja  va2  s .  c  o m*/
        });

    } else {
        log.error("Username can't be blank for basic auth.");
        throw new InitializationException("Username blank for basic auth");
    }
}

From source file:de.escidoc.core.http.PreemtiveAuthHttpRequestInterceptor.java

@Override
public void process(final HttpRequest httpRequest, final HttpContext httpContext)
        throws HttpException, IOException {
    final AuthState authState = (AuthState) httpContext.getAttribute(ClientContext.TARGET_AUTH_STATE);
    final CredentialsProvider credsProvider = (CredentialsProvider) httpContext
            .getAttribute(ClientContext.CREDS_PROVIDER);
    final HttpHost targetHost = (HttpHost) httpContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    // If not auth scheme has been initialized yet
    if (authState.getAuthScheme() == null) {
        final AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
        // Obtain credentials matching the target host
        final Credentials creds = credsProvider.getCredentials(authScope);
        // If found, generate BasicScheme preemptively
        if (creds != null) {
            authState.setAuthScheme(new BasicScheme());
            authState.setCredentials(creds);
        }//from ww  w . ja v  a 2  s . c om
    }
}

From source file:org.callimachusproject.client.HttpAuthenticator.java

private void generateAuthResponse(final HttpRequest request, final AuthState authState,
        final HttpContext context) throws HttpException, IOException {
    AuthScheme authScheme = authState.getAuthScheme();
    Credentials creds = authState.getCredentials();
    switch (authState.getState()) {
    case FAILURE:
        return;/*from  w w  w .ja  va2 s  . c  om*/
    case SUCCESS:
        ensureAuthScheme(authScheme);
        if (authScheme.isConnectionBased()) {
            return;
        }
        break;
    case CHALLENGED:
        final Queue<AuthOption> authOptions = authState.getAuthOptions();
        if (authOptions != null) {
            while (!authOptions.isEmpty()) {
                final AuthOption authOption = authOptions.remove();
                authScheme = authOption.getAuthScheme();
                creds = authOption.getCredentials();
                authState.update(authScheme, creds);
                if (this.log.isDebugEnabled()) {
                    this.log.debug("Generating response to an authentication challenge using "
                            + authScheme.getSchemeName() + " scheme");
                }
                try {
                    final Header header = doAuth(authScheme, creds, request, context);
                    request.addHeader(header);
                    break;
                } catch (final AuthenticationException ex) {
                    if (this.log.isWarnEnabled()) {
                        this.log.warn(authScheme + " authentication error: " + ex.getMessage());
                    }
                }
            }
            return;
        } else {
            ensureAuthScheme(authScheme);
        }
    }
    if (authScheme != null) {
        try {
            final Header header = doAuth(authScheme, creds, request, context);
            request.addHeader(header);
        } catch (final AuthenticationException ex) {
            if (this.log.isErrorEnabled()) {
                this.log.error(authScheme + " authentication error: " + ex.getMessage());
            }
        }
    }
}

From source file:com.joyent.http.signature.apache.httpclient.HttpSignatureAuthenticationStrategy.java

/**
 * Determines if the given HTTP response response represents
 * an authentication challenge that was sent back as a result
 * of authentication failure.// w w w  .  jav a 2s . c  o  m
 *
 * @param authHost authentication host.
 * @param response HTTP response.
 * @param context  HTTP context.
 * @return {@code true} if user authentication is required,
 *                      {@code false} otherwise.
 */
@Override
public boolean isAuthenticationRequested(final HttpHost authHost, final HttpResponse response,
        final HttpContext context) {
    final StatusLine line = response.getStatusLine();
    final int code = line.getStatusCode();
    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    final AuthState authState = clientContext.getTargetAuthState();
    final AuthProtocolState authProtocolState = authState.getState();

    if (code == HttpStatus.SC_UNAUTHORIZED) {
        if (authProtocolState.equals(AuthProtocolState.CHALLENGED)) {
            clientContext.getTargetAuthState().setState(AuthProtocolState.FAILURE);
            authFailed(authHost, authState.getAuthScheme(), context);
        }

        return true;
    }

    if (clientContext.getTargetAuthState() == null) {
        return true;
    }

    return false;
}

From source file:org.dataconservancy.archive.impl.fcrepo.ri.MultiThreadedHttpClient.java

public MultiThreadedHttpClient(HttpClientConfig config) {
    this.config = config;
    if (config.getPreemptiveAuthN()) {
        HttpRequestInterceptor interceptor = new HttpRequestInterceptor() {
            @Override/*  w  ww. j a  va  2 s .com*/
            public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
                AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
                CredentialsProvider credsProvider = (CredentialsProvider) context
                        .getAttribute(ClientContext.CREDS_PROVIDER);
                HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
                // If no auth scheme has been initialized yet
                if (authState.getAuthScheme() == null) {
                    AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                    // Obtain credentials matching the target host
                    Credentials creds = credsProvider.getCredentials(authScope);
                    // If found, generate BasicScheme preemptively
                    if (creds != null) {
                        authState.setAuthScheme(new BasicScheme());
                        authState.setCredentials(creds);
                    }
                }
            }

        };
        addRequestInterceptor(interceptor, 0);
    }

}

From source file:com.rightscale.provider.rest.DashboardSession.java

private HttpRequestInterceptor createBasicAuthInterceptor() {
    return new HttpRequestInterceptor() {
        public void process(final HttpRequest request, final HttpContext context)
                throws HttpException, IOException {
            AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
            CredentialsProvider credsProvider = (CredentialsProvider) context
                    .getAttribute(ClientContext.CREDS_PROVIDER);
            HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

            if (authState.getAuthScheme() == null) {
                AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                Credentials creds = credsProvider.getCredentials(authScope);
                if (creds != null) {
                    authState.setAuthScheme(new BasicScheme());
                    authState.setCredentials(creds);
                }/*w  w  w  .j av a2 s.co  m*/
            }
        }
    };
}