Search in sources :

Example 1 with Credentials

use of org.apache.http.auth.Credentials in project camel by apache.

the class PreemptiveAuthInterceptor method process.

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
    // If no auth scheme avaialble yet, try to initialize it preemptively
    if (authState.getAuthScheme() == null) {
        AuthScheme authScheme = (AuthScheme) context.getAttribute("preemptive-auth");
        CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
        if (authScheme != null) {
            Credentials creds = credsProvider.getCredentials(AuthScope.ANY);
            if (creds == null) {
                throw new HttpException("No credentials for preemptive authentication");
            }
            authState.update(authScheme, creds);
        }
    }
}
Also used : AuthState(org.apache.http.auth.AuthState) HttpException(org.apache.http.HttpException) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials) AuthScheme(org.apache.http.auth.AuthScheme)

Example 2 with Credentials

use of org.apache.http.auth.Credentials in project hadoop by apache.

the class AuthenticatorTestCase method getHttpClient.

private HttpClient getHttpClient() {
    HttpClientBuilder builder = HttpClientBuilder.create();
    // Register auth schema
    builder.setDefaultAuthSchemeRegistry(s -> httpContext -> new SPNegoScheme(true, true));
    Credentials useJaasCreds = new Credentials() {

        public String getPassword() {
            return null;
        }

        public Principal getUserPrincipal() {
            return null;
        }
    };
    CredentialsProvider jaasCredentialProvider = new BasicCredentialsProvider();
    jaasCredentialProvider.setCredentials(AuthScope.ANY, useJaasCreds);
    // Set credential provider
    builder.setDefaultCredentialsProvider(jaasCredentialProvider);
    return builder.build();
}
Also used : SPNegoScheme(org.apache.http.impl.auth.SPNegoScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) Credentials(org.apache.http.auth.Credentials)

Example 3 with Credentials

use of org.apache.http.auth.Credentials in project OpenMEAP by OpenMEAP.

the class OmSlicCredentialsProvider method getCredentials.

@Override
public Credentials getCredentials(final AuthScope authScope) {
    this.authScope = authScope;
    if (memory.containsKey(authScope)) {
        return memory.get(authScope);
    }
    synchronized (this) {
        try {
            launcher.launchLoginForm(this);
            wait();
        } catch (InterruptedException ie) {
            throw new RuntimeException(ie);
        }
    }
    if (username == null || password == null) {
        return super.getCredentials(authScope);
    }
    Credentials creds = new UsernamePasswordCredentials(username, password);
    if (remember == null || remember == false) {
        password = null;
        username = null;
    } else {
        memory.put(authScope, creds);
    }
    return creds;
}
Also used : UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Credentials(org.apache.http.auth.Credentials) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 4 with Credentials

use of org.apache.http.auth.Credentials in project robovm by robovm.

the class RequestProxyAuthentication method process.

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    if (request == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    if (context == null) {
        throw new IllegalArgumentException("HTTP context may not be null");
    }
    if (request.containsHeader(AUTH.PROXY_AUTH_RESP)) {
        return;
    }
    // Obtain authentication state
    AuthState authState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
    if (authState == null) {
        return;
    }
    AuthScheme authScheme = authState.getAuthScheme();
    if (authScheme == null) {
        return;
    }
    Credentials creds = authState.getCredentials();
    if (creds == null) {
        this.log.debug("User credentials not available");
        return;
    }
    if (authState.getAuthScope() != null || !authScheme.isConnectionBased()) {
        try {
            request.addHeader(authScheme.authenticate(creds, request));
        } catch (AuthenticationException ex) {
            if (this.log.isErrorEnabled()) {
                this.log.error("Proxy authentication error: " + ex.getMessage());
            }
        }
    }
}
Also used : AuthState(org.apache.http.auth.AuthState) AuthenticationException(org.apache.http.auth.AuthenticationException) Credentials(org.apache.http.auth.Credentials) AuthScheme(org.apache.http.auth.AuthScheme)

Example 5 with Credentials

use of org.apache.http.auth.Credentials in project robolectric by robolectric.

the class DefaultRequestDirector method updateAuthState.

private void updateAuthState(final AuthState authState, final HttpHost host, final CredentialsProvider credsProvider) {
    if (!authState.isValid()) {
        return;
    }
    String hostname = host.getHostName();
    int port = host.getPort();
    if (port < 0) {
        Scheme scheme = connManager.getSchemeRegistry().getScheme(host);
        port = scheme.getDefaultPort();
    }
    AuthScheme authScheme = authState.getAuthScheme();
    AuthScope authScope = new AuthScope(hostname, port, authScheme.getRealm(), authScheme.getSchemeName());
    if (this.log.isDebugEnabled()) {
        this.log.debug("Authentication scope: " + authScope);
    }
    Credentials creds = authState.getCredentials();
    if (creds == null) {
        creds = credsProvider.getCredentials(authScope);
        if (this.log.isDebugEnabled()) {
            if (creds != null) {
                this.log.debug("Found credentials");
            } else {
                this.log.debug("Credentials not found");
            }
        }
    } else {
        if (authScheme.isComplete()) {
            this.log.debug("Authentication failed");
            creds = null;
        }
    }
    authState.setAuthScope(authScope);
    authState.setCredentials(creds);
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) AuthScheme(org.apache.http.auth.AuthScheme) AuthScope(org.apache.http.auth.AuthScope) Credentials(org.apache.http.auth.Credentials) AuthScheme(org.apache.http.auth.AuthScheme)

Aggregations

Credentials (org.apache.http.auth.Credentials)39 AuthScope (org.apache.http.auth.AuthScope)22 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)16 AuthScheme (org.apache.http.auth.AuthScheme)15 CredentialsProvider (org.apache.http.client.CredentialsProvider)13 AuthState (org.apache.http.auth.AuthState)11 AuthenticationException (org.apache.http.auth.AuthenticationException)9 HttpHost (org.apache.http.HttpHost)8 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)7 NTCredentials (org.apache.http.auth.NTCredentials)5 IOException (java.io.IOException)4 Header (org.apache.http.Header)4 HttpException (org.apache.http.HttpException)4 HttpRequest (org.apache.http.HttpRequest)4 HttpResponse (org.apache.http.HttpResponse)4 Scheme (org.apache.http.conn.scheme.Scheme)4 BasicScheme (org.apache.http.impl.auth.BasicScheme)4 HttpEntity (org.apache.http.HttpEntity)3 AbortableHttpRequest (org.apache.http.client.methods.AbortableHttpRequest)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)3