devise-token_authenticatable 0.3.0 → 0.3.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3f3ab41f44f9d2b947755138573fac61db295d5a
4
+ data.tar.gz: 2ea7960b707e719f26bf13fa53d18c92d964ae90
5
+ SHA512:
6
+ metadata.gz: 1da828318ff32ff89522262dc366c5eb60d1308ae3da8c0dea18cb3afe0f4f0e8bd5acf1c4f0ab4cf96cc74cb8a0d42297de6d7ee39b8d0d191ee9ebf0bd5af0
7
+ data.tar.gz: ba3cd720cf3883632686a705072486ff21248d5453f520795ed32a46c513b74a7fea5b9ee6cb788d30e83c5b299f07b8758d724e90fdbb2d7421190f91728063
@@ -3,6 +3,7 @@ rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
5
  - 2.1.2
6
+ - 2.2.0
6
7
  - jruby-19mode
7
8
 
8
9
  env:
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Devise::TokenAuthenticatable
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/devise-token_authenticatable.png)](http://badge.fury.io/rb/devise-token_authenticatable) [![Build Status](https://travis-ci.org/baschtl/devise-token_authenticatable.png?branch=master)](https://travis-ci.org/baschtl/devise-token_authenticatable) [![Code Climate](https://codeclimate.com/github/baschtl/devise-token_authenticatable.png)](https://codeclimate.com/github/baschtl/devise-token_authenticatable)
3
+ [![Tag](https://img.shields.io/github/tag/baschtl/devise-token_authenticatable.svg?style=flat-square)](https://github.com/baschtl/devise-token_authenticatable/releases) [![Build Status](https://img.shields.io/travis/baschtl/devise-token_authenticatable.svg?style=flat-square)](https://travis-ci.org/baschtl/devise-token_authenticatable) [![Code Climate](https://img.shields.io/codeclimate/github/baschtl/devise-token_authenticatable.svg?style=flat-square)](https://codeclimate.com/github/baschtl/devise-token_authenticatable)
4
4
 
5
5
  This gem provides the extracted Token Authenticatable module of devise. It includes the functionality that was also in [version 3.1.2](https://github.com/plataformatec/devise/tree/v3.1.2) of devise. With the inclusion of this module a user is able to sign in via an authentication token. This token can be given via a query string or HTTP Basic Authentication. See the hint below to understand which version of this gem supports which version of devise.
6
6
 
@@ -24,9 +24,9 @@ Or install it yourself as:
24
24
 
25
25
  *devise-token_authenticatable* | *devise*
26
26
  -------------------------------:|---------
27
- 0.1.0 | >= 3.2.0
28
- 0.2.0 | >= 3.3.0
29
- 0.3.0 | >= 3.4.0
27
+ 0.1.0 | ~> 3.2.0
28
+ 0.2.0 | ~> 3.3.0
29
+ 0.3.x | ~> 3.4.0
30
30
 
31
31
  ## Usage
32
32
 
@@ -36,10 +36,22 @@ Add `:token_authenticatable` to your devise model:
36
36
  devise :database_authenticatable, :token_authenticatable
37
37
  end
38
38
 
39
- The authentication key name used by this module defaults to `auth_token`. Use the following configuration (e.g., in a Rails initializer) to alter the name:
39
+ ## Configuration
40
+
41
+ This gem can be configured as shown in the following:
40
42
 
41
43
  Devise::TokenAuthenticatable.setup do |config|
44
+ # set the authentication key name used by this module,
45
+ # defaults to :auth_token
42
46
  config.token_authentication_key = :other_key_name
47
+
48
+ # enable reset of the authentication token before the model is saved,
49
+ # defaults to false
50
+ config.should_reset_authentication_token = true
51
+
52
+ # enables the setting of the authentication token - if not already - before the model is saved,
53
+ # defaults to false
54
+ config.should_ensure_authentication_token = true
43
55
  end
44
56
 
45
57
  ## Documentation
@@ -7,6 +7,14 @@ module Devise
7
7
  mattr_accessor :token_authentication_key
8
8
  @@token_authentication_key = :auth_token
9
9
 
10
+ # Defines if the authentication token is reset before the model is saved.
11
+ mattr_accessor :should_reset_authentication_token
12
+ @@should_reset_authentication_token = false
13
+
14
+ # Defines if the authentication token is set - if not already - before the model is saved.
15
+ mattr_accessor :should_ensure_authentication_token
16
+ @@should_ensure_authentication_token = false
17
+
10
18
  # Enable the configuration of the TokenAuthenticatable
11
19
  # strategy with a block:
12
20
  #
@@ -1,17 +1,10 @@
1
1
  module Devise
2
2
  module Models
3
- # The TokenAuthenticatable module is responsible for generating an authentication token and
3
+ # The +TokenAuthenticatable+ module is responsible for generating an authentication token and
4
4
  # validating the authenticity of the same while signing in.
5
5
  #
6
6
  # This module only provides a few helpers to help you manage the token, but it is up to you
7
- # to choose how to use it. For example, if you want to have a new token every time the user
8
- # saves his account, you can do the following:
9
- #
10
- # before_save :reset_authentication_token
11
- #
12
- # On the other hand, if you want to generate token unless one exists, you should use instead:
13
- #
14
- # before_save :ensure_authentication_token
7
+ # to choose how to use it.
15
8
  #
16
9
  # If you want to delete the token after it is used, you can do so in the
17
10
  # after_token_authentication callback.
@@ -28,15 +21,32 @@ module Devise
28
21
  # request.env['devise.skip_trackable'] = true
29
22
  # end
30
23
  #
31
- # == Options
32
- #
33
- # TokenAuthenticatable adds the following options to devise_for:
34
- #
35
- # * +token_authentication_key+: Defines name of the authentication token params key. E.g. /users/sign_in?some_key=...
36
- #
37
24
  module TokenAuthenticatable
38
25
  extend ActiveSupport::Concern
39
26
 
27
+ included do
28
+ before_save :reset_authentication_token_before_save
29
+ before_save :ensure_authentication_token_before_save
30
+ end
31
+
32
+ module ClassMethods
33
+
34
+ def find_for_token_authentication(conditions)
35
+ find_for_authentication(authentication_token: conditions[Devise::TokenAuthenticatable.token_authentication_key])
36
+ end
37
+
38
+ # Generate a token checking if one does not already exist in the database.
39
+ def authentication_token
40
+ loop do
41
+ token = Devise.friendly_token
42
+ break token unless to_adapter.find_first({ authentication_token: token })
43
+ end
44
+ end
45
+
46
+ Devise::Models.config(self, :expire_auth_token_on_timeout)
47
+
48
+ end
49
+
40
50
  def self.required_fields(klass)
41
51
  [:authentication_token]
42
52
  end
@@ -70,21 +80,16 @@ module Devise
70
80
  self.class.expire_auth_token_on_timeout
71
81
  end
72
82
 
73
- module ClassMethods
74
- def find_for_token_authentication(conditions)
75
- find_for_authentication(authentication_token: conditions[Devise::TokenAuthenticatable.token_authentication_key])
76
- end
83
+ private
77
84
 
78
- # Generate a token checking if one does not already exist in the database.
79
- def authentication_token
80
- loop do
81
- token = Devise.friendly_token
82
- break token unless to_adapter.find_first({ authentication_token: token })
83
- end
85
+ def reset_authentication_token_before_save
86
+ reset_authentication_token if Devise::TokenAuthenticatable.should_reset_authentication_token
84
87
  end
85
88
 
86
- Devise::Models.config(self, :token_authentication_key, :expire_auth_token_on_timeout)
87
- end
89
+ def ensure_authentication_token_before_save
90
+ ensure_authentication_token if Devise::TokenAuthenticatable.should_ensure_authentication_token
91
+ end
92
+
88
93
  end
89
94
  end
90
95
  end
@@ -23,13 +23,6 @@ module Devise
23
23
  # by Rails: http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Token.html
24
24
  # The token options are stored in request.env['devise.token_options']
25
25
  #
26
- #
27
- # Changes regarding the original +TokenAuthenticatable+ implementation:
28
- #
29
- # The private method +remember_me?+ in +TokenAuthenticatable+ returns +false+.
30
- # For +TokenAuthenticatable+ this method was removed. This results in the
31
- # usage of the default implementation in +Authenticatable+.
32
- #
33
26
  class TokenAuthenticatable < Authenticatable
34
27
  def store?
35
28
  super && !mapping.to.skip_session_storage.include?(:token_auth)
@@ -1,5 +1,5 @@
1
1
  module Devise
2
2
  module TokenAuthenticatable
3
- VERSION = "0.3.0".freeze
3
+ VERSION = "0.3.1".freeze
4
4
  end
5
5
  end
@@ -38,6 +38,7 @@ shared_examples "token authenticatable" do
38
38
  end
39
39
  end
40
40
  end
41
+
41
42
  end
42
43
 
43
44
  context "class methods" do
@@ -70,8 +71,81 @@ shared_examples "token authenticatable" do
70
71
  :authentication_token
71
72
  ])
72
73
  end
74
+
75
+ end
76
+
77
+ end
78
+
79
+ context "before_save" do
80
+
81
+ let(:entity) { create(described_class.name.underscore.to_sym, :with_authentication_token) }
82
+
83
+ context "when the authentication token should be reset" do
84
+
85
+ before :each do
86
+ Devise::TokenAuthenticatable.setup do |config|
87
+ config.should_reset_authentication_token = true
88
+ end
89
+ end
90
+
91
+ after :each do
92
+ Devise::TokenAuthenticatable.setup do |config|
93
+ config.should_reset_authentication_token = false
94
+ end
95
+ end
96
+
97
+ it "resets the authentication token" do
98
+ expect(entity).to receive(:reset_authentication_token).once
99
+
100
+ entity.update_attributes(created_at: Time.now)
101
+ end
102
+
103
+ end
104
+
105
+ context "when the authentication token should not be reset" do
106
+
107
+ it "does not reset the authentication token" do
108
+ expect(entity).to_not receive(:reset_authentication_token)
109
+
110
+ entity.update_attributes(created_at: Time.now)
111
+ end
112
+
113
+ end
114
+
115
+ context "when the authentication token should be ensured" do
116
+
117
+ before :each do
118
+ Devise::TokenAuthenticatable.setup do |config|
119
+ config.should_ensure_authentication_token = true
120
+ end
121
+ end
122
+
123
+ after :each do
124
+ Devise::TokenAuthenticatable.setup do |config|
125
+ config.should_ensure_authentication_token = false
126
+ end
127
+ end
128
+
129
+ it "sets the authentication token" do
130
+ expect(entity).to receive(:ensure_authentication_token).once
131
+
132
+ entity.update_attributes(created_at: Time.now)
133
+ end
134
+
73
135
  end
136
+
137
+ context "when the authentication token should not be ensured" do
138
+
139
+ it "does not set the authentication token" do
140
+ expect(entity).to_not receive(:ensure_authentication_token)
141
+
142
+ entity.update_attributes(created_at: Time.now)
143
+ end
144
+
145
+ end
146
+
74
147
  end
148
+
75
149
  end
76
150
 
77
151
  describe User do
@@ -24,16 +24,8 @@ ActiveRecord::Migration.verbose = false
24
24
  # RSpec configuration
25
25
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
26
26
  RSpec.configure do |config|
27
- config.use_transactional_fixtures = true
28
- config.run_all_when_everything_filtered = true
29
-
30
- config.filter_run :focus
31
-
32
- # Run specs in random order to surface order dependencies. If you find an
33
- # order dependency and want to debug it, you can fix the order by providing
34
- # the seed, which is printed after each run.
35
- # --seed 1234
36
- #config.order = 'random'
27
+ config.use_transactional_fixtures = true
28
+ config.run_all_when_everything_filtered = true
37
29
 
38
30
  config.include FactoryGirl::Syntax::Methods
39
31
 
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Devise::TokenAuthenticatable do
4
+
5
+ context "configuring the token_authentication_key" do
6
+ let(:new_key) { :other_key }
7
+
8
+ it "should set the configuration" do
9
+ expect {
10
+ Devise::TokenAuthenticatable.setup do |config|
11
+ config.token_authentication_key = new_key
12
+ end
13
+ }.to change { Devise::TokenAuthenticatable.token_authentication_key }.from(:auth_token).to(new_key)
14
+ end
15
+ end
16
+
17
+ context "configuring the should_reset_authentication_token" do
18
+ let(:should_reset) { true }
19
+
20
+ it "should set the configuration" do
21
+ expect {
22
+ Devise::TokenAuthenticatable.setup do |config|
23
+ config.should_reset_authentication_token = should_reset
24
+ end
25
+ }.to change { Devise::TokenAuthenticatable.should_reset_authentication_token }.from(false).to(should_reset)
26
+ end
27
+ end
28
+
29
+ context "configuring the should_ensure_authentication_token" do
30
+ let(:should_ensure) { true }
31
+
32
+ it "should set the configuration" do
33
+ expect {
34
+ Devise::TokenAuthenticatable.setup do |config|
35
+ config.should_ensure_authentication_token = should_ensure
36
+ end
37
+ }.to change { Devise::TokenAuthenticatable.should_ensure_authentication_token }.from(false).to(should_ensure)
38
+ end
39
+ end
40
+
41
+ end
metadata CHANGED
@@ -1,156 +1,139 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise-token_authenticatable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
5
- prerelease:
4
+ version: 0.3.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Sebastian Oelke
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-10-24 00:00:00.000000000 Z
11
+ date: 2015-05-15 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: devise
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: 3.4.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: 3.4.0
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rails
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
33
  version: 4.1.0
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
40
  version: 4.1.0
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec-rails
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ~>
45
+ - - "~>"
52
46
  - !ruby/object:Gem::Version
53
47
  version: 3.0.2
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ~>
52
+ - - "~>"
60
53
  - !ruby/object:Gem::Version
61
54
  version: 3.0.2
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: pry
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ~>
59
+ - - "~>"
68
60
  - !ruby/object:Gem::Version
69
61
  version: 0.10.0
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ~>
66
+ - - "~>"
76
67
  - !ruby/object:Gem::Version
77
68
  version: 0.10.0
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: factory_girl_rails
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ~>
73
+ - - "~>"
84
74
  - !ruby/object:Gem::Version
85
75
  version: 4.4.0
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ~>
80
+ - - "~>"
92
81
  - !ruby/object:Gem::Version
93
82
  version: 4.4.0
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: timecop
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ~>
87
+ - - "~>"
100
88
  - !ruby/object:Gem::Version
101
89
  version: 0.7.0
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ~>
94
+ - - "~>"
108
95
  - !ruby/object:Gem::Version
109
96
  version: 0.7.0
110
97
  - !ruby/object:Gem::Dependency
111
98
  name: bundler
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
- - - ~>
101
+ - - "~>"
116
102
  - !ruby/object:Gem::Version
117
103
  version: '1.6'
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  requirements:
123
- - - ~>
108
+ - - "~>"
124
109
  - !ruby/object:Gem::Version
125
110
  version: '1.6'
126
111
  - !ruby/object:Gem::Dependency
127
112
  name: sqlite3
128
113
  requirement: !ruby/object:Gem::Requirement
129
- none: false
130
114
  requirements:
131
- - - ~>
115
+ - - "~>"
132
116
  - !ruby/object:Gem::Version
133
117
  version: '1.3'
134
118
  type: :development
135
119
  prerelease: false
136
120
  version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
121
  requirements:
139
- - - ~>
122
+ - - "~>"
140
123
  - !ruby/object:Gem::Version
141
124
  version: '1.3'
142
- description: ! "This gem provides the extracted Token Authenticatable module of devise.\n
143
- \ It enables the user to sign in via an authentication token.
144
- This token\n can be given via a query string or HTTP Basic
145
- Authentication."
125
+ description: |-
126
+ This gem provides the extracted Token Authenticatable module of devise.
127
+ It enables the user to sign in via an authentication token. This token
128
+ can be given via a query string or HTTP Basic Authentication.
146
129
  email:
147
130
  - dev@sohleeatsworld.de
148
131
  executables: []
149
132
  extensions: []
150
133
  extra_rdoc_files: []
151
134
  files:
152
- - .gitignore
153
- - .travis.yml
135
+ - ".gitignore"
136
+ - ".travis.yml"
154
137
  - Gemfile
155
138
  - LICENSE
156
139
  - README.md
@@ -198,30 +181,30 @@ files:
198
181
  - spec/support/rails_app/public/500.html
199
182
  - spec/support/rails_app/public/favicon.ico
200
183
  - spec/support/session_helper.rb
184
+ - spec/token_authenticatable_spec.rb
201
185
  homepage: https://github.com/baschtl/devise-token_authenticatable
202
186
  licenses:
203
187
  - MIT
188
+ metadata: {}
204
189
  post_install_message:
205
190
  rdoc_options: []
206
191
  require_paths:
207
192
  - lib
208
193
  required_ruby_version: !ruby/object:Gem::Requirement
209
- none: false
210
194
  requirements:
211
- - - ! '>='
195
+ - - ">="
212
196
  - !ruby/object:Gem::Version
213
197
  version: '0'
214
198
  required_rubygems_version: !ruby/object:Gem::Requirement
215
- none: false
216
199
  requirements:
217
- - - ! '>='
200
+ - - ">="
218
201
  - !ruby/object:Gem::Version
219
202
  version: '0'
220
203
  requirements: []
221
204
  rubyforge_project:
222
- rubygems_version: 1.8.23
205
+ rubygems_version: 2.2.2
223
206
  signing_key:
224
- specification_version: 3
207
+ specification_version: 4
225
208
  summary: Provides authentication based on an authentication token for devise 3.2 and
226
209
  up.
227
210
  test_files:
@@ -262,3 +245,4 @@ test_files:
262
245
  - spec/support/rails_app/public/500.html
263
246
  - spec/support/rails_app/public/favicon.ico
264
247
  - spec/support/session_helper.rb
248
+ - spec/token_authenticatable_spec.rb