Making an authenticated REST call from within a Jira listener

SCic December 31, 2022

Hi,

I've written a Jira Java plugin which implements a listener for issue update events.

Once an event occurs, I need to make a REST call to the Jira server, using the current authentication (the user that initiated the issue update).

I know how to make the call to the connected Confluence server, using code similar to this:

String url = "/rest/myrest/1.0/test";

ApplicationLink appLinkapplicationLinkService.getPrimaryApplicationLink(ConfluenceApplicationType.class);  

ApplicationLinkRequestFactory applicationLinkRequestFactory = appLink.createAuthenticatedRequestFactory();            

ApplicationLinkRequest request = applicationLinkRequestFactory.createRequest(MethodType.GET, url);

String response = request.execute();

This works fine and performs the proper authentication.

Is there a service similar to the ApplicationLinkService which can make the authenticated REST call to my own Jira server?

I know I can make a a simple REST call using any Java REST client, but since I don't know the username & password, I can not perform the authentication.

Any ideas?

 

Thank You,

Shay

1 answer

2 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 10, 2023

Yes, take a look at com.atlassian.sal.api.net.TrustedRequestFactory

Something like this:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.sal.api.net.Request;
import com.atlassian.sal.api.net.TrustedRequest;
import com.atlassian.sal.api.net.TrustedRequestFactory;
import javax.ws.rs.core.UriBuilder;
import java.net.URI;


String url = "https://example.com/path?query=value";
URI uri = UriBuilder.fromUri(url).build();

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

TrustedRequestFactory trustedRequestFactory = ComponentAccessor.getComponent(TrustedRequestFactory.class);
TrustedRequest request = trustedRequestFactory.createTrustedRequest(Request.MethodType.GET, url);
request.addTrustedTokenAuthentication(uri.getHost(), currentUser.getName());
String response = request.execute();

Alfira Merinova February 7, 2024

Hi @Peter-Dave Sheehan ,

Thank you for sharing that solution. I tried it in ScriptRunner plugin Console.

TrustedRequestFactory trustedRequestFactory = ComponentAccessor.getComponent(TrustedRequestFactory.class)

When I run the code, a trustedRequestFactory is null. Could you please share how to resolve it, because like in raised question I need to call Jira REST API from ScriptRunner plugin installed on the same Jira instance?

 

Jira Server v9.4.14
ScriptRunner v.8.1.0
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 7, 2024

I don't recall if that code used to work on older Jira versions, or if I just made a typo in my original answer.

There are 2 ways you can get that component from scriptrunner with current versions:

 

TrustedRequestFactory trustedRequestFactory = ComponentAccessor.getOSGiComponentInstanceOfType(TrustedRequestFactory)

Or

import com.onresolve.scriptrunner.runner.customisers.PluginModule
@PluginModule TrustedRequestFactory trustedRequestFactory
Like Alfira Merinova likes this
Alfira Merinova February 7, 2024

Thanks a lot! First solution worked. It's very cool.

TrustedRequestFactory trustedRequestFactory = ComponentAccessor.getOSGiComponentInstanceOfType(TrustedRequestFactory)

 

Suggest an answer

Log in or Sign up to answer