Skip to content

Commit 39e6584

Browse files
committed
add security info to README
1 parent 7d0d961 commit 39e6584

File tree

9 files changed

+72
-96
lines changed

9 files changed

+72
-96
lines changed

.travis.yml

-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
1-
env:
2-
- "RAILS_VERSION=5.1.0"
3-
- "RAILS_VERSION=5.0.0"
4-
- "RAILS_VERSION=4.2.0"
5-
- "RAILS_VERSION=4.1.0"
6-
- "RAILS_VERSION=4.0.0"

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,28 @@ config.filestack_rails.client_name = "custom_client_name"
4545
```
4646
The client name defaults to "filestack_client" and is injected into your client-side Javascript. This is because v3 of the File Picker lives in the Javascript of your web application. For more information, please see our [File Picker documenation](https://www.filestack.com/docs/javascript-api/pick-v3).
4747

48+
### Security
49+
50+
If your account has security enabled, then you must initialize the File Picker with a signature and policy. This is easily enabled through the configuration options by setting your application secret and security options:
51+
52+
```ruby
53+
config.filestack_rails.app_secret = 'YOUR_APP_SECRET'
54+
config.filestack_rails.security = {'call' => %w[pick store read convert] }
55+
```
56+
If you set security to an empty object like so
57+
```ruby
58+
config.filestack_rails.security = {}
59+
```
60+
it will provide a policy and signature with only an expiry setting (this defaults to one hour).
61+
62+
You can access the generated policy and signature anytime by calling their attributes on the created security object.
63+
64+
```ruby
65+
puts config.filestack_rails.security.policy
66+
puts config.filestack_rails.security.signature
67+
```
68+
You can also generate a new security object at any time, although this will only affect the filestack_image tag, and not the File Picker client.
69+
4870
## Usage
4971

5072
The Filestack-Rails plugin provides three main functionalities:

app/helpers/filestack_rails/application_helper.rb

+18-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ def filestack_js_include_tag
1010

1111
def filestack_js_init_tag
1212
client_name, apikey = get_client_and_api_key
13-
javascript_string = "var #{client_name} = filestack.init('#{apikey}');"
13+
signature, policy = get_policy_and_signature
14+
javascript_string = if policy && signature
15+
"var #{client_name} = filestack.init('#{apikey}'," \
16+
"{'signature': '#{signature}', 'policy': '#{policy}'});"
17+
else
18+
"var #{client_name} = filestack.init('#{apikey}');"
19+
end
1420
javascript_tag javascript_string
1521
end
1622

@@ -57,5 +63,16 @@ def get_client_and_api_key
5763
apikey = ::Rails.application.config.filestack_rails.api_key
5864
[client_name, apikey]
5965
end
66+
67+
def get_policy_and_signature
68+
if ::Rails.application.config.filestack_rails.security
69+
signature = ::Rails.application.config.filestack_rails.security.signature
70+
policy = ::Rails.application.config.filestack_rails.security.policy
71+
else
72+
signature = nil
73+
policy = nil
74+
end
75+
return [signature, policy]
76+
end
6077
end
6178
end

filestack_rails.gemspec filestack-rails.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require "filestack_rails/version"
55

66
# Describe your gem and declare its dependencies:
77
Gem::Specification.new do |s|
8-
s.name = "filestack_rails"
8+
s.name = "filestack-rails"
99
s.version = FilestackRails::VERSION
1010
s.authors = ["filestack"]
1111
s.email = ["[email protected]"]
File renamed without changes.

lib/filestack_rails/configuration.rb

+9-67
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,7 @@
11
module FilestackRails
22
class Configuration
3+
attr_accessor :api_key, :client_name, :secret_key, :security, :expiry, :app_secret
34

4-
# Define your API Key to be used.
5-
#
6-
# #### Examples
7-
#
8-
# This is to be used on the `config/application.rb`:
9-
#
10-
# config.filepicker_rails.api_key = 'Your filepicker.io API Key'
11-
#
12-
attr_writer :api_key
13-
14-
attr_writer :client_name
15-
16-
# Define your Secret key to be used on Policy.
17-
#
18-
# More info about Policy on [Ink documentation](https://developers.filepicker.io/docs/security/)
19-
#
20-
# #### Examples
21-
#
22-
# This is to be used on the `config/application.rb`:
23-
#
24-
# config.filepicker_rails.secret_key = 'Your filepicker.io Secret Key'
25-
#
26-
attr_writer :secret_key
27-
28-
# @private
29-
attr_reader :secret_key
30-
31-
# Set your CDN Path to be used
32-
#
33-
# More info about CDN on [Ink documentation](https://developers.filepicker.io/docs/cdn/)
34-
#
35-
# #### Examples
36-
#
37-
# This is to be used on the `config/application.rb`:
38-
#
39-
# config.filepicker_rails.cdn_host = 'Your CDN host name'
40-
#
41-
attr_writer :cdn_host
42-
43-
# @private
44-
attr_reader :cdn_host
45-
46-
# @private
475
def api_key
486
@api_key or raise "Set config.filepicker_rails.api_key"
497
end
@@ -52,32 +10,16 @@ def client_name
5210
@client_name or 'filestack_client'
5311
end
5412

55-
# Define the expire time when using Policy.
56-
#
57-
# By default the expiry time is 10 minutes.
58-
# If you need to change the expiry time this should be an integer and
59-
# it is expressed in seconds since the [Epoch](http://en.wikipedia.org/wiki/Unix_time).
60-
#
61-
# #### Examples
62-
#
63-
# This is to be used on the `config/application.rb`:
64-
#
65-
# config.filepicker_rails.expiry = -> { (Time.zone.now + 5.minutes).to_i }
66-
# # Define the expiry time to 5 minutes
67-
#
68-
# If you need always the same url, a static expiry time, to do some cache.
69-
# You can set a date starting of the Epoch.
70-
#
71-
# config.filepicker_rails.expiry = -> { 100.years.since(Time.at(0)).to_i }
72-
#
73-
def expiry=(expiry)
74-
raise ArgumentError, 'Must be a callable' unless expiry.respond_to?(:call)
75-
@expiry = expiry
13+
def expiry
14+
@expiry or ( Time.zone.now.to_i + 600 )
7615
end
7716

78-
# @private
79-
def expiry
80-
@expiry ||= -> { Time.zone.now.to_i + 600 }
17+
def security=(security_options = {})
18+
if @app_secret.nil?
19+
raise 'You must have secret key to use security'
20+
end
21+
@security = FilestackSecurity.new(@app_secret, options: security_options)
8122
end
23+
8224
end
8325
end

lib/filestack_rails/transform.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
class FilestackTransform
44
def initialize(apikey)
5-
@transform = Transform.new(apikey:apikey)
5+
security = ::Rails.application.config.filestack_rails.security
6+
if !security.nil?
7+
@transform = Transform.new(apikey: apikey, security: security)
8+
else
9+
@transform = Transform.new(apikey: apikey)
10+
end
611
end
712

813
def method_missing(method_name, **args)

spec/dummy/config/application.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'rails/all'
44

55
Bundler.require(*Rails.groups)
6-
require 'filestack_rails'
6+
require 'filestack-rails'
77

88
module Dummy
99
class Application < Rails::Application
@@ -12,6 +12,7 @@ class Application < Rails::Application
1212
config.assets.compile = true
1313
config.filestack_rails.api_key = 'API_KEY'
1414
config.filestack_rails.client_name = 'rich_client'
15+
config.filestack_rails.app_secret = 'OLHJG4JUABB7DFL6CGATC4SN74'
1516

1617
# Settings in config/environments/* take precedence over those specified here.
1718
# Application configuration should go into files in config/initializers

spec/lib/configuration_spec.rb

+14-19
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,11 @@
66
end
77

88
describe "#api_key=" do
9-
it "respond to api_key=" do
9+
it "responds to api_key=" do
1010
expect(configuration).to respond_to(:api_key=)
1111
end
1212
end
1313

14-
describe "#secret_key=" do
15-
it "respond to secret_key=" do
16-
expect(configuration).to respond_to(:secret_key=)
17-
end
18-
end
19-
20-
describe "#expiry=" do
21-
it "respond to expiry=" do
22-
expect(configuration).to respond_to(:expiry=)
23-
end
24-
25-
it 'raises error if not receive a callable' do
26-
expect do
27-
configuration.expiry = 12
28-
end.to raise_error(ArgumentError, 'Must be a callable')
29-
end
30-
end
31-
3214
describe "#api_key" do
3315
it "have defined value" do
3416
configuration.api_key = "my api key"
@@ -41,4 +23,17 @@
4123
end.to raise_error(RuntimeError, "Set config.filepicker_rails.api_key")
4224
end
4325
end
26+
27+
describe "#security" do
28+
it "has no security" do
29+
expect(configuration.security).to be(nil)
30+
end
31+
32+
it "has security" do
33+
configuration.app_secret = 'somesecret'
34+
configuration.security = {}
35+
expect(configuration.security.policy)
36+
expect(configuration.security.signature)
37+
end
38+
end
4439
end

0 commit comments

Comments
 (0)