UNPKG

synergia-jira-connector

Version:

Easy to use NodeJS wrapper for the Jira REST API.

253 lines (218 loc) 10.9 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: lib/oauth_util.js</title> <script src="scripts/prettify/prettify.js"></script> <script src="scripts/prettify/lang-css.js"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: lib/oauth_util.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>"use strict"; // npm packages var Oauth = require('oauth'); // Core packages var url = require('url'); // Custom Packages var errorStrings = require('./error'); /** * @namespace OauthUtil */ /** * Attempts to get an OAuth verification URL using the given API configuration. * * @memberOf OauthUtil * @param {Object} config The information needed to access the Jira API * @param {string} config.host The hostname of the Jira API. * @param {string} [config.protocol=https] - The protocol used to accses the Jira API. * @param {number} [config.port=443] - The port number used to connect to Jira. * @param {string} [config.version=2] - The version of the Jira API to which you will be connecting. Currently, only * version 2 is supported. * @param {Object} config.oauth The oauth information * @param {string} config.oauth.consumer_key The consumer key of the application accessing Jira. * @param {string} config.oauth.private_key The private key of the application accessing Jira. * @param {string} [config.oauth.callback_url] The callback URL to be called after the token is generated. If this is * not included, the user will be given a verification code after authorizing the token, instead of Jira making a * callback to the application. * @param {OauthUtil~getOauthUrlCallback} callback The function called when the URL has been retrieved. */ exports.getAuthorizeURL = function (config, callback) { var AUTH_TOKEN_APPEND = '/oauth/authorize'; var SERVLET_BASE_URL = '/plugins/servlet'; var authURL = url.format({ protocol: config.protocol ? config.protocol : 'https', hostname: config.host, port: config.port ? config.port : null, pathname: SERVLET_BASE_URL + AUTH_TOKEN_APPEND }); var oauth = generateOAuthObject(config); oauth.getOAuthRequestToken(function (err, token, token_secret) { if (err) { return callback(err); } return callback(null, {url: authURL + "?oauth_token=" + token, token: token, token_secret: token_secret}); }); }; /** * Given an OAuth token, the token secret, and an access verification code (provided by Jira), swap an OAuth request * token with an OAuth access token. * * @memberOf OauthUtil * @param {Object} config The information needed to access the Jira API * @param {string} config.host The hostname of the Jira API. * @param {string} [config.protocol=https] - The protocol used to accses the Jira API. * @param {number} [config.port=443] - The port number used to connect to Jira. * @param {string} [config.version=2] - The version of the Jira API to which you will be connecting. Currently, only * version 2 is supported. * @param {Object} config.oauth The oauth information * @param {string} config.oauth.consumer_key The consumer key of the application accessing Jira. * @param {string} config.oauth.private_key The private key of the application accessing Jira. * @param {string} config.oauth.token The OAuth Token supplied by Jira. * @param {string} config.oauth.token_secret The OAuth Token secret supplied by Jira. * @param {string} config.oauth.oauth_verifier The verified code given to the user after authorizing the OAuth token. * @param {OauthUtil~swapRequestTokenCallback} callback The function called when the token has been swapped. */ exports.swapRequestTokenWithAccessToken = function(config, callback) { if(!config.oauth.oauth_verifier) { throw new Error(errorStrings.NO_VERIFIER_ERROR); } var oauth = generateOAuthObject(config); var token = config.oauth.token; var secret = config.oauth.token_secret; var verifier = config.oauth.oauth_verifier; oauth.getOAuthAccessToken(token, secret, verifier, callback); }; /** * Utility function to generate an OAuth object. * * @memberOf OauthUtil * @param {Object} config The information needed to access the Jira API * @param {string} config.host The hostname of the Jira API. * @param {string} [config.protocol=https] - The protocol used to accses the Jira API. * @param {number} [config.port=443] - The port number used to connect to Jira. * @param {string} [config.version=2] - The version of the Jira API to which you will be connecting. Currently, only * version 2 is supported. * @param {Object} config.oauth The oauth information * @param {string} config.oauth.consumer_key The consumer key of the application accessing Jira. * @param {string} config.oauth.private_key The private key of the application accessing Jira. * @param {string} [config.oauth.callback_url] The callback URL to be called after the token is generated. If this is * not included, the user will be given a verification code after authorizing the token, instead of Jira making a * callback to the application. * * @returns {exports.OAuth} The generated object. */ function generateOAuthObject(config) { var SERVLET_BASE_URL = '/plugins/servlet'; var REQ_TOKEN_APPEND = '/oauth/request-token'; var ACCESS_TOKEN_APPEND = '/oauth/access-token'; var sig = 'RSA-SHA1'; if (!config.host) { throw new Error(errorStrings.NO_HOST_ERROR); } else if (!config.oauth.consumer_key) { throw new Error(errorStrings.NO_CONSUMER_KEY_ERROR); } else if (!config.oauth.private_key) { throw new Error(errorStrings.NO_PRIVATE_KEY_ERROR); } var consumer_key = config.oauth.consumer_key; var private_key = config.oauth.private_key; var reqURL = url.format({ protocol: config.protocol ? config.protocol : 'https', hostname: config.host, port: config.port ? config.port : null, pathname: SERVLET_BASE_URL + REQ_TOKEN_APPEND }); var accessURL = url.format({ protocol: config.protocol ? config.protocol : 'https', hostname: config.host, port: config.port ? config.port : null, pathname: SERVLET_BASE_URL + ACCESS_TOKEN_APPEND }); var cb = config.oauth.callback_url ? config.oauth.callback_url : 'oob'; return new Oauth.OAuth(reqURL, accessURL, consumer_key, private_key, '1.0', cb, sig); } /** * Callback used by getOauthUrl. * @callback OauthUtil~getOauthUrlCallback * @param {*} error The error which occurred, if any. * @param {Object} oauth The OAuth information retrieved from the Jira API. * @param {String} oauth.url The URL that should be visited by the user to verify the OAuth access. * @param {String} oauth.token The OAuth Token retrieved from the Jira API. * @param {String} oauth.token_secret The OAuth Token Secret retrieved from the Jira API. */ /** * Callback used by swapRequestTokenWithAccessToken * @callback OauthUtil~swapRequestTokenCallback * @param {*} error The error which occurred, if any. * @param {string} access_token The access token retrieved from Jira. */</code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2> <h3>Classes</h3> <ul> <li><a href="ApplicationPropertiesClient.html">ApplicationPropertiesClient</a></li> <li><a href="AttachmentClient.html">AttachmentClient</a></li> <li><a href="AuditingClient.html">AuditingClient</a></li> <li><a href="AvatarClient.html">AvatarClient</a></li> <li><a href="CommentClient.html">CommentClient</a></li> <li><a href="ComponentClient.html">ComponentClient</a></li> <li><a href="CustomFieldOptionClient.html">CustomFieldOptionClient</a></li> <li><a href="DashboardClient.html">DashboardClient</a></li> <li><a href="FieldClient.html">FieldClient</a></li> <li><a href="FilterClient.html">FilterClient</a></li> <li><a href="GroupClient.html">GroupClient</a></li> <li><a href="GroupsClient.html">GroupsClient</a></li> <li><a href="GroupUserPickerClient.html">GroupUserPickerClient</a></li> <li><a href="IssueClient.html">IssueClient</a></li> <li><a href="IssueLinkClient.html">IssueLinkClient</a></li> <li><a href="IssueLinkTypeClient.html">IssueLinkTypeClient</a></li> <li><a href="IssueTypeClient.html">IssueTypeClient</a></li> <li><a href="JiraClient.html">JiraClient</a></li> <li><a href="JqlClient.html">JqlClient</a></li> <li><a href="LicenseRoleClient.html">LicenseRoleClient</a></li> <li><a href="LicenseValidatorClient.html">LicenseValidatorClient</a></li> <li><a href="MyPermissionsClient.html">MyPermissionsClient</a></li> <li><a href="MyPreferencesClient.html">MyPreferencesClient</a></li> <li><a href="MyselfClient.html">MyselfClient</a></li> <li><a href="PasswordClient.html">PasswordClient</a></li> <li><a href="PriorityClient.html">PriorityClient</a></li> <li><a href="ProjectClient.html">ProjectClient</a></li> <li><a href="ProjectValidateClient.html">ProjectValidateClient</a></li> <li><a href="ReindexClient.html">ReindexClient</a></li> <li><a href="ResolutionClient.html">ResolutionClient</a></li> <li><a href="ScreensClient.html">ScreensClient</a></li> <li><a href="SearchClient.html">SearchClient</a></li> <li><a href="SecurityLevelClient.html">SecurityLevelClient</a></li> <li><a href="ServerInfoClient.html">ServerInfoClient</a></li> <li><a href="SettingsClient.html">SettingsClient</a></li> <li><a href="StatusCategoryClient.html">StatusCategoryClient</a></li> <li><a href="StatusClient.html">StatusClient</a></li> <li><a href="UserClient.html">UserClient</a></li> <li><a href="VersionClient.html">VersionClient</a></li> <li><a href="WorkflowClient.html">WorkflowClient</a></li> <li><a href="WorkflowSchemeClient.html">WorkflowSchemeClient</a></li> </ul> <h3>Namespaces</h3> <ul> <li><a href="OauthUtil.html">OauthUtil</a></li> </ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha13</a> on Sat Aug 08 2015 19:48:01 GMT-0500 (CDT) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"></script> </body> </html>