Skip to content

Commit 9227c50

Browse files
committed
Merge branch 'feature/signed-urls'
# Conflicts: # spec/webhook/processor_spec.rb
2 parents 159c1ae + 47fbbc7 commit 9227c50

6 files changed

Lines changed: 94 additions & 1 deletion

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,26 @@ The `verify`/`verify_request` methods return `true`/`false`, use `verify!` or `v
207207
You can read the [full list of events](https://cloudconvert.com/api/v2/webhooks) CloudConvert can notify you about in our documentation.
208208

209209

210+
Signed URL
211+
--------
212+
213+
Signed URLs allow converting files on demand only using URL query parameters. The Ruby SDK allows to generate such URLs. Therefore, you need to obtain a signed URL base and a signing secret on the [CloudConvert Dashboard](https://cloudconvert.com/dashboard/api/v2/signed-urls).
214+
215+
```rb
216+
base = 'https://s.cloudconvert.com/...' # You can find it in your signed URL settings.
217+
signing_secret = '...' # You can find it in your signed URL settings.
218+
cache_key = 'cache-key' # Allows caching of the result file for 24h
219+
220+
job = {
221+
tasks: {
222+
"import-it": { operation: "import/url", filename: "test.file", url: "http://invalid.url" },
223+
"convert-it": { input: "import-it", operation: "convert", output_format: "pdf" },
224+
}
225+
}
226+
227+
url = CloudConvert::SignedUrl.sign(base, signing_secret, job, cache_key)
228+
```
229+
210230
Development
211231
-----------
212232

lib/cloudconvert.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
require "cloudconvert/version"
3131
require "cloudconvert/webhook"
3232
require "cloudconvert/webhook/processor"
33+
require "cloudconvert/signed_url"
3334

3435
module CloudConvert
3536
API_URL = "https://api.cloudconvert.com".freeze

lib/cloudconvert/signed_url.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module CloudConvert
2+
class SignedUrl
3+
4+
class << self
5+
6+
# @param base [String] The base from for your signed URL settings.
7+
# @param signing_secret [String] The signing secret from for your signed URL settings.
8+
# @param job [Hash] The job to create the signed URL for
9+
# @param cache_key [String] Allows caching of the result file for 24h
10+
# @return [String] The signed URL
11+
def sign(base, signing_secret, job, cache_key = nil)
12+
13+
url = base
14+
15+
url += "?job=" + Base64.urlsafe_encode64(job.to_json, :padding => false)
16+
17+
unless cache_key.nil?
18+
url += "&cache_key=" + cache_key
19+
end
20+
21+
url += "&s=" + OpenSSL::HMAC.hexdigest("SHA256", signing_secret, url)
22+
23+
return url
24+
25+
end
26+
27+
28+
end
29+
end
30+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module CloudConvert
2+
class SignedUrl
3+
# @param payload [String] The full request body (the JSON string) of our request to the webhook URL.
4+
# @param signature [String] The value from the CloudConvert-Signature.
5+
# @param secret [String] The signing secret from for your webhook settings.
6+
# @return [Boolean]
7+
def self.sign: (untyped base, untyped signing_secret, untyped job, ?untyped? cache_key) -> untyped
8+
end
9+
end

spec/signed_url_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
describe CloudConvert::SignedUrl, :unit do
2+
payload = fixture("requests/webhook_job_finished_payload.json").read
3+
signature = "576b653f726c85265a389532988f483b5c7d7d5f40cede5f5ddf9c3f02934f35"
4+
invalid_signature = SecureRandom.alphanumeric(64)
5+
secret = "secret"
6+
7+
describe ".sign" do
8+
it "should return a signed url" do
9+
10+
job = {
11+
tasks: {
12+
"import-it": { operation: "import/url", filename: "test.file", url: "http://invalid.url" },
13+
"convert-it": { input: "import-it", operation: "convert", output_format: "pdf" },
14+
}
15+
}
16+
17+
base = "https://s.cloudconvert.com/b3d85428-584e-4639-bc11-76b7dee9c109"
18+
signing_secret = "NT8dpJkttEyfSk3qlRgUJtvTkx64vhyX"
19+
cache_key = "mykey"
20+
21+
url = CloudConvert::SignedUrl.sign(base, signing_secret, job, cache_key)
22+
23+
print url
24+
25+
expect(url).to start_with base
26+
expect(url).to include "?job="
27+
expect(url).to include "&cache_key=mykey"
28+
expect(url).to include "&s=3fb529168264bccea28ba9a1d02f2a4662d1917029829ee77e753a7748b98904"
29+
30+
end
31+
32+
end
33+
end

spec/webhook/processor_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def webhook_secret(event)
8080
let(:controller) { ControllerWithoutMethod.new(request) }
8181

8282
it "raises an error" do
83-
expect { controller.create }.to raise_error(NoMethodError)
83+
expect { controller.create }.to raise_error NoMethodError
8484
end
8585
end
8686

0 commit comments

Comments
 (0)