simple_token_authentication 1.5.1 → 1.5.2

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 (80) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +23 -24
  3. data/Rakefile +31 -11
  4. data/doc/README.md +18 -0
  5. data/lib/simple_token_authentication.rb +39 -0
  6. data/lib/simple_token_authentication/acts_as_token_authenticatable.rb +18 -7
  7. data/lib/simple_token_authentication/acts_as_token_authentication_handler.rb +12 -123
  8. data/lib/simple_token_authentication/adapter.rb +7 -0
  9. data/lib/simple_token_authentication/adapters/active_record_adapter.rb +14 -0
  10. data/lib/simple_token_authentication/adapters/rails_adapter.rb +14 -0
  11. data/lib/simple_token_authentication/configuration.rb +25 -0
  12. data/lib/simple_token_authentication/entities_manager.rb +10 -0
  13. data/lib/simple_token_authentication/entity.rb +64 -0
  14. data/lib/simple_token_authentication/fallback_authentication_handler.rb +11 -0
  15. data/lib/simple_token_authentication/sign_in_handler.rb +19 -0
  16. data/lib/simple_token_authentication/token_authentication_handler.rb +138 -0
  17. data/lib/simple_token_authentication/token_comparator.rb +13 -0
  18. data/lib/simple_token_authentication/token_generator.rb +9 -0
  19. data/lib/simple_token_authentication/version.rb +1 -1
  20. data/spec/configuration/action_controller_callbacks_options_spec.rb +53 -0
  21. data/spec/configuration/fallback_to_devise_option_spec.rb +128 -0
  22. data/spec/configuration/header_names_option_spec.rb +454 -0
  23. data/spec/configuration/sign_in_token_option_spec.rb +92 -0
  24. data/spec/lib/simple_token_authentication/acts_as_token_authenticatable_spec.rb +108 -0
  25. data/spec/lib/simple_token_authentication/acts_as_token_authentication_handler_spec.rb +127 -0
  26. data/spec/lib/simple_token_authentication/adapter_spec.rb +21 -0
  27. data/spec/lib/simple_token_authentication/adapters/active_record_adapter_spec.rb +21 -0
  28. data/spec/lib/simple_token_authentication/adapters/rails_adapter_spec.rb +21 -0
  29. data/spec/lib/simple_token_authentication/configuration_spec.rb +121 -0
  30. data/spec/lib/simple_token_authentication/entities_manager_spec.rb +67 -0
  31. data/spec/lib/simple_token_authentication/entity_spec.rb +190 -0
  32. data/spec/lib/simple_token_authentication/fallback_authentication_handler_spec.rb +24 -0
  33. data/spec/lib/simple_token_authentication/sign_in_handler_spec.rb +43 -0
  34. data/spec/lib/simple_token_authentication/token_authentication_handler_spec.rb +250 -0
  35. data/spec/lib/simple_token_authentication/token_comparator_spec.rb +19 -0
  36. data/spec/lib/simple_token_authentication/token_generator_spec.rb +19 -0
  37. data/spec/lib/simple_token_authentication_spec.rb +86 -0
  38. data/spec/spec_helper.rb +13 -0
  39. data/spec/support/dummy_classes_helper.rb +80 -0
  40. data/spec/support/spec_for_adapter.rb +6 -0
  41. data/spec/support/spec_for_authentication_handler_interface.rb +8 -0
  42. data/spec/support/spec_for_configuration_option_interface.rb +28 -0
  43. data/spec/support/spec_for_entities_manager_interface.rb +8 -0
  44. data/spec/support/spec_for_sign_in_handler_interface.rb +8 -0
  45. data/spec/support/spec_for_token_comparator_interface.rb +8 -0
  46. data/spec/support/spec_for_token_generator_interface.rb +8 -0
  47. data/spec/support/specs_for_token_authentication_handler_interface.rb +8 -0
  48. metadata +80 -132
  49. data/lib/tasks/cucumber.rake +0 -65
  50. data/spec/dummy/README.rdoc +0 -28
  51. data/spec/dummy/Rakefile +0 -6
  52. data/spec/dummy/app/assets/javascripts/application.js +0 -13
  53. data/spec/dummy/app/assets/stylesheets/application.css +0 -13
  54. data/spec/dummy/app/controllers/application_controller.rb +0 -5
  55. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  56. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  57. data/spec/dummy/bin/bundle +0 -3
  58. data/spec/dummy/bin/rails +0 -4
  59. data/spec/dummy/bin/rake +0 -4
  60. data/spec/dummy/config.ru +0 -4
  61. data/spec/dummy/config/application.rb +0 -24
  62. data/spec/dummy/config/boot.rb +0 -5
  63. data/spec/dummy/config/database.yml +0 -25
  64. data/spec/dummy/config/environment.rb +0 -5
  65. data/spec/dummy/config/environments/development.rb +0 -29
  66. data/spec/dummy/config/environments/production.rb +0 -80
  67. data/spec/dummy/config/environments/test.rb +0 -36
  68. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  69. data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  70. data/spec/dummy/config/initializers/inflections.rb +0 -16
  71. data/spec/dummy/config/initializers/mime_types.rb +0 -5
  72. data/spec/dummy/config/initializers/secret_token.rb +0 -12
  73. data/spec/dummy/config/initializers/session_store.rb +0 -3
  74. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
  75. data/spec/dummy/config/locales/en.yml +0 -23
  76. data/spec/dummy/config/routes.rb +0 -56
  77. data/spec/dummy/public/404.html +0 -58
  78. data/spec/dummy/public/422.html +0 -58
  79. data/spec/dummy/public/500.html +0 -57
  80. data/spec/dummy/public/favicon.ico +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4aa1abbb8d50757dcf03bc2258df433e83f0e6fa
4
- data.tar.gz: a1e59e796dc712d0915f960e94ed6291226a0513
3
+ metadata.gz: 6e7a77a49ad9d0cecdf975b21c733087091a4671
4
+ data.tar.gz: 6ea3dea47399dc595603ebbe97bc859417bfe775
5
5
  SHA512:
6
- metadata.gz: 2a991fdfebff8510b53bdb51a8c3c7eb50768568307977104225506d598d7cf05164da64fcb362851b44038a8f97c350c2b0adc31fd833a1a4ff1adbbea2140c
7
- data.tar.gz: 2f02cb5a9bc457ecde73c588fae28c8e3235f352cb140a2cb64beff25ad18662ea56d613d49bb6fe480fd5303765fef68fb9828fdcd8180d48890aaff757ac27
6
+ metadata.gz: c8f34f63afbf988faccb9d952740ef54283846e1d9163d937323102e793948bed2567fd0cacca4c311cb6243d97b42223874045fad0f39b611d3e7bf29e889eb
7
+ data.tar.gz: 9409b4492ef48a1ceb2f3c4ed00a509e37cf0b9de0338486474fb5101c4eccb685eb09017d0a215944c6cafe453eeea9bb4bde06e1ec9bae5cc5f834437abdf0
data/README.md CHANGED
@@ -1,10 +1,12 @@
1
1
  Simple Token Authentication
2
2
  ===========================
3
3
 
4
- [![Gem Version](https://badge.fury.io/rb/simple_token_authentication.png)](http://badge.fury.io/rb/simple_token_authentication)
5
- [![Build Status](https://travis-ci.org/gonzalo-bulnes/simple_token_authentication.png?branch=master)](https://travis-ci.org/gonzalo-bulnes/simple_token_authentication)
6
- [![Code Climate](https://codeclimate.com/github/gonzalo-bulnes/simple_token_authentication.png)](https://codeclimate.com/github/gonzalo-bulnes/simple_token_authentication)
4
+ [![Gem Version](https://badge.fury.io/rb/simple_token_authentication.svg)](http://badge.fury.io/rb/simple_token_authentication)
5
+ [![Build Status](https://travis-ci.org/gonzalo-bulnes/simple_token_authentication.svg?branch=master)](https://travis-ci.org/gonzalo-bulnes/simple_token_authentication)
6
+ [![Code Climate](https://codeclimate.com/github/gonzalo-bulnes/simple_token_authentication.svg)](https://codeclimate.com/github/gonzalo-bulnes/simple_token_authentication)
7
7
  [![Dependency Status](https://gemnasium.com/gonzalo-bulnes/simple_token_authentication.svg)](https://gemnasium.com/gonzalo-bulnes/simple_token_authentication)
8
+ [![security](https://hakiri.io/github/gonzalo-bulnes/simple_token_authentication/master.svg)](https://hakiri.io/github/gonzalo-bulnes/simple_token_authentication/master)
9
+ [![Inline docs](http://inch-ci.org/github/gonzalo-bulnes/simple_token_authentication.svg?branch=master)](http://inch-ci.org/github/gonzalo-bulnes/simple_token_authentication)
8
10
 
9
11
  Token authentication support has been removed from [Devise][devise] for security reasons. In [this gist][original-gist], Devise's [José Valim][josevalim] explains how token authentication should be performed in order to remain safe.
10
12
 
@@ -170,19 +172,6 @@ If sign-in is successful, no other authentication method will be run, but if it
170
172
  Documentation
171
173
  -------------
172
174
 
173
- ### Executable documentation
174
-
175
- The Cucumber scenarii describe how to setup demonstration applications for different use cases. While you can read the `rake` output, you may prefer to read it in HTML format: see `doc/features.html`. The file is generated automatically by Cucumber, if necessary, you can update it by yourself:
176
-
177
- ```bash
178
- cd simple_token_authentication
179
- rake features_html # generate the features documentation
180
-
181
- # Open doc/features.html in your preferred web browser.
182
- ```
183
-
184
- I find that HTML output quite enjoyable, I hope you'll do so!
185
-
186
175
  ### Frequently Asked Questions
187
176
 
188
177
  Any question? Please don't hesitate to open a new issue to get help. I keep questions tagged to make possible to [review the open questions][open-questions], while closed questions are organized as a sort of [FAQ][faq].
@@ -199,18 +188,23 @@ Releases are commented to provide a brief [changelog][changelog].
199
188
  Development
200
189
  -----------
201
190
 
202
- ### Testing
191
+ ### Testing and documentation
203
192
 
204
- Since `v1.0.0`, this gem development is test-driven. Each use case should be described with [RSpec][rspec] within an example app. That app will be created and configured automatically by [Aruba][aruba] as a [Cucumber][cucumber] feature.
193
+ This gem development has been test-driven since `v1.0.0`. Until `v1.5.1`, the gem behaviour was described using [Cucumber][cucumber] and [RSpec][rspec] in a dummy app generated by [Aruba][aruba]. Since `v1.5.2` it is described using Rspec alone.
205
194
 
206
- The resulting Cucumber features are a bit verbose, and their output when errors occur is not ideal, but their output when they are passing, on the contrary, provides an easy-to-reproduce recipe to build the example app (see [Executable documentation][exec-doc]). I find that useful enough to be patient with red scenarii for now.
195
+ RSpec [tags][tags] are used to categorize the spec examples.
196
+
197
+ Spec examples that are tagged as `public` describe aspects of the gem public API, and MAY be considered as the gem documentation.
198
+
199
+ The `private` or `protected` specs are written for development purpose only. Because they describe internal behaviour which may change at any moment without notice, they are only executed as a secondary task by the [continuous integration service][travis] and SHOULD be ignored.
200
+
201
+ Run `rake spec:public` to print the gem public documentation.
207
202
 
208
203
  [aruba]: https://github.com/cucumber/aruba
209
204
  [cucumber]: https://github.com/cucumber/cucumber-rails
210
205
  [rspec]: https://www.relishapp.com/rspec/rspec-rails/docs
211
- [exec-doc]: https://github.com/gonzalo-bulnes/simple_token_authentication#executable-documentation
212
-
213
- You can run the full test suite with `cd simple_token_authentication && rake`.
206
+ [tags]: https://www.relishapp.com/rspec/rspec-core/v/3-1/docs/command-line/tag-option
207
+ [travis]: https://travis-ci.org/gonzalo-bulnes/simple_token_authentication/builds
214
208
 
215
209
  ### Contributions
216
210
 
@@ -218,16 +212,21 @@ Contributions are welcome! I'm not personally maintaining any [list of contribut
218
212
 
219
213
  [contributors]: https://github.com/gonzalo-bulnes/simple_token_authentication/graphs/contributors
220
214
 
215
+ Please be sure to [review the open issues][open-questions] and contribute with your ideas or code in the issue best suited to the topic. Keeping discussions in a single place makes easier to everyone interested in that topic to keep track of the contributions.
216
+
221
217
  Credits
222
218
  -------
223
219
 
224
- It may sound a bit redundant, but this gem wouldn't exist without [this gist][original-gist].
220
+ It may sound a bit redundant, but this gem wouldn't exist without [this gist][original-gist], nor without the [comments][issues] and [contributions][pulls] of many people. Thank them if you see them!
221
+
222
+ [issues]: https://github.com/gonzalo-bulnes/simple_token_authentication/issues
223
+ [pulls]: https://github.com/gonzalo-bulnes/simple_token_authentication/pulls
225
224
 
226
225
  License
227
226
  -------
228
227
 
229
228
  Simple Token Authentication
230
- Copyright (C) 2013 Gonzalo Bulnes Guilpain
229
+ Copyright (C) 2013, 2014 Gonzalo Bulnes Guilpain
231
230
 
232
231
  This program is free software: you can redistribute it and/or modify
233
232
  it under the terms of the GNU General Public License as published by
data/Rakefile CHANGED
@@ -19,23 +19,43 @@ Bundler::GemHelper.install_tasks
19
19
 
20
20
 
21
21
  begin
22
- require 'cucumber'
23
- require 'cucumber/rake/task'
22
+ require 'inch/rake'
24
23
 
25
- Cucumber::Rake::Task.new(:features) do |t|
26
- t.cucumber_opts = "--format pretty"
24
+ Inch::Rake::Suggest.new(:inch) do |suggest|
25
+ suggest.args << "--private"
26
+ suggest.args << "--pedantic"
27
27
  end
28
+ rescue LoadError
29
+ desc 'Inch rake task not available'
30
+ task :inch do
31
+ abort 'Inch rake task is not available. Be sure to install inch as a gem or plugin'
32
+ end
33
+ end
34
+
35
+ begin
36
+ require 'rspec/core/rake_task'
37
+
38
+ desc 'Provide private interfaces documentation'
39
+ RSpec::Core::RakeTask.new(:spec)
28
40
 
29
- Cucumber::Rake::Task.new(:features_html) do |t|
30
- t.cucumber_opts = "--format html --out doc/features.html"
41
+ namespace :spec do
42
+ desc 'Provide public interfaces documentation'
43
+ RSpec::Core::RakeTask.new(:public) do |t|
44
+ t.rspec_opts = "--tag public"
45
+ end
31
46
  end
32
47
 
48
+ namespace :spec do
49
+ desc 'Provide private interfaces documentation for development purpose'
50
+ RSpec::Core::RakeTask.new(:development) do |t|
51
+ t.rspec_opts = "--tag protected --tag private"
52
+ end
53
+ end
33
54
  rescue LoadError
34
- desc 'Cucumber rake task not available'
35
- task :features do
36
- abort 'Cucumber rake task is not available. Be sure to install cucumber as a gem or plugin'
55
+ desc 'RSpec rake task not available'
56
+ task :spec do
57
+ abort 'RSpec rake task is not available. Be sure to install rspec-core as a gem or plugin'
37
58
  end
38
59
  end
39
60
 
40
-
41
- task default: :features
61
+ task default: ['spec:public', 'spec:development', :inch]
data/doc/README.md ADDED
@@ -0,0 +1,18 @@
1
+ Documentation
2
+ =============
3
+
4
+ **Looking for the HTML features decription?**
5
+
6
+ The Cucumber features that documented the gem behaviour until `v1.5.1` constituted a robust tests suite, but they were slow and writting them was difficult enough to become a continuous bottleneck.
7
+
8
+ I decided to tackle the issue by replacing most scenarios by unit tests (see [#104][issue]), and since `v1.5.2` the gem behaviour is documented using RSpec only.
9
+
10
+ I liked the [executable documentation][exec-doc] idea, and I do not discard using Cucumber again to test _Simple Token Authentication_.
11
+ However, truth is that neither the somewhat intricated [Cucumber][cucumber] - [Aruba][aruba] - [RSpec][rspec] setup or the steps I wrote were exemplary enough to make justice to the great tool Cucumber is. So I decided to stop maintaining the features and to remove them. The RSpec test suite provides a nice [documentation][doc], and sometimes the best is a fresh start.
12
+
13
+ [exec-doc]: https://github.com/gonzalo-bulnes/simple_token_authentication/tree/v1.5.1#executable-documentation
14
+ [doc]: #testing-and-documentation
15
+ [issue]: https://github.com/gonzalo-bulnes/simple_token_authentication/issues/104
16
+ [aruba]: https://github.com/cucumber/aruba
17
+ [cucumber]: https://github.com/cucumber/cucumber-rails
18
+ [rspec]: https://www.relishapp.com/rspec/rspec-rails/docs
@@ -4,4 +4,43 @@ require 'simple_token_authentication/configuration'
4
4
 
5
5
  module SimpleTokenAuthentication
6
6
  extend Configuration
7
+
8
+ private
9
+
10
+ def self.ensure_models_can_act_as_token_authenticatables model_adapters
11
+ model_adapters.each do |model_adapter|
12
+ model_adapter.base_class.send :include, SimpleTokenAuthentication::ActsAsTokenAuthenticatable
13
+ end
14
+ end
15
+
16
+ def self.ensure_controllers_can_act_as_token_authentication_handlers controller_adapters
17
+ controller_adapters.each do |controller_adapter|
18
+ controller_adapter.base_class.send :extend, SimpleTokenAuthentication::ActsAsTokenAuthenticationHandler
19
+ end
20
+ end
21
+
22
+ # Private: Load the available adapters.
23
+ #
24
+ # adapters_short_names - Array of names of the adapters to load if available
25
+ #
26
+ # Example
27
+ #
28
+ # load_available_adapters ['unavailable_adapter', 'available_adapter']
29
+ # # => [SimpleTokenAuthentication::Adapters::AvailableAdapter]
30
+ #
31
+ # Returns an Array of available adapters
32
+ def self.load_available_adapters adapters_short_names
33
+ adapters_short_names.collect do |short_name|
34
+ adapter_name = "simple_token_authentication/adapters/#{short_name}_adapter"
35
+ if require adapter_name
36
+ adapter_name.camelize.constantize
37
+ end
38
+ end
39
+ end
40
+
41
+ available_model_adapters = load_available_adapters SimpleTokenAuthentication.model_adapters
42
+ ensure_models_can_act_as_token_authenticatables available_model_adapters
43
+
44
+ available_controller_adapters = load_available_adapters SimpleTokenAuthentication.controller_adapters
45
+ ensure_controllers_can_act_as_token_authentication_handlers available_controller_adapters
7
46
  end
@@ -1,33 +1,44 @@
1
+ require 'active_support/concern'
2
+ require 'simple_token_authentication/token_generator'
3
+
1
4
  module SimpleTokenAuthentication
2
5
  module ActsAsTokenAuthenticatable
3
- extend ActiveSupport::Concern
6
+ extend ::ActiveSupport::Concern
4
7
 
5
8
  # Please see https://gist.github.com/josevalim/fb706b1e933ef01e4fb6
6
9
  # before editing this file, the discussion is very interesting.
7
10
 
8
11
  included do
9
12
  private :generate_authentication_token
13
+ private :token_suitable?
14
+ private :token_generator
10
15
  end
11
16
 
12
17
  def ensure_authentication_token
13
18
  if authentication_token.blank?
14
- self.authentication_token = generate_authentication_token
19
+ self.authentication_token = generate_authentication_token(token_generator)
15
20
  end
16
21
  end
17
22
 
18
- def generate_authentication_token
23
+ def generate_authentication_token(token_generator)
19
24
  loop do
20
- token = Devise.friendly_token
21
- break token unless self.class.exists?(authentication_token: token)
25
+ token = token_generator.generate_token
26
+ break token if token_suitable?(token)
22
27
  end
23
28
  end
24
29
 
30
+ def token_suitable?(token)
31
+ not self.class.exists?(authentication_token: token)
32
+ end
33
+
34
+ def token_generator
35
+ @token_generator ||= TokenGenerator.new
36
+ end
37
+
25
38
  module ClassMethods
26
39
  def acts_as_token_authenticatable(options = {})
27
- include SimpleTokenAuthentication::ActsAsTokenAuthenticatable
28
40
  before_save :ensure_authentication_token
29
41
  end
30
42
  end
31
43
  end
32
44
  end
33
- ActiveRecord::Base.send :include, SimpleTokenAuthentication::ActsAsTokenAuthenticatable
@@ -1,133 +1,22 @@
1
- module SimpleTokenAuthentication
2
- module ActsAsTokenAuthenticationHandlerMethods
3
- extend ActiveSupport::Concern
4
-
5
- # Please see https://gist.github.com/josevalim/fb706b1e933ef01e4fb6
6
- # before editing this file, the discussion is very interesting.
7
-
8
- included do
9
- private :authenticate_entity_from_token!
10
- private :header_token_name
11
- private :header_email_name
12
-
13
- # This is necessary to test which arguments were passed to sign_in
14
- # from authenticate_entity_from_token!
15
- # See https://github.com/gonzalo-bulnes/simple_token_authentication/pull/32
16
- ActionController::Base.send :include, Devise::Controllers::SignInOut if Rails.env.test?
17
- end
18
-
19
- def authenticate_entity!(entity_class)
20
- # Caution: entity should be a singular camel-cased name but could be pluralized or underscored.
21
- self.method("authenticate_#{entity_class.name.singularize.underscore}!".to_sym).call
22
- end
23
-
24
-
25
- # For this example, we are simply using token authentication
26
- # via parameters. However, anyone could use Rails's token
27
- # authentication features to get the token from a header.
28
- def authenticate_entity_from_token!(entity_class)
29
- # Set the authentication token params if not already present,
30
- # see http://stackoverflow.com/questions/11017348/rails-api-authentication-by-headers-token
31
- params_token_name = "#{entity_class.name.singularize.underscore}_token".to_sym
32
- params_email_name = "#{entity_class.name.singularize.underscore}_email".to_sym
33
- if token = params[params_token_name].blank? && request.headers[header_token_name(entity_class)]
34
- params[params_token_name] = token
35
- end
36
- if email = params[params_email_name].blank? && request.headers[header_email_name(entity_class)]
37
- params[params_email_name] = email
38
- end
39
-
40
- email = params[params_email_name].presence
41
- # See https://github.com/ryanb/cancan/blob/1.6.10/lib/cancan/controller_resource.rb#L108-L111
42
- entity = nil
43
- if entity_class.respond_to? "find_by"
44
- entity = email && entity_class.find_by(email: email)
45
- elsif entity_class.respond_to? "find_by_email"
46
- entity = email && entity_class.find_by_email(email)
47
- end
48
-
49
- # Notice how we use Devise.secure_compare to compare the token
50
- # in the database with the token given in the params, mitigating
51
- # timing attacks.
52
- if entity && Devise.secure_compare(entity.authentication_token, params[params_token_name])
53
- # Sign in using token should not be tracked by Devise trackable
54
- # See https://github.com/plataformatec/devise/issues/953
55
- env["devise.skip_trackable"] = true
56
-
57
- # Notice the store option defaults to false, so the entity
58
- # is not actually stored in the session and a token is needed
59
- # for every request. That behaviour can be configured through
60
- # the sign_in_token option.
61
- sign_in entity, store: SimpleTokenAuthentication.sign_in_token
62
- end
63
- end
64
-
65
- # Private: Return the name of the header to watch for the token authentication param
66
- def header_token_name(entity_class)
67
- if SimpleTokenAuthentication.header_names["#{entity_class.name.singularize.underscore}".to_sym].presence
68
- SimpleTokenAuthentication.header_names["#{entity_class.name.singularize.underscore}".to_sym][:authentication_token]
69
- else
70
- "X-#{entity_class.name.singularize.camelize}-Token"
71
- end
72
- end
73
-
74
- # Private: Return the name of the header to watch for the email param
75
- def header_email_name(entity_class)
76
- if SimpleTokenAuthentication.header_names["#{entity_class.name.singularize.underscore}".to_sym].presence
77
- SimpleTokenAuthentication.header_names["#{entity_class.name.singularize.underscore}".to_sym][:email]
78
- else
79
- "X-#{entity_class.name.singularize.camelize}-Email"
80
- end
81
- end
82
- end
1
+ require 'active_support/deprecation'
2
+ require 'simple_token_authentication/token_authentication_handler'
83
3
 
4
+ module SimpleTokenAuthentication
84
5
  module ActsAsTokenAuthenticationHandler
85
- extend ActiveSupport::Concern
86
6
 
87
- # I have insulated the methods into an additional module to avoid before_filters
88
- # to be applied by the `included` block before acts_as_token_authentication_handler_for was called.
7
+ # This module ensures that no TokenAuthenticationHandler behaviour
8
+ # is added before the class actually `acts_as_token_authentication_handler_for`
9
+ # some token authenticatable model.
89
10
  # See https://github.com/gonzalo-bulnes/simple_token_authentication/issues/8#issuecomment-31707201
90
11
 
91
- included do
92
- # nop
12
+ def acts_as_token_authentication_handler_for(model, options = {})
13
+ include SimpleTokenAuthentication::TokenAuthenticationHandler
14
+ handle_token_authentication_for(model, options)
93
15
  end
94
16
 
95
- module ClassMethods
96
- def acts_as_token_authentication_handler_for(entity, options = {})
97
- options = { fallback_to_devise: true }.merge(options)
98
-
99
- include SimpleTokenAuthentication::ActsAsTokenAuthenticationHandlerMethods
100
-
101
- define_acts_as_token_authentication_helpers_for(entity)
102
-
103
- authenticate_method = if options[:fallback_to_devise]
104
- :"authenticate_#{entity.name.singularize.underscore}_from_token!"
105
- else
106
- :"authenticate_#{entity.name.singularize.underscore}_from_token"
107
- end
108
- before_filter authenticate_method, options.slice(:only, :except)
109
- end
110
-
111
- def acts_as_token_authentication_handler
112
- ActiveSupport::Deprecation.warn "`acts_as_token_authentication_handler()` is deprecated and may be removed from future releases, use `acts_as_token_authentication_handler_for(User)` instead.", caller
113
- acts_as_token_authentication_handler_for User
114
- end
115
-
116
- def define_acts_as_token_authentication_helpers_for(entity_class)
117
- entity_underscored = entity_class.name.singularize.underscore
118
-
119
- class_eval <<-METHODS, __FILE__, __LINE__ + 1
120
- def authenticate_#{entity_underscored}_from_token
121
- authenticate_entity_from_token!(#{entity_class.name})
122
- end
123
-
124
- def authenticate_#{entity_underscored}_from_token!
125
- authenticate_entity_from_token!(#{entity_class.name})
126
- authenticate_entity!(#{entity_class.name})
127
- end
128
- METHODS
129
- end
17
+ def acts_as_token_authentication_handler
18
+ ::ActiveSupport::Deprecation.warn "`acts_as_token_authentication_handler()` is deprecated and may be removed from future releases, use `acts_as_token_authentication_handler_for(User)` instead.", caller
19
+ acts_as_token_authentication_handler_for User
130
20
  end
131
21
  end
132
22
  end
133
- ActionController::Base.send :include, SimpleTokenAuthentication::ActsAsTokenAuthenticationHandler
@@ -0,0 +1,7 @@
1
+ module SimpleTokenAuthentication
2
+ module Adapter
3
+ def base_class
4
+ raise NotImplementedError
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ require 'active_record'
2
+ require 'simple_token_authentication/adapter'
3
+
4
+ module SimpleTokenAuthentication
5
+ module Adapters
6
+ class ActiveRecordAdapter
7
+ extend SimpleTokenAuthentication::Adapter
8
+
9
+ def self.base_class
10
+ ::ActiveRecord::Base
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'action_controller'
2
+ require 'simple_token_authentication/adapter'
3
+
4
+ module SimpleTokenAuthentication
5
+ module Adapters
6
+ class RailsAdapter
7
+ extend SimpleTokenAuthentication::Adapter
8
+
9
+ def self.base_class
10
+ ::ActionController::Base
11
+ end
12
+ end
13
+ end
14
+ end