Skip to content

Commit

Permalink
Build security per request
Browse files Browse the repository at this point in the history
  • Loading branch information
gabifija committed Jun 20, 2019
1 parent 691ad05 commit f39bd24
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 12 deletions.
19 changes: 12 additions & 7 deletions app/helpers/filestack_rails/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,11 @@ def get_client_and_api_key
end

def get_policy_and_signature
if ::Rails.application.config.filestack_rails.security
signature = ::Rails.application.config.filestack_rails.security.signature
policy = ::Rails.application.config.filestack_rails.security.policy
else
signature = nil
policy = nil
if security
signature = security.signature
policy = security.policy
end
return [signature, policy]
[signature, policy]
end

def get_policy_and_signature_string
Expand All @@ -104,5 +101,13 @@ def get_policy_and_signature_string
"''"
end
end

def security
security_options = ::Rails.application.config.filestack_rails.security
app_secret = ::Rails.application.config.filestack_rails.app_secret

return nil unless security_options
FilestackSecurity.new(app_secret, options: security_options)
end
end
end
5 changes: 4 additions & 1 deletion lib/filestack_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ def security=(security_options = {})
if @app_secret.nil?
raise 'You must have secret key to use security'
end
@security = FilestackSecurity.new(@app_secret, options: security_options)
@security = security_options

This comment has been minimized.

Copy link
@webdev36

webdev36 Mar 20, 2020

I think this is wrong code

This comment has been minimized.

Copy link
@webdev36

webdev36 Mar 20, 2020

The error occurs when using the enabled security filestack app

This comment has been minimized.

Copy link
@taylorthurlow

taylorthurlow Apr 11, 2020

I agree.

This comment has been minimized.

Copy link
@taylorthurlow

taylorthurlow Apr 11, 2020

@gabifiolek please see my recent comment on #228

This comment has been minimized.

Copy link
@taylorthurlow

taylorthurlow Apr 11, 2020

See my comment, I've come to the conclusion that there's no bug, just a breaking change to the public API of FilestackConfiguration, without the appropriate version bump.

end

def app_secret
@app_secret or nil
end
end
end
24 changes: 22 additions & 2 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,37 @@
end

describe "#get_policy_and_signature_string" do
let(:signature) { "signature123" }
let(:policy) { "policy321" }

it "returns correct data" do
allow_any_instance_of(FilestackRails::ApplicationHelper).to receive(:get_policy_and_signature)
.and_return(["21312SDFSDF", "4234DSFSDFDSF"])
.and_return([:signature, :policy])

expect(get_policy_and_signature_string).to eq(
"{\"security\":{\"signature\":\"21312SDFSDF\",\"policy\":\"4234DSFSDFDSF\"}}"
{"security":{"signature": :signature, "policy": :policy}}.to_json
)
end

it "returns empty data" do
expect(get_policy_and_signature_string).to eq("''")
end
end

describe "#security" do
it "returns signature and policy" do
allow(Rails.application.config.filestack_rails).to receive(:security)
.and_return({call: %w[read store pick stat write], expiry: 60})
allow(Rails.application.config.filestack_rails).to receive(:app_secret)
.and_return('app_secret123')

expect(security.policy).to be
expect(security.signature).to be
end

it "does not return signature and policy" do
allow(::Rails.application.config.filestack_rails).to receive(:security).and_return(nil)
expect(security).to be(nil)
end
end
end
14 changes: 12 additions & 2 deletions spec/lib/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
it 'has security' do
configuration.app_secret = 'somesecret'
configuration.security = {}
expect(configuration.security.policy)
expect(configuration.security.signature)
expect(configuration.security).to eq({})
end

it 'has not defined @app_secret' do
Expand Down Expand Up @@ -84,4 +83,15 @@
expect(configuration.expiry).to eq expiry
end
end

describe '#app_secret' do
it 'has defined value' do
configuration.app_secret = "my app secret"
expect(configuration.app_secret).to eq("my app secret")
end

it 'has no @app_secret' do
expect(configuration.app_secret).to eq(nil)
end
end
end

0 comments on commit f39bd24

Please sign in to comment.