Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authenticate JIRA REST API using OAuth with Node.JS

I'm building a client application in Node.js for creating new JIRA issues and I want to authenticate users using OAuth. The Atlassian docs are pretty bad for Jira and Oauth newcomers. So, I'm looking for a single example that describes exactly how to set up the JIRA application link, and the how to build a basic app in node that connects to Jira via OAuth. I'm not sure where else to look. (I'm using JIRA v6.0.4)

like image 298
Colyn Brown Avatar asked Jul 16 '13 07:07

Colyn Brown


People also ask

Can OAuth be used for REST API?

OAuth is an authorization framework that enables an application or service to obtain limited access to a protected HTTP resource. To use REST APIs with OAuth in Oracle Integration, you need to register your Oracle Integration instance as a trusted application in Oracle Identity Cloud Service.


2 Answers

There is an example for Node.JS with OAuth in the Atlassian repo that Brian also mentioned. I think it is for the 2-legged authentication.

It needs a pre-negotiated consumer key already set up by you. Here is an example how to obtain a token you can save in your config file: https://developer.atlassian.com/jiradev/api-reference/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-oauth-authentication

like image 126
Koshinae Avatar answered Sep 18 '22 16:09

Koshinae


Here's a blog describing node.js and jira authentication using Oauth

It is in an express framework. I paste some part of the code below.

var base_url = "YOUR_JIRA_BASE_URL"; //example https://test.atlassian.net

app.get('/jira', function(req, res) {

var oa = new OAuth(base_url + "/plugins/servlet/oauth/request-token", //request token
    base_url + "/plugins/servlet/oauth/access-token", //access token
    "mykey", //consumer key 
    "YOUR_PEM_FILE_CONTENT", //consumer secret, eg. fs.readFileSync('jira.pem', 'utf8')
    '1.0', //OAuth version
    "http://localhost:1337/jira/callback", //callback url
    "RSA-SHA1");
oa.getOAuthRequestToken(function(error, oauthToken, oauthTokenSecret) {
    if (error) {
        console.log(error.data);
        response.send('Error getting OAuth access token');
    } else {
        req.session.oa = oa;
        req.session.oauth_token = oauthToken;
        req.session.oauth_token_secret = oauthTokenSecret;
        return res.redirect(base_url + "/plugins/servlet/oauth/authorize?oauth_token=" + oauthToken);
    }
});
});

If anyone is confused about any part of the code, you can add comment to this answer.

like image 20
L4reds Avatar answered Sep 18 '22 16:09

L4reds