It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

Muhammad Ali Nov 13, 2017

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

1 answer

Muhammad Ali Nov 14, 2017

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 [cPrime] Community Leader 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

Muhammad Ali Nov 14, 2017

@Alexey Matveev [cPrime]

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 [cPrime] Community Leader Nov 14, 2017

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

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

994 views 12 15
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you