-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support SONATYPE_TOKEN env var for token auth
- Loading branch information
Showing
3 changed files
with
65 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/scala/xerial/sbt/sonatype/EnvironmentCredentials.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package xerial.sbt.sonatype | ||
|
||
final class UsernameAndPassword(val username: String, val password: String) | ||
|
||
object UsernameAndPassword { | ||
def fromToken(token: String): UsernameAndPassword = token.split(':') match { | ||
case Array(user, pass) => new UsernameAndPassword(user, pass) | ||
} | ||
} | ||
|
||
object EnvironmentCredentials { | ||
val Username = "SONATYPE_USERNAME" | ||
val Password = "SONATYPE_PASSWORD" | ||
val Token = "SONATYPE_TOKEN" | ||
|
||
def getUsernameAndPassword(envVars: Map[String, String] = sys.env): Option[UsernameAndPassword] = | ||
envVars.get(Token).map(UsernameAndPassword.fromToken).orElse { | ||
for { | ||
u <- envVars.get(Username) | ||
p <- envVars.get(Password) | ||
} yield new UsernameAndPassword(u, p) | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/test/scala/xerial/sbt/sonatype/EnvironmentCredentialsTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package xerial.sbt.sonatype | ||
|
||
import wvlet.airspec.AirSpec | ||
|
||
class EnvironmentCredentialsTest extends AirSpec { | ||
test("Extract a username & password from a SONATYPE_TOKEN environment variable") { | ||
val usernameAndPassword = EnvironmentCredentials.getUsernameAndPassword(Map( | ||
"SONATYPE_TOKEN" -> "user:pass" | ||
)).get | ||
|
||
usernameAndPassword.username shouldBe "user" | ||
usernameAndPassword.password shouldBe "pass" | ||
} | ||
|
||
test("Extract a legacy Sonatype Username & Password from environment variables") { | ||
val usernameAndPassword = EnvironmentCredentials.getUsernameAndPassword(Map( | ||
"SONATYPE_USERNAME" -> "user", | ||
"SONATYPE_PASSWORD" -> "pass" | ||
)).get | ||
|
||
usernameAndPassword.username shouldBe "user" | ||
usernameAndPassword.password shouldBe "pass" | ||
} | ||
|
||
test("Prefer a Sonatype token over a legacy Sonatype Username & Password") { | ||
val usernameAndPassword = EnvironmentCredentials.getUsernameAndPassword(Map( | ||
"SONATYPE_TOKEN" -> "newUser:newPass", | ||
"SONATYPE_USERNAME" -> "oldUser", | ||
"SONATYPE_PASSWORD" -> "oldPass" | ||
)).get | ||
|
||
usernameAndPassword.username shouldBe "newUser" | ||
usernameAndPassword.password shouldBe "newPass" | ||
} | ||
} |