Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Basic Authentication with JIRA REST API in JavaScript?

I'm creating a JavaScript app for a Smart TV to show dashboards on the tv. I get the list of dashboards with the JIRA REST API. The url I use for this is:

jira/rest/api/2/dashboard?startAt=&maxResults=

afterwards I create a wallboard as followed to show them on the tv:

jira/plugins/servlet/Wallboard/?dashboardId=&os_username=&os_password=

because of the os_username and os_password, JIRA knows i'm authenticated and gets the right list. this list is the one i need from the beginning but because i call the url above with the parameters os_username and os_password it does get the right list

but on start up of the tv/the first time i get the list of dashboards with JIRA there is no one authenticated so it takes a random list, not the one i need to get.

there is some way to authenticate in JIRA with the command below:

curl -D- -u fred:fred -X GET -H "Content-Type: application/json"         http://example.com/rest/api/2/issue/createmeta

but i don't know how to use that command in JavaScript.

So can anyone tell me how i can authenticate in JIRA with basic authentication and very important IT HAS TO BE JAVASCRIPT.

like image 956
user2096102 Avatar asked Feb 21 '13 15:02

user2096102


2 Answers

You got this from here, I presume. Well on the same page it is explained how to "do it yourself". I will "translate" the steps that you need to do in order to manage to do the same request in JS.

  1. Request Method should be GET
  2. You should have 2 headers: Authorization Header and Content-Type Header. Your Content-type header should looks like: "Content-Type: application/json"

For the Authorization header:

  1. Build a string of the form username:password
  2. Base64 encode the string (Use window.btoa() and window.atob()) - You actually DO NOT need the second one but I put it there for reference
  3. Supply an "Authorization" header with content "Basic " followed by the encoded string. For example, the string "fred:fred" encodes to "ZnJlZDpmcmVk" in base64, so your Authorization Header should look like": "Authorization: Basic ZnJlZDpmcmVk"

So in the end you need to make your GET request with the two Headers supplied and you should be Authorized. Also BEWARE Base64 is reverseable and NOT an encryption function. It is encoding. The request will go through HTTPS of course, but the Base64 encoded string could be reversed if SSL is broken.

like image 152
Borislav Sabev Avatar answered Oct 27 '22 00:10

Borislav Sabev


In case you are having your own JIRA instance you should whitelist your site to enable CORS for your domain first:

If you are a JIRA Server customer, simply go to the "Whitelist" section of JIRA Administration and add the domains you wish to request resources from.

Otherwise your request would end in an error, as it violates the browsers same-origin policy (http://enable-cors.org/).

Important: If you are using JIRA on demand / cloud, you won't be able to successfully access the API with Basic Authentication using pure JavaScript / client-side code. This is due to security reasons, and unfortunately there seems to be no solution provided by the Atlassian team yet. One solution to solve that, could be setting up a proxy on your server, which forwards your JavaScript requests from the browser to the actual JIRA instance.

To avoid any possible problems due to bugs in your code, I highly suggest to try access to the JIRA API by using CURL as a first step:

  curl -D- -X GET -H "Authorization: Basic %BASE64-CREDENTIDALS%" -H "Content-Type: application/json" "https://webjets.atlassian.net/rest/api/2/issue/WJ-333"

Note that you have to replace %BASE64-CREDENTIDALS% with your actual Base64 encoded user/password pair. It is important to know, that this works only with the JIRA username, not with the JIRA users email address. This might be confusing, as normally you are also able to sign-in at JIRA using the email adress but won't work with the API.

After having white-listed your domain you are ready to go - build a GET request with following details:

  1. Add a content type header: Content-Type: application/json
  2. Add a basic authentication header by first Base64 encoding username and password:

    var authCode = window.btoa( user + ":" + password ); var authHeader = "Authorization: Basic " + authCode;

  3. Perform the request on an JIRA API endpoint URL, like:

    https://%PROJECT%.atlassian.net/rest/api/2/issue/%ISSUE-ID%

See StackOverflow question "How to use Basic Auth with jQuery and AJAX?" for approaches how to perform the request using jQuery.

Security notice: Be aware that the authorization code is not an encrypted value but an encoding. Take care where you store or provide it!

like image 44
david Avatar answered Oct 27 '22 00:10

david