Skip to content

Commit

Permalink
Hash Transition (#131)
Browse files Browse the repository at this point in the history
* All services switched to Authorization V2

* Signature verification function implemented

* typo fix

* Included missing tests & signature verification samples

* project dependencies updated

* request replaced with postman-request

* Update IyzipayResource.js

Signed-off-by: Bünyamin Yaşar <[email protected]>

---------

Signed-off-by: Bünyamin Yaşar <[email protected]>
Co-authored-by: Osman Keser <[email protected]>
  • Loading branch information
byasarcse and osman-keser authored Nov 4, 2024
1 parent a324a43 commit 5e915c8
Show file tree
Hide file tree
Showing 7 changed files with 3,710 additions and 42 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,5 @@ com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties



# Nyc
.nyc_output
37 changes: 21 additions & 16 deletions lib/IyzipayResource.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict';

var request = require('request'),
crypto = require('crypto'),
utils = require('./utils');
const request = require('postman-request');
const utils = require('./utils');

function IyzipayResource() {
}
Expand All @@ -11,36 +10,42 @@ IyzipayResource.RANDOM_STRING_SIZE = 8;
IyzipayResource.RANDOM_HEADER_NAME = 'x-iyzi-rnd';
IyzipayResource.CLIENT_VERSION = 'x-iyzi-client-version';
IyzipayResource.AUTHORIZATION = 'Authorization';
IyzipayResource.AUTHORIZATION_FALLBACK_HEADER = 'Authorization_Fallback';
IyzipayResource.IYZI_WS_HEADER_NAME = 'IYZWS';
IyzipayResource.IYZI_WS_HEADER_NAME_V2 = 'IYZWSv2';
IyzipayResource.SEPARATOR = ':';

IyzipayResource.prototype._getHttpHeaders = function (method) {
var headers = {};
var randomString = utils.generateRandomString(IyzipayResource.RANDOM_STRING_SIZE);
var v2AuthUrlRegex = RegExp(/\/v2\//);
const headers = {};
const randomString = utils.generateRandomString(IyzipayResource.RANDOM_STRING_SIZE);
headers[IyzipayResource.RANDOM_HEADER_NAME] = randomString;
headers[IyzipayResource.CLIENT_VERSION] = "iyzipay-node-2.0.61";
if (v2AuthUrlRegex.test(this._api[method].path)) {
headers[IyzipayResource.AUTHORIZATION] = utils.generateAuthorizationHeaderV2(
IyzipayResource.IYZI_WS_HEADER_NAME_V2,

const generateHttpHeadersV1 = () => {
return utils.generateAuthorizationHeader(
IyzipayResource.IYZI_WS_HEADER_NAME,
this._config.apiKey,
IyzipayResource.SEPARATOR,
this._config.secretKey,
this._api[method].generatedPath,
this._getBody(method),
this._getPkiString(method),
randomString
);
} else {
headers[IyzipayResource.AUTHORIZATION] = utils.generateAuthorizationHeader(
IyzipayResource.IYZI_WS_HEADER_NAME,
};

const generateHttpHeadersV2 = () => {
return utils.generateAuthorizationHeaderV2(
IyzipayResource.IYZI_WS_HEADER_NAME_V2,
this._config.apiKey,
IyzipayResource.SEPARATOR,
this._config.secretKey,
this._getPkiString(method),
this._api[method].generatedPath,
this._getBody(method),
randomString
);
}
};

headers[IyzipayResource.AUTHORIZATION] = generateHttpHeadersV2();
headers[IyzipayResource.AUTHORIZATION_FALLBACK_HEADER] = generateHttpHeadersV1();
return headers;
};

Expand Down
6 changes: 3 additions & 3 deletions lib/resources/ThreedsInitialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var IyzipayResource = require('../IyzipayResource');

function ThreedsInitializ() {
function ThreedsInitialize() {
this._config = arguments[0];
this._api = {
create: {
Expand All @@ -13,6 +13,6 @@ function ThreedsInitializ() {
};
}

ThreedsInitializ.prototype = new IyzipayResource();
ThreedsInitialize.prototype = new IyzipayResource();

module.exports = ThreedsInitializ;
module.exports = ThreedsInitialize;
10 changes: 9 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var utils = module.exports = {
'randomKey' + separator + randomString,
'signature' + separator + signature
];
return new Buffer(authorizationParams.join('&')).toString('base64');
return new Buffer.from(authorizationParams.join('&')).toString('base64');
},
generateRandomString: function (size) {
return process.hrtime()[0] + Math.random().toString(size).slice(2);
Expand Down Expand Up @@ -60,5 +60,13 @@ var utils = module.exports = {
}
}
return mergedObject;
},
calculateHmacSHA256Signature: (params, secretKey) => {
const dataToCheck = params.join(':');

return crypto
.createHmac('sha256', secretKey)
.update(dataToCheck)
.digest('hex');
}
};
Loading

0 comments on commit 5e915c8

Please sign in to comment.