jive-sdk
Version:
Node.js SDK for Jive Software to assist with the development of add-ons
395 lines (298 loc) • 10.3 kB
HTML
<html lang="en">
<head>
<meta charset="utf-8">
<title>Jive SDK Source: jive-sdk-api/lib/util/oauthHandler.js</title>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
<link type="text/css" rel="stylesheet" href="styles/site.spacelab.css">
</head>
<body>
<div class="container-fluid">
<div class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<a class="brand" href="index.html">Jive SDK</a>
<ul class="nav">
<li class="dropdown">
<a href="modules.list.html" class="dropdown-toggle" data-toggle="dropdown">Modules<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="module-abstractDefinitions.html">abstractDefinitions</a>
</li>
<li>
<a href="module-abstractInstances.html">abstractInstances</a>
</li>
<li>
<a href="module-addOnRoutes.html">addOnRoutes</a>
</li>
<li>
<a href="module-api.html">api</a>
</li>
<li>
<a href="module-community.html">community</a>
</li>
<li>
<a href="module-constants.html">constants</a>
</li>
<li>
<a href="module-devRoutes.html">devRoutes</a>
</li>
<li>
<a href="module-events.html">events</a>
</li>
<li>
<a href="module-extensions.html">extensions</a>
</li>
<li>
<a href="module-extstreamsInstances.html">extstreamsInstances</a>
</li>
<li>
<a href="module-jiveutil.html">jiveutil</a>
</li>
<li>
<a href="module-oauthRoutes.html">oauthRoutes</a>
</li>
<li>
<a href="module-request.html">request</a>
</li>
<li>
<a href="module-security.html">security</a>
</li>
<li>
<a href="module-service.html">service</a>
</li>
<li>
<a href="module-tasks.html">tasks</a>
</li>
<li>
<a href="module-tileInstances.html">tileInstances</a>
</li>
<li>
<a href="module-tileRoutes.html">tileRoutes</a>
</li>
<li>
<a href="module-tilesDefinitions.html">tilesDefinitions</a>
</li>
<li>
<a href="module-webhooks.html">webhooks</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="extstreamsDefinitions.html">extstreamsDefinitions</a>
</li>
<li>
<a href="filePersistence.html">filePersistence</a>
</li>
<li>
<a href="memoryPersistence.html">memoryPersistence</a>
</li>
<li>
<a href="memoryScheduler.html">memoryScheduler</a>
</li>
<li>
<a href="oauthHandler.html">oauthHandler</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div id="main">
<h1 class="page-title">Source: jive-sdk-api/lib/util/oauthHandler.js</h1>
<section>
<article>
<pre class="sunlight-highlight-javascript linenums">var jive = require('../../../jive-sdk-service/api');
var q = require('q');
/**
* An abstract class encapsulating the access token refresh flow.
* <ol>
* <li>Attempt an operation.</li>
* <li>If the operation does not suceed due to 401, then attempt an OAuth2 access token refresh exchange.</li>
* <li>If the refresh is successful, retry the operation.</li>
* </ol>
* Override the accessTokenRefresher member function, to handle the request logic specific to the target OAuth2 provider.
* @class oauthHandler
* @abstract
*/
///////////////////////////////////////////////////////////////////////////////////
// private
/**
* This is the default implementation of access token refresh.
* @private
* @memberof oauthHandler
* @param {Object} oauth
* @returns {Promise} Promise
*/
var accessTokenRefresher = function (oauth) {
var postObject = {
'grant_type': 'refresh_token',
'refresh_token': oauth['refreshToken'],
'client_id': oauth['oauth2ConsumerKey'],
'client_secret': oauth['oauth2ConsumerSecret']
};
var headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
return jive.util.buildRequest(oauth['originServerTokenRequestUrl'], 'POST', postObject, headers);
};
///////////////////////////////////////////////////////////////////////////////////
// public
/**
* The default implementation of access token refresh. If you
* use oauth.js buildOAuthHandler, you will be required to provide
* your own implementation of this function.
* @memberof oauthHandler
* @param oauth
* @returns {}
*/
exports.accessTokenRefresher = function(oauth) {
return accessTokenRefresher(oauth);
};
exports.doRequest = function( options ) {
options = options || {};
var url = options.url,
headers = options.headers || {},
oauth = options.oauth,
method = options.method,
postBody = options.postBody,
requestOptions = options.requestOptions;
if( !oauth ) {
jive.logger.warn("No oauth credentials found. Continuing without them.");
return jive.util.buildRequest( url, method, postBody, headers, requestOptions );
}
return exports.doOperation( function() {
return jive.util.buildRequest( url, method, postBody, headers, requestOptions );
}, {}, oauth, true );
};
exports.doOperation = function( operation, operationContext, oauth ) {
return this.handleOperation( operation, operationContext, oauth, true );
};
exports.doRefreshTokenFlow = function(operationContext, oauth ) {
jive.logger.debug("Trying refresh flow");
var deferred = q.defer();
this.accessTokenRefresher(operationContext, oauth).then(
// success
function (operationContext) {
// success
jive.logger.debug('Successfully refreshed token.');
deferred.resolve(operationContext);
},
// failure
function (result) {
jive.logger.warn("RefreshTokenFlow failed.", result || '');
deferred.reject( {statusCode: result.statusCode,
error: 'Error refreshing token. Response in details field', details: result } );
}
);
return deferred.promise;
};
exports.handleError = function( operationContext, oauth, response, retryIfFail ) {
var deferred = q.defer();
if (response.statusCode == 400) {
jive.logger.info('Bad request (400)', response);
deferred.reject(response);
}
else if (response.statusCode == 401 || response.statusCode == 403) {
jive.logger.info("Unauthorized (" + response.statusCode + ")", response);
if ( !retryIfFail ) {
jive.logger.error('Not executing refresh flow. Failure on second attempt.', response);
deferred.reject( response );
} else {
this.doRefreshTokenFlow( operationContext, oauth ).then(
// successful token refresh
function(update) {
deferred.resolve(update);
},
// failed token refresh
function(err) {
deferred.reject( err );
}
);
}
} else {
deferred.reject(response);
}
return deferred.promise;
};
exports.handleOperation = function (operation, operationContext, oauth, retryIfFail) {
var p = q.defer();
var self = this;
operation( operationContext, oauth ).then(
// successful push
function (response) {
p.resolve(response);
},
// failed push
function(response) {
return self.handleError( operationContext, oauth, response, retryIfFail).then(
function(updatedOAuth) {
jive.logger.debug("Retrying operation.");
// retry operation once if refreshtoken was reason for error
self.handleOperation( operation, operationContext, updatedOAuth, false).then(
function(r) {
p.resolve(r);
},
function(e) {
p.reject(e);
}
);
},
function( err ) {
p.reject(err);
}
);
}
);
return p.promise;
};
</pre>
</article>
</section>
</div>
<div class="clearfix"></div>
<footer>
<span class="copyright">
Jive Software, Inc
</span>
<br />
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-dev</a>
on Wed Jan 22 2014 12:29:37 GMT-0800 (PST) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
</div>
<br clear="both">
</div>
</div>
<script src="scripts/sunlight.js"></script>
<script src="scripts/sunlight.javascript.js"></script>
<script src="scripts/sunlight-plugin.doclinks.js"></script>
<script src="scripts/sunlight-plugin.linenumbers.js"></script>
<script src="scripts/sunlight-plugin.menu.js"></script>
<script src="scripts/jquery.min.js"></script>
<script src="scripts/jquery.scrollTo.js"></script>
<script src="scripts/jquery.localScroll.js"></script>
<script src="scripts/bootstrap-dropdown.js"></script>
<script src="scripts/toc.js"></script>
<script> Sunlight.highlightAll({lineNumbers:true, showMenu: true, enableDoclinks :true}); </script>
<script>
$( function () {
$( "#toc" ).toc( {
selectors : "h1,h2,h3,h4",
showAndHide : false,
scrollTo : 60
} );
$( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" );
$( "#main span[id^='toc']" ).addClass( "toc-shim" );
} );
</script>
</body>
</html>