all files / axway/lib/ oauth1a.js

95.65% Statements 44/46
87.5% Branches 7/8
75% Functions 6/8
94.59% Lines 35/37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80                                                                                         
/*
 * Copyright (c) 2016 Cloud Elements. All rights reserved.
 * CLOUD-ELEMENTS PROPRIETARY / CONFIDENTIAL. Use is subject to license
 * terms.
 */
 
const _ = require('lodash');
const scribe = require('scribe-node').load(['OAuth']);
const url = require('url');
 
const hasProp = {}.hasOwnProperty;
const extend = function(child, parent) { for (var key in parent) { Eif (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
 
const MyApi = function(rte, ate, authUrl) {
  return (function(superClass) {
    extend(MyApi, superClass);
 
    function MyApi() {
      this.requestTokenEndpoint = rte;
      this.accessTokenEndpoint = ate;
      this.authUrl = authUrl;
    }
 
    MyApi.prototype.getRequestTokenEndpoint = function() {
      return this.requestTokenEndpoint;
    };
 
    MyApi.prototype.getAccessTokenEndpoint = function() {
      return this.accessTokenEndpoint;
    };
 
    MyApi.prototype.getAuthorizationUrl = function(tok) {
      return this.authUrl + url.format({
        query: {
          oauth_token: tok
        }
      });
    };
 
    return MyApi;
  })(scribe.DefaultApi10a);
};
 
exports.signReqParams = (requestUrl, body, authorizationUrl, tokenUrl,
    method, apiKey, apiSecret, userToken, userSecret, query) => {
 
  let service = new scribe.ServiceBuilder()
    .provider(MyApi(requestUrl, tokenUrl, authorizationUrl))
    .apiKey(apiKey)
    .apiSecret(apiSecret)
    .build();
 
  let accessToken = new scribe.Token(userToken, userSecret);
  let verb = method.toUpperCase();
  let OAuthRequest = service.request.constructor;
  let request = new OAuthRequest(verb, requestUrl);
 
  if (query !== null) {
    for (let key of Object.keys(query)) {
      let value = query[key];
      if (_.isArray(value)) {
        for (let listValue of value) {
          request.addQueryStringParameter(key, listValue);
        }
      } else {
        request.addQueryStringParameter(key, query[key]);
      }
    }
  }
 
  if (body !== null) {
    for (let key of Object.keys(body)) {
      request.addBodyParameter(key, body[key].toString());
    }
  }
 
  service.signRequest(accessToken, request);
  return request.oauthParameters;
};