ac-signature
Advanced tools
Comparing version 2.0.6 to 2.0.7
@@ -0,1 +1,12 @@ | ||
<a name="2.0.7"></a> | ||
## [2.0.7](https://github.com/mmpro/ac-signature/compare/v2.0.6..v2.0.7) (2020-11-23 11:48:54) | ||
### Bug Fix | ||
* **App:** Added some debugging options | MP | [6546fff22703f0c92e56d471eccd56d7f13ce66e](https://github.com/mmpro/ac-signature/commit/6546fff22703f0c92e56d471eccd56d7f13ce66e) | ||
* **App:** Added some debugging options | MP | [786979514ec3f6ef7c51d6fcb3beb72b109cf651](https://github.com/mmpro/ac-signature/commit/786979514ec3f6ef7c51d6fcb3beb72b109cf651) | ||
Added some debugging options | ||
<a name="2.0.6"></a> | ||
@@ -2,0 +13,0 @@ |
25
index.js
@@ -12,16 +12,18 @@ /** | ||
const sign = (params) => { | ||
let accessSecret = params.accessSecret; | ||
const accessSecret = params.accessSecret; | ||
if (!accessSecret) return 'accessSecretMissing'; | ||
let controller = params.controller; | ||
const controller = params.controller; | ||
if (!controller) return 'controllerMissing'; | ||
let action = params.action; | ||
const action = params.action; | ||
if (!action) return 'actionMissing'; | ||
let data = _.isObject(params.payload) && params.payload; | ||
const data = _.isObject(params.payload) && params.payload; | ||
if (!data) return 'payloadMustBeObject'; | ||
// debugging | ||
const accessKey = _.get(params, 'accessKey') // only for debugging | ||
// make sure payload keys are ordered from A-Z! | ||
let keys = _.sortBy(_.keys(data), key => { | ||
const keys = _.sortBy(_.keys(data), key => { | ||
return key; | ||
}); | ||
let payload = {}; | ||
const payload = {}; | ||
_.each(keys, key => { | ||
@@ -31,8 +33,8 @@ payload[key] = data[key]; | ||
// for debugging you can use your own timestamp | ||
const ts = _.get(params, 'ts', parseInt(new Date().getTime()/1000)) | ||
const valueToHash = _.toLower(controller) + '\n' + _.toLower(action) + '\n' + ts + (_.isEmpty(payload) ? '' : '\n'+JSON.stringify(payload)); | ||
const mechanism = crypto.createHmac('sha256',accessSecret); | ||
const hash = mechanism.update(valueToHash).digest("hex"); | ||
let ts = parseInt(new Date().getTime()/1000); | ||
let valueToHash = _.toLower(controller) + '\n' + _.toLower(action) + '\n' + ts + (_.isEmpty(payload) ? '' : '\n'+JSON.stringify(payload)); | ||
let mechanism = crypto.createHmac('sha256',accessSecret); | ||
let hash = mechanism.update(valueToHash).digest("hex"); | ||
if (params.debug) { | ||
@@ -42,2 +44,3 @@ const debugPrefix = _.padEnd('ACSignature', 14) | ||
console.log(_.pad('Create Signature', 80, '-')) | ||
if (accessKey) console.log('%s | %s | %s', debugPrefix, _.padEnd('API Key', debugPadding), accessKey) | ||
console.log('%s | %s | %s/%s', debugPrefix, _.padEnd('Controller/Action', debugPadding), controller, action) | ||
@@ -44,0 +47,0 @@ console.log('%s | %s | %s', debugPrefix, _.padEnd('Payload to hash', debugPadding), valueToHash.replace(/\n/g, '/')) |
{ | ||
"name": "ac-signature", | ||
"description": "Sign payload for AdmiralCloud API", | ||
"version": "2.0.6", | ||
"version": "2.0.7", | ||
"author": "Mark Poepping / support@admiralcloud.com", | ||
@@ -6,0 +6,0 @@ "repository": { |
20993
255