Skip to content

Commit

Permalink
Add bearer token support (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
chcg authored Oct 9, 2024
1 parent cf2ae63 commit 87b530e
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2022 Atlassian
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jenkinsci.plugins.JiraTestResultReporter;

import com.atlassian.httpclient.api.Request;
import com.atlassian.jira.rest.client.api.AuthenticationHandler;

/**
* Handler for Bearer (Token) authentication
*/
public class BearerAuthenticationHandler implements AuthenticationHandler {

private static final String AUTHORIZATION_HEADER = "Authorization";

private final String token;

public BearerAuthenticationHandler(String token) {
this.token = token;
}

@Override
public void configure(Request.Builder builder) {
builder.setHeader(AUTHORIZATION_HEADER, "Bearer " + token);
}

Check warning on line 38 in src/main/java/org/jenkinsci/plugins/JiraTestResultReporter/BearerAuthenticationHandler.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 31-38 are not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ public JiraTestDataPublisherDescriptor() {
private URI jiraUri = null;
private String username = null;
private Secret password = null;
private boolean useBearerAuth = false;
private String defaultSummary;
private String defaultDescription;

Expand All @@ -484,6 +485,10 @@ public Secret getPassword() {
return password;
}

public boolean getUseBearerAuth() {
return useBearerAuth;
}

public String getJiraUrl() {
return jiraUri != null ? jiraUri.toString() : null;
}
Expand Down Expand Up @@ -536,13 +541,23 @@ public MetadataCache.CacheEntry getCacheEntry(String projectKey, String issueTyp
public Object readResolve() {
if (jiraUri != null && username != null && password != null) {
AsynchronousJiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
restClient = factory.createWithBasicHttpAuthentication(jiraUri, username, password.getPlainText());
restClientExtension = new JiraRestClientExtension(
jiraUri,
new AsynchronousHttpClientFactory()
.createClient(
jiraUri,
new BasicHttpAuthenticationHandler(username, password.getPlainText())));
if (useBearerAuth) {
BearerAuthenticationHandler handler = new BearerAuthenticationHandler(password.getPlainText());
restClient = factory.create(jiraUri, handler);

restClientExtension = new JiraRestClientExtension(
jiraUri,
new AsynchronousHttpClientFactory()
.createClient(jiraUri, new BearerAuthenticationHandler(password.getPlainText())));
} else {
restClient = factory.createWithBasicHttpAuthentication(jiraUri, username, password.getPlainText());
restClientExtension = new JiraRestClientExtension(
jiraUri,
new AsynchronousHttpClientFactory()
.createClient(
jiraUri,
new BasicHttpAuthenticationHandler(username, password.getPlainText())));
}
tryCreatingStatusToCategoryMap();
}
return this;
Expand Down Expand Up @@ -575,25 +590,38 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti

username = json.getString("username");
password = Secret.fromString(json.getString("password"));
useBearerAuth = json.getBoolean("useBearerAuth");
defaultSummary = json.getString("summary");
defaultDescription = json.getString("description");

if (json.getString("jiraUrl").equals("")
|| json.getString("username").equals("")
|| json.getString("password").equals("")) {
useBearerAuth = false;
restClient = null;
restClientExtension = null;
save();
return true;
}

AsynchronousJiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
restClient = factory.createWithBasicHttpAuthentication(jiraUri, username, password.getPlainText());
restClientExtension = new JiraRestClientExtension(
jiraUri,
new AsynchronousHttpClientFactory()
.createClient(
jiraUri, new BasicHttpAuthenticationHandler(username, password.getPlainText())));
if (useBearerAuth) {
BearerAuthenticationHandler handler = new BearerAuthenticationHandler(password.getPlainText());
restClient = factory.create(jiraUri, handler);

restClientExtension = new JiraRestClientExtension(
jiraUri,
new AsynchronousHttpClientFactory()
.createClient(jiraUri, new BearerAuthenticationHandler(password.getPlainText())));
} else {
restClient = factory.createWithBasicHttpAuthentication(jiraUri, username, password.getPlainText());
restClientExtension = new JiraRestClientExtension(
jiraUri,
new AsynchronousHttpClientFactory()
.createClient(
jiraUri,
new BasicHttpAuthenticationHandler(username, password.getPlainText())));
}
tryCreatingStatusToCategoryMap();
save();
return super.configure(req, json);
Expand Down Expand Up @@ -639,11 +667,15 @@ public TestDataPublisher newInstance(StaplerRequest req, JSONObject json) throws
* @param jiraUrl

Check warning on line 667 in src/main/java/org/jenkinsci/plugins/JiraTestResultReporter/JiraTestDataPublisher.java

View check run for this annotation

ci.jenkins.io / JavaDoc

JavaDoc @param

NORMAL: no description for @param
* @param username

Check warning on line 668 in src/main/java/org/jenkinsci/plugins/JiraTestResultReporter/JiraTestDataPublisher.java

View check run for this annotation

ci.jenkins.io / JavaDoc

JavaDoc @param

NORMAL: no description for @param
* @param password

Check warning on line 669 in src/main/java/org/jenkinsci/plugins/JiraTestResultReporter/JiraTestDataPublisher.java

View check run for this annotation

ci.jenkins.io / JavaDoc

JavaDoc @param

NORMAL: no description for @param
* @param useBearerAuth

Check warning on line 670 in src/main/java/org/jenkinsci/plugins/JiraTestResultReporter/JiraTestDataPublisher.java

View check run for this annotation

ci.jenkins.io / JavaDoc

JavaDoc @param

NORMAL: no description for @param
* @return

Check warning on line 671 in src/main/java/org/jenkinsci/plugins/JiraTestResultReporter/JiraTestDataPublisher.java

View check run for this annotation

ci.jenkins.io / JavaDoc

JavaDoc @return

NORMAL: no description for @return
*/
@RequirePOST
public FormValidation doValidateGlobal(
@QueryParameter String jiraUrl, @QueryParameter String username, @QueryParameter String password) {
@QueryParameter String jiraUrl,
@QueryParameter String username,
@QueryParameter String password,
@QueryParameter boolean useBearerAuth) {

Jenkins.get().checkPermission(Jenkins.ADMINISTER);
String serverName;
Expand All @@ -657,8 +689,14 @@ public FormValidation doValidateGlobal(
// JIRA does not offer ways to validate username and password, so we try to query some server
// metadata, to see if the configured user is authorized on this server
AsynchronousJiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
JiraRestClient restClient =
factory.createWithBasicHttpAuthentication(uri, username, pass.getPlainText());
JiraRestClient restClient;
if (useBearerAuth) {
BearerAuthenticationHandler handler = new BearerAuthenticationHandler(pass.getPlainText());
restClient = factory.create(uri, handler);
} else {
restClient = factory.createWithBasicHttpAuthentication(uri, username, pass.getPlainText());

Check warning on line 697 in src/main/java/org/jenkinsci/plugins/JiraTestResultReporter/JiraTestDataPublisher.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 489-697 are not covered by tests
}

MetadataRestClient client = restClient.getMetadataClient();
Promise<ServerInfo> serverInfoPromise = client.getServerInfo();
ServerInfo serverInfo = serverInfoPromise.claim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
<f:entry title="Password">
<f:password field="password"/>
</f:entry>

<f:entry title="Use Bearer authentication instead of Basic authentication" field="useBearerAuth" >
<f:checkbox/>
</f:entry>

<f:validateButton title="Validate settings"
method="validateGlobal" with="jiraUrl,username,password" />
method="validateGlobal" with="jiraUrl,username,password,useBearerAuth" />
<f:advanced>
<f:entry title="Default Summary" field="summary">
<f:textbox field="summary" default="${descriptor.defaultSummary}"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
Uses password data as input for Bearer token, PAT(personal access token) access to enterprise jira (https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html).
Bearer authentication is only supported in Jira Server, for Jira Cloud leave this unchecked
</div>

0 comments on commit 87b530e

Please sign in to comment.