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

The following examples show how to use org.eclipse.jgit.transport.CredentialItem#Password . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
Source File: CredentialsProviderImplTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testSupportsPassword() {
    CredentialItem.Password password = new CredentialItem.Password();
    assertNull(password.getValue());
    assertTrue(provider.supports(password));
    assertTrue(provider.get(uri, password));
    assertNotNull(password.getValue());
}
 
Example 9
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 10
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 11
Source File: JGitEnvironmentRepositoryTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void gitCredentialsProviderFactoryCreatesSkipSslValidationProvider()
		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("https://somegitserver/somegitrepo");
	envRepository.setBasedir(new File("./mybasedir"));
	envRepository.setUsername(username);
	envRepository.setPassword(password);
	envRepository.setSkipSslValidation(true);
	envRepository.setCloneOnStart(true);
	envRepository.afterPropertiesSet();

	CredentialsProvider provider = mockCloneCommand.getCredentialsProvider();

	assertThat(provider instanceof GitSkipSslValidationCredentialsProvider).isTrue();

	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 12
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 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: ElytronClientCredentialsProvider.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public boolean supports(CredentialItem... items) {
    for (CredentialItem i : items) {
        if (!(i instanceof CredentialItem.Username) && !(i instanceof CredentialItem.Password)) {
            return false;
        }
    }
    return true;
}
 
Example 15
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 16
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 17
Source File: TransportCommand.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supports (CredentialItem... items) {
    for (CredentialItem i : items) {
        if (!(i instanceof CredentialItem.Username
                || i instanceof CredentialItem.Password)) {
            return false;
        }
    }
    return true;
}
 
Example 18
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;
}
 
Example 19
Source File: GitCredentialsProvider.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
	for (CredentialItem item : items) {
		if (item instanceof CredentialItem.Username || item instanceof CredentialItem.Password) {
			if ((this.privateKey == null || this.privateKey.length == 0) && (this.publicKey == null || this.publicKey.length == 0) && (this.passphrase == null || this.passphrase.length == 0)) {
				CredentialItem.Username u = new CredentialItem.Username();
				CredentialItem.Password p = new CredentialItem.Password();
				super.get(uri, u, p);
				if ((u.getValue() == null || u.getValue().length() == 0) && (p.getValue() == null || p.getValue().length == 0)) {
					if (uri != null) {
						if (this.remoteUser != null) {
							/* see if a user token is available */
							String uriString = uri.toString();
							IGitHubToken token = tokenCache.get(uriString);
							if (token != null && token.getExpiry() != 0 && token.getExpiry() < System.currentTimeMillis()) {
								token = null;
							}
							if (token == null) {
								for (int i = 0; token == null && i < GithubTokenProviders.size(); i++) {
									token = GithubTokenProviders.get(i).getToken(uriString, remoteUser);
								}
							}
							if (token != null) {
								if (item instanceof CredentialItem.Username) {
									((CredentialItem.Username)item).setValue(token.getUsername());
								} else {
									((CredentialItem.Password)item).setValue(token.getPassword());
								}
								tokenCache.put(uriString, token);
								continue;
							}
						}
					}
				}
			}
		}
		if (super.supports(item)) {
			super.get(uri, item);
		} else if (item instanceof CredentialItem.StringType) {
			if (item.getPromptText().toLowerCase(Locale.ENGLISH).contains("passphrase") && passphrase != null && passphrase.length > 0) {
				((CredentialItem.StringType) item).setValue(new String(passphrase));
			} else {
				((CredentialItem.StringType) item).setValue("");
			}
		} else if (item instanceof CredentialItem.CharArrayType) {
			((CredentialItem.CharArrayType) item).setValue(new char[0]);
		} else {
			throw new UnsupportedCredentialItem(uri, item.getPromptText());
		}
	}
	return true; // we assume that user provided all credentials that are needed
}
 
Example 20
Source File: GitSshSessionFactory.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public RemoteSession getSession(URIish uri, CredentialsProvider credentialsProvider, FS fs, int tms) throws TransportException {
	int port = uri.getPort();
	String user = uri.getUser();
	String pass = uri.getPass();
	if (credentialsProvider instanceof GitCredentialsProvider) {
		if (port <= 0)
			port = SSH_PORT;

		GitCredentialsProvider cp = (GitCredentialsProvider) credentialsProvider;
		if (user == null) {
			CredentialItem.Username u = new CredentialItem.Username();
			if (cp.supports(u) && cp.get(cp.getUri(), u)) {
				user = u.getValue();
			}
		}
		if (pass == null) {
			CredentialItem.Password p = new CredentialItem.Password();
			if (cp.supports(p) && cp.get(cp.getUri(), p)) {
				pass = new String(p.getValue());
			}
		}

		try {
			final SessionHandler session = new SessionHandler(user, uri.getHost(), port, cp.getKnownHosts(), cp.getPrivateKey(), cp.getPublicKey(),
					cp.getPassphrase());
			if (pass != null)
				session.setPassword(pass);
			if (!credentialsProvider.isInteractive()) {
				session.setUserInfo(new CredentialsProviderUserInfo(session.getSession(), credentialsProvider));
			}

			session.connect(tms);

			return new JschSession(session.getSession(), uri);
		} catch (JSchException e) {
			throw new TransportException(uri, e.getMessage(), e);
		}
	}
	return null;
}