Skip to content

Commit ab77030

Browse files
committed
signature generation changes
1 parent 671bdc5 commit ab77030

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ The `.url()` method accepts the following parameters
7878
| transformation | Optional. An array of objects specifying the transformation to be applied in the URL. The transformation name and the value should be specified as a key-value pair in the object. Different steps of a [chained transformation](https://docs.imagekit.io/imagekit-docs/chained-transformations) can be specified as different objects of the array. The complete list of supported transformations in the SDK and some examples of using them are given later. If you use a transformation name that is not specified in the SDK, it gets applied as it is in the URL. |
7979
| transformationPostion | Optional. Default value is `path` that places the transformation string as a path parameter in the URL. Can also be specified as `query` which adds the transformation string as the query parameter `tr` in the URL. If you use `src` parameter to create the URL, then the transformation string is always added as a query parameter. |
8080
| queryParameters | Optional. These are the other query parameters that you want to add to the final URL. These can be any query parameters and not necessarily related to ImageKit. Especially useful, if you want to add some versioning parameter to your URLs. |
81-
| signed | Optional. Boolean. Default is `false`. If set to `true`, the SDK generates a signed image URL adding the image signature to the image URL. |
81+
| signed | Optional. Boolean. Default is `false`. If set to `true`, the SDK generates a signed image URL adding the image signature to the image URL. This can only be used if you are creating the URL with the `urlEndpoint` and `path` parameters, and not with the `src` parameter. |
8282
| expireSeconds | Optional. Integer. Meant to be used along with the `signed` parameter to specify the time in seconds from now when the URL should expire. If specified, the URL contains the expiry timestamp in the URL and the image signature is modified accordingly. |
8383

8484
#### Examples of generating URLs

libs/url/builder.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,37 @@ module.exports.buildURL = function(opts) {
7373
urlObject.search = queryParameters.toString();
7474

7575
// Signature String and Timestamp
76-
if(opts.signed === true) {
76+
// We can do this only for URLs that are created using urlEndpoint and path parameter
77+
// because we need to know the endpoint to be able to remove it from the URL to create a signature
78+
// for the remaining. With the src parameter, we would not know the "pattern" in the URL
79+
var expiryTimestamp;
80+
if(opts.signed === true && !isSrcParameterUsedForURL) {
7781
if(opts.expireSeconds) {
78-
queryParameters.set(TIMESTAMP_PARAMETER, getSignatureTimestamp(opts.expireSeconds));
79-
urlObject.search = queryParameters.toString();
82+
expiryTimestamp = getSignatureTimestamp(opts.expireSeconds);
83+
} else {
84+
expiryTimestamp = DEFAULT_TIMESTAMP;
8085
}
81-
}
8286

83-
var intermediateURL = url.format(urlObject);
87+
var intermediateURL = url.format(urlObject);
88+
89+
var urlSignature = getSignature({
90+
privateKey : opts.privateKey,
91+
url : intermediateURL,
92+
urlEndpoint : opts.urlEndpoint,
93+
expiryTimestamp : expiryTimestamp
94+
});
8495

85-
var urlSignature = getSignature({
86-
privateKey : opts.privateKey,
87-
url : intermediateURL
88-
});
96+
if(opts.signed === true) {
97+
if(expiryTimestamp && expiryTimestamp != DEFAULT_TIMESTAMP) {
98+
queryParameters.set(TIMESTAMP_PARAMETER, expiryTimestamp);
99+
}
100+
queryParameters.set(SIGNATURE_PARAMETER, urlSignature);
101+
urlObject.search = queryParameters.toString();
102+
}
89103

90-
if(opts.signed === true) {
91-
queryParameters.set(SIGNATURE_PARAMETER, urlSignature);
92-
urlObject.search = queryParameters.toString();
93104
}
94105

106+
95107
return url.format(urlObject);
96108
};
97109

@@ -147,7 +159,7 @@ function getSignatureTimestamp(seconds) {
147159
}
148160

149161
function getSignature(opts) {
150-
if(!opts.privateKey || !opts.url) return "";
162+
if(!opts.privateKey || !opts.url || !opts.urlEndpoint) return "";
151163

152-
return crypto.createHmac('sha1', opts.privateKey).update(opts.url.replace(PROTOCOL_QUERY, "")).digest('hex');
164+
return crypto.createHmac('sha1', opts.privateKey).update(opts.url.replace(opts.urlEndpoint, "") + (opts.expiryTimestamp ? opts.expiryTimestamp : "9999999999")).digest('hex');
153165
}

0 commit comments

Comments
 (0)