imageboss-rb 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee6455671dc8650560c56a4ee50124f95c249aca5f17ff05df4788f5b02947b1
4
- data.tar.gz: 7e35f1d19d98e1a2f9a232cc76280d26f7de20828dc9ee765afad7fef597eb20
3
+ metadata.gz: 2accf545dd9b59902fdb8563cdcc357a6c65a9b5027c4c2983880edf76a0bd05
4
+ data.tar.gz: 68f3e5ceccbe24a201b0376e3ce290bfa936d9e572f32f0e3e9906b0619baf03
5
5
  SHA512:
6
- metadata.gz: cea525427ac649f445d43ae0032e3fb284bed2430c12ad7cfd5f096513caf99f7eb547170d18e09dfe72d1487a0b1f759a34f072ed3ccb73f1e1174c21994dc4
7
- data.tar.gz: b1f77be4a61ed57f2f2e79601819daac6eabcb9c3d83cfa9c6e1fdf90ddb22fe0cfd0e46d57865200c785a564111d003d4bf704f6958762c1370955898923a81
6
+ metadata.gz: 101b7a57abdf76727184d1caeaf0df11a338f73a9700284cc08fdb28689377c0a11f2b4a79e2143edbb8f386e7d30285bd0f04c12b06427fb3b7120048a113d6
7
+ data.tar.gz: 9c80869ef7ac386926f0fc4d5d9489f5b9f1886c1371f8e276e12acb5117eaef5002a1f5b1d2de09b9618c2baec163f224d5590dbeb97d9abf90fafdac110523
data/README.md CHANGED
@@ -15,6 +15,7 @@ Official Gem for Generating ImageBoss URLs.
15
15
  - [Example `Image Resizing With Extra Options`](#example-image-resizing-with-extra-options)
16
16
  - [All operations and options for Image Resizing](#all-operations-and-options-for-image-resizing)
17
17
  - [Disabling URL generation](#disabling-url-generation)
18
+ - [Signing your URLs](#signing-your-urls)
18
19
  - [Tested on](#tested-on)
19
20
 
20
21
  ## Installation
@@ -76,6 +77,20 @@ image_url = client.path('/images/img01.jpg')
76
77
  ```
77
78
  This will give you the ability to see your image without adding extra code to handle this situation.
78
79
 
80
+ ### Signing your URLs
81
+ Read more about this feature here:
82
+ https://www.imageboss.me/docs/security
83
+
84
+ ```ruby
85
+ client = ImageBoss::Client.new(source: 'mywebsite', secret: '<MY_SECRET>')
86
+
87
+ image_url = client.path('/images/img01.jpg')
88
+ .operation(:width, width: 100)
89
+
90
+ #=> https://img.imageboss.me/width/100/images/img01.jpg?bossToken=ff74a46c7228ee4262c39b8d501c488293c5be9d433bb9ca957f32c9c3d844ab
91
+ ```
92
+ This will give you the ability to see your image without adding extra code to handle this situation.
93
+
79
94
  ## Tested on
80
95
  Ruby
81
96
  - 2.6.3
@@ -1,9 +1,10 @@
1
1
  module ImageBoss
2
2
  class Client
3
- def initialize(source:, enabled: true)
3
+ def initialize(source:, enabled: true, secret: false)
4
4
  @options = {
5
5
  source: source,
6
- enabled: enabled
6
+ enabled: enabled,
7
+ secret: secret
7
8
  }
8
9
  end
9
10
 
@@ -1,5 +1,8 @@
1
1
  module ImageBoss
2
2
  class Path
3
+ require 'uri'
4
+ require 'openssl'
5
+
3
6
  SERVICE_URL = 'https://img.imageboss.me'.freeze
4
7
  OPERATIONS = {
5
8
  cover: {
@@ -16,6 +19,7 @@ module ImageBoss
16
19
  @client_options = client_options
17
20
  @service_url = client_options[:service_url] || SERVICE_URL
18
21
  @source = client_options[:source]
22
+ @secret = client_options[:secret]
19
23
  @asset_path = asset_path
20
24
  end
21
25
 
@@ -37,10 +41,23 @@ module ImageBoss
37
41
  @operation[:recipe].chomp('/'),
38
42
  @asset_path.gsub(/^\/?(.+)/, "\\1")
39
43
  ].join
40
- parse(recipe)
44
+
45
+ @secret == false ? parse(recipe) : add_params(parse(recipe), { bossToken: create_token(@asset_path) })
41
46
  end
47
+
42
48
  private
43
49
 
50
+ def create_token(path)
51
+ OpenSSL::HMAC.hexdigest('sha256', @secret, path)
52
+ end
53
+
54
+ def add_params(url, params = {})
55
+ uri = URI(url)
56
+ params = Hash[URI.decode_www_form(uri.query || '')].merge(params)
57
+ uri.query = URI.encode_www_form(params)
58
+ uri.to_s
59
+ end
60
+
44
61
  def parse(recipe)
45
62
  recipe
46
63
  .sub(':source', @source.to_s)
@@ -1,3 +1,3 @@
1
1
  module ImageBoss
2
- VERSION = '2.0.0'
2
+ VERSION = '2.1.0'
3
3
  end
@@ -14,7 +14,8 @@ describe ImageBoss::Client do
14
14
  context 'initialize' do
15
15
  it { expect(subject.instance_variable_get(:@options)).to eq({
16
16
  source: client_args[:source],
17
- enabled: true
17
+ enabled: true,
18
+ secret: false
18
19
  })
19
20
  }
20
21
  end
@@ -67,7 +68,7 @@ describe ImageBoss::Client do
67
68
  it { expect(image_url).to eq "#{service}/#{source}/cdn/assets/img01.jpg" }
68
69
  end
69
70
 
70
- fcontext 'disabled client' do
71
+ context 'disabled client' do
71
72
  let(:client_args) {{
72
73
  source: source,
73
74
  enabled: false
@@ -77,4 +78,29 @@ describe ImageBoss::Client do
77
78
  it { expect(image_url).to eq '/assets/img01.jpg' }
78
79
  end
79
80
  end
81
+
82
+ context 'secure token' do
83
+ let(:client_args) {{
84
+ source: source,
85
+ secret: 'abc'
86
+ }}
87
+
88
+ let(:operation_args) { [:cover, width: 100, height: 100] }
89
+
90
+ let(:path) {
91
+ subject
92
+ .path('/assets/img01.jpg?existing=oh')
93
+ }
94
+
95
+ let(:image_url) { path.operation(*operation_args) }
96
+
97
+ subject { described_class.new(**client_args) }
98
+
99
+ context 'initialize' do
100
+ context 'width' do
101
+ let(:operation_args) { [:width, width: 100 ] }
102
+ it { expect(image_url).to eq "#{service}/#{source}/width/100/assets/img01.jpg?existing=oh&bossToken=ff74a46c7228ee4262c39b8d501c488293c5be9d433bb9ca957f32c9c3d844ab" }
103
+ end
104
+ end
105
+ end
80
106
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imageboss-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Escobar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-28 00:00:00.000000000 Z
11
+ date: 2020-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec