Skip to content

[JENKINS-70897] Add support for personal access token authentication #1566

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

Open
wants to merge 3 commits into
base: master
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
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ Refer to the link:https://git-scm.com/book/en/v2/Git-Internals-The-Refspec[git r

The git plugin supports username / password credentials and private key credentials provided by the
https://plugins.jenkins.io/credentials[Jenkins credentials plugin].
It does not support other credential types like secret text, secret file, or certificates.
It does not support other credential types like secret file or certificates.
Select credentials from the job definition drop down menu or enter their identifiers in Pipeline job definitions.

When the remote repository is accessed with the **HTTP or HTTPS protocols**, the plugin requires a **username / password credential**.
Expand Down
43 changes: 29 additions & 14 deletions src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package hudson.plugins.git;

import com.cloudbees.plugins.credentials.CredentialsMatcher;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import com.google.common.collect.Iterables;

Expand Down Expand Up @@ -64,6 +62,7 @@
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import org.jenkinsci.plugins.gitclient.*;
import org.jenkinsci.plugins.plaincredentials.StringCredentials;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
Expand Down Expand Up @@ -924,7 +923,7 @@ private GitClient createClient(TaskListener listener, EnvVars environment, @NonN
listener.getLogger().println("No credentials specified");
} else {
String url = getParameterString(uc.getUrl(), environment);
StandardUsernameCredentials credentials = lookupScanCredentials(build, url, ucCredentialsId);
var credentials = lookupScanCredentials(build, url, ucCredentialsId);
if (credentials != null) {
c.addCredentials(url, credentials);
if(!isHideCredentials()) {
Expand All @@ -943,19 +942,35 @@ private GitClient createClient(TaskListener listener, EnvVars environment, @NonN
return c;
}

private static StandardUsernameCredentials lookupScanCredentials(@NonNull Run<?, ?> build,
@CheckForNull String url,
@CheckForNull String ucCredentialsId) {
private static StandardCredentials lookupScanCredentials(@NonNull Run<?, ?> build,
@CheckForNull String url,
@CheckForNull String ucCredentialsId) {
if (Util.fixEmpty(ucCredentialsId) == null) {
return null;
} else {
StandardUsernameCredentials c = CredentialsProvider.findCredentialById(
ucCredentialsId,
StandardUsernameCredentials.class,
build,
URIRequirementBuilder.fromUri(url).build());
return c != null && GitClient.CREDENTIALS_MATCHER.matches(c) ? c : null;
}

StandardCredentials c;
c = CredentialsProvider.findCredentialById(
ucCredentialsId,
StandardUsernameCredentials.class,
build,
URIRequirementBuilder.fromUri(url).build());

if (c != null && GitClient.CREDENTIALS_MATCHER.matches(c)) {
return c;
}

c = CredentialsProvider.findCredentialById(
ucCredentialsId,
StringCredentials.class,
build,
URIRequirementBuilder.fromUri(url).build());

if (c != null && GitClient.CREDENTIALS_MATCHER.matches(c)) {
return c;
}

return null;
}

@NonNull
Expand Down