Example usage for org.apache.http.auth Credentials getClass

List of usage examples for org.apache.http.auth Credentials getClass

Introduction

In this page you can find the example usage for org.apache.http.auth Credentials getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.datatorrent.stram.util.WebServicesClientTest.java

public static void checkUserCredentials(String username, String password, AuthScheme authScheme)
        throws NoSuchFieldException, IllegalAccessException {
    CredentialsProvider provider = getCredentialsProvider();
    String httpScheme = AuthScope.ANY_SCHEME;
    if (authScheme == AuthScheme.BASIC) {
        httpScheme = AuthSchemes.BASIC;/*  ww w . jav a  2s  . c  o  m*/
    } else if (authScheme == AuthScheme.DIGEST) {
        httpScheme = AuthSchemes.DIGEST;
    }
    AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM,
            httpScheme);
    Credentials credentials = provider.getCredentials(authScope);
    Assert.assertNotNull("Credentials", credentials);
    Assert.assertTrue("Credentials type is user",
            UsernamePasswordCredentials.class.isAssignableFrom(credentials.getClass()));
    UsernamePasswordCredentials pwdCredentials = (UsernamePasswordCredentials) credentials;
    Assert.assertEquals("Username", username, pwdCredentials.getUserName());
    Assert.assertEquals("Password", password, pwdCredentials.getPassword());
}

From source file:com.androidquery.test.NTLMScheme.java

public Header authenticate(final Credentials credentials, final HttpRequest request)
        throws AuthenticationException {
    NTCredentials ntcredentials = null;/*from   w ww .j av a  2 s . co m*/
    try {
        ntcredentials = (NTCredentials) credentials;
    } catch (ClassCastException e) {
        throw new InvalidCredentialsException(
                "Credentials cannot be used for NTLM authentication: " + credentials.getClass().getName());
    }
    String response = null;
    if (this.state == State.CHALLENGE_RECEIVED || this.state == State.FAILED) {
        response = this.engine.generateType1Msg(ntcredentials.getDomain(), ntcredentials.getWorkstation());
        this.state = State.MSG_TYPE1_GENERATED;
    } else if (this.state == State.MSG_TYPE2_RECEVIED) {
        response = this.engine.generateType3Msg(ntcredentials.getUserName(), ntcredentials.getPassword(),
                ntcredentials.getDomain(), ntcredentials.getWorkstation(), this.challenge);
        this.state = State.MSG_TYPE3_GENERATED;
    } else {
        throw new AuthenticationException("Unexpected state: " + this.state);
    }
    CharArrayBuffer buffer = new CharArrayBuffer(32);
    if (isProxy()) {
        buffer.append(AUTH.PROXY_AUTH_RESP);
    } else {
        buffer.append(AUTH.WWW_AUTH_RESP);
    }
    buffer.append(": NTLM ");
    buffer.append(response);
    return new BufferedHeader(buffer);
}

From source file:org.hawk.service.emf.impl.HawkResourceImpl.java

protected void subscribeToChanges(final HawkModelDescriptor descriptor, Credentials lazyCreds)
        throws HawkInstanceNotFound, HawkInstanceNotRunning, TException, Exception, IllegalAccessException,
        InvocationTargetException, NoSuchMethodException, ActiveMQException {
    final SubscriptionDurability sd = descriptor.getSubscriptionDurability();

    final Subscription subscription = client.watchModelChanges(descriptor.getHawkInstance(),
            descriptor.getHawkRepository(), Arrays.asList(descriptor.getHawkFilePatterns()),
            descriptor.getSubscriptionClientID(), sd);

    subscriber = APIUtils.connectToArtemis(subscription, sd);
    Principal fetchedUser = null;
    if (lazyCreds != null) {
        // If security is disabled for the Thrift API, we do not want to trigger the secure storage here either.
        // These methods in the LazyCredentials class do just that.
        fetchedUser = (Principal) lazyCreds.getClass().getMethod("getRawUserPrincipal").invoke(lazyCreds);
    }/*ww  w . j a  v a  2  s .  co  m*/
    if (fetchedUser != null) {
        final String fetchedPass = (String) lazyCreds.getClass().getMethod("getRawPassword").invoke(lazyCreds);
        subscriber.openSession(fetchedUser.getName(), fetchedPass);
    } else {
        subscriber.openSession(descriptor.getUsername(), descriptor.getPassword());
    }
    subscriber.processChangesAsync(
            new HawkResourceMessageHandler(descriptor.getThriftProtocol().getProtocolFactory()));
}

From source file:org.apache.http.impl.auth.win.WindowsNegotiateScheme.java

@Override
public Header authenticate(final Credentials credentials, final HttpRequest request, final HttpContext context)
        throws AuthenticationException {

    final String response;
    if (clientCred == null) {
        // ?? We don't use the credentials, should we allow anything?
        if (!(credentials instanceof CurrentWindowsCredentials)) {
            throw new InvalidCredentialsException("Credentials cannot be used for " + getSchemeName()
                    + " authentication: " + credentials.getClass().getName());
        }//  w ww .  j  a v  a2s.c o  m

        // client credentials handle
        try {
            final String username = CurrentWindowsCredentials.getCurrentUsername();
            final TimeStamp lifetime = new TimeStamp();

            clientCred = new CredHandle();
            final int rc = Secur32.INSTANCE.AcquireCredentialsHandle(username, scheme,
                    Sspi.SECPKG_CRED_OUTBOUND, null, null, null, null, clientCred, lifetime);

            if (WinError.SEC_E_OK != rc) {
                throw new Win32Exception(rc);
            }

            response = getToken(null, null, username);
        } catch (Throwable t) {
            dispose();
            throw new AuthenticationException("Authentication Failed", t);
        }
    } else if (this.challenge == null || this.challenge.length() == 0) {
        dispose();
        throw new AuthenticationException("Authentication Failed");
    } else {
        try {
            final byte[] continueTokenBytes = Base64.decodeBase64(this.challenge);
            final SecBufferDesc continueTokenBuffer = new SecBufferDesc(Sspi.SECBUFFER_TOKEN,
                    continueTokenBytes);
            response = getToken(this.sppicontext, continueTokenBuffer, "localhost");
        } catch (Throwable t) {
            dispose();
            throw new AuthenticationException("Authentication Failed", t);
        }
    }

    final CharArrayBuffer buffer = new CharArrayBuffer(scheme.length() + 30);
    if (isProxy()) {
        buffer.append(AUTH.PROXY_AUTH_RESP);
    } else {
        buffer.append(AUTH.WWW_AUTH_RESP);
    }
    buffer.append(": ");
    buffer.append(scheme); // NTLM or Negotiate
    buffer.append(" ");
    buffer.append(response);
    return new BufferedHeader(buffer);
}