From 64ccd14614182c517995a88b888f0d7b7212f668 Mon Sep 17 00:00:00 2001 From: Christopher Vogt Date: Mon, 6 Mar 2017 20:07:57 -0500 Subject: support publishing to basic-auth protected maven repositories --- stage1/Stage1Lib.scala | 14 +++++++++++--- stage1/cbt.scala | 2 +- stage2/Lib.scala | 10 +++------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/stage1/Stage1Lib.scala b/stage1/Stage1Lib.scala index 87d3a79..91e63f1 100644 --- a/stage1/Stage1Lib.scala +++ b/stage1/Stage1Lib.scala @@ -56,18 +56,26 @@ class Stage1Lib( logger: Logger ) extends BaseLib{ def write(file: File, content: String, options: OpenOption*): File = Stage0Lib.write(file, content, options:_*) + def addHttpCredentials( connection: HttpURLConnection, credentials: String ): Unit = { + val encoding = new sun.misc.BASE64Encoder().encode(credentials.getBytes) + connection.setRequestProperty("Authorization", "Basic " ++ encoding) + } + def download(url: URL, target: File, sha1: Option[String], replace: Boolean = false): Boolean = { if( target.exists && !replace ){ - logger.resolver(green("found ") ++ url.string) + logger.resolver(green("found ") ++ url.show) true } else { val incomplete = ( target ++ ".incomplete" ).toPath; val connection = Stage0Lib.openConnectionConsideringProxy(url) + Option(url.getUserInfo).filter(_ != "").foreach( + addHttpCredentials(connection,_) + ) if(connection.getResponseCode != HttpURLConnection.HTTP_OK){ - logger.resolver(blue("not found: ") ++ url.string) + logger.resolver(blue("not found: ") ++ url.show) false } else { - System.err.println(blue("downloading ") ++ url.string) + System.err.println(blue("downloading ") ++ url.show) logger.resolver(blue("to ") ++ target.string) target.getParentFile.mkdirs val stream = connection.getInputStream diff --git a/stage1/cbt.scala b/stage1/cbt.scala index d28789c..0305dd2 100644 --- a/stage1/cbt.scala +++ b/stage1/cbt.scala @@ -65,7 +65,7 @@ object `package`{ } implicit class URLExtensionMethods( url: URL ){ def ++( s: String ): URL = new URL( url.toString ++ s ) - def string = url.toString + def show = "/[^/@]+@".r.replaceFirstIn( url.toString, "/" ) // remove credentials when showing url for security reasons } implicit class SeqExtensions[T](seq: Seq[T]){ def maxOption(implicit ev: Ordering[T]): Option[T] = try{ Some(seq.max) } catch { diff --git a/stage2/Lib.scala b/stage2/Lib.scala index 8646f8c..c95ee2a 100644 --- a/stage2/Lib.scala +++ b/stage2/Lib.scala @@ -148,6 +148,7 @@ final class Lib(val logger: Logger) extends Stage1Lib(logger){ obj match { case Some(s) => render(s) case None => "" + case url: URL => url.show // to remove credentials case d: Dependency => lib.usage(d.getClass, d.show()) case c: ClassPath => c.string case ExitCode(int) => System.err.println(int); System.exit(int); ??? @@ -462,17 +463,12 @@ final class Lib(val logger: Logger) extends Stage1Lib(logger){ import java.net._ import java.io._ val url = baseUrl ++ "/" ++ fileName - System.err.println(blue("uploading ") ++ url.toString) + System.err.println(blue("uploading ") ++ url.show) val httpCon = Stage0Lib.openConnectionConsideringProxy(url) try{ httpCon.setDoOutput(true) httpCon.setRequestMethod("PUT") - credentials.foreach( - c => { - val encoding = new sun.misc.BASE64Encoder().encode(c.getBytes) - httpCon.setRequestProperty("Authorization", "Basic " ++ encoding) - } - ) + (credentials orElse Option(baseUrl.getUserInfo)).foreach(addHttpCredentials(httpCon,_)) httpCon.setRequestProperty("Content-Type", "application/binary") httpCon.getOutputStream.write( fileContents -- cgit v1.2.3