action_interceptor 0.5.3 → 1.0.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.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -2
  3. data/README.md +30 -107
  4. data/config/initializers/action_interceptor.rb +22 -48
  5. data/lib/action_interceptor.rb +6 -19
  6. data/lib/action_interceptor/action_controller/base.rb +94 -0
  7. data/lib/action_interceptor/configuration.rb +7 -0
  8. data/lib/action_interceptor/engine.rb +1 -3
  9. data/lib/action_interceptor/strategies.rb +19 -0
  10. data/lib/action_interceptor/strategies/session.rb +28 -0
  11. data/lib/action_interceptor/version.rb +1 -1
  12. data/spec/dummy/config/application.rb +0 -2
  13. data/spec/dummy/config/environments/production.rb +1 -1
  14. data/spec/dummy/config/environments/test.rb +1 -1
  15. data/spec/dummy/config/initializers/action_interceptor.rb +3 -11
  16. data/spec/dummy/lib/strategies/dummy.rb +24 -0
  17. data/spec/dummy/log/test.log +1 -0
  18. data/spec/lib/action_interceptor/action_controller/base_spec.rb +65 -0
  19. data/spec/lib/action_interceptor/configuration_spec.rb +20 -0
  20. data/spec/lib/action_interceptor/strategies/session_spec.rb +29 -0
  21. data/spec/lib/action_interceptor/strategies_spec.rb +23 -0
  22. data/spec/lib/action_interceptor_spec.rb +2 -18
  23. metadata +35 -35
  24. data/lib/action_interceptor/action_controller.rb +0 -175
  25. data/lib/action_interceptor/action_mailer.rb +0 -21
  26. data/lib/action_interceptor/common.rb +0 -74
  27. data/lib/action_interceptor/encryptor.rb +0 -36
  28. data/lib/action_interceptor/undefined_interceptor.rb +0 -4
  29. data/spec/lib/action_interceptor/action_controller_spec.rb +0 -62
  30. data/spec/lib/action_interceptor/action_mailer_spec.rb +0 -16
  31. data/spec/lib/action_interceptor/common_spec.rb +0 -25
  32. data/spec/lib/action_interceptor/encryptor_spec.rb +0 -19
@@ -1,21 +0,0 @@
1
- module ActionInterceptor
2
- module ActionMailer
3
-
4
- def self.included(base)
5
- base.helper_method :interceptor_enabled, :interceptor_enabled=
6
- end
7
-
8
- protected
9
-
10
- def interceptor_enabled
11
- false
12
- end
13
-
14
- def interceptor_enabled=(arg)
15
- false
16
- end
17
-
18
- end
19
- end
20
-
21
- ActionMailer::Base.send :include, ActionInterceptor::ActionMailer
@@ -1,74 +0,0 @@
1
- require 'action_interceptor/action_controller'
2
-
3
- module ActionInterceptor
4
- module Common
5
-
6
- def self.included(base)
7
- base.alias_method_chain :url_for, :interceptor
8
- end
9
-
10
- def url_for_with_interceptor(options = {})
11
- url = url_for_without_interceptor(options)
12
- return url unless interceptor_enabled
13
-
14
- @interceptor_url_for_hash ||= is_interceptor? ? \
15
- intercepted_url_hash : \
16
- current_url_hash
17
-
18
- uri = URI(url)
19
- new_query = HashWithIndifferentAccess[
20
- URI.decode_www_form(uri.query || '')
21
- ].merge(@interceptor_url_for_hash)
22
- uri.query = URI.encode_www_form(new_query)
23
- uri.to_s
24
- end
25
-
26
- protected
27
-
28
- # Executes the given block as if it was inside an interceptor
29
- def with_interceptor(&block)
30
- previous_interceptor_enabled = interceptor_enabled
31
-
32
- begin
33
- # Send the referer with intercepted requests
34
- # So we don't rely on the user's browser to do it for us
35
- self.interceptor_enabled = true
36
-
37
- # Execute the block as if it was defined in this controller
38
- instance_exec &block
39
- rescue LocalJumpError => e
40
- # Silently ignore `return` errors in the block
41
- # and return the given value
42
- e.exit_value
43
- ensure
44
- self.interceptor_enabled = previous_interceptor_enabled
45
- end
46
- end
47
-
48
- # Executes the given block as if it was not inside an interceptor
49
- def without_interceptor(&block)
50
- previous_interceptor_enabled = interceptor_enabled
51
-
52
- begin
53
- # Send the referer with intercepted requests
54
- # So we don't rely on the user's browser to do it for us
55
- self.interceptor_enabled = false
56
-
57
- # Execute the block as if it was defined in this controller
58
- instance_exec &block
59
- rescue LocalJumpError => e
60
- # Silently ignore `return` errors in the block
61
- # and return the given value
62
- e.exit_value
63
- ensure
64
- self.interceptor_enabled = previous_interceptor_enabled
65
- end
66
- end
67
-
68
- end
69
- end
70
-
71
- ActionController::Base.send :include, ActionInterceptor::Common
72
- ActionView::Base.send :include, ActionInterceptor::Common
73
- ActionView::RoutingUrlFor.send :include, ActionInterceptor::Common \
74
- if defined?(ActionView::RoutingUrlFor)
@@ -1,36 +0,0 @@
1
- module ActionInterceptor
2
- class Encryptor
3
-
4
- def self.encrypt_and_sign(value)
5
- message_encryptor.encrypt_and_sign(value)
6
- end
7
-
8
- def self.decrypt_and_verify(value)
9
- message_encryptor.decrypt_and_verify(value)
10
- end
11
-
12
- protected
13
-
14
- def self.message_encryptor
15
- return @message_encryptor if @message_encryptor
16
-
17
- application = Rails.application
18
- config = application.config
19
- application_secret = application.secrets[:secret_key_base] \
20
- if application.respond_to?(:secrets)
21
- application_secret ||= config.secret_key_base if config.respond_to?(:secret_key_base)
22
- application_secret ||= config.secret_token
23
-
24
- # This is how Rails 4 generates keys for encrypted cookies
25
- # Except that, in Rails 4, MessageEncryptor can take 2 different secrets,
26
- # one for encryption and one for verification
27
- salt = 'encrypted intercepted url'
28
- secret = OpenSSL::PKCS5.pbkdf2_hmac_sha1(
29
- application_secret, salt, 2**16, 64)
30
-
31
- @message_encryptor = ActiveSupport::MessageEncryptor.new(secret,
32
- :serializer => ActiveSupport::MessageEncryptor::NullSerializer)
33
- end
34
-
35
- end
36
- end
@@ -1,4 +0,0 @@
1
- module ActionInterceptor
2
- class UndefinedInterceptor < StandardError
3
- end
4
- end
@@ -1,62 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module ActionInterceptor
4
- describe ActionController do
5
-
6
- it 'modifies ActionController::Base' do
7
- expect(::ActionController::Base).to respond_to(:interceptor_filters)
8
- expect(::ActionController::Base.interceptor_filters).to be_a(Hash)
9
-
10
- expect(::ActionController::Base).to respond_to(:interceptor)
11
- expect(::ActionController::Base).to respond_to(:skip_interceptor)
12
- expect(::ActionController::Base).to respond_to(:acts_as_interceptor)
13
-
14
- expect(::ActionController::Base.new.respond_to?(
15
- :is_interceptor?, true)).to eq(true)
16
- expect(::ActionController::Base.new.send :is_interceptor?).to eq(false)
17
-
18
- expect(::ActionController::Base.new.respond_to?(
19
- :interceptor_enabled, true)).to eq(true)
20
- expect(::ActionController::Base.new.respond_to?(
21
- :current_page?, true)).to eq(true)
22
- expect(::ActionController::Base.new.respond_to?(
23
- :current_url, true)).to eq(true)
24
- expect(::ActionController::Base.new.respond_to?(
25
- :current_url_hash, true)).to eq(true)
26
- end
27
-
28
- it 'modifies classes that act_as_interceptor' do
29
- expect(RegistrationsController.new.send :is_interceptor?).to eq(true)
30
-
31
- expect(RegistrationsController.new.respond_to?(
32
- :intercepted_url, true)).to eq(true)
33
- expect(RegistrationsController.new.respond_to?(
34
- :intercepted_url=, true)).to eq(true)
35
- expect(RegistrationsController.new.respond_to?(
36
- :intercepted_url_hash, true)).to eq(true)
37
- expect(RegistrationsController.new.respond_to?(
38
- :redirect_back, true)).to eq(true)
39
- end
40
-
41
- it 'registers and skips before_filters' do
42
- filters = RegistrationsController.new._process_action_callbacks
43
- .collect{|c| c.filter}
44
- expect(filters).not_to include(:my_interceptor)
45
-
46
- RegistrationsController.interceptor :my_interceptor
47
- filters = RegistrationsController.new._process_action_callbacks
48
- .collect{|c| c.filter}
49
- expect(filters).to include(:my_interceptor)
50
-
51
- filters = ApplicationController.new._process_action_callbacks
52
- .collect{|c| c.filter}
53
- expect(filters).to include(:my_interceptor)
54
-
55
- ApplicationController.skip_interceptor :my_interceptor
56
- filters = ApplicationController.new._process_action_callbacks
57
- .collect{|c| c.filter}
58
- expect(filters).not_to include(:my_interceptor)
59
- end
60
-
61
- end
62
- end
@@ -1,16 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module ActionInterceptor
4
- describe ActionMailer do
5
-
6
- it 'modifies ActionMailer::Base' do
7
- mailer = ::ActionMailer::Base.send(:new)
8
- expect(mailer.respond_to?(:interceptor_enabled, true)).to eq(true)
9
- expect(mailer.respond_to?(:interceptor_enabled=, true)).to eq(true)
10
-
11
- expect(mailer.send(:interceptor_enabled)).to eq(false)
12
- expect(mailer.send(:interceptor_enabled=, true)).to eq(false)
13
- end
14
-
15
- end
16
- end
@@ -1,25 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module ActionInterceptor
4
- describe Common do
5
-
6
- it 'modifies ActionController::Base' do
7
- expect(::ActionController::Base.new.respond_to?(
8
- :url_for, true)).to eq(true)
9
- expect(::ActionController::Base.new.respond_to?(
10
- :with_interceptor, true)).to eq(true)
11
- expect(::ActionController::Base.new.respond_to?(
12
- :without_interceptor, true)).to eq(true)
13
- end
14
-
15
- it 'modifies ActionView::Base' do
16
- expect(::ActionView::Base.new.respond_to?(
17
- :url_for, true)).to eq(true)
18
- expect(::ActionView::Base.new.respond_to?(
19
- :with_interceptor, true)).to eq(true)
20
- expect(::ActionView::Base.new.respond_to?(
21
- :without_interceptor, true)).to eq(true)
22
- end
23
-
24
- end
25
- end
@@ -1,19 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module ActionInterceptor
4
- describe Encryptor do
5
-
6
- it 'encrypts and decrypts strings' do
7
- my_string = 'My string'
8
- encrypted_string = Encryptor.encrypt_and_sign(my_string)
9
- expect(encrypted_string).not_to include(my_string)
10
-
11
- decrypted_string = Encryptor.decrypt_and_verify(encrypted_string)
12
- expect(decrypted_string).to eq(my_string)
13
-
14
- expect{Encryptor.decrypt_and_verify(my_string)}.to(
15
- raise_error(ActiveSupport::MessageVerifier::InvalidSignature))
16
- end
17
-
18
- end
19
- end