How to perform the cookie base authentication through groovy in post script function.

i want to cookie base authentication through script post function to call the rest api.

1 answer

Thanks Alexey, but this article is not opening as it says that we are unable to find the page you are looking for.

Alexey Matveev Community Champion Nov 14, 2017

try this one

https://developer.atlassian.com/jiradev/jira-apis/about-the-jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-cookie-based-authentication

In short you would need to do the following:

1. Connect to Jira and get the cookie:

curl -X POST \
  http://jira.example.com:8090/jira/rest/auth/1/session \
  -H 'content-type: application/json' \
  -d '{ "username": "myuser", "password": "mypassword" }'

It will return it the code like this

{
   "loginInfo" : {
      "lastFailedLoginTime" : "2013-11-27T09:43:28.839+0000",
      "previousLoginTime" : "2013-12-04T07:54:59.824+0000",
      "loginCount" : 2,
      "failedLoginCount" : 1
   },
   "session" : {
      "name" : "JSESSIONID",
      "value" : "6E3487971234567896704A9EB4AE501F"
   }
}

2. Use the cookie in the next REST API calls:

Take the value for JSESSIONID and add it to the next REST API call. For example like this


var Client = require('node-rest-client').Client; client = new Client(); // Provide user credentials, which will be used to log in to JIRA. var loginArgs = { data: { "username": "admin", "password": "admin" }, headers: { "Content-Type": "application/json" } }; client.post("http://localhost:8090/jira/rest/auth/1/session", loginArgs, function(data, response){ if (response.statusCode == 200) { console.log('succesfully logged in, session:', data.session); var session = data.session; // Get the session information and store it in a cookie in the header var searchArgs = { headers: { // Set the cookie from the session information cookie: session.name + '=' + session.value, "Content-Type": "application/json" }, data: { // Provide additional data for the JIRA search. You can modify the JQL to search for whatever you want. jql: "type=Bug AND status=Closed" } }; // Make the request return the search results, passing the header information including the cookie. client.post("http://localhost:8090/jira/rest/api/2/search", searchArgs, function(searchResult, response) { console.log('status code:', response.statusCode); console.log('search result:', searchResult); }); } else { throw "Login failed :("; } });

 That was code for Node.js. In post function you have to change it.  If you have any futher question , then ask

@Alexey Matveev

i do not have the password of the user, if i would have it , i would have gone for the basic authentication, but to overcome this i want to get the cookie(JSESSIONID) so by passing it i could perform the cookie base authentication.

this has become the obstacle for me as i want to create the page in confluence on the script post function and to do this i have to perform the cookie base or oauth authentication but im unable to proceed with this. hoping to see a resolution from you on this problem.

Thanking in anticipation.

Regards

Aetisam

Alexey Matveev Community Champion Nov 14, 2017

JSESSIONID received after connection. You can not get it if you are not connected

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,274 views 14 20
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot