Skip to content

Commit f88ce96

Browse files
leptiansstrausr
authored andcommitted
Add feature to allow override on timestamp and signature (#295)
* Add feature to allow override on timestamp and signature
1 parent 814de57 commit f88ce96

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

lib-es5/utils/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ function build_upload_params(options) {
364364
quality_analysis: utils.as_safe_bool(options.quality_analysis),
365365
responsive_breakpoints: utils.generate_responsive_breakpoints_string(options.responsive_breakpoints),
366366
return_delete_token: utils.as_safe_bool(options.return_delete_token),
367-
timestamp: exports.timestamp(),
367+
timestamp: options.timestamp || exports.timestamp(),
368368
transformation: utils.generate_transformation_string(clone(options)),
369369
type: options.type,
370370
unique_filename: utils.as_safe_bool(options.unique_filename),
@@ -1074,6 +1074,8 @@ function process_request_params(params, options) {
10741074
if (options.unsigned != null && options.unsigned) {
10751075
params = exports.clear_blank(params);
10761076
delete params.timestamp;
1077+
} else if (options.signature) {
1078+
params = exports.clear_blank(options);
10771079
} else {
10781080
params = exports.sign_request(params, options);
10791081
}
@@ -1084,7 +1086,7 @@ function private_download_url(public_id, format) {
10841086
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
10851087

10861088
var params = exports.sign_request({
1087-
timestamp: exports.timestamp(),
1089+
timestamp: options.timestamp || exports.timestamp(),
10881090
public_id: public_id,
10891091
format: format,
10901092
type: options.type,
@@ -1103,7 +1105,7 @@ function zip_download_url(tag) {
11031105
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
11041106

11051107
var params = exports.sign_request({
1106-
timestamp: exports.timestamp(),
1108+
timestamp: options.timestamp || exports.timestamp(),
11071109
tag: tag,
11081110
transformation: utils.generate_transformation_string(options)
11091111
}, options);
@@ -1346,7 +1348,7 @@ function archive_params() {
13461348
target_format: options.target_format,
13471349
target_public_id: options.target_public_id,
13481350
target_tags: options.target_tags && exports.build_array(options.target_tags),
1349-
timestamp: options.timestamp ? options.timestamp : exports.timestamp(),
1351+
timestamp: options.timestamp || exports.timestamp(),
13501352
transformations: utils.build_eager(options.transformations),
13511353
type: options.type,
13521354
use_original_filename: exports.as_safe_bool(options.use_original_filename)
@@ -1539,4 +1541,4 @@ exports.build_streaming_profiles_param = build_streaming_profiles_param;
15391541
exports.hashToParameters = hashToParameters;
15401542
exports.present = present;
15411543
exports.only = only;
1542-
exports.jsonArrayParam = jsonArrayParam;
1544+
exports.jsonArrayParam = jsonArrayParam;

lib/utils/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ function build_upload_params(options) {
356356
quality_analysis: utils.as_safe_bool(options.quality_analysis),
357357
responsive_breakpoints: utils.generate_responsive_breakpoints_string(options.responsive_breakpoints),
358358
return_delete_token: utils.as_safe_bool(options.return_delete_token),
359-
timestamp: exports.timestamp(),
359+
timestamp: options.timestamp || exports.timestamp(),
360360
transformation: utils.generate_transformation_string(clone(options)),
361361
type: options.type,
362362
unique_filename: utils.as_safe_bool(options.unique_filename),
@@ -1058,6 +1058,8 @@ function process_request_params(params, options) {
10581058
if ((options.unsigned != null) && options.unsigned) {
10591059
params = exports.clear_blank(params);
10601060
delete params.timestamp;
1061+
} else if (options.signature ) {
1062+
params = exports.clear_blank(options);
10611063
} else {
10621064
params = exports.sign_request(params, options);
10631065
}
@@ -1066,7 +1068,7 @@ function process_request_params(params, options) {
10661068

10671069
function private_download_url(public_id, format, options = {}) {
10681070
let params = exports.sign_request({
1069-
timestamp: exports.timestamp(),
1071+
timestamp: options.timestamp || exports.timestamp(),
10701072
public_id: public_id,
10711073
format: format,
10721074
type: options.type,
@@ -1083,7 +1085,7 @@ function private_download_url(public_id, format, options = {}) {
10831085

10841086
function zip_download_url(tag, options = {}) {
10851087
let params = exports.sign_request({
1086-
timestamp: exports.timestamp(),
1088+
timestamp: options.timestamp || exports.timestamp(),
10871089
tag: tag,
10881090
transformation: utils.generate_transformation_string(options),
10891091
}, options);
@@ -1309,7 +1311,7 @@ function archive_params(options = {}) {
13091311
target_format: options.target_format,
13101312
target_public_id: options.target_public_id,
13111313
target_tags: options.target_tags && exports.build_array(options.target_tags),
1312-
timestamp: options.timestamp ? options.timestamp : exports.timestamp(),
1314+
timestamp: options.timestamp || exports.timestamp(),
13131315
transformations: utils.build_eager(options.transformations),
13141316
type: options.type,
13151317
use_original_filename: exports.as_safe_bool(options.use_original_filename),

test/uploader_spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,4 +936,32 @@ describe("uploader", function () {
936936
});
937937
});
938938
});
939+
940+
describe("sign requests", function () {
941+
var configBck2 = void 0;
942+
var writeSpy;
943+
writeSpy = void 0;
944+
beforeEach(function () {
945+
writeSpy = sinon.spy(ClientRequest.prototype, 'write');
946+
configBck2 = cloudinary.config();
947+
cloudinary.config({
948+
api_key: "1234",
949+
api_secret: "",
950+
});
951+
});
952+
afterEach(function () {
953+
cloudinary.config(configBck2);
954+
writeSpy.restore();
955+
});
956+
it("should allow a signature and timestamp parameter on uploads", function () {
957+
cloudinary.v2.uploader.upload(IMAGE_FILE, {
958+
public_id: 'folder/file',
959+
version: '1234',
960+
timestamp: 1569707219,
961+
signature: 'b77fc0b0dffbf7e74bdad36b615225fb6daff81e',
962+
});
963+
sinon.assert.calledWith(writeSpy, sinon.match(helper.uploadParamMatcher('signature', "b77fc0b0dffbf7e74bdad36b615225fb6daff81e")));
964+
sinon.assert.calledWith(writeSpy, sinon.match(helper.uploadParamMatcher('timestamp', '1569707219')));
965+
});
966+
});
939967
});

test/utils_spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,20 @@ describe("utils", function () {
13991399
sign_url: true,
14001400
}, "http://res.cloudinary.com/test123/image/fetch/s--hH_YcbiS--/v1234/http://google.com/path/to/image.png", {});
14011401
});
1402+
});
1403+
context("sign requests", function () {
1404+
var configBck = void 0;
1405+
before(function () {
1406+
configBck = cloudinary.config();
1407+
cloudinary.config({
1408+
cloud_name: 'test123',
1409+
api_key: "1234",
1410+
api_secret: "b",
1411+
});
1412+
});
1413+
after(function () {
1414+
cloudinary.config(configBck);
1415+
});
14021416
it("should correctly sign_request", function () {
14031417
var params = utils.sign_request({
14041418
public_id: "folder/file",

0 commit comments

Comments
 (0)