Skip to content

Commit

Permalink
From module to a class, which accepts API Key and Secret
Browse files Browse the repository at this point in the history
  • Loading branch information
webdevotion committed Jun 23, 2018
1 parent a5d0065 commit be05b2d
Showing 1 changed file with 65 additions and 44 deletions.
109 changes: 65 additions & 44 deletions lib/core/authorization.js
@@ -1,49 +1,70 @@
import * as Utils from '../helpers/utils'
import * as Security from '../helpers/security'

const getApiKey = () => {
return '00000-00000-00000';
}
export default class Authorization {
constructor(key,secret){
this.key = key;
this.secret = secret
}

getApiKey () {
return this.key;
}

getAPISecret () {
return this.secret;
}

getEpoch () {
return Utils.epoch();
}

// PRIVATE
const options = (url, authHeaderValue) => ({
headers: {
Authorization: `hmac ${authHeaderValue}`,
'Content-Type': 'application/json',
},
url,
});

const getEpoch = () => Utils.epoch();

const getNonce = () => Utils.randomString(32);

const getHmacInputText = input => Utils.concat(input, '');

export function inputForHmac (apiKeyValue, endpoint, bodyHash) {
const httpMethod = endpoint.method;
const action = endpoint.path;
const epoch = getEpoch();
const nonce = getNonce();
const concatenated = getHmacInputText([apiKeyValue, httpMethod, action, epoch, nonce, bodyHash]);
return {
text: concatenated,
epoch,
nonce,
getNonce () {
return Utils.randomString(32);
}

getHmacInputText (input){
return Utils.concat(input, '');
}

options (url, authHeaderValue) {
return {
headers: {
Authorization: `hmac ${authHeaderValue}`,
'Content-Type': 'application/json',
},
url,
}
};
};

function authorizationHeaderValue (apiKeyValue, endpoint) {
const bodyHash = ''; // empty, no body request at the moment
const hmacParams = inputForHmac(apiKeyValue, endpoint, bodyHash);
const generatedHmac = Security.hmacify(hmacParams.text, hmacParams.nonce);

// https://api.combell.com/v2/documentation#section/Authentication/Sending-an-authorized-request
// > your_api_key:generated_hmac:nonce:unix_timestamp
return Utils.concat([apiKeyValue, generatedHmac, hmacParams.nonce, hmacParams.epoch], ':');
};

export function headers (endpoint) {
const val = authorizationHeaderValue(getApiKey(), endpoint);
return options(endpoint.url, val);
};

inputForHmac (apiKeyValue, endpoint, bodyHash) {
const httpMethod = endpoint.method;
const action = encodeURIComponent(endpoint.path);
const epoch = this.getEpoch();
const nonce = this.getNonce();
const secret = this.getAPISecret();

const concatenated = this.getHmacInputText([apiKeyValue, httpMethod, action, epoch, nonce, bodyHash]);
return {
text: concatenated,
epoch,
nonce,
secret
};
};

authorizationHeaderValue (apiKeyValue, endpoint) {
const bodyHash = null; // no body request at the moment, must be null
const hmacParams = this.inputForHmac(apiKeyValue, endpoint, bodyHash);
const generatedHmac = Security.hmacify(hmacParams.text, hmacParams.secret);

// https://api.combell.com/v2/documentation#section/Authentication/Sending-an-authorized-request
// > your_api_key:generated_hmac:nonce:unix_timestamp
return Utils.concat([apiKeyValue, generatedHmac, hmacParams.nonce, hmacParams.epoch], ':');
};

headers (endpoint) {
const val = this.authorizationHeaderValue(this.getApiKey(), endpoint);
return this.options(endpoint.url, val);
};
}

0 comments on commit be05b2d

Please sign in to comment.