Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
From module to a class, which accepts API Key and Secret
- Loading branch information
1 parent
a5d0065
commit be05b2d
Showing
1 changed file
with
65 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; | ||
} |