devise_token_auth 0.1.21.alpha1 → 0.1.21.alpha2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cceda7b13b2df7fdd541d948992dbc48b7dfd3a1
4
- data.tar.gz: 8fd2c90a8d52644a61a38e4e6509c5c089858661
3
+ metadata.gz: 2a480547b2d43a6475cd6e9a0182ceb204f2fd8d
4
+ data.tar.gz: 8b690b9d3390f156bfa02427bc6d2af282187b53
5
5
  SHA512:
6
- metadata.gz: 3ef99d9eb62eecc7cfca98cd68ae981e1411e48ddf9da3a1a1c0315dfea9e53e141cb4a2a0e9dcd23f0ab8f4184243cfde9a24e202d5fde5c1532ea094c3bb4b
7
- data.tar.gz: 9219e6c4029c2fa29ff17b78078814295327794e322f8228ddcf03ed1795235040a29297100882da3c6b66152ef063c304bae34f597e48d6d7d2f94555cf86af
6
+ metadata.gz: 932fc776673a4114055e7403f8d17e44575630743d6ac69dd7ae86d2089effcafcfd86b8b19007953bb084c4d09c34d23ddf522673c93edd9b669bf80aa3181b
7
+ data.tar.gz: 80448a5ea78f8e2c9a211b51de8fad7cbeb5b135092cecfe5b89230f75487f1a63cf8f9edf233bea918be0614091f21ba81ec34f509a133c912c39f1c74dd093
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # Devise Token Auth
2
2
 
3
- ![build](https://travis-ci.org/lynndylanhurley/devise_token_auth.svg)
3
+ [![Build Status](https://travis-ci.org/lynndylanhurley/devise_token_auth.svg?branch=master)](https://travis-ci.org/lynndylanhurley/devise_token_auth)
4
4
  [![Code Climate](https://codeclimate.com/github/lynndylanhurley/devise_token_auth.png)](https://codeclimate.com/github/lynndylanhurley/devise_token_auth)
5
5
  [![Test Coverage](https://codeclimate.com/github/lynndylanhurley/devise_token_auth/coverage.png)](https://codeclimate.com/github/lynndylanhurley/devise_token_auth)
6
+ [![Dependency Status](https://gemnasium.com/lynndylanhurley/devise_token_auth.svg)](https://gemnasium.com/lynndylanhurley/devise_token_auth)
6
7
 
7
8
  This gem provides simple, secure token based authentication.
8
9
 
@@ -35,7 +36,7 @@ bundle install
35
36
 
36
37
  # Configuration TLDR;
37
38
 
38
- You will need to create a user model, define routes, and you may want to alter some of the default settings for this gem. Run the following to append the routes and generate the model, migration, and initializer files:
39
+ You will need to create a [user model](#model-concerns), [define routes](#mounting-routes), [include concerns](#controller-concerns), and you may want to alter some of the [default settings](#initializer-settings) for this gem. Run the following to append the routes and generate the model, migration, and initializer files:
39
40
 
40
41
  ~~~bash
41
42
  rails g devise_token_auth:install [USER_CLASS] [MOUNT_PATH]
@@ -50,18 +51,26 @@ This generator accepts the following optional arguments:
50
51
 
51
52
  The following events will take place when using the install generator:
52
53
 
53
- * An initializer will be created at `config/initializers/devise_token_auth.rb`. [Read more](#initializer-settings).
54
+ * An initializer will be created at `config/initializers/devise_token_auth.rb`. [Read more](#initializer-settings).
54
55
 
55
56
  * A model will be created in the `app/models` directory. If the model already exists, a concern will be included at the top of the file. [Read more](#model-concerns).
56
57
 
57
58
  * Routes will be appended to file at `config/routes.rb`. [Read more](#mounting-routes).
58
59
 
60
+ * A concern will be included by your application controller at `app/controllers/application_controller.rb`. [Read more](#controller-concerns).
61
+
59
62
  * A migration file will be created in the `db/migrate` directory. Inspect the migrations file, add additional columns if necessary, and then run the migration:
60
63
 
61
64
  ~~~bash
62
65
  rake db:migrate
63
66
  ~~~
64
67
 
68
+ You will also need to configure the following features that are external to this gem:
69
+
70
+ * [Omniauth providers](#omniauth-authentication) for 3rd party oauth2 authentication.
71
+ * [Cross Origin Request Settings](#cors) when using cross-domain clients.
72
+ * [Email](#email-authentication) for email registration.
73
+
65
74
  [Jump here](#configuration-cont) for more configuration information.
66
75
 
67
76
  # Usage TLDR;
@@ -1,3 +1,3 @@
1
1
  module DeviseTokenAuth
2
- VERSION = "0.1.21.alpha1"
2
+ VERSION = "0.1.21.alpha2"
3
3
  end
@@ -22,6 +22,10 @@ Example:
22
22
  after the class definition:
23
23
  include DeviseTokenAuth::Concerns::User
24
24
 
25
+ The following line will be inserted into your application controller at
26
+ app/controllers/application_controller.rb:
27
+ include DeviseTokenAuth::Concerns::SetUserByToken
28
+
25
29
  The following line will be inserted at the top of 'config/routes.rb' if it
26
30
  does not already exist:
27
31
  mount_devise_token_auth_for "User", at: '/auth'
@@ -37,6 +37,20 @@ module DeviseTokenAuth
37
37
  end
38
38
  end
39
39
 
40
+ def include_controller_concerns
41
+ fname = "app/controllers/application_controller.rb"
42
+ line = "include DeviseTokenAuth::Concerns::SetUserByToken"
43
+
44
+ if parse_file_for_line(fname, line)
45
+ say_status("skipped", "Concern is already included in the application controller.")
46
+ else
47
+ inject_into_file fname, after: "class ApplicationController < ActionController::Base\n" do <<-'RUBY'
48
+ include DeviseTokenAuth::Concerns::SetUserByToken
49
+ RUBY
50
+ end
51
+ end
52
+ end
53
+
40
54
  def add_route_mount
41
55
  f = "config/routes.rb"
42
56
  str = "mount_devise_token_auth_for '#{user_class}', at: '#{mount_path}'"
@@ -7,175 +7,94 @@ require 'test_helper'
7
7
  # was the appropriate message delivered in the json payload?
8
8
 
9
9
  class DemoControllerTest < ActionController::TestCase
10
- describe DemoController, "Token access" do
11
- before do
12
- @user = users(:confirmed_email_user)
13
- @user.skip_confirmation!
14
- @user.save!
15
-
16
- @auth_header = @user.create_new_auth_token
17
-
18
- @token = @auth_header[/token=(.*?) /,1]
19
- @client_id = @auth_header[/client=(.*?) /,1]
20
- @expiry = @auth_header[/expiry=(.*?) /,1]
21
- end
22
-
23
- describe 'successful request' do
10
+ describe DemoController do
11
+ describe "Token access" do
24
12
  before do
25
- # ensure that request is not treated as batch request
26
- age_token(@user, @client_id)
13
+ @user = users(:confirmed_email_user)
14
+ @user.skip_confirmation!
15
+ @user.save!
27
16
 
28
- request.headers['Authorization'] = @auth_header
29
- xhr :get, :members_only
17
+ @auth_header = @user.create_new_auth_token
30
18
 
31
- @resp_auth_header = response.headers['Authorization']
32
- @resp_token = @resp_auth_header[/token=(.*?) /,1]
33
- @resp_client_id = @resp_auth_header[/client=(.*?) /,1]
34
- @resp_expiry = @resp_auth_header[/expiry=(.*?) /,1]
35
- @resp_uid = @resp_auth_header[/uid=(.*?)$/,1]
19
+ @token = @auth_header[/token=(.*?) /,1]
20
+ @client_id = @auth_header[/client=(.*?) /,1]
21
+ @expiry = @auth_header[/expiry=(.*?) /,1]
36
22
  end
37
23
 
38
- it 'should return success status' do
39
- assert_equal 200, response.status
40
- end
41
-
42
- it 'should receive new token after successful request' do
43
- refute_equal @token, @resp_token
44
- end
45
-
46
- it 'should preserve the client id from the first request' do
47
- assert_equal @client_id, @resp_client_id
48
- end
49
-
50
- it "should return the user's uid in the auth header" do
51
- assert_equal @user.uid, @resp_uid
52
- end
53
-
54
- it 'should not treat this request as a batch request' do
55
- refute assigns(:is_batch_request)
56
- end
57
-
58
- describe 'succesive requests' do
24
+ describe 'successful request' do
59
25
  before do
60
- @user.reload
61
26
  # ensure that request is not treated as batch request
62
27
  age_token(@user, @client_id)
63
28
 
64
- request.headers['Authorization'] = @resp_auth_header
65
-
29
+ request.headers['Authorization'] = @auth_header
66
30
  xhr :get, :members_only
67
- end
68
31
 
69
- it 'should not treat this request as a batch request' do
70
- refute assigns(:is_batch_request)
32
+ @resp_auth_header = response.headers['Authorization']
33
+ @resp_token = @resp_auth_header[/token=(.*?) /,1]
34
+ @resp_client_id = @resp_auth_header[/client=(.*?) /,1]
35
+ @resp_expiry = @resp_auth_header[/expiry=(.*?) /,1]
36
+ @resp_uid = @resp_auth_header[/uid=(.*?)$/,1]
71
37
  end
72
38
 
73
- it "should allow a new request to be made using new token" do
39
+ it 'should return success status' do
74
40
  assert_equal 200, response.status
75
41
  end
76
- end
77
- end
78
-
79
- describe 'failed request' do
80
- before do
81
- request.headers['Authorization'] = "token=bogus client=#{@client_id} uid=#{@user.uid}"
82
- xhr :get, :members_only
83
- end
84
-
85
- it 'should not return any auth headers' do
86
- refute response.headers['Authorization']
87
- end
88
-
89
- it 'should return error: unauthorized status' do
90
- assert_equal 401, response.status
91
- end
92
- end
93
-
94
- describe 'disable change_headers_on_each_request' do
95
- before do
96
- DeviseTokenAuth.change_headers_on_each_request = false
97
- @user.reload
98
- age_token(@user, @client_id)
99
-
100
- request.headers['Authorization'] = @auth_header
101
- xhr :get, :members_only
102
-
103
- @first_is_batch_request = assigns(:is_batch_request)
104
- @first_user = assigns(:user).dup
105
- @first_auth_headers = response.headers['Authorization'].clone
106
- @first_response_status = response.status
107
-
108
- @user.reload
109
- age_token(@user, @client_id)
110
42
 
111
- # use expired auth header
112
- request.headers['Authorization'] = @auth_header
113
- xhr :get, :members_only
43
+ it 'should receive new token after successful request' do
44
+ refute_equal @token, @resp_token
45
+ end
114
46
 
115
- @second_is_batch_request = assigns(:is_batch_request)
116
- @second_user = assigns(:user)
117
- @second_auth_headers = response.headers['Authorization']
118
- @second_response_status = response.status
119
- end
47
+ it 'should preserve the client id from the first request' do
48
+ assert_equal @client_id, @resp_client_id
49
+ end
120
50
 
121
- after do
122
- DeviseTokenAuth.change_headers_on_each_request = true
123
- end
51
+ it "should return the user's uid in the auth header" do
52
+ assert_equal @user.uid, @resp_uid
53
+ end
124
54
 
125
- it 'should allow the first request through' do
126
- assert_equal 200, @first_response_status
127
- end
55
+ it 'should not treat this request as a batch request' do
56
+ refute assigns(:is_batch_request)
57
+ end
128
58
 
129
- it 'should allow the second request through' do
130
- assert_equal 200, @second_response_status
131
- end
59
+ describe 'succesive requests' do
60
+ before do
61
+ @user.reload
62
+ # ensure that request is not treated as batch request
63
+ age_token(@user, @client_id)
132
64
 
133
- it 'should return auth headers from the first request' do
134
- assert @first_auth_headers
135
- end
65
+ request.headers['Authorization'] = @resp_auth_header
136
66
 
137
- it 'should return auth headers from the second request' do
138
- assert @second_auth_headers
139
- end
67
+ xhr :get, :members_only
68
+ end
140
69
 
141
- it 'should define user during first request' do
142
- assert @first_user
143
- end
70
+ it 'should not treat this request as a batch request' do
71
+ refute assigns(:is_batch_request)
72
+ end
144
73
 
145
- it 'should define user during second request' do
146
- assert @second_user
74
+ it "should allow a new request to be made using new token" do
75
+ assert_equal 200, response.status
76
+ end
77
+ end
147
78
  end
148
- end
149
79
 
150
- describe 'batch requests' do
151
- describe 'success' do
80
+ describe 'failed request' do
152
81
  before do
153
- request.headers['Authorization'] = @auth_header
82
+ request.headers['Authorization'] = "token=bogus client=#{@client_id} uid=#{@user.uid}"
154
83
  xhr :get, :members_only
155
-
156
- @first_is_batch_request = assigns(:is_batch_request)
157
- @first_user = assigns(:user)
158
- @first_auth_headers = response.headers['Authorization']
159
-
160
- request.headers['Authorization'] = @auth_header
161
- xhr :get, :members_only
162
-
163
- @second_is_batch_request = assigns(:is_batch_request)
164
- @second_user = assigns(:user)
165
- @second_auth_headers = response.headers['Authorization']
166
84
  end
167
85
 
168
- it 'should allow both requests through' do
169
- assert_equal 200, response.status
86
+ it 'should not return any auth headers' do
87
+ refute response.headers['Authorization']
170
88
  end
171
89
 
172
- it 'should return the same auth headers for both requests' do
173
- assert_equal @first_auth_headers, @second_auth_headers
90
+ it 'should return error: unauthorized status' do
91
+ assert_equal 401, response.status
174
92
  end
175
93
  end
176
94
 
177
- describe 'time out' do
95
+ describe 'disable change_headers_on_each_request' do
178
96
  before do
97
+ DeviseTokenAuth.change_headers_on_each_request = false
179
98
  @user.reload
180
99
  age_token(@user, @client_id)
181
100
 
@@ -200,69 +119,152 @@ class DemoControllerTest < ActionController::TestCase
200
119
  @second_response_status = response.status
201
120
  end
202
121
 
122
+ after do
123
+ DeviseTokenAuth.change_headers_on_each_request = true
124
+ end
125
+
203
126
  it 'should allow the first request through' do
204
127
  assert_equal 200, @first_response_status
205
128
  end
206
129
 
207
- it 'should not allow the second request through' do
208
- assert_equal 401, @second_response_status
130
+ it 'should allow the second request through' do
131
+ assert_equal 200, @second_response_status
209
132
  end
210
133
 
211
134
  it 'should return auth headers from the first request' do
212
135
  assert @first_auth_headers
213
136
  end
214
137
 
215
- it 'should not return auth headers from the second request' do
216
- refute @second_auth_headers
138
+ it 'should return auth headers from the second request' do
139
+ assert @second_auth_headers
217
140
  end
218
141
 
219
142
  it 'should define user during first request' do
220
143
  assert @first_user
221
144
  end
222
145
 
223
- it 'should not define user during second request' do
224
- refute @second_user
146
+ it 'should define user during second request' do
147
+ assert @second_user
225
148
  end
226
149
  end
227
- end
228
- end
229
150
 
230
- # test with non-standard user class
231
- describe DemoController, "Alternate user class" do
232
- setup do
233
- @request.env['devise.mapping'] = Devise.mappings[:mang]
234
- end
151
+ describe 'batch requests' do
152
+ describe 'success' do
153
+ before do
154
+ request.headers['Authorization'] = @auth_header
155
+ xhr :get, :members_only
235
156
 
236
- teardown do
237
- @request.env['devise.mapping'] = Devise.mappings[:user]
238
- end
157
+ @first_is_batch_request = assigns(:is_batch_request)
158
+ @first_user = assigns(:user)
159
+ @first_auth_headers = response.headers['Authorization']
160
+
161
+ request.headers['Authorization'] = @auth_header
162
+ xhr :get, :members_only
163
+
164
+ @second_is_batch_request = assigns(:is_batch_request)
165
+ @second_user = assigns(:user)
166
+ @second_auth_headers = response.headers['Authorization']
167
+ end
168
+
169
+ it 'should allow both requests through' do
170
+ assert_equal 200, response.status
171
+ end
172
+
173
+ it 'should return the same auth headers for both requests' do
174
+ assert_equal @first_auth_headers, @second_auth_headers
175
+ end
176
+ end
177
+
178
+ describe 'time out' do
179
+ before do
180
+ @user.reload
181
+ age_token(@user, @client_id)
182
+
183
+ request.headers['Authorization'] = @auth_header
184
+ xhr :get, :members_only
239
185
 
240
- before do
241
- @user = mangs(:confirmed_email_user)
242
- @user.skip_confirmation!
243
- @user.save!
186
+ @first_is_batch_request = assigns(:is_batch_request)
187
+ @first_user = assigns(:user).dup
188
+ @first_auth_headers = response.headers['Authorization'].clone
189
+ @first_response_status = response.status
244
190
 
245
- @auth_header = @user.create_new_auth_token
191
+ @user.reload
192
+ age_token(@user, @client_id)
246
193
 
247
- @token = @auth_header[/token=(.*?) /,1]
248
- @client_id = @auth_header[/client=(.*?) /,1]
249
- @expiry = @auth_header[/expiry=(.*?) /,1]
194
+ # use expired auth header
195
+ request.headers['Authorization'] = @auth_header
196
+ xhr :get, :members_only
250
197
 
251
- # ensure that request is not treated as batch request
252
- age_token(@user, @client_id)
198
+ @second_is_batch_request = assigns(:is_batch_request)
199
+ @second_user = assigns(:user)
200
+ @second_auth_headers = response.headers['Authorization']
201
+ @second_response_status = response.status
202
+ end
253
203
 
254
- request.headers['Authorization'] = @auth_header
255
- xhr :get, :members_only
204
+ it 'should allow the first request through' do
205
+ assert_equal 200, @first_response_status
206
+ end
256
207
 
257
- @resp_auth_header = response.headers['Authorization']
258
- @resp_token = @resp_auth_header[/token=(.*?) /,1]
259
- @resp_client_id = @resp_auth_header[/client=(.*?) /,1]
260
- @resp_expiry = @resp_auth_header[/expiry=(.*?) /,1]
261
- @resp_uid = @resp_auth_header[/uid=(.*?)$/,1]
208
+ it 'should not allow the second request through' do
209
+ assert_equal 401, @second_response_status
210
+ end
211
+
212
+ it 'should return auth headers from the first request' do
213
+ assert @first_auth_headers
214
+ end
215
+
216
+ it 'should not return auth headers from the second request' do
217
+ refute @second_auth_headers
218
+ end
219
+
220
+ it 'should define user during first request' do
221
+ assert @first_user
222
+ end
223
+
224
+ it 'should not define user during second request' do
225
+ refute @second_user
226
+ end
227
+ end
228
+ end
262
229
  end
263
230
 
264
- it 'should return success status' do
265
- assert_equal 200, response.status
231
+ # test with non-standard user class
232
+ describe "Alternate user class" do
233
+ setup do
234
+ @request.env['devise.mapping'] = Devise.mappings[:mang]
235
+ end
236
+
237
+ teardown do
238
+ @request.env['devise.mapping'] = Devise.mappings[:user]
239
+ end
240
+
241
+ before do
242
+ @user = mangs(:confirmed_email_user)
243
+ @user.skip_confirmation!
244
+ @user.save!
245
+
246
+ @auth_header = @user.create_new_auth_token
247
+
248
+ @token = @auth_header[/token=(.*?) /,1]
249
+ @client_id = @auth_header[/client=(.*?) /,1]
250
+ @expiry = @auth_header[/expiry=(.*?) /,1]
251
+
252
+ # ensure that request is not treated as batch request
253
+ age_token(@user, @client_id)
254
+
255
+ request.headers['Authorization'] = @auth_header
256
+ xhr :get, :members_only
257
+
258
+ @resp_auth_header = response.headers['Authorization']
259
+ @resp_token = @resp_auth_header[/token=(.*?) /,1]
260
+ @resp_client_id = @resp_auth_header[/client=(.*?) /,1]
261
+ @resp_expiry = @resp_auth_header[/expiry=(.*?) /,1]
262
+ @resp_uid = @resp_auth_header[/uid=(.*?)$/,1]
263
+ end
264
+
265
+ it 'should return success status' do
266
+ assert_equal 200, response.status
267
+ end
266
268
  end
267
269
  end
268
270
  end