Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of BasicAuthentication Authentication to Git #736

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the license header


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
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this javadock in common way:
/**
* <description>
*
* @author <author>
Like you did it in PR to 4.x

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