mirror of https://github.com/apache/maven.git
add basic auth support
This commit is contained in:
parent
12892d3995
commit
d395ca47f3
|
@ -16,16 +16,9 @@
|
|||
|
||||
package org.apache.maven.wrapper;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Authenticator;
|
||||
import java.net.PasswordAuthentication;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.*;
|
||||
|
||||
/**
|
||||
* @author Hans Dockter
|
||||
|
@ -79,6 +72,7 @@ public class DefaultDownloader implements Downloader {
|
|||
URL url = address.toURL();
|
||||
out = new BufferedOutputStream(new FileOutputStream(destination));
|
||||
conn = url.openConnection();
|
||||
addBasicAuthentication(address, conn);
|
||||
final String userAgentValue = calculateUserAgent();
|
||||
conn.setRequestProperty("User-Agent", userAgentValue);
|
||||
in = conn.getInputStream();
|
||||
|
@ -104,6 +98,54 @@ public class DefaultDownloader implements Downloader {
|
|||
}
|
||||
}
|
||||
|
||||
private void addBasicAuthentication(URI address, URLConnection connection) throws IOException {
|
||||
String userInfo = calculateUserInfo(address);
|
||||
if (userInfo == null) {
|
||||
return;
|
||||
}
|
||||
if (!"https".equals(address.getScheme())) {
|
||||
Logger.info("WARNING Using HTTP Basic Authentication over an insecure connection to download the Gradle distribution. Please consider using HTTPS.");
|
||||
}
|
||||
connection.setRequestProperty("Authorization", "Basic " + base64Encode(userInfo));
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64 encode user info for HTTP Basic Authentication.
|
||||
*
|
||||
* Try to use {@literal java.util.Base64} encoder which is available starting with Java 8.
|
||||
* Fallback to {@literal javax.xml.bind.DatatypeConverter} from JAXB which is available starting with Java 6 but is not anymore in Java 9.
|
||||
* Fortunately, both of these two Base64 encoders implement the right Base64 flavor, the one that does not split the output in multiple lines.
|
||||
*
|
||||
* @param userInfo user info
|
||||
* @return Base64 encoded user info
|
||||
* @throws RuntimeException if no public Base64 encoder is available on this JVM
|
||||
*/
|
||||
private String base64Encode(String userInfo) {
|
||||
ClassLoader loader = getClass().getClassLoader();
|
||||
try {
|
||||
Method getEncoderMethod = loader.loadClass("java.util.Base64").getMethod("getEncoder");
|
||||
Method encodeMethod = loader.loadClass("java.util.Base64$Encoder").getMethod("encodeToString", byte[].class);
|
||||
Object encoder = getEncoderMethod.invoke(null);
|
||||
return (String) encodeMethod.invoke(encoder, new Object[]{userInfo.getBytes("UTF-8")});
|
||||
} catch (Exception java7OrEarlier) {
|
||||
try {
|
||||
Method encodeMethod = loader.loadClass("javax.xml.bind.DatatypeConverter").getMethod("printBase64Binary", byte[].class);
|
||||
return (String) encodeMethod.invoke(null, new Object[]{userInfo.getBytes("UTF-8")});
|
||||
} catch (Exception java5OrEarlier) {
|
||||
throw new RuntimeException("Downloading Gradle distributions with HTTP Basic Authentication is not supported on your JVM.", java5OrEarlier);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String calculateUserInfo(URI uri) {
|
||||
String username = System.getProperty("maven.wrapperUser");
|
||||
String password = System.getProperty("maven.wrapperPassword");
|
||||
if (username != null && password != null) {
|
||||
return username + ':' + password;
|
||||
}
|
||||
return uri.getUserInfo();
|
||||
}
|
||||
|
||||
private String calculateUserAgent() {
|
||||
String appVersion = applicationVersion;
|
||||
|
||||
|
|
Loading…
Reference in New Issue