Advertisement
Guest User

Untitled

a guest
May 5th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. /**
  2. * getAccountInfoAndToken wraps both getLoginInformation and getOauthToken to return a single object
  3. * containing the oAuth Token and baseUrl for future calls. This is mostly a
  4. * convenience function.
  5. *
  6. * @memberOf Auth
  7. * @public
  8. * @alias getAuthInfo
  9. * @function
  10. * @param {string} email - Email address of the DocuSign user.
  11. * @param {string} password - Password of the DocuSign user.
  12. * @param {function} [callback] - Returned in the form of function(error, response).
  13. * @returns {Promise} - A thenable bluebird Promise; if callback is given it is called before the promise is resolved
  14. */
  15. exports.getAccountInfoAndToken = function (email, password, callback) {
  16. return exports.getLoginInfo(email, password).asCallback(callback)
  17. .then(function (accountInfo) {
  18. return exports.getOauthToken(email, password, accountInfo.baseUrl)
  19. .then(function (accessToken) {
  20. var accountAndAuthInfo = assign({
  21. accessToken: accessToken
  22. }, accountInfo);
  23. return accountAndAuthInfo;
  24. });
  25. })
  26. .catch(function (err) {
  27. var errMsg = 'Error getting API token: ' + JSON.stringify(err);
  28. var error = new DocuSignError(errMsg, err);
  29. throw error;
  30. });
  31. };
  32.  
  33. /**
  34. * Gets login information for the default account/organization.
  35. *
  36. * @memberOf Auth
  37. * @private
  38. * @function
  39. * @param {string} email - Email address of the DocuSign user.
  40. * @param {string} password - Password of the DocuSign user.
  41. * @returns {Promise} - A thenable bluebird Promise fulfilled with login info.
  42. */
  43. exports.getLoginInfo = function (email, password) {
  44. var options = {
  45. method: 'GET',
  46. url: dsUtils.getApiUrl() + '/login_information?include_account_id_guid=true',
  47. headers: {
  48. 'X-DocuSign-Authentication': JSON.stringify({
  49. Username: email,
  50. Password: password,
  51. IntegratorKey: dsUtils.internalState.integratorKey
  52. })
  53. }
  54. };
  55.  
  56. return dsUtils.makeRequest('Get DS User Account Info', options).then(function (response) {
  57. var loginInfo = response.loginAccounts.filter(function (account) {
  58. return account.isDefault === 'true';
  59. })[0];
  60. return loginInfo;
  61. });
  62. };
  63.  
  64. /**
  65. * Gets an oAuth Token for given username & password that can be used to access
  66. * the DocuSign API on subsequent calls instead of using the password repeatedly.
  67. *
  68. * @memberOf Auth
  69. * @private
  70. * @function
  71. * @param {string} email - Email address of the DocuSign user.
  72. * @param {string} password - Password of the DocuSign user.
  73. * @param {string} baseUrl - DocuSign API base URL.
  74. * @returns {Promise} - A thenable bluebird Promise fulfilled with an access token
  75. */
  76. exports.getOauthToken = function (email, password, baseUrl) {
  77. var options = {
  78. method: 'POST',
  79. url: _getTokenEndpoint(baseUrl, 'token'),
  80. headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  81. body: querystring.stringify({
  82. grant_type: 'password',
  83. client_id: dsUtils.internalState.integratorKey,
  84. username: email,
  85. password: password,
  86. scope: 'api'
  87. })
  88. };
  89.  
  90. return dsUtils.makeRequest('Get DS OAuth2 Token', options).then(function (response) {
  91. return response.access_token;
  92. });
  93. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement