Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] resolve the issue with generating a nonce #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions javascript/api.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import CryptoJS from "crypto-js";
import request from "request-promise-native"
import qs from 'qs'

function createNonce() {
var s = '', length = 32;
do {
s += Math.random().toString(36).substr(2);
} while (s.length < length);
s = s.substr(0, length);
return s;
}
import uuid from 'uuid'

const getAuthHeader = (apiKey, apiSecret, time, nonce, organizationId = '', request = {}) => {
const hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, apiSecret);
Expand Down Expand Up @@ -40,7 +32,7 @@ const getAuthHeader = (apiKey, apiSecret, time, nonce, organizationId = '', requ

class Api {

constructor({locale, apiHost, apiKey, apiSecret, orgId}) {
constructor({ locale, apiHost, apiKey, apiSecret, orgId }) {
this.locale = locale || 'en';
this.host = apiHost;
this.key = apiKey;
Expand All @@ -61,16 +53,16 @@ class Api {
});
}

apiCall(method, path, {query, body, time} = {}) {
if(this.localTimeDiff === null) {
apiCall(method, path, { query, body, time } = {}) {
if (this.localTimeDiff === null) {
return Promise.reject(new Error('Get server time first .getTime()'));
}

// query in path
var [pathOnly,pathQuery] = path.split('?');
if(pathQuery) query = {...qs.parse(pathQuery), ...query};
var [pathOnly, pathQuery] = path.split('?');
if (pathQuery) query = { ...qs.parse(pathQuery), ...query };

const nonce = createNonce();
const nonce = uuid.v4();
const timestamp = (time || (+new Date() + this.localTimeDiff)).toString();
const options = {
uri: this.host + pathOnly,
Expand Down
Loading