Skip to content

Commit

Permalink
Fix username-only creds
Browse files Browse the repository at this point in the history
Username-only creds are useful for things like GitHub access tokens.
Prior to this commit, specifying only a username resulted in a null
password, which caused an ambiguous method call to the
UserNamePasswordCredentialsProvider constructor.

Now Credentials has been beefed up to fallback to empty strings for
values that aren't provided. There's also an explicit method for how to
check the validity of a Credentials object rather than needing to
specify it directly in each case.

Fixes #153
  • Loading branch information
ajoberstar committed Apr 9, 2017
1 parent 5b627b0 commit 13eb6c9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 26 deletions.
20 changes: 16 additions & 4 deletions src/main/groovy/org/ajoberstar/grgit/Credentials.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,26 @@
*/
package org.ajoberstar.grgit

import groovy.transform.Immutable
import groovy.transform.Canonical

/**
* Credentials to use for remote operations.
* @since 0.2.0
*/
@Immutable
@Canonical
class Credentials {
String username
String password
final String username
final String password

String getUsername() {
return username ?: ''
}

String getPassword() {
return password ?: ''
}

boolean isPopulated() {
return username != null
}
}
6 changes: 1 addition & 5 deletions src/main/groovy/org/ajoberstar/grgit/auth/AuthConfig.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,7 @@ class AuthConfig {
if (allows(Option.HARDCODED)) {
String username = props[USERNAME_OPTION] ?: env[USERNAME_ENV_VAR]
String password = props[PASSWORD_OPTION] ?: env[PASSWORD_ENV_VAR]
if (username) {
return new Credentials(username, password)
} else {
return null
}
return new Credentials(username, password)
} else {
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ final class TransportOpUtil {

private static CredentialsProvider determineCredentialsProvider(AuthConfig config, Credentials creds) {
Credentials systemCreds = config.hardcodedCreds
if (config.allows(AuthConfig.Option.HARDCODED) && creds?.username) {
if (config.allows(AuthConfig.Option.HARDCODED) && creds?.populated) {
logger.info('using hardcoded credentials provided programmatically')
return new UsernamePasswordCredentialsProvider(creds.username, creds.password)
} else if (config.allows(AuthConfig.Option.HARDCODED) && systemCreds) {
} else if (config.allows(AuthConfig.Option.HARDCODED) && systemCreds?.populated) {
logger.info('using hardcoded credentials from system properties')
return new UsernamePasswordCredentialsProvider(systemCreds.username, systemCreds.password)
} else if (config.allows(AuthConfig.Option.INTERACTIVE) && !GraphicsEnvironment.isHeadless()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ class AuthConfigSpec extends Specification {
AuthConfig.fromMap(props).getHardcodedCreds() == new Credentials('myuser', null)
}

def 'getHardcodedCreds returns null if username is not set'() {
def 'getHardcodedCreds are not populated if username is not set'() {
expect:
AuthConfig.fromMap([:]).getHardcodedCreds() == null
!AuthConfig.fromMap([:]).getHardcodedCreds().isPopulated()
}

def 'getSessionConfig returns empty map if nothing specified'() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class LinuxAuthenticationSpec extends Specification {
private static final String HTTPS_URI = 'https://github.com/ajoberstar/grgit-test.git'

static Credentials hardcodedCreds
static Credentials partialCreds

@Rule TemporaryFolder tempDir = new TemporaryFolder()

Expand All @@ -45,6 +46,7 @@ class LinuxAuthenticationSpec extends Specification {
def username = System.properties['org.ajoberstar.grgit.test.username']
def password = System.properties['org.ajoberstar.grgit.test.password']
hardcodedCreds = new Credentials(username, password)
partialCreds = new Credentials(password, null)
assert hardcodedCreds.username && hardcodedCreds.password
}

Expand All @@ -61,7 +63,7 @@ class LinuxAuthenticationSpec extends Specification {
System.properties[AuthConfig.FORCE_OPTION] = method
assert System.properties[AuthConfig.USERNAME_OPTION] == null, 'Username should not already be set.'
assert System.properties[AuthConfig.PASSWORD_OPTION] == null, 'Password should not already be set.'
ready(ssh, creds)
ready(ssh)
if (!creds) {
System.properties[AuthConfig.USERNAME_OPTION] = hardcodedCreds.username
System.properties[AuthConfig.PASSWORD_OPTION] = hardcodedCreds.password
Expand All @@ -72,18 +74,18 @@ class LinuxAuthenticationSpec extends Specification {
then:
grgit.branch.status(branch: 'master').aheadCount == 0
where:
method | ssh | creds
method | ssh | creds
'hardcoded' | false | hardcodedCreds
'hardcoded' | false | null
'hardcoded' | false | partialCreds
'interactive' | false | null
'interactive' | true | null
'sshagent' | true | null
'sshagent' | true | null
}

private void ready(boolean ssh, Credentials credentials = null) {
private void ready(boolean ssh) {
File repoDir = tempDir.newFolder('repo')
String uri = ssh ? SSH_URI : HTTPS_URI
grgit = Grgit.clone(uri: uri, dir: repoDir, credentials: credentials)
grgit = Grgit.clone(uri: uri, dir: repoDir)
grgit.repository.jgit.repo.config.with {
setString('user', null, 'name', person.name)
setString('user', null, 'email', person.email)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class WindowsAuthenticationSpec extends Specification {
def username = System.properties['org.ajoberstar.grgit.test.username']
def password = System.properties['org.ajoberstar.grgit.test.password']
hardcodedCreds = new Credentials(username, password)
partialCreds = new Credentials(username, null)
partialCreds = new Credentials(password, null)
assert hardcodedCreds.username && hardcodedCreds.password
}

Expand All @@ -63,7 +63,7 @@ class WindowsAuthenticationSpec extends Specification {
assert System.properties[AuthConfig.USERNAME_OPTION] == null, 'Username should not already be set.'
assert System.properties[AuthConfig.PASSWORD_OPTION] == null, 'Password should not already be set.'
System.properties[AuthConfig.FORCE_OPTION] = method
ready(ssh, creds)
ready(ssh)
if (!creds) {
System.properties[AuthConfig.USERNAME_OPTION] = hardcodedCreds.username
System.properties[AuthConfig.PASSWORD_OPTION] = hardcodedCreds.password
Expand All @@ -74,20 +74,20 @@ class WindowsAuthenticationSpec extends Specification {
then:
grgit.branch.status(branch: 'master').aheadCount == 0
where:
method | ssh | creds
method | ssh | creds
'hardcoded' | false | hardcodedCreds
'hardcoded' | false | partialCreds
'hardcoded' | false | null
'interactive' | false | null
'interactive' | true | null
'sshagent' | true | null
'pageant' | true | null
'sshagent' | true | null
'pageant' | true | null
}

private void ready(boolean ssh, Credentials credentials = null) {
private void ready(boolean ssh) {
File repoDir = tempDir.newFolder('repo')
String uri = ssh ? SSH_URI : HTTPS_URI
grgit = Grgit.clone(uri: uri, dir: repoDir, credentials: credentials)
grgit = Grgit.clone(uri: uri, dir: repoDir)
grgit.repository.jgit.repo.config.with {
setString('user', null, 'name', person.name)
setString('user', null, 'email', person.email)
Expand Down

0 comments on commit 13eb6c9

Please sign in to comment.