org.eclipse.jgit.errors.UnsupportedCredentialItem Java Examples

The following examples show how to use org.eclipse.jgit.errors.UnsupportedCredentialItem. 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: PrivateKeyCredentialsProvider.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
    for (CredentialItem i : items) {
        if (i instanceof Identity) {
            ((Identity) i).setValue(identityFile);
            continue;
        }
        if (i instanceof KnownHosts) {
            ((KnownHosts) i).setValue(knownHostsFile);
            continue;
        }
        throw new UnsupportedCredentialItem(uri, i.getClass().getName()
                + ":" + i.getPromptText()); //$NON-NLS-1$
    }
    return true;
}
 
Example #2
Source File: GitOperations.java    From spring-data-dev-tools with Apache License 2.0 6 votes vote down vote up
@Override
public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {

	if (!matchesKey(items)) {
		return false;
	}

	for (CredentialItem item : items) {
		if (item instanceof CharArrayType) {
			((CharArrayType) item).setValueNoCopy(gpg.getPassword().toString().toCharArray());

			return true;
		}
	}
	return false;
}
 
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: 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 #5
Source File: CredentialsProviderImplTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testThrowsUnsupportedOperationException() {
    CredentialItem.InformationalMessage message = new CredentialItem.InformationalMessage("Some info");
    assertFalse(provider.supports(message));
    assertThrows(UnsupportedCredentialItem.class,
                 () -> {
                     provider.get(uri, message);
                 });
}
 
Example #6
Source File: CredentialsProviderImplTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testSpecialStringTypeThrowsException() {
    CredentialItem.StringType specialStringType = new CredentialItem.StringType("Bad Password: ", false);
    assertFalse(provider.supports(specialStringType));
    assertThrows(UnsupportedCredentialItem.class,
                 () -> {
                     provider.get(uri, specialStringType);
                 });
}
 
Example #7
Source File: SmartCredentialsProviderTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testGetThrowsException() {
    String expectedUsername = "expected-add-credentials-username";
    Secret secret = Secret.fromString("password-secret");
    StandardUsernamePasswordCredentials credentials = new StandardUsernamePasswordCredentialsImpl(expectedUsername, secret);
    provider.addCredentials(gitURI.toString(), credentials);
    assertThrows(UnsupportedCredentialItem.class,
                 () ->
                 {
                     provider.get(gitURI, username, password, maskedUsername, unmaskedUsername, maskedStringType);
                 });
}
 
Example #8
Source File: TrileadSessionFactory.java    From git-client-plugin with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public RemoteSession getSession(URIish uri, CredentialsProvider credentialsProvider, FS fs, int tms) throws TransportException {
    try {
        int p = uri.getPort();
        if (p<0)    p = 22;
        Connection con = new Connection(uri.getHost(), p);
        con.setTCPNoDelay(true);
        con.connect();  // TODO: host key check

        boolean authenticated;
        if (credentialsProvider instanceof SmartCredentialsProvider) {
            final SmartCredentialsProvider smart = (SmartCredentialsProvider) credentialsProvider;
            StandardUsernameCredentialsCredentialItem
                    item = new StandardUsernameCredentialsCredentialItem("Credentials for " + uri, false);
            authenticated = smart.supports(item)
                    && smart.get(uri, item)
                    && SSHAuthenticator.newInstance(con, item.getValue(), uri.getUser())
                    .authenticate(smart.listener);
        } else if (credentialsProvider instanceof CredentialsProviderImpl) {
            CredentialsProviderImpl sshcp = (CredentialsProviderImpl) credentialsProvider;

            authenticated = SSHAuthenticator.newInstance(con, sshcp.cred).authenticate(sshcp.listener);
        } else {
            authenticated = false;
        }
        if (!authenticated && con.isAuthenticationComplete())
            throw new TransportException("Authentication failure");

        return wrap(con);
    } catch (UnsupportedCredentialItem | IOException | InterruptedException e) {
        throw new TransportException(uri,"Failed to connect",e);
    }
}
 
Example #9
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 #10
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 #11
Source File: AwsCodeCommitCredentialsProviderTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void testThrowsUnsupportedCredentialException() throws URISyntaxException {
	CredentialItem[] goodCredentialItems = makeCredentialItems();
	CredentialItem[] badCredentialItems = new CredentialItem[] {
			goodCredentialItems[0], goodCredentialItems[1],
			new CredentialItem.YesNoType("OK?") };
	try {
		this.provider.get(new URIish(AWS_REPO), badCredentialItems);
		fail("Expected UnsupportedCredentialItem exception");
	}
	catch (UnsupportedCredentialItem e) {
		assertThat(e.getMessage()).isNotNull();
	}
}
 
Example #12
Source File: AwsCodeCommitCredentialsProviderTests.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Test
public void testUriWithCurlyBracesReturnsTrue()
		throws UnsupportedCredentialItem, URISyntaxException {
	GitCredentialsProviderFactory factory = new GitCredentialsProviderFactory();
	this.provider = (AwsCodeCommitCredentialProvider) factory
			.createFor(CURLY_BRACES_REPO, USER, PASSWORD, null, false);
	CredentialItem[] credentialItems = makeCredentialItems();
	assertThat(this.provider.get(new URIish(CURLY_BRACES_REPO), credentialItems))
			.isTrue();
}
 
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: PassphraseCredentialsProvider.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
/**
 * Ask for the credential items to be populated with the passphrase.
 * @param uri the URI of the remote resource that needs authentication.
 * @param items the items the application requires to complete authentication.
 * @return {@code true} if the request was successful and values were supplied;
 * {@code false} if the user canceled the request and did not supply all requested
 * values.
 * @throws UnsupportedCredentialItem if one of the items supplied is not supported.
 */
@Override
public boolean get(URIish uri, CredentialItem... items)
		throws UnsupportedCredentialItem {
	for (final CredentialItem item : items) {
		if (item instanceof CredentialItem.StringType
				&& item.getPromptText().startsWith(PROMPT)) {
			((CredentialItem.StringType) item).setValue(this.passphrase);
			continue;
		}
		throw new UnsupportedCredentialItem(uri,
				item.getClass().getName() + ":" + item.getPromptText());
	}
	return true;
}
 
Example #15
Source File: GitSkipSslValidationCredentialsProvider.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
@Override
public boolean get(URIish uri, CredentialItem... items)
		throws UnsupportedCredentialItem {
	List<CredentialItem> unprocessedItems = new ArrayList<>();

	for (CredentialItem item : items) {
		if (item instanceof CredentialItem.YesNoType) {
			CredentialItem.YesNoType yesNoItem = (CredentialItem.YesNoType) item;
			String prompt = yesNoItem.getPromptText();
			if (prompt == null) {
				unprocessedItems.add(item);
			}
			else if (prompt.equals(JGitText.get().sslTrustNow) || prompt.startsWith(
					stripFormattingPlaceholders(JGitText.get().sslTrustForRepo))) {
				yesNoItem.setValue(true);
			}
			else if (prompt.equals(JGitText.get().sslTrustAlways)) {
				yesNoItem.setValue(false);
			}
			else {
				unprocessedItems.add(item);
			}
		}
		else if (!item.getPromptText()
				.contains(JGitText.get().sslFailureTrustExplanation)) {
			unprocessedItems.add(item);
		}
	}

	if (unprocessedItems.isEmpty()) {
		return true;
	}
	if (this.delegate != null) {
		return this.delegate.get(uri,
				unprocessedItems.toArray(new CredentialItem[0]));
	}
	throw new UnsupportedCredentialItem(uri,
			unprocessedItems.size() + " credential items not supported");
}
 
Example #16
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 #17
Source File: AllowHostsCredentialsProvider.java    From Notebook with Apache License 2.0 5 votes vote down vote up
@Override
public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
	for(CredentialItem item : items){
		if(item instanceof CredentialItem.YesNoType){
			((CredentialItem.YesNoType) item).setValue(true);
			return true;
		}
	}
	return false;
}
 
Example #18
Source File: AwsCodeCommitCredentialsProviderTests.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
@Test
public void testBadUriReturnsFalse()
		throws UnsupportedCredentialItem, URISyntaxException {
	CredentialItem[] credentialItems = makeCredentialItems();
	assertThat(this.provider.get(new URIish(BAD_REPO), credentialItems)).isFalse();
}
 
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
}