Created
June 1, 2017 16:51
-
-
Save dnetix/fea3868afe915229c7d140967e4d8519 to your computer and use it in GitHub Desktop.
NodeJS authentication generator required for PlacetoPay's Redirection (Web Checkout)
This file contains hidden or 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
var crypto = require('crypto'); | |
/** | |
* Generates authentication data in order to communicate with PlacetoPay Web Checkout service | |
* | |
* USAGE: | |
* | |
* var authGenerator = new RedirectionAuth("YOUR_LOGIN", "YOUR_TRANKEY"); | |
* var auth = authGenerator.asObject(); | |
* | |
* IMPORTANT | |
* | |
* If you need to make another request and you have already the object instanciated, please make sure to | |
* use the generate method BEFORE using the asObject again | |
* | |
* var auth = authGenerator.generate().asObject() | |
* | |
* @param login | |
* @param tranKey | |
* @constructor | |
*/ | |
function RedirectionAuth(login, tranKey) { | |
var self = this; | |
var _nonce; | |
var _seed; | |
this.generate = function() { | |
_nonce = Math.random().toString(36).substring(7); | |
_seed = (new Date()).toISOString(); | |
return self; | |
}; | |
this.getRealNonce = function() { | |
return _nonce; | |
}; | |
this.login = function() { | |
return login; | |
}; | |
this.nonce = function() { | |
return new Buffer(_nonce).toString('base64'); | |
}; | |
this.seed = function() { | |
return _seed; | |
}; | |
this.tranKey = function() { | |
return crypto.createHash('sha1').update(_nonce + seed + tranKey).digest('b64').toString('base64'); | |
}; | |
this.asObject = function() { | |
return { | |
"login": self.login(), | |
"tranKey": self.tranKey(), | |
"seed": self.seed(), | |
"nonce": self.nonce() | |
} | |
}; | |
// For testing purposes | |
this.setSeed = function(seed) { | |
_seed = seed; | |
return self; | |
}; | |
this.setNonce = function(nonce) { | |
_nonce = nonce; | |
return self; | |
}; | |
this.generate(); | |
} | |
module.exports = RedirectionAuth; |
The seed format should be 'YYYY-MM-DDTHH:mm:ssZ' Line 29
_seed = (new Date()).toISOString();
I resolved it using moment (import momento before)
_seed = moment().format('YYYY-MM-DDTHH:mm:ssZ');
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 50 should be
change seed to _seed