Skip to content

Commit 2643a56

Browse files
authored
Fix JENKINS-67807: Use system level access to fetch the access token (#61)
1 parent c2612a7 commit 2643a56

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/main/java/org/jenkinsci/plugin/gitea/GiteaSCMFileSystem.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,20 @@
3131
import edu.umd.cs.findbugs.annotations.NonNull;
3232
import hudson.Extension;
3333
import hudson.model.Item;
34+
import hudson.model.Queue.Task;
3435
import hudson.scm.SCM;
3536
import hudson.scm.SCMDescriptor;
37+
import hudson.security.ACL;
3638
import java.io.IOException;
3739
import jenkins.authentication.tokens.api.AuthenticationTokens;
38-
import jenkins.model.Jenkins;
3940
import jenkins.scm.api.SCMFile;
4041
import jenkins.scm.api.SCMFileSystem;
4142
import jenkins.scm.api.SCMHead;
4243
import jenkins.scm.api.SCMRevision;
4344
import jenkins.scm.api.SCMSource;
4445
import jenkins.scm.api.SCMSourceDescriptor;
4546
import jenkins.scm.api.SCMSourceOwner;
47+
import org.acegisecurity.Authentication;
4648
import org.apache.commons.lang.StringUtils;
4749
import org.jenkinsci.plugin.gitea.client.api.Gitea;
4850
import org.jenkinsci.plugin.gitea.client.api.GiteaAuth;
@@ -146,20 +148,24 @@ public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @Ch
146148
SCMSourceOwner owner = source.getOwner();
147149
String serverUrl = src.getServerUrl();
148150
String credentialsId = src.getCredentialsId();
149-
StandardCredentials credentials = StringUtils.isBlank(credentialsId)
150-
? null
151-
: CredentialsMatchers.firstOrNull(
151+
StandardCredentials credentials = null;
152+
if (!StringUtils.isBlank(credentialsId)) {
153+
Authentication authentication = owner instanceof Task
154+
? ((Task) owner).getDefaultAuthentication()
155+
: ACL.SYSTEM;
156+
credentials = CredentialsMatchers.firstOrNull(
152157
CredentialsProvider.lookupCredentials(
153158
StandardCredentials.class,
154159
owner,
155-
Jenkins.getAuthentication(),
160+
authentication,
156161
URIRequirementBuilder.fromUri(serverUrl).build()
157162
),
158163
CredentialsMatchers.allOf(
159164
AuthenticationTokens.matcher(GiteaAuth.class),
160165
CredentialsMatchers.withId(credentialsId)
161166
)
162-
);
167+
);
168+
}
163169
if (owner != null) {
164170
CredentialsProvider.track(owner, credentials);
165171
}

0 commit comments

Comments
 (0)