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.
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.
For the Authorization header:
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.
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:
Content-Type: application/json
Add a basic authentication header by first Base64 encoding username and password:
var authCode = window.btoa( user + ":" + password );
var authHeader = "Authorization: Basic " + authCode;
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With