devise_token_auth 0.1.42 → 0.1.43.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of devise_token_auth might be problematic. Click here for more details.

Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +39 -38
  3. data/app/controllers/devise_token_auth/application_controller.rb +15 -0
  4. data/app/controllers/devise_token_auth/concerns/resource_finder.rb +38 -0
  5. data/app/controllers/devise_token_auth/concerns/set_user_by_token.rb +21 -19
  6. data/app/controllers/devise_token_auth/confirmations_controller.rb +12 -7
  7. data/app/controllers/devise_token_auth/omniauth_callbacks_controller.rb +3 -7
  8. data/app/controllers/devise_token_auth/passwords_controller.rb +16 -26
  9. data/app/controllers/devise_token_auth/registrations_controller.rb +6 -2
  10. data/app/controllers/devise_token_auth/sessions_controller.rb +3 -14
  11. data/app/controllers/devise_token_auth/unlocks_controller.rb +105 -0
  12. data/app/models/devise_token_auth/concerns/user.rb +29 -12
  13. data/app/models/devise_token_auth/concerns/user_omniauth_callbacks.rb +5 -8
  14. data/app/views/devise/mailer/unlock_instructions.html.erb +1 -1
  15. data/app/views/devise_token_auth/omniauth_external_window.html.erb +1 -1
  16. data/config/initializers/devise.rb +1 -1
  17. data/config/locales/da-DK.yml +50 -0
  18. data/config/locales/en.yml +4 -0
  19. data/lib/devise_token_auth/controllers/helpers.rb +30 -20
  20. data/lib/devise_token_auth/rails/routes.rb +1 -1
  21. data/lib/devise_token_auth/url.rb +1 -1
  22. data/lib/devise_token_auth/version.rb +1 -1
  23. data/lib/generators/devise_token_auth/templates/devise_token_auth_create_users.rb.erb +2 -1
  24. data/lib/generators/devise_token_auth/templates/user.rb +1 -1
  25. data/test/controllers/custom/custom_confirmations_controller_test.rb +5 -10
  26. data/test/controllers/custom/custom_omniauth_callbacks_controller_test.rb +15 -16
  27. data/test/controllers/custom/custom_passwords_controller_test.rb +28 -34
  28. data/test/controllers/custom/custom_registrations_controller_test.rb +23 -21
  29. data/test/controllers/custom/custom_sessions_controller_test.rb +16 -18
  30. data/test/controllers/custom/custom_token_validations_controller_test.rb +13 -11
  31. data/test/controllers/demo_group_controller_test.rb +19 -5
  32. data/test/controllers/demo_mang_controller_test.rb +37 -16
  33. data/test/controllers/demo_user_controller_test.rb +70 -38
  34. data/test/controllers/devise_token_auth/confirmations_controller_test.rb +40 -22
  35. data/test/controllers/devise_token_auth/omniauth_callbacks_controller_test.rb +108 -82
  36. data/test/controllers/devise_token_auth/passwords_controller_test.rb +127 -145
  37. data/test/controllers/devise_token_auth/registrations_controller_test.rb +258 -274
  38. data/test/controllers/devise_token_auth/sessions_controller_test.rb +112 -117
  39. data/test/controllers/devise_token_auth/token_validations_controller_test.rb +14 -15
  40. data/test/controllers/devise_token_auth/unlocks_controller_test.rb +194 -0
  41. data/test/controllers/overrides/confirmations_controller_test.rb +8 -9
  42. data/test/controllers/overrides/omniauth_callbacks_controller_test.rb +16 -12
  43. data/test/controllers/overrides/passwords_controller_test.rb +19 -15
  44. data/test/controllers/overrides/registrations_controller_test.rb +7 -7
  45. data/test/controllers/overrides/sessions_controller_test.rb +6 -6
  46. data/test/controllers/overrides/token_validations_controller_test.rb +7 -4
  47. data/test/dummy/app/controllers/auth_origin_controller.rb +2 -2
  48. data/test/dummy/app/controllers/custom/omniauth_callbacks_controller.rb +0 -2
  49. data/test/dummy/app/controllers/overrides/confirmations_controller.rb +11 -7
  50. data/test/dummy/app/controllers/overrides/passwords_controller.rb +10 -8
  51. data/test/dummy/app/controllers/overrides/sessions_controller.rb +1 -1
  52. data/test/dummy/db/migrate/20140715061447_devise_token_auth_create_users.rb +2 -2
  53. data/test/dummy/db/migrate/20140715061805_devise_token_auth_create_mangs.rb +2 -2
  54. data/test/dummy/db/migrate/20140829044006_add_operating_thetan_to_user.rb +1 -1
  55. data/test/dummy/db/migrate/20140916224624_add_favorite_color_to_mangs.rb +1 -1
  56. data/test/dummy/db/migrate/20140928231203_devise_token_auth_create_evil_users.rb +2 -1
  57. data/test/dummy/db/migrate/20141222035835_devise_token_auth_create_only_email_users.rb +1 -1
  58. data/test/dummy/db/migrate/20141222053502_devise_token_auth_create_unregisterable_users.rb +2 -1
  59. data/test/dummy/db/migrate/20150409095712_devise_token_auth_create_nice_users.rb +2 -1
  60. data/test/dummy/db/migrate/20150708104536_devise_token_auth_create_unconfirmable_users.rb +2 -1
  61. data/test/dummy/db/migrate/20160103235141_devise_token_auth_create_scoped_users.rb +2 -1
  62. data/test/dummy/db/migrate/20160629184441_devise_token_auth_create_lockable_users.rb +2 -1
  63. data/test/dummy/db/schema.rb +157 -162
  64. data/test/dummy/tmp/generators/app/models/user.rb +1 -1
  65. data/test/dummy/tmp/generators/db/migrate/{20170517171822_devise_token_auth_create_users.rb → 20171014052631_devise_token_auth_create_users.rb} +2 -1
  66. data/test/lib/generators/devise_token_auth/install_generator_test.rb +5 -1
  67. data/test/models/user_test.rb +35 -1
  68. data/test/test_helper.rb +17 -11
  69. metadata +85 -80
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8e417664fd64bb5d235bbb2e071516172ff8125b
4
- data.tar.gz: e75006fbdcf1b00f00117db24fce2410eb257162
3
+ metadata.gz: 49f4bcdd5d7a311223110f453bd4575fe89768e0
4
+ data.tar.gz: f6ccefa63103a9da877b9872a2f23aa26ce5bbf0
5
5
  SHA512:
6
- metadata.gz: 7aa61841da344dd20e7921d74c98e015b6c21a0efe662e8bf2b1b7e4b5f27c4c0da53ed009a7df803b7aa97a969c834d93918a05e825618c65c92870725d90a8
7
- data.tar.gz: cd4cc39794d409dd91016fba7e4cbcd8c5568f6180ba5346559f22dfd18b6800a04a5a9785e699ead20008554d36f0dc849e0bee3f46bd82d3248c3a9aaa223f
6
+ metadata.gz: bd75219ef40a2a64ed0cba0f57631b2d097c7d0187ae596d3db16b9b59f38fa8ae47edc7c87627ff65c4f3e5f1c0c626b610cb6d585c310f37a023709838dd0d
7
+ data.tar.gz: 66658c65532b7271c542302f75c2798db7f6bd425fc642ad70f67ca7f9d3dd6869ca0199df6d911fa2eced7a6fdba8997f30775193143b40634d6959e38c6945
data/README.md CHANGED
@@ -1,3 +1,9 @@
1
+ # Contributors wanted!
2
+
3
+ See our [Contribution Guidelines](https://github.com/lynndylanhurley/devise_token_auth/blob/master/.github/CONTRIBUTING.md). We're making an effort to bring back this gem and fix everything open! Feel free to submit pull requests, review pull requests, or review open issues. If you'd like to get in contact, [Zach Feldman](https://github.com/zachfeldman) has been wrangling this effort, you can reach him with his name @gmail. Further discussion of this in [this issue](https://github.com/lynndylanhurley/devise_token_auth/issues/969).
4
+
5
+ <hr>
6
+
1
7
  ![Serious Trust](https://github.com/lynndylanhurley/devise_token_auth/raw/master/test/dummy/app/assets/images/logo.jpg "Serious Trust")
2
8
 
3
9
  [![Gem Version](https://badge.fury.io/rb/devise_token_auth.svg)](http://badge.fury.io/rb/devise_token_auth)
@@ -60,6 +66,7 @@ Please read the [issue reporting guidelines](#issue-reporting) before posting is
60
66
  * [Custom Controller Overrides](#custom-controller-overrides)
61
67
  * [Passing blocks to Controllers](#passing-blocks-controllers)
62
68
  * [Email Template Overrides](#email-template-overrides)
69
+ * [Testing](#testing)
63
70
  * [Issue Reporting Guidelines](#issue-reporting)
64
71
  * [FAQ](#faq)
65
72
  * [Conceptual Diagrams](#conceptual)
@@ -140,8 +147,8 @@ The following routes are available for use by your client. These routes live rel
140
147
 
141
148
  | path | method | purpose |
142
149
  |:-----|:-------|:--------|
143
- | / | POST | Email registration. Requires **`email`**, **`password`**, and **`password_confirmation`** params. A verification email will be sent to the email address provided. Accepted params can be customized using the [`devise_parameter_sanitizer`](https://github.com/plataformatec/devise#strong-parameters) system. |
144
- | / | DELETE | Account deletion. This route will destroy users identified by their **`uid`**, **`access_token`** and **`client`** headers. |
150
+ | / | POST | Email registration. Requires **`email`**, **`password`**, **`password_confirmation`**, and **`confirm_success_url`** params (this last one can be omitted if you have set `config.default_confirm_success_url` in `config/initializers/devise_token_auth.rb`). A verification email will be sent to the email address provided. Upon clicking the link in the confirmation email, the API will redirect to the URL specified in **`confirm_success_url`**. Accepted params can be customized using the [`devise_parameter_sanitizer`](https://github.com/plataformatec/devise#strong-parameters) system. |
151
+ | / | DELETE | Account deletion. This route will destroy users identified by their **`uid`**, **`access-token`** and **`client`** headers. |
145
152
  | / | PUT | Account updates. This route will update an existing user's account settings. The default accepted params are **`password`** and **`password_confirmation`**, but this can be customized using the [`devise_parameter_sanitizer`](https://github.com/plataformatec/devise#strong-parameters) system. If **`config.check_current_password_before_update`** is set to `:attributes` the **`current_password`** param is checked before any update, if it is set to `:password` the **`current_password`** param is checked only if the request updates user password. |
146
153
  | /sign_in | POST | Email authentication. Requires **`email`** and **`password`** as params. This route will return a JSON representation of the `User` model on successful login along with the `access-token` and `client` in the header of the response. |
147
154
  | /sign_out | DELETE | Use this route to end the user's current session. This route will invalidate the user's authentication token. You must pass in **`uid`**, **`client`**, and **`access-token`** in the request headers. |
@@ -434,7 +441,7 @@ The authentication information should be included by the client in the headers o
434
441
  "uid": "zzzzz"
435
442
  ~~~
436
443
 
437
- The authentication headers consists of the following params:
444
+ The authentication headers (each one is a seperate header) consists of the following params:
438
445
 
439
446
  | param | description |
440
447
  |---|---|
@@ -489,7 +496,7 @@ Models that include the `DeviseTokenAuth::Concerns::User` concern will have acce
489
496
  # store client + token in user's token hash
490
497
  @resource.tokens[client_id] = {
491
498
  token: BCrypt::Password.create(token),
492
- expiry: (Time.now + DeviseTokenAuth.token_lifespan).to_i
499
+ expiry: (Time.now + @resource.token_lifespan).to_i
493
500
  }
494
501
 
495
502
  # generate auth headers for response
@@ -766,18 +773,21 @@ These files may be edited to suit your taste. You can customize the e-mail subje
766
773
 
767
774
  **Note:** if you choose to modify these templates, do not modify the `link_to` blocks unless you absolutely know what you are doing.
768
775
 
776
+ ## Testing
777
+
778
+ In order to authorise a request when testing your API you will need to pass the four headers through with your request, the easiest way to gain appropriate values for those headers is to use `resource.create_new_auth_token` e.g.
779
+
780
+ ```Ruby
781
+ request.headers.merge! resource.create_new_auth_token
782
+ get '/api/authenticated_resource'
783
+ # success
784
+ ```
785
+
769
786
  # Issue Reporting
770
787
 
771
- When posting issues, please include the following information to speed up the troubleshooting process:
788
+ When posting issues, please include the information mentioned in the [ISSUE_TEMPLATE.md].
772
789
 
773
- * **Version**: which version of this gem (and [ng-token-auth](https://github.com/lynndylanhurley/ng-token-auth), [jToker](https://github.com/lynndylanhurley/j-toker) or [Angular2-Token](https://github.com/neroniaky/angular2-token) if applicable) are you using?
774
- * **Request and response headers**: these can be found in the "Network" tab of your browser's web inspector.
775
- * **Rails Stacktrace**: this can be found in the `log/development.log` of your API.
776
- * **Environmental Info**: How is your application different from the [reference implementation](https://github.com/lynndylanhurley/devise_token_auth_demo)? This may include (but is not limited to) the following details:
777
- * **Routes**: are you using some crazy namespace, scope, or constraint?
778
- * **Gems**: are you using MongoDB, Grape, RailsApi, ActiveAdmin, etc.?
779
- * **Custom Overrides**: what have you done in terms of [custom controller overrides](#custom-controller-overrides)?
780
- * **Custom Frontend**: are you using [ng-token-auth](https://github.com/lynndylanhurley/ng-token-auth), [jToker](https://github.com/lynndylanhurley/j-toker), [Angular2-Token](https://github.com/neroniaky/angular2-token), or something else?
790
+ [ISSUE_TEMPLATE.md]: https://github.com/lynndylanhurley/devise_token_auth/blob/master/.github/ISSUE_TEMPLATE.md
781
791
 
782
792
  # FAQ
783
793
 
@@ -833,6 +843,15 @@ class ApplicationController < ActionController::Base
833
843
  end
834
844
  ~~~
835
845
 
846
+ ### How can I use this gem with Grape?
847
+
848
+ You may be interested in [GrapeTokenAuth](https://github.com/mcordell/grape_token_auth) or [GrapeDeviseTokenAuth](https://github.com/mcordell/grape_devise_token_auth).
849
+
850
+ ### I already have an user, how can I add the new fields?
851
+
852
+ Check [Setup migrations for an existing User table](https://github.com/lynndylanhurley/devise_token_auth/wiki/Setup-migrations-for-an-existing-User-table)
853
+
854
+
836
855
  # Conceptual
837
856
 
838
857
  None of the following information is required to use this gem, but read on if you're curious.
@@ -853,7 +872,8 @@ These measures are taken by default when using this gem.
853
872
 
854
873
  By default, the API should update the auth token for each request ([read more](#about-token-management)). But sometimes it's necessary to make several concurrent requests to the API, for example:
855
874
 
856
- #####Batch request example
875
+ ##### Batch request example
876
+
857
877
  ~~~javascript
858
878
  $scope.getResourceData = function() {
859
879
 
@@ -879,7 +899,7 @@ The following diagram details the relationship between the client, server, and a
879
899
 
880
900
  ![batch request detail](https://github.com/lynndylanhurley/ng-token-auth/raw/master/test/app/images/flow/batch-request-detail.jpg)
881
901
 
882
- Note that when the server identifies that a request is part of a batch request, the user's auth token is not updated. The auth token will be updated for the first request in the batch, and then that same token will be returned in the responses for each subsequent request in the batch (as shown in the diagram).
902
+ Note that when the server identifies that a request is part of a batch request, the user's auth token is not updated. The auth token will be updated and returned with the first request in the batch, and the subsequent requests in the batch will not return a token. This is necessary because the order of the responses cannot be guaranteed to the client, and we need to be sure that the client does not receive an outdated token *after* the the last valid token is returned.
883
903
 
884
904
  This gem automatically manages batch requests. You can change the time buffer for what is considered a batch request using the `batch_request_buffer_throttle` parameter in `config/initializers/devise_token_auth.rb`.
885
905
 
@@ -906,6 +926,8 @@ But the most important step is to use HTTPS. You are on the hook for that.
906
926
  Thanks to the following contributors:
907
927
 
908
928
  * [@booleanbetrayal](https://github.com/booleanbetrayal)
929
+ * [@zachfeldman](https://github.com/zachfeldman)
930
+ * [@MaicolBen](https://github.com/MaicolBen)
909
931
  * [@guilhermesimoes](https://github.com/guilhermesimoes)
910
932
  * [@jasonswett](https://github.com/jasonswett)
911
933
  * [@m2omou](https://github.com/m2omou)
@@ -918,30 +940,9 @@ Thanks to the following contributors:
918
940
 
919
941
  # Contributing
920
942
 
921
- 1. Create a feature branch with your changes.
922
- 2. Write some test cases.
923
- 3. Make all the tests pass.
924
- 4. Issue a pull request.
925
-
926
- I will grant you commit access if you send quality pull requests.
927
-
928
- To run the test suite do the following:
929
-
930
- 1. Clone this repo
931
- 2. Run `bundle install`
932
- 3. Run `rake db:migrate`
933
- 4. Run `RAILS_ENV=test rake db:migrate`
934
- 5. Run `guard`
935
-
936
- The last command will open the [guard](https://github.com/guard/guard) test-runner. Guard will re-run each test suite when changes are made to its corresponding files.
937
-
938
- To run just one test:
943
+ See the [CONTRIBUTING.md] document.
939
944
 
940
- 1. Clone this repo
941
- 2. Run `bundle install`
942
- 3. Run `rake db:migrate`
943
- 4. Run `RAILS_ENV=test rake db:migrate`
944
- 5. See this link for various ways to run a single file or a single test: http://flavio.castelli.name/2010/05/28/rails_execute_single_test/
945
+ [CONTRIBUTING.md]: https://github.com/lynndylanhurley/devise_token_auth/blob/master/.github/CONTRIBUTING.md
945
946
 
946
947
  # License
947
948
  This project uses the WTFPL
@@ -1,6 +1,7 @@
1
1
  module DeviseTokenAuth
2
2
  class ApplicationController < DeviseController
3
3
  include DeviseTokenAuth::Concerns::SetUserByToken
4
+ include DeviseTokenAuth::Concerns::ResourceFinder
4
5
 
5
6
  def resource_data(opts={})
6
7
  response_data = opts[:resource_json] || @resource.as_json
@@ -16,6 +17,20 @@ module DeviseTokenAuth
16
17
 
17
18
  protected
18
19
 
20
+ def build_redirect_headers(access_token, client, redirect_header_options = {})
21
+ {
22
+ DeviseTokenAuth.headers_names[:"access-token"] => access_token,
23
+ DeviseTokenAuth.headers_names[:"client"] => client,
24
+ :config => params[:config],
25
+
26
+ # Legacy parameters which may be removed in a future release.
27
+ # Consider using "client" and "access-token" in client code.
28
+ # See: github.com/lynndylanhurley/devise_token_auth/issues/993
29
+ :client_id => client,
30
+ :token => access_token
31
+ }.merge(redirect_header_options)
32
+ end
33
+
19
34
  def params_for_resource(resource)
20
35
  devise_parameter_sanitizer.instance_values['permitted'][resource].each do |type|
21
36
  params[type.to_s] ||= request.headers[type.to_s] unless request.headers[type.to_s].nil?
@@ -0,0 +1,38 @@
1
+ module DeviseTokenAuth::Concerns::ResourceFinder
2
+ extend ActiveSupport::Concern
3
+ include DeviseTokenAuth::Controllers::Helpers
4
+
5
+ def get_case_insensitive_field_from_resource_params(field)
6
+ # honor Devise configuration for case_insensitive keys
7
+ q_value = resource_params[field.to_sym]
8
+
9
+ if resource_class.case_insensitive_keys.include?(field.to_sym)
10
+ q_value.downcase!
11
+ end
12
+ q_value
13
+ end
14
+
15
+ def find_resource(field, value)
16
+ # fix for mysql default case insensitivity
17
+ q = "#{field.to_s} = ? AND provider='#{provider.to_s}'"
18
+ if ActiveRecord::Base.connection.adapter_name.downcase.starts_with? 'mysql'
19
+ q = "BINARY " + q
20
+ end
21
+
22
+ @resource = resource_class.where(q, value).first
23
+ end
24
+
25
+ def resource_class(m=nil)
26
+ if m
27
+ mapping = Devise.mappings[m]
28
+ else
29
+ mapping = Devise.mappings[resource_name] || Devise.mappings.values.first
30
+ end
31
+
32
+ mapping.to
33
+ end
34
+
35
+ def provider
36
+ 'email'
37
+ end
38
+ end
@@ -1,6 +1,6 @@
1
1
  module DeviseTokenAuth::Concerns::SetUserByToken
2
2
  extend ActiveSupport::Concern
3
- include DeviseTokenAuth::Controllers::Helpers
3
+ include DeviseTokenAuth::Concerns::ResourceFinder
4
4
 
5
5
  included do
6
6
  before_action :set_request_start
@@ -13,6 +13,12 @@ module DeviseTokenAuth::Concerns::SetUserByToken
13
13
  def set_request_start
14
14
  @request_started_at = Time.now
15
15
  @used_auth_by_token = true
16
+
17
+ # initialize instance variables
18
+ @client_id = nil
19
+ @resource = nil
20
+ @token = nil
21
+ @is_batch_request = nil
16
22
  end
17
23
 
18
24
  # user auth
@@ -23,7 +29,7 @@ module DeviseTokenAuth::Concerns::SetUserByToken
23
29
  # no default user defined
24
30
  return unless rc
25
31
 
26
- #gets the headers names, which was set in the initialize file
32
+ # gets the headers names, which was set in the initialize file
27
33
  uid_name = DeviseTokenAuth.headers_names[:'uid']
28
34
  access_token_name = DeviseTokenAuth.headers_names[:'access-token']
29
35
  client_name = DeviseTokenAuth.headers_names[:'client']
@@ -47,7 +53,7 @@ module DeviseTokenAuth::Concerns::SetUserByToken
47
53
  end
48
54
 
49
55
  # user has already been found and authenticated
50
- return @resource if @resource && @resource.class == rc
56
+ return @resource if @resource && @resource.is_a?(rc)
51
57
 
52
58
  # ensure we clear the client_id
53
59
  if !@token
@@ -75,10 +81,9 @@ module DeviseTokenAuth::Concerns::SetUserByToken
75
81
  end
76
82
  end
77
83
 
78
-
79
84
  def update_auth_header
80
85
  # cannot save object if model has invalid params
81
- return unless @resource && @resource.valid? && @client_id
86
+ return unless defined?(@resource) && @resource && @resource.valid? && @client_id
82
87
 
83
88
  # Generate new client_id with existing authentication
84
89
  @client_id = nil unless @used_auth_by_token
@@ -113,31 +118,28 @@ module DeviseTokenAuth::Concerns::SetUserByToken
113
118
  if @is_batch_request
114
119
  auth_header = @resource.extend_batch_buffer(@token, @client_id)
115
120
 
121
+ # Do not return token for batch requests to avoid invalidated
122
+ # tokens returned to the client in case of race conditions.
123
+ # Use a blank string for the header to still be present and
124
+ # being passed in a XHR response in case of
125
+ # 304 Not Modified responses.
126
+ auth_header[DeviseTokenAuth.headers_names[:"access-token"]] = ' '
127
+ auth_header[DeviseTokenAuth.headers_names[:"expiry"]] = ' '
128
+
116
129
  # update Authorization response header with new token
117
130
  else
118
131
  auth_header = @resource.create_new_auth_token(@client_id)
119
-
120
- # update the response header
121
- response.headers.merge!(auth_header)
122
132
  end
123
133
 
124
- end # end lock
134
+ # update the response header
135
+ response.headers.merge!(auth_header)
125
136
 
126
- end
127
-
128
- end
137
+ end # end lock
129
138
 
130
- def resource_class(m=nil)
131
- if m
132
- mapping = Devise.mappings[m]
133
- else
134
- mapping = Devise.mappings[resource_name] || Devise.mappings.values.first
135
139
  end
136
140
 
137
- mapping.to
138
141
  end
139
142
 
140
-
141
143
  private
142
144
 
143
145
 
@@ -8,23 +8,28 @@ module DeviseTokenAuth
8
8
  client_id = SecureRandom.urlsafe_base64(nil, false)
9
9
  token = SecureRandom.urlsafe_base64(nil, false)
10
10
  token_hash = BCrypt::Password.create(token)
11
- expiry = (Time.now + DeviseTokenAuth.token_lifespan).to_i
11
+ expiry = (Time.now + @resource.token_lifespan).to_i
12
+
13
+ if @resource.sign_in_count > 0
14
+ expiry = (Time.now + 1.second).to_i
15
+ end
12
16
 
13
17
  @resource.tokens[client_id] = {
14
18
  token: token_hash,
15
19
  expiry: expiry
16
20
  }
17
21
 
22
+ sign_in(@resource)
18
23
  @resource.save!
19
24
 
20
25
  yield @resource if block_given?
21
26
 
22
- redirect_to(@resource.build_auth_url(params[:redirect_url], {
23
- token: token,
24
- client_id: client_id,
25
- account_confirmation_success: true,
26
- config: params[:config]
27
- }))
27
+ redirect_header_options = {account_confirmation_success: true}
28
+ redirect_headers = build_redirect_headers(token,
29
+ client_id,
30
+ redirect_header_options)
31
+ redirect_to(@resource.build_auth_url(params[:redirect_url],
32
+ redirect_headers))
28
33
  else
29
34
  raise ActionController::RoutingError.new('Not Found')
30
35
  end
@@ -79,12 +79,8 @@ module DeviseTokenAuth
79
79
 
80
80
  # break out provider attribute assignment for easy method extension
81
81
  def assign_provider_attrs(user, auth_hash)
82
- user.assign_attributes({
83
- nickname: auth_hash['info']['nickname'],
84
- name: auth_hash['info']['name'],
85
- image: auth_hash['info']['image'],
86
- email: auth_hash['info']['email']
87
- })
82
+ attrs = auth_hash['info'].slice(*user.attributes.keys)
83
+ user.assign_attributes(attrs)
88
84
  end
89
85
 
90
86
  # derive allowed params from the standard devise parameter sanitizer
@@ -164,7 +160,7 @@ module DeviseTokenAuth
164
160
  # create token info
165
161
  @client_id = SecureRandom.urlsafe_base64(nil, false)
166
162
  @token = SecureRandom.urlsafe_base64(nil, false)
167
- @expiry = (Time.now + DeviseTokenAuth.token_lifespan).to_i
163
+ @expiry = (Time.now + @resource.token_lifespan).to_i
168
164
  @config = omniauth_params['config_name']
169
165
  end
170
166
 
@@ -27,21 +27,8 @@ module DeviseTokenAuth
27
27
  end
28
28
  end
29
29
 
30
- # honor devise configuration for case_insensitive_keys
31
- if resource_class.case_insensitive_keys.include?(:email)
32
- @email = resource_params[:email].downcase
33
- else
34
- @email = resource_params[:email]
35
- end
36
-
37
- q = "uid = ? AND provider='email'"
38
-
39
- # fix for mysql default case insensitivity
40
- if ActiveRecord::Base.connection.adapter_name.downcase.starts_with? 'mysql'
41
- q = "BINARY uid = ? AND provider='email'"
42
- end
43
-
44
- @resource = resource_class.where(q, @email).first
30
+ @email = get_case_insensitive_field_from_resource_params(:email)
31
+ @resource = find_resource(:uid, @email)
45
32
 
46
33
  @errors = nil
47
34
  @error_status = 400
@@ -72,15 +59,16 @@ module DeviseTokenAuth
72
59
 
73
60
  # this is where users arrive after visiting the password reset confirmation link
74
61
  def edit
75
- @resource = resource_class.reset_password_by_token({
76
- reset_password_token: resource_params[:reset_password_token]
77
- })
62
+ # if a user is not found, return nil
63
+ @resource = resource_class.with_reset_password_token(
64
+ resource_params[:reset_password_token]
65
+ )
78
66
 
79
- if @resource && @resource.id
67
+ if @resource
80
68
  client_id = SecureRandom.urlsafe_base64(nil, false)
81
69
  token = SecureRandom.urlsafe_base64(nil, false)
82
70
  token_hash = BCrypt::Password.create(token)
83
- expiry = (Time.now + DeviseTokenAuth.token_lifespan).to_i
71
+ expiry = (Time.now + @resource.token_lifespan).to_i
84
72
 
85
73
  @resource.tokens[client_id] = {
86
74
  token: token_hash,
@@ -94,14 +82,15 @@ module DeviseTokenAuth
94
82
  @resource.allow_password_change = true;
95
83
 
96
84
  @resource.save!
85
+
97
86
  yield @resource if block_given?
98
87
 
99
- redirect_to(@resource.build_auth_url(params[:redirect_url], {
100
- token: token,
101
- client_id: client_id,
102
- reset_password: true,
103
- config: params[:config]
104
- }))
88
+ redirect_header_options = {reset_password: true}
89
+ redirect_headers = build_redirect_headers(token,
90
+ client_id,
91
+ redirect_header_options)
92
+ redirect_to(@resource.build_auth_url(params[:redirect_url],
93
+ redirect_headers))
105
94
  else
106
95
  render_edit_error
107
96
  end
@@ -125,6 +114,7 @@ module DeviseTokenAuth
125
114
 
126
115
  if @resource.send(resource_update_method, password_resource_params)
127
116
  @resource.allow_password_change = false
117
+ @resource.save!
128
118
 
129
119
  yield @resource if block_given?
130
120
  return render_update_success