diff --git a/che-core-git-impl-jgit/src/main/java/org/eclipse/che/git/impl/jgit/JGitConnection.java b/che-core-git-impl-jgit/src/main/java/org/eclipse/che/git/impl/jgit/JGitConnection.java index a273d01b0..0938bf955 100644 --- a/che-core-git-impl-jgit/src/main/java/org/eclipse/che/git/impl/jgit/JGitConnection.java +++ b/che-core-git-impl-jgit/src/main/java/org/eclipse/che/git/impl/jgit/JGitConnection.java @@ -69,6 +69,7 @@ import org.eclipse.che.api.git.shared.TagCreateRequest; import org.eclipse.che.api.git.shared.TagDeleteRequest; import org.eclipse.che.api.git.shared.TagListRequest; +import org.eclipse.che.api.git.shared.GitRequest; import org.eclipse.che.git.impl.jgit.ssh.SshKeyProvider; import org.eclipse.jgit.api.AddCommand; import org.eclipse.jgit.api.CheckoutCommand; @@ -445,7 +446,7 @@ public void clone(CloneRequest request) throws GitException, UnauthorizedExcepti cloneCommand.setBranchesToClone(request.getBranchesToFetch()); } - executeRemoteCommand(remoteUri, cloneCommand); + executeRemoteCommand(remoteUri, cloneCommand, request); StoredConfig repositoryConfig = getRepository().getConfig(); GitUser gitUser = getUser(); @@ -560,7 +561,7 @@ public void fetch(FetchRequest request) throws GitException, UnauthorizedExcepti } fetchCommand.setRemoveDeletedRefs(request.isRemoveDeletedRefs()); - executeRemoteCommand(remoteUri, fetchCommand); + executeRemoteCommand(remoteUri, fetchCommand , request); } catch (GitException | GitAPIException exception) { String errorMessage; if (exception.getMessage().contains("Invalid remote: ")) { @@ -871,7 +872,7 @@ public PullResponse pull(PullRequest request) throws GitException, UnauthorizedE fetchCommand.setTimeout(timeout); } - FetchResult fetchResult = (FetchResult)executeRemoteCommand(remoteUri, fetchCommand); + FetchResult fetchResult = (FetchResult)executeRemoteCommand(remoteUri, fetchCommand, request); Ref remoteBranchRef = fetchResult.getAdvertisedRef(remoteBranch); if (remoteBranchRef == null) { @@ -942,7 +943,7 @@ public PushResponse push(PushRequest request) throws GitException, UnauthorizedE } @SuppressWarnings("unchecked") - Iterable pushResults = (Iterable)executeRemoteCommand(remoteUri, pushCommand); + Iterable pushResults = (Iterable)executeRemoteCommand(remoteUri, pushCommand, request); PushResult pushResult = pushResults.iterator().next(); return addCommandOutputUpdates(pushResponseDto, request, pushResult); } catch (GitAPIException exception) { @@ -1461,7 +1462,7 @@ public boolean accept(File dir) { * @throws GitAPIException * @throws UnauthorizedException */ - private Object executeRemoteCommand(String remoteUrl, TransportCommand command) + private Object executeRemoteCommand(String remoteUrl, TransportCommand command, GitRequest request) throws GitException, GitAPIException, UnauthorizedException { String sshKeyDirectoryPath = ""; try { @@ -1492,10 +1493,16 @@ public void configure(Transport transport) { } }); } else { - UserCredential credentials = credentialsLoader.getUserCredential(remoteUrl); - if (credentials != null) { - command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(credentials.getUserName(), - credentials.getPassword())); + String gitUser = request.getAttributes().get("username"); + String gitPassword = request.getAttributes().get("password"); + if (gitUser != null && gitPassword != null) { + command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(gitUser, gitPassword)); + } else { + UserCredential credentials = credentialsLoader.getUserCredential(remoteUrl); + if (credentials != null) { + command.setCredentialsProvider( + new UsernamePasswordCredentialsProvider(credentials.getUserName(), credentials.getPassword())); + } } } return command.call(); diff --git a/platform-api/che-core-api-git/src/main/java/org/eclipse/che/api/git/GitBasicAuthenticationCredentialsProvider.java b/platform-api/che-core-api-git/src/main/java/org/eclipse/che/api/git/GitBasicAuthenticationCredentialsProvider.java new file mode 100644 index 000000000..a50bd3a35 --- /dev/null +++ b/platform-api/che-core-api-git/src/main/java/org/eclipse/che/api/git/GitBasicAuthenticationCredentialsProvider.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2012-2016 Codenvy, S.A. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Codenvy, S.A. - initial API and implementation + *******************************************************************************/ +package org.eclipse.che.api.git; + +import org.eclipse.che.api.git.server.dto.DtoServerImpls; +import org.eclipse.che.api.git.shared.GitUser; + +/** + * Credentials provider for Git basic authentication + * + * @author Yossi Balan + */ +public class GitBasicAuthenticationCredentialsProvider implements CredentialsProvider { + + private static ThreadLocal currRequestCredentials = new ThreadLocal<>(); + private static final String BASIC_PROVIDER_NAME = "basic"; + + @Override + public UserCredential getUserCredential() { + return currRequestCredentials.get(); + } + + @Override + public GitUser getUser() throws GitException { + DtoServerImpls.GitUserImpl gitUser = new DtoServerImpls.GitUserImpl(); + gitUser.setName(currRequestCredentials.get().getUserName()); + return gitUser; + } + + @Override + public String getId() { + return BASIC_PROVIDER_NAME; + } + + @Override + public boolean canProvideCredentials(String url) { + return getUserCredential() != null; + } + + public static void setCurrentCredentials(String user, String password) { + UserCredential creds = new UserCredential(user, password, BASIC_PROVIDER_NAME); + currRequestCredentials.set(creds); + } + + public static void clearCredentials() { + currRequestCredentials.set(null); + } + +} diff --git a/platform-api/che-core-api-git/src/main/java/org/eclipse/che/api/git/GitProjectImporter.java b/platform-api/che-core-api-git/src/main/java/org/eclipse/che/api/git/GitProjectImporter.java index 440c26784..fbce916b0 100644 --- a/platform-api/che-core-api-git/src/main/java/org/eclipse/che/api/git/GitProjectImporter.java +++ b/platform-api/che-core-api-git/src/main/java/org/eclipse/che/api/git/GitProjectImporter.java @@ -46,6 +46,8 @@ import java.util.List; import java.util.Map; +import static org.eclipse.che.api.git.GitBasicAuthenticationCredentialsProvider.clearCredentials; +import static org.eclipse.che.api.git.GitBasicAuthenticationCredentialsProvider.setCurrentCredentials; import static org.eclipse.che.dto.server.DtoFactory.newDto; /** @@ -96,6 +98,7 @@ public void importSources(FolderEntry baseFolder, String location, Map