-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 #1940
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/******************************************************************************* | ||
* 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 com.google.inject.Singleton; | ||
import org.eclipse.che.api.git.shared.ProviderInfo; | ||
|
||
/** | ||
* Credentials provider for Git basic authentication | ||
* | ||
* @author Yossi Balan | ||
*/ | ||
@Singleton | ||
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 String getId() { | ||
return BASIC_PROVIDER_NAME; | ||
} | ||
|
||
@Override | ||
public boolean canProvideCredentials(String url) { | ||
return getUserCredential() != null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we provide credentials for all urls? Even ssh? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sleshchenko - only http or https |
||
} | ||
|
||
@Override | ||
public ProviderInfo getProviderInfo() { | ||
return new ProviderInfo(BASIC_PROVIDER_NAME); | ||
} | ||
|
||
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 |
---|---|---|
|
@@ -81,6 +81,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.plugin.ssh.key.script.SshKeyProvider; | ||
import org.eclipse.che.commons.proxy.ProxyAuthenticator; | ||
import org.eclipse.jgit.api.AddCommand; | ||
|
@@ -515,7 +516,7 @@ protected void onEndTask(String taskName, int workCurr, int workTotal, int perce | |
} | ||
}); | ||
|
||
executeRemoteCommand(remoteUri, cloneCommand); | ||
executeRemoteCommand(remoteUri, cloneCommand , request); | ||
|
||
StoredConfig repositoryConfig = getRepository().getConfig(); | ||
GitUser gitUser = getUser(); | ||
|
@@ -696,7 +697,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: ")) { | ||
|
@@ -1013,7 +1014,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) { | ||
|
@@ -1081,7 +1082,7 @@ public PushResponse push(PushRequest request) throws GitException, UnauthorizedE | |
} | ||
try { | ||
@SuppressWarnings("unchecked") | ||
Iterable<PushResult> pushResults = (Iterable<PushResult>)executeRemoteCommand(remoteUri, pushCommand); | ||
Iterable<PushResult> pushResults = (Iterable<PushResult>)executeRemoteCommand(remoteUri, pushCommand, request); | ||
PushResult pushResult = pushResults.iterator().next(); | ||
String commandOutput = pushResult.getMessages().isEmpty() ? "Successfully pushed to " + remoteUri : pushResult.getMessages(); | ||
Collection<RemoteRefUpdate> refUpdates = pushResult.getRemoteUpdates(); | ||
|
@@ -1589,10 +1590,11 @@ public boolean accept(File dir) { | |
* @throws UnauthorizedException | ||
*/ | ||
@VisibleForTesting | ||
Object executeRemoteCommand(String remoteUrl, TransportCommand command) | ||
Object executeRemoteCommand(String remoteUrl, TransportCommand command, GitRequest request) | ||
throws GitException, GitAPIException, UnauthorizedException { | ||
File keyDirectory = null; | ||
UserCredential credentials = null; | ||
|
||
try { | ||
if (GitUrlUtils.isSSH(remoteUrl)) { | ||
keyDirectory = Files.createTempDir(); | ||
|
@@ -1622,14 +1624,19 @@ protected JSch getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException | |
String password = remoteUrl.substring(remoteUrl.lastIndexOf(":") + 1, remoteUrl.indexOf("@")); | ||
command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password)); | ||
} else { | ||
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please format this according to https://github.com/codenvy/codenvy-dev-resources/wiki/Formatting#for-assignment-operator-the-break-comes-after-the-symbol. |
||
if (gitUser != null && gitPassword != null) { | ||
command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(gitUser, gitPassword)); | ||
} else { | ||
credentials = credentialsLoader.getUserCredential(remoteUrl); | ||
if (credentials != null) { | ||
command.setCredentialsProvider( | ||
new UsernamePasswordCredentialsProvider(credentials.getUserName(), credentials.getPassword())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
ProxyAuthenticator.initAuthenticator(remoteUrl); | ||
return command.call(); | ||
} catch (GitException | TransportException exception) { | ||
|
@@ -1656,7 +1663,6 @@ protected JSch getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException | |
throw new GitException("Can't remove SSH key directory", exception); | ||
} | ||
} | ||
|
||
ProxyAuthenticator.resetAuthenticator(); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also please fix java doc of getAuthenticateUrl method and describe that it can return null value and add org.eclipse.che.commons.annotation.Nullable for it