Java Code Examples for org.eclipse.jgit.transport.CredentialItem#Username

The following examples show how to use org.eclipse.jgit.transport.CredentialItem#Username . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CredentialsProviderImpl.java    From git-client-plugin with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * If username/password is given, use it for HTTP auth.
 */
@Override
public boolean supports(CredentialItem... items) {
    if (!(cred instanceof StandardUsernamePasswordCredentials))
        return false;

    for (CredentialItem i : items) {
        if (i instanceof CredentialItem.Username)
            continue;

        else if (i instanceof CredentialItem.Password)
            continue;

        else
            return false;
    }
    return true;
}
 
Example 2
Source File: JGitCredentialsProvider.java    From mOrgAnd with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean get(URIish uri, CredentialItem... items)
        throws UnsupportedCredentialItem {
    for (CredentialItem item : items) {
        if (item instanceof CredentialItem.Username) {
            ((CredentialItem.Username) item).setValue(username);
        } else if (item instanceof CredentialItem.Password) {
            ((CredentialItem.Password) item).setValue(password.toCharArray());
        } else if (item instanceof CredentialItem.StringType) {
            ((CredentialItem.StringType) item).setValue(password);
        } else if (item instanceof CredentialItem.InformationalMessage) {
            throw new UnsupportedCredentialItem(uri, "Not supported");
        } else if (item instanceof CredentialItem.YesNoType) {
            // TODO handle strict host key checking here
            throw new UnsupportedCredentialItem(uri, "Not supported");
        } else {
            throw new UnsupportedCredentialItem(uri, "Not supported");
        }
    }
    return true;
}
 
Example 3
Source File: GitSkipSslValidationCredentialsProviderTest.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetUnrelatedCredentialItemTypesWithDelegate()
		throws URISyntaxException {
	this.skipSslValidationCredentialsProvider = new GitSkipSslValidationCredentialsProvider(
			this.mockDelegateCredentialsProvider);
	URIish uri = new URIish("https://example.com/repo.git");
	CredentialItem usernameCredentialItem = new CredentialItem.Username();
	CredentialItem passwordCredentialItem = new CredentialItem.Password();

	when(this.mockDelegateCredentialsProvider.get(uri, usernameCredentialItem,
			passwordCredentialItem)).thenReturn(true);

	boolean getSuccessful = this.skipSslValidationCredentialsProvider.get(uri,
			usernameCredentialItem, passwordCredentialItem);

	assertThat(getSuccessful).as("GitSkipSslValidationCredentialsProvider "
			+ "must successfully get the types supported by its delegate CredentialsProvider")
			.isTrue();
}
 
Example 4
Source File: GitSkipSslValidationCredentialsProviderTest.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Test
public void testSupportsUnrelatedCredentialItemTypesWithDelegate() {
	this.skipSslValidationCredentialsProvider = new GitSkipSslValidationCredentialsProvider(
			this.mockDelegateCredentialsProvider);
	CredentialItem usernameCredentialItem = new CredentialItem.Username();

	when(this.mockDelegateCredentialsProvider.supports(usernameCredentialItem))
			.thenReturn(true);

	boolean supportsItems = this.skipSslValidationCredentialsProvider
			.supports(usernameCredentialItem);

	assertThat(supportsItems).as(
			"GitSkipSslValidationCredentialsProvider must support the types supported by its delegate CredentialsProvider")
			.isTrue();
}
 
Example 5
Source File: AwsCodeCommitCredentialProvider.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
/**
 * We support username and password credential items only.
 * @see org.eclipse.jgit.transport.CredentialsProvider#supports(org.eclipse.jgit.transport.CredentialItem[])
 */
@Override
public boolean supports(CredentialItem... items) {
	for (CredentialItem i : items) {
		if (i instanceof CredentialItem.Username) {
			continue;
		}
		else if (i instanceof CredentialItem.Password) {
			continue;
		}
		else {
			return false;
		}
	}
	return true;
}
 
Example 6
Source File: SmartCredentialsProviderTest.java    From git-client-plugin with MIT License 6 votes vote down vote up
@Before
public void setUp() {
    listener = StreamTaskListener.fromStdout();
    provider = new SmartCredentialsProvider(listener);
    username = new CredentialItem.Username();
    password = new CredentialItem.Password();

    maskedUsername = new StandardUsernameCredentialsCredentialItem(MASKED_USER_NAME_PROMPT, true);
    unmaskedUsername = new StandardUsernameCredentialsCredentialItem(UNMASKED_USER_NAME_PROMPT, false);

    maskedStringType = new CredentialItem.StringType(MASKED_STRING_TYPE_PROMPT, true);
    unmaskedStringType = new CredentialItem.StringType(UNMASKED_STRING_TYPE_PROMPT, false);
    specialStringType = new CredentialItem.StringType(SPECIAL_STRING_TYPE_PROMPT, false);

    assertNull(username.getValue());
    assertNull(password.getValue());
    assertNull(maskedUsername.getValue());
    assertNull(unmaskedUsername.getValue());
    assertNull(maskedStringType.getValue());
    assertNull(unmaskedStringType.getValue());
}
 
Example 7
Source File: UsernamePasswordCredentialsProvider.java    From gerrit-code-review-plugin with Apache License 2.0 6 votes vote down vote up
public UsernamePassword getUsernamePassword(URIish uri) {
  addDefaultCredentials(credentials);

  String username = uri.getUser();
  String password = uri.getPass();

  CredentialItem.Username u = new CredentialItem.Username();
  CredentialItem.Password p = new CredentialItem.Password();

  if (supports(u, p) && get(uri, u, p)) {
    username = u.getValue();
    char[] v = p.getValue();
    password = (v == null) ? null : new String(p.getValue());
    p.clear();
  }

  return new UsernamePassword(username, password);
}
 
Example 8
Source File: HttpDelegatingCredentialsProvider.java    From gitflow-incremental-builder with MIT License 5 votes vote down vote up
@Override
public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {

    // only handle HTTP(s)
    if (uri.getScheme() != null && !uri.getScheme().startsWith("http")) {
        return false;
    }

    CredentialsPair credentialsPair = credentials.computeIfAbsent(uri, u -> {
        try {
            return lookupCredentials(uri);
        } catch (IOException | InterruptedException | RuntimeException e) {
            logger.warn("Failed to look up credentials via 'git credential fill' for: " + uri, e);
            return null;
        }
    });
    if (credentialsPair == null) {
        return false;
    }

    // map extracted credentials to CredentialItems, see also: org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider
    for (CredentialItem item : items) {
        if (item instanceof CredentialItem.Username) {
            ((CredentialItem.Username) item).setValue(credentialsPair.username);
        } else if (item instanceof CredentialItem.Password) {
            ((CredentialItem.Password) item).setValue(credentialsPair.password);
        } else if (item instanceof CredentialItem.StringType && item.getPromptText().equals("Password: ")) {
            ((CredentialItem.StringType) item).setValue(new String(credentialsPair.password));
        } else {
            throw new UnsupportedCredentialItem(uri, item.getClass().getName() + ":" + item.getPromptText());
        }
    }

    return true;
}
 
Example 9
Source File: CredentialsProviderImplTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testSupportsUsername() {
    CredentialItem.Username username = new CredentialItem.Username();
    assertNull(username.getValue());
    assertTrue(provider.supports(username));
    assertTrue(provider.get(uri, username));
    assertEquals(USER_NAME, username.getValue());
}
 
Example 10
Source File: CredentialsProviderImpl.java    From git-client-plugin with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * If username/password is given, use it for HTTP auth.
 */
@Override
public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
    if (!(cred instanceof StandardUsernamePasswordCredentials))
        return false;
    StandardUsernamePasswordCredentials _cred = (StandardUsernamePasswordCredentials) cred;

    for (CredentialItem i : items) {
        if (i instanceof CredentialItem.Username) {
            ((CredentialItem.Username) i).setValue(_cred.getUsername());
            continue;
        }
        if (i instanceof CredentialItem.Password) {
            ((CredentialItem.Password) i).setValue(
                    _cred.getPassword().getPlainText().toCharArray());
            continue;
        }
        if (i instanceof CredentialItem.StringType) {
            if (i.getPromptText().equals("Password: ")) {
                ((CredentialItem.StringType) i).setValue(
                        _cred.getPassword().getPlainText());
                continue;
            }
        }
        throw new UnsupportedCredentialItem(uri, i.getClass().getName()
                + ":" + i.getPromptText());
    }
    return true;
}
 
Example 11
Source File: GitSkipSslValidationCredentialsProviderTest.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void testSupportsUnrelatedCredentialItemTypes() {
	CredentialItem usernameCredentialItem = new CredentialItem.Username();

	boolean supportsItems = this.skipSslValidationCredentialsProvider
			.supports(usernameCredentialItem);

	assertThat(supportsItems).as(
			"Credential item types not related to SSL validation skipping should not be supported")
			.isFalse();
}
 
Example 12
Source File: TransportCommand.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected final URIish getUriWithUsername (boolean pushUri) throws URISyntaxException {
    URIish uri = getUri(pushUri);
    if (credentialsProvider != null) {
        CredentialItem.Username itm = new CredentialItem.Username();
        credentialsProvider.get(uri, itm);
        if (itm.getValue() != null) {
            if (itm.getValue().isEmpty()) {
                uri = uri.setUser(null);
            } else {
                uri = uri.setUser(itm.getValue());
            }
        }
    }
    return uri;
}
 
Example 13
Source File: GitSkipSslValidationCredentialsProviderTest.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test(expected = UnsupportedCredentialItem.class)
public void testGetUnrelatedCredentialItemTypes() throws URISyntaxException {
	URIish uri = new URIish("https://example.com/repo.git");
	CredentialItem usernameCredentialItem = new CredentialItem.Username();
	CredentialItem passwordCredentialItem = new CredentialItem.Password();

	this.skipSslValidationCredentialsProvider.get(uri, usernameCredentialItem,
			passwordCredentialItem);
}
 
Example 14
Source File: TransportCommand.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean get (URIish uriish, CredentialItem... items) throws UnsupportedCredentialItem {
    String user = uriish.getUser();
    if (user == null) {
        user = "";
    }
    String password = uriish.getPass();
    if (password == null) {
        password = "";
    }
    for (CredentialItem i : items) {
        if (i instanceof CredentialItem.Username) {
            ((CredentialItem.Username) i).setValue(user);
            continue;
        }
        if (i instanceof CredentialItem.Password) {
            ((CredentialItem.Password) i).setValue(password.toCharArray());
            continue;
        }
        if (i instanceof CredentialItem.StringType) {
            if (i.getPromptText().equals("Password: ")) { //NOI18N
                ((CredentialItem.StringType) i).setValue(password);
                continue;
            }
        }
        throw new UnsupportedCredentialItem(uriish, i.getClass().getName()
                + ":" + i.getPromptText()); //NOI18N
    }
    return true;
}
 
Example 15
Source File: JGitEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void usernamePasswordShouldSetCredentials() throws Exception {
	Git mockGit = mock(Git.class);
	MockCloneCommand mockCloneCommand = new MockCloneCommand(mockGit);

	JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(
			this.environment, new JGitEnvironmentProperties());
	envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
	envRepository.setUri("git+ssh://git@somegitserver/somegitrepo");
	envRepository.setBasedir(new File("./mybasedir"));
	final String username = "someuser";
	final String password = "mypassword";
	envRepository.setUsername(username);
	envRepository.setPassword(password);
	envRepository.setCloneOnStart(true);
	envRepository.afterPropertiesSet();

	assertTrue(mockCloneCommand
			.getCredentialsProvider() instanceof UsernamePasswordCredentialsProvider);

	CredentialsProvider provider = mockCloneCommand.getCredentialsProvider();
	CredentialItem.Username usernameCredential = new CredentialItem.Username();
	CredentialItem.Password passwordCredential = new CredentialItem.Password();
	assertThat(provider.supports(usernameCredential)).isTrue();
	assertThat(provider.supports(passwordCredential)).isTrue();

	provider.get(new URIish(), usernameCredential);
	assertThat(username).isEqualTo(usernameCredential.getValue());
	provider.get(new URIish(), passwordCredential);
	assertThat(password).isEqualTo(String.valueOf(passwordCredential.getValue()));
}
 
Example 16
Source File: JGitEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void gitCredentialsProviderFactoryCreatesUsernamePasswordProvider()
		throws Exception {
	Git mockGit = mock(Git.class);
	MockCloneCommand mockCloneCommand = new MockCloneCommand(mockGit);
	final String username = "someuser";
	final String password = "mypassword";

	JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(
			this.environment, new JGitEnvironmentProperties());
	envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
	envRepository.setUri("git+ssh://git@somegitserver/somegitrepo");
	envRepository.setBasedir(new File("./mybasedir"));
	envRepository.setUsername(username);
	envRepository.setPassword(password);
	envRepository.setCloneOnStart(true);
	envRepository.afterPropertiesSet();

	assertTrue(mockCloneCommand
			.getCredentialsProvider() instanceof UsernamePasswordCredentialsProvider);

	CredentialsProvider provider = mockCloneCommand.getCredentialsProvider();
	CredentialItem.Username usernameCredential = new CredentialItem.Username();
	CredentialItem.Password passwordCredential = new CredentialItem.Password();
	assertThat(provider.supports(usernameCredential)).isTrue();
	assertThat(provider.supports(passwordCredential)).isTrue();

	provider.get(new URIish(), usernameCredential);
	assertThat(username).isEqualTo(usernameCredential.getValue());
	provider.get(new URIish(), passwordCredential);
	assertThat(password).isEqualTo(String.valueOf(passwordCredential.getValue()));
}
 
Example 17
Source File: SmartCredentialsProvider.java    From git-client-plugin with MIT License 5 votes vote down vote up
private boolean supports(StandardCredentials c, CredentialItem i) {
    if (c == null) {
        return false;
    }
    if (i instanceof StandardUsernameCredentialsCredentialItem) {
        return c instanceof StandardUsernameCredentials;
    }
    if (i instanceof CredentialItem.Username) {
        return c instanceof UsernameCredentials;
    }
    if (i instanceof CredentialItem.Password) {
        return c instanceof PasswordCredentials;
    }
    return false;
}
 
Example 18
Source File: SmartCredentialsProvider.java    From git-client-plugin with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public synchronized boolean get(URIish uri, CredentialItem... credentialItems) throws UnsupportedCredentialItem {
    StandardCredentials c = specificCredentials.get(uri == null ? null : uri.toString());
    if (c == null) {
        c = defaultCredentials;
    }
    if (c == null) {
        return false;
    }
    for (CredentialItem i : credentialItems) {
        if (i instanceof StandardUsernameCredentialsCredentialItem && c instanceof StandardUsernameCredentials) {
            ((StandardUsernameCredentialsCredentialItem) i).setValue((StandardUsernameCredentials) c);
            continue;
        }
        if (i instanceof CredentialItem.Username && c instanceof UsernameCredentials) {
            ((CredentialItem.Username) i).setValue(((UsernameCredentials)c).getUsername());
            continue;
        }
        if (i instanceof CredentialItem.Password && c instanceof PasswordCredentials) {
            ((CredentialItem.Password) i).setValue(
                    ((PasswordCredentials) c).getPassword().getPlainText().toCharArray());
            continue;
        }
        if (i instanceof CredentialItem.StringType) {
            if (i.getPromptText().equals("Password: ") && c instanceof PasswordCredentials) {
                ((CredentialItem.StringType) i).setValue(((PasswordCredentials) c).getPassword().getPlainText());
                continue;
            }
        }
        throw new UnsupportedCredentialItem(uri, i.getClass().getName()
                + ":" + i.getPromptText());
    }
    return true;
}
 
Example 19
Source File: CredentialsProviderImplTest.java    From git-client-plugin with MIT License 4 votes vote down vote up
@Test
public void testSupportsNullItems() {
    CredentialItem.Username nullItem = null;
    assertFalse(provider.supports(nullItem));
}
 
Example 20
Source File: AwsCodeCommitCredentialsProviderTests.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
private CredentialItem[] makeCredentialItems() {
	CredentialItem[] credentialItems = new CredentialItem[2];
	credentialItems[0] = new CredentialItem.Username();
	credentialItems[1] = new CredentialItem.Password();
	return credentialItems;
}