Skip to content

Commit

Permalink
Add support of BasicAuthentication Authentication to Git
Browse files Browse the repository at this point in the history
Change-Id: I16aed5c77cf321173c5259c26b014b80366a55c3
Signed-off-by: i053322 <yossi.balan@sap.com>
  • Loading branch information
i053322 committed Sep 5, 2016
1 parent 988c2c8 commit cad939e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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: ")) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -942,7 +943,7 @@ public PushResponse push(PushRequest request) throws GitException, UnauthorizedE
}

@SuppressWarnings("unchecked")
Iterable<PushResult> pushResults = (Iterable<PushResult>)executeRemoteCommand(remoteUri, pushCommand);
Iterable<PushResult> pushResults = (Iterable<PushResult>)executeRemoteCommand(remoteUri, pushCommand, request);
PushResult pushResult = pushResults.iterator().next();
return addCommandOutputUpdates(pushResponseDto, request, pushResult);
} catch (GitAPIException exception) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.eclipse.che.api.git;

import org.eclipse.che.api.git.server.dto.DtoServerImpls;
import org.eclipse.che.api.git.shared.GitUser;
import org.slf4j.LoggerFactory;

/**
* Created by I053322 on 9/4/2016.
*/
public class GitBasicAuthenticationCredentialsProvider implements CredentialsProvider {

private static ThreadLocal<UserCredential> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -96,6 +98,7 @@ public void importSources(FolderEntry baseFolder, String location, Map<String, S
LineConsumerFactory consumerFactory)
throws ForbiddenException, ConflictException, UnauthorizedException, IOException, ServerException {
GitConnection git = null;
boolean credentialsHaveBeenSet = false;
try {
// For factory: checkout particular commit after clone
String commitId = null;
Expand Down Expand Up @@ -124,6 +127,12 @@ public void importSources(FolderEntry baseFolder, String location, Map<String, S
recursiveEnabled = true;
}
branchMerge = parameters.get("branchMerge");
final String user = parameters.get("userName");
final String pass = parameters.get("password");
if (user != null && pass != null) {
credentialsHaveBeenSet = true;
setCurrentCredentials(user, pass);
}
}
// Get path to local file. Git works with local filesystem only.
final String localPath = localPathResolver.resolve((VirtualFileImpl)baseFolder.getVirtualFile());
Expand Down Expand Up @@ -186,6 +195,9 @@ public void importSources(FolderEntry baseFolder, String location, Map<String, S
if (git != null) {
git.close();
}
if (credentialsHaveBeenSet) {
clearCredentials();
}
}
}

Expand Down

0 comments on commit cad939e

Please sign in to comment.