atlassian-oauth-helper

0.0.3 • Public • Published

atlassian-oauth-helper

Allows simple configuration for Atlassian OAuth consumers using express.

npm install atlassian-oauth-helper

Setup an incoming Application Link

You must setup an incoming Application Link before connecting to an Atlassian product using OAuth.

  1. Go to the admin section of the Atlassian product you wish to connect to.
  2. Select "Application Links".
  3. Enter the URL of your application (don't worry, you can change it later) and click "Create new link".
  4. Enter your Application Name (this is just for reference on the admin screen, select "Generic Application" for the Application Type, check "Create incoming link", and click Continue.
  5. Enter a Consumer Key (this is used in your application code), a Consumer Name (this is shown to users) and a public key. (Public and private key generation is not covered here.)

Usage

var express = require('express'),
    logger = require('morgan'),
    session = require('express-session');

var app = express();

app.use(logger('tiny'));
app.use(session({ secret: "ssshhhh!", resave: false, saveUninitialized: false }));

var oauth = require('atlassian-oauth-helper');

oauth.configure({
    host: "bamboo.atlassian.com",
    consumerKey: "oauth-sample-consumer",
    privateKeyFile: "./rsa.pem",
    callbackUrl: "http://localhost:8080/oauth/access_token"
});

app.use(function(request, response, next) {
    response.locals.session = request.session;
    next();
});

app.get('/', function (request, response) {
    oauth.get("https://bamboo.atlassian.com/rest/api/latest/plan/MY-PLAN.json",
        request.session.oauthAccessToken,
        request.session.oauthAccessTokenSecret,
        function (error, data, resp) {
            response.send(data);
        }
    );
});

app.use('/oauth/request_token', oauth.requestTokenRouter);
app.use('/oauth/access_token', oauth.accessTokenRouter);
app.use('/oauth/access_token', function(request, response, next) {
    response.redirect('/');
});

app.listen(parseInt(process.env.PORT || 8080));

API

.configure(configObject) - Configures the OAuth helper for subsequent requests.

oauth.configure({
    host: "bamboo.atlassian.com",                               // The hostname of your Atlassian product.
    consumerKey: "oauth-sample-consumer",                       // The consumer key you configured in your Application Link.
    privateKeyFile: "./rsa.pem",                                // The private key file for the public key you configured in your Application Link.
    callbackUrl: "http://localhost:8080/oauth/access_token"     // The URL where your Atlassian product will redirect to with your request token.
});

.get(url, accessToken, accessTokenSecret, callback) - Makes a request using your existing access token and access token secret.

oauth.get(
    "https://bamboo.atlassian.com/rest/api/latest/plan/MY-PLAN.json",       // The rest API endpoint
    request.session.oauthAccessToken,                                       // Your OAuth access token from session.
    request.session.oauthAccessTokenSecret,                                 // Your OAuth access token secret from session.
    function (error, data, resp) {                                          // The callback to be executed after the request.
        response.send(data);
    }
);

.requestTokenRouter - An express router that will automatically redirect to your configured product.

// This router can be mounted at any URL you like.
app.use('/login', oauth.requestTokenRouter);

.accessTokenRouter - An express router that will authorize a request token received from your configured product.

// This should match the callbackUrl from your configuration.
app.use('/authorize', oauth.accessTokenRouter);

Package Sidebar

Install

npm i atlassian-oauth-helper

Weekly Downloads

0

Version

0.0.3

License

Apache-2.0

Last publish

Collaborators

  • drew.walker