Example usage for org.apache.commons.httpclient NTCredentials getHost

List of usage examples for org.apache.commons.httpclient NTCredentials getHost

Introduction

In this page you can find the example usage for org.apache.commons.httpclient NTCredentials getHost.

Prototype

public String getHost() 

Source Link

Usage

From source file:com.gargoylesoftware.htmlunit.DefaultCredentialsProviderTest.java

/**
 * Test for NTLM credentials.//from  ww w.j  ava  2s .c o m
 * @throws Exception if the test fails
 */
@Test
public void addNTLMCredentials() throws Exception {
    final String username = "foo";
    final String domain = "myDomain";
    final String password = "password";
    final String host = "my.host";
    final String clientHost = "client.host";
    final int port = 1234;

    final DefaultCredentialsProvider provider = new DefaultCredentialsProvider();
    provider.addNTLMCredentials(username, password, host, port, clientHost, domain);

    final NTLMScheme scheme = new NTLMScheme("NTLM");
    final Credentials credentials = provider.getCredentials(scheme, host, port, false);
    assertNotNull(credentials);
    assertTrue(NTCredentials.class.isInstance(credentials));

    final NTCredentials ntCredentials = (NTCredentials) credentials;
    assertEquals(username, ntCredentials.getUserName());
    assertEquals(password, ntCredentials.getPassword());
    assertEquals(clientHost, ntCredentials.getHost());
    assertEquals(domain, ntCredentials.getDomain());
}

From source file:it.greenvulcano.gvesb.http.auth.impl.NTLMHttpAuth.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override/*from   w w w  .jav a 2s  .c  o  m*/
public void setAuthentication(Options options, String host, GVBuffer gvBuffer, Map<String, Object> props)
        throws HttpAuthException {
    try {
        if (props == null) {
            props = GVBufferPropertiesHelper.getPropertiesMapSO(gvBuffer, true);
        }

        String currUser = PropertiesHandler.expand(userName, props, gvBuffer);
        String currPassword = XMLConfig.getDecrypted(PropertiesHandler.expand(password, props, gvBuffer));
        String currDomain = PropertiesHandler.expand(domain, props, gvBuffer);

        List ntlmPref = new ArrayList();
        ntlmPref.add(HttpTransportProperties.Authenticator.NTLM);

        NTCredentials creds = new NTCredentials(currUser, currPassword, host, currDomain);

        Authenticator ntlmAuth = new HttpTransportProperties.Authenticator();
        ntlmAuth.setUsername(creds.getUserName());
        ntlmAuth.setPassword(creds.getPassword());
        ntlmAuth.setDomain(creds.getDomain());
        ntlmAuth.setHost(creds.getHost());
        ntlmAuth.setAuthSchemes(ntlmPref);
        ntlmAuth.setPreemptiveAuthentication(true);

        options.setProperty(HTTPConstants.AUTHENTICATE, ntlmAuth);
    } catch (Exception exc) {
        throw new HttpAuthException("Error using NTLM authenticator", exc);
    }
}

From source file:it.greenvulcano.gvesb.http.ntlm.JCIFS_NTLMScheme.java

@Override
public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
    if (this.state == UNINITIATED) {
        throw new IllegalStateException("NTLM authentication process has not been initiated");
    }//from w w w .  j ava  2  s.  co  m

    NTCredentials ntcredentials = null;
    try {
        ntcredentials = (NTCredentials) credentials;
    } catch (ClassCastException e) {
        throw new InvalidCredentialsException(
                "Credentials cannot be used for NTLM authentication: " + credentials.getClass().getName());
    }

    NTLM ntlm = new NTLM();
    ntlm.setCredentialCharset(method.getParams().getCredentialCharset());

    String response = null;
    if ((this.state == INITIATED) || (this.state == FAILED)) {
        response = ntlm.generateType1Msg(ntcredentials.getHost(), ntcredentials.getDomain());
        this.state = TYPE1_MSG_GENERATED;
    } else {
        response = ntlm.generateType3Msg(ntcredentials.getUserName(), ntcredentials.getPassword(),
                ntcredentials.getHost(), ntcredentials.getDomain(), this.ntlmchallenge);
        this.state = TYPE3_MSG_GENERATED;
    }

    return "NTLM " + response;
}

From source file:com.mobilehelix.appserver.auth.JCIFS_NTLMScheme.java

@Override
public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {

    LOG.log(Level.FINEST, "Enter JCIFS_NTLMScheme.authenticate(Credentials, HttpMethod)");

    if (this.state == UNINITIATED) {
        throw new IllegalStateException("NTLM authentication process has not been initiated");
    }//from   www.ja v  a  2  s . c  om

    NTCredentials ntcredentials = null;
    try {
        ntcredentials = (NTCredentials) credentials;
    } catch (ClassCastException e) {
        throw new InvalidCredentialsException(
                "Credentials cannot be used for NTLM authentication: " + credentials.getClass().getName());

    }

    NTLM ntlm = new NTLM();
    ntlm.setCredentialCharset(method.getParams().getCredentialCharset());
    String response = null;
    if (this.state == INITIATED || this.state == FAILED) {
        response = ntlm.generateType1Msg(ntcredentials.getHost(), ntcredentials.getDomain());
        this.state = TYPE1_MSG_GENERATED;
    } else {
        response = ntlm.generateType3Msg(ntcredentials.getUserName(), ntcredentials.getPassword(),
                ntcredentials.getHost(), ntcredentials.getDomain(), this.ntlmchallenge);
        this.state = TYPE3_MSG_GENERATED;
    }

    return "NTLM " + response;
}

From source file:davmail.http.NTLMv2Scheme.java

/**
 * Produces NTLM authorization string for the given set of
 * {@link Credentials}.//  w ww.  j av a2s  .c o  m
 *
 * @param credentials The set of credentials to be used for authentication
 * @param httpMethod  The method being authenticated
 * @return an NTLM authorization string
 * @throws InvalidCredentialsException if authentication credentials
 *                                     are not valid or not applicable for this authentication scheme
 * @throws AuthenticationException     if authorization string cannot
 *                                     be generated due to an authentication failure
 */
public String authenticate(Credentials credentials, HttpMethod httpMethod) throws AuthenticationException {
    if (this.state == UNINITIATED) {
        throw new IllegalStateException("NTLM authentication process has not been initiated");
    }

    NTCredentials ntcredentials;
    try {
        ntcredentials = (NTCredentials) credentials;
    } catch (ClassCastException e) {
        throw new InvalidCredentialsException(
                "Credentials cannot be used for NTLM authentication: " + credentials.getClass().getName());
    }
    String response;
    if (this.state == INITIATED || this.state == FAILED) {
        int flags = NtlmFlags.NTLMSSP_NEGOTIATE_NTLM2 | NtlmFlags.NTLMSSP_NEGOTIATE_ALWAYS_SIGN
                | NtlmFlags.NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED
                | NtlmFlags.NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED | NtlmFlags.NTLMSSP_NEGOTIATE_NTLM
                | NtlmFlags.NTLMSSP_REQUEST_TARGET | NtlmFlags.NTLMSSP_NEGOTIATE_OEM
                | NtlmFlags.NTLMSSP_NEGOTIATE_UNICODE | NtlmFlags.NTLMSSP_NEGOTIATE_56
                | NtlmFlags.NTLMSSP_NEGOTIATE_128;
        Type1Message type1Message = new Type1Message(flags, ntcredentials.getDomain(), ntcredentials.getHost());
        response = EncodingUtil.getAsciiString(Base64.encodeBase64(type1Message.toByteArray()));
        this.state = TYPE1_MSG_GENERATED;
    } else {
        int flags = NtlmFlags.NTLMSSP_NEGOTIATE_NTLM2 | NtlmFlags.NTLMSSP_NEGOTIATE_ALWAYS_SIGN
                | NtlmFlags.NTLMSSP_NEGOTIATE_NTLM | NtlmFlags.NTLMSSP_NEGOTIATE_UNICODE;
        Type3Message type3Message = new Type3Message(type2Message, ntcredentials.getPassword(),
                ntcredentials.getDomain(), ntcredentials.getUserName(), ntcredentials.getHost(), flags);
        response = EncodingUtil.getAsciiString(Base64.encodeBase64(type3Message.toByteArray()));
        this.state = TYPE3_MSG_GENERATED;
    }
    return "NTLM " + response;
}

From source file:org.mule.transport.http.ntlm.NtlmMessageFactory.java

/**
 * Creates a {@link Type3Message} for NTLM authentication.
 *
 * @param ntCredentials the credentials used for the authentication
 * @param type2Message the {@link Type2Message} received from the server
 *        in response to a {@link Type1Message} message previously sent.
 * @return a {@link Type3Message} to continue the authentication process.
 *//* w  w  w .  j a va2 s .c om*/
public Type3Message createType3Message(NTCredentials ntCredentials, Type2Message type2Message) {
    return new Type3Message(type2Message, ntCredentials.getPassword(), type2Message.getTarget(),
            ntCredentials.getUserName(), ntCredentials.getHost(), DEFAULT_TYPE_3_MESSAGE_FLAGS);
}

From source file:org.wso2.carbon.mediator.ntlm.CustomNTLMAuthScheme.java

/**
 * Produces NTLM authorization string for the given set of
 * {@link Credentials}.//  w  w w .ja  v a2  s .co  m
 *
 * @param credentials
 *            The set of credentials to be used for athentication
 * @param method
 *            The method being authenticated
 *
 * @throws InvalidCredentialsException
 *             if authentication credentials are not valid or not applicable
 *             for this authentication scheme
 * @throws AuthenticationException
 *             if authorization string cannot be generated due to an
 *             authentication failure
 *
 * @return an NTLM authorization string
 *
 * @since 3.0
 */
public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
    LOG.trace("enter NTLMScheme.authenticate (Credentials, HttpMethod)");

    if (this.state == UNINITIATED) {
        throw new IllegalStateException("NTLM authentication process has not been initiated");
    }

    NTCredentials ntcredentials = null;
    try {
        ntcredentials = (NTCredentials) credentials;
    } catch (ClassCastException e) {
        throw new InvalidCredentialsException(
                "Credentials cannot be used for NTLM authentication: " + credentials.getClass().getName());
    }
    byte[] msgBytes = null;
    String response = null;
    if (this.state == INITIATED) {
        Type1Message msg = new Type1Message();
        // @see http://davenport.sourceforge.net/ntlm.html#theType1Message
        // dont' support Unicode
        // negotiate OEM
        // request authentication realm in Type2 response
        // not signed
        // not encrypted
        // not authenticated
        // no lan manager key
        // negotiate NTLM
        msg.setFlags(0x5206);
        msg.setSuppliedWorkstation(ntcredentials.getHost());
        msg.setSuppliedDomain(ntcredentials.getDomain());
        msgBytes = msg.toByteArray();
        this.state = TYPE1_MSG_GENERATED;
    } else if (this.state == TYPE2_MSG_RECEIVED) {
        byte[] msg2Bytes = Base64.decodeBase64(
                EncodingUtil.getBytes(this.ntlmChallenge, method.getParams().getCredentialCharset()));
        try {
            Type2Message msg2 = new Type2Message(msg2Bytes);
            int flags = Type3Message.NTLMSSP_NEGOTIATE_OEM | Type3Message.NTLMSSP_NEGOTIATE_LM_KEY;

            Type3Message msg3 = new Type3Message(msg2, ntcredentials.getPassword(), ntcredentials.getDomain(),
                    ntcredentials.getUserName(), ntcredentials.getHost(), flags);

            msgBytes = msg3.toByteArray();
        } catch (IOException ex) {
            throw new AuthenticationException("unable to parse Type2Message", ex);
        }
        this.state = TYPE3_MSG_GENERATED;
    } else {
        throw new RuntimeException("failed to authenticate");
    }
    response = EncodingUtil.getAsciiString(Base64.encodeBase64(msgBytes));
    return "NTLM " + response;
}

From source file:org.zaproxy.zap.network.ZapNTLMScheme.java

@Override
public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
    NTCredentials ntcredentials = null;
    try {// w  w w. j ava2  s .c om
        ntcredentials = (NTCredentials) credentials;
    } catch (final ClassCastException e) {
        throw new AuthenticationException(
                "Credentials cannot be used for NTLM authentication: " + credentials.getClass().getName());
    }
    String response = null;
    if (this.state == State.FAILED) {
        throw new AuthenticationException("NTLM authentication failed");
    } else if (this.state == State.CHALLENGE_RECEIVED) {
        response = this.engine.generateType1Msg(ntcredentials.getDomain(), ntcredentials.getHost());
        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.getHost(), this.challenge);
        this.state = State.MSG_TYPE3_GENERATED;
    } else {
        throw new AuthenticationException("Unexpected state: " + this.state);
    }
    return "NTLM " + response;
}