Skip to content

Commit

Permalink
Use JCIFS for NTLM authentication
Browse files Browse the repository at this point in the history
 - Added JCIFS to NOTICE and LICENSE files
  • Loading branch information
bigdaz committed Feb 7, 2012
1 parent 8bb4a15 commit 08fbe04
Show file tree
Hide file tree
Showing 8 changed files with 584 additions and 4 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Expand Up @@ -86,6 +86,7 @@ libraries.commons_httpclient = dependencies.module('org.apache.httpcomponents:ht
dependency "org.apache.httpcomponents:httpcore:4.1.2@jar"
dependency libraries.jcl_to_slf4j
dependency "commons-codec:commons-codec:1.4@jar"
dependency "org.samba.jcifs:jcifs:1.3.17"
}

libraries.maven_ant_tasks = dependencies.module("org.apache.maven:maven-ant-tasks:2.1.3") {
Expand Down Expand Up @@ -133,6 +134,9 @@ allprojects {
ivy {
url 'http://repo.jfrog.org/artifactory/gradle-wharf-local'
}
maven {
url 'http://repository.amdatu.org/maven2'
}
}

configurations {
Expand Down
508 changes: 508 additions & 0 deletions src/toplevel/LICENSE

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/toplevel/NOTICE
Expand Up @@ -13,6 +13,7 @@ Groovy (http://groovy.codehaus.org)
Logback (http://logback.qos.ch)
SLF4J (http://www.slf4j.org)
Junit (http://www.junit.org)
JCIFS (http://jcifs.samba.org)

For licenses see the LICENSE file.

Expand Down
Expand Up @@ -28,6 +28,8 @@
import org.apache.http.impl.conn.ProxySelectorRoutePlanner;
import org.apache.http.protocol.HttpContext;
import org.gradle.api.artifacts.repositories.PasswordCredentials;
import org.gradle.api.internal.artifacts.repositories.transport.http.ntlm.NTLMCredentials;
import org.gradle.api.internal.artifacts.repositories.transport.http.ntlm.NTLMSchemeFactory;
import org.gradle.internal.UncheckedException;
import org.gradle.util.GUtil;
import org.gradle.util.GradleVersion;
Expand Down Expand Up @@ -56,6 +58,7 @@ private UsernamePasswordCredentials createRepositoryCredentials(PasswordCredenti
}

public void configure(DefaultHttpClient httpClient) {
NTLMSchemeFactory.register(httpClient);
configureCredentials(httpClient, httpSettings.getCredentials());
configureProxy(httpClient, httpSettings.getProxySettings());
configureRetryHandler(httpClient);
Expand Down
Expand Up @@ -13,14 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.internal.artifacts.repositories.transport.http;
package org.gradle.api.internal.artifacts.repositories.transport.http.ntlm;

import org.gradle.api.artifacts.repositories.PasswordCredentials;

import java.net.InetAddress;
import java.net.UnknownHostException;

class NTLMCredentials {
public class NTLMCredentials {
private static final String DEFAULT_DOMAIN = "";
private static final String DEFAULT_WORKSTATION = "";
private final String domain;
Expand Down
@@ -0,0 +1,64 @@
/*
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.internal.artifacts.repositories.transport.http.ntlm;

import jcifs.ntlmssp.Type1Message;
import jcifs.ntlmssp.Type2Message;
import jcifs.ntlmssp.Type3Message;
import jcifs.util.Base64;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthSchemeFactory;
import org.apache.http.impl.auth.NTLMEngine;
import org.apache.http.impl.auth.NTLMEngineException;
import org.apache.http.impl.auth.NTLMScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;

import java.io.IOException;

// Copied from http://hc.apache.org/httpcomponents-client-ga/ntlm.html
public class NTLMSchemeFactory implements AuthSchemeFactory {

public static void register(DefaultHttpClient httpClient) {
httpClient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
}

public AuthScheme newInstance(HttpParams params) {
return new NTLMScheme(new JCIFSEngine());
}

private static class JCIFSEngine implements NTLMEngine {

public String generateType1Msg(String domain, String workstation) throws NTLMEngineException {
Type1Message type1Message = new Type1Message(Type1Message.getDefaultFlags(), domain, workstation);
return Base64.encode(type1Message.toByteArray());
}

public String generateType3Msg(String username, String password, String domain, String workstation, String challenge) throws NTLMEngineException {
Type2Message type2Message = decodeType2Message(challenge);
Type3Message type3Message = new Type3Message(type2Message, password, domain, username, workstation, Type3Message.getDefaultFlags());
return Base64.encode(type3Message.toByteArray());
}

private Type2Message decodeType2Message(String challenge) throws NTLMEngineException {
try {
return new Type2Message(Base64.decode(challenge));
} catch (final IOException exception) {
throw new NTLMEngineException("Invalid Type2 message", exception);
}
}
}
}
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.internal.artifacts.repositories.transport.http;
package org.gradle.api.internal.artifacts.repositories.transport.http.ntlm;


import org.gradle.api.artifacts.repositories.PasswordCredentials
Expand Down
2 changes: 1 addition & 1 deletion subprojects/docs/src/docs/userguide/thisAndThat.xml
Expand Up @@ -181,7 +181,7 @@ systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost
<para>If your proxy requires NTLM authentication, you may need to provide the authentication domain as well as the username and password.
There are 2 ways that you can provide the domain for authenticating to a NTLM proxy:
<itemizedlist>
<listitem>Set the <literal>http.proxyUser</literal> system property to a value like <literal>[domain]/[username]</literal>.
<listitem>Set the <literal>http.proxyUser</literal> system property to a value like <literal><replaceable>domain</replaceable>/<replaceable>username</replaceable></literal>.
</listitem>
<listitem>Provide the authentication domain via the <literal>http.auth.ntlm.domain</literal> system property.
</listitem>
Expand Down

0 comments on commit 08fbe04

Please sign in to comment.