Search in sources :

Example 1 with InvalidCredentialsException

use of org.apache.commons.httpclient.auth.InvalidCredentialsException in project wso2-synapse by wso2.

the class CustomNTLMAuthScheme method authenticate.

/**
 * Produces NTLM authorization string for the given set of
 * {@link Credentials}.
 *
 * @param credentials The set of credentials to be used for athentication
 * @param method      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
 * @since 3.0
 */
public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
    if (logger.isDebugEnabled()) {
        logger.debug("[CustomNTLMAuthScheme] NTLM Scheme Authentication Method Invoked.");
    }
    if (this.state == UNINITIATED) {
        throw new IllegalStateException("[CustomNTLMAuthScheme] NTLM authentication process has not been initiated");
    }
    // Get the NTLM version from the NTLMMediator and identify the flags to be used for authentication.
    String ntlmVersion = getNTLMVersion();
    if (logger.isDebugEnabled()) {
        logger.debug("[CustomNTLMAuthScheme] The NTLM version going to use is: " + ntlmVersion);
    }
    int flags = 0;
    if (ntlmVersion.toUpperCase().equals("V1")) {
        flags = NtlmFlags.NTLMSSP_NEGOTIATE_NTLM;
    } else if (ntlmVersion.toUpperCase().equals("V2")) {
        flags = NtlmFlags.NTLMSSP_NEGOTIATE_NTLM2;
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("[CustomNTLMAuthScheme] NTLM Version not specified.");
        }
    }
    NTCredentials ntcredentials = null;
    try {
        ntcredentials = (NTCredentials) credentials;
    } catch (ClassCastException e) {
        throw new InvalidCredentialsException("[CustomNTLMAuthScheme] Credentials cannot be used for NTLM authentication: " + credentials.getClass().getName());
    }
    byte[] msgBytes = null;
    String response = null;
    if (this.state == INITIATED) {
        Type1Message type1Message = new Type1Message(flags, ntcredentials.getDomain(), ntcredentials.getHost());
        msgBytes = type1Message.toByteArray();
        this.state = TYPE1_MSG_GENERATED;
        if (logger.isDebugEnabled()) {
            logger.debug("[CustomNTLMAuthScheme] Type1Message Generated.");
        }
    } else if (this.state == TYPE2_MSG_RECEIVED) {
        if (logger.isDebugEnabled()) {
            logger.debug("[CustomNTLMAuthScheme] Type2Message Received.");
        }
        Type2Message type2Message;
        try {
            type2Message = new jcifs.ntlmssp.Type2Message(jcifs.util.Base64.decode(this.ntlmChallenge));
        } catch (IOException e) {
            throw new RuntimeException("[CustomNTLMAuthScheme] Invalid Type2 message", e);
        }
        Type3Message type3Message = new Type3Message(type2Message, ntcredentials.getPassword(), ntcredentials.getDomain(), ntcredentials.getUserName(), ntcredentials.getHost(), flags);
        msgBytes = type3Message.toByteArray();
        this.state = TYPE3_MSG_GENERATED;
        if (logger.isDebugEnabled()) {
            logger.debug("[CustomNTLMAuthScheme] Type3Message Generated.");
        }
    } else {
        throw new RuntimeException("[CustomNTLMAuthScheme] Failed to Authenticate");
    }
    response = EncodingUtil.getAsciiString(Base64.encodeBase64(msgBytes));
    return "NTLM " + response;
}
Also used : IOException(java.io.IOException) Type2Message(jcifs.ntlmssp.Type2Message) Type3Message(jcifs.ntlmssp.Type3Message) NTCredentials(org.apache.commons.httpclient.NTCredentials) Type1Message(jcifs.ntlmssp.Type1Message) InvalidCredentialsException(org.apache.commons.httpclient.auth.InvalidCredentialsException)

Example 2 with InvalidCredentialsException

use of org.apache.commons.httpclient.auth.InvalidCredentialsException in project tdi-studio-se by Talend.

the class JCIFS_NTLMScheme method authenticate.

public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
    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());
    }
    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;
}
Also used : InvalidCredentialsException(org.apache.commons.httpclient.auth.InvalidCredentialsException) NTCredentials(org.apache.commons.httpclient.NTCredentials)

Aggregations

NTCredentials (org.apache.commons.httpclient.NTCredentials)2 InvalidCredentialsException (org.apache.commons.httpclient.auth.InvalidCredentialsException)2 IOException (java.io.IOException)1 Type1Message (jcifs.ntlmssp.Type1Message)1 Type2Message (jcifs.ntlmssp.Type2Message)1 Type3Message (jcifs.ntlmssp.Type3Message)1