From c4fed700bf1ee86e34897831824a29b4e12c4c7f Mon Sep 17 00:00:00 2001 From: Christopher Vogt Date: Sun, 14 Aug 2016 21:19:00 -0400 Subject: Some cosmetic changes to git auth --- stage2/GitDependency.scala | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'stage2') diff --git a/stage2/GitDependency.scala b/stage2/GitDependency.scala index 6519012..8faabc5 100644 --- a/stage2/GitDependency.scala +++ b/stage2/GitDependency.scala @@ -1,5 +1,6 @@ package cbt import java.io._ +import java.nio.file.Files.readAllBytes import java.net._ import org.eclipse.jgit.api._ import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider @@ -17,38 +18,38 @@ case class GitDependency( // TODO: add support for authentication via ssh and/or https // See http://www.codeaffine.com/2014/12/09/jgit-authentication/ private val GitUrl( _, domain, path ) = url - case class GitCredentials( - username: String, - password: String - ) + + private val credentialsFile = context.projectDirectory ++ "/git.login" private object checkoutCache extends Cache[File] def checkout: File = checkoutCache{ - def authenticate(creds: Option[GitCredentials], git: CloneCommand): CloneCommand = creds match { - case Some(credentials) => git.setCredentialsProvider(new UsernamePasswordCredentialsProvider(credentials.username, credentials.password)) - case None => git - } val checkoutDirectory = context.cache ++ s"/git/$domain/$path/$ref" if(checkoutDirectory.exists){ logger.git(s"Found existing checkout of $url#$ref in $checkoutDirectory") } else { logger.git(s"Cloning $url into $checkoutDirectory") - val credentials = { - try { - val credentials = scala.io.Source.fromFile(context.projectDirectory + "/git.login").mkString.split("\n") - Some(GitCredentials(credentials(0), credentials(1))) - } catch { - case e: FileNotFoundException => None - } - } - val git = authenticate(credentials, Git.cloneRepository().setURI(url)) + val git = { + val _git = Git + .cloneRepository() + .setURI(url) .setDirectory(checkoutDirectory) - .call() + + if(!credentialsFile.exists){ + _git + } else { + val (user, password) = { + // TODO: implement safer method than reading credentials from plain text file + val c = new String(readAllBytes(credentialsFile.toPath)).split("\n").head.trim.split(":") + (c(0), c.drop(1).mkString(":")) + } + _git.setCredentialsProvider( new UsernamePasswordCredentialsProvider(user, password) ) + } + }.call() logger.git(s"Checking out ref $ref") git.checkout() - .setName(ref) - .call() + .setName(ref) + .call() } checkoutDirectory } -- cgit v1.2.3