authie 4.0.0.rc4 → 4.0.0.rc7

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
  SHA256:
3
- metadata.gz: 99cb3ea1143001fdb259df6d251223036ae94ac4f2661d9732e01b9438a67702
4
- data.tar.gz: 261dd3764202502100c0ba19bc649cb7cf3c09b57250d1643b0f14c35e10f2f3
3
+ metadata.gz: 44634d5c60eeabd533457e52d0c6926fc7e72a91affd8d9bd1ff18bc07884c5c
4
+ data.tar.gz: 310b072244bbcd463d7615e2e77b94c7829bae98af0508276b952ea000e3bb44
5
5
  SHA512:
6
- metadata.gz: b1bfae12c744d812a64dc757b6524f3b09f951adf5f62528e96560dcb93f0424d4590575b5a082a715402c1dbfe8f8721b184fb4b369a9f71b1b51a9c318c81b
7
- data.tar.gz: 8c344e7933ec8d13bea40130ba0974a9f2ac97fd5bcb3aa0694305b140b290f4a6a1082cfcfbba82261a11f0bf6c2611662acdb7fc65506b5107b09250f61fae
6
+ metadata.gz: c560dafa6d2b626f753e6afac7152ea2d950536b4a520fcd4454b88fbd8978884523fe44ce1ae90547565a1090e8e447d48a983f91d2a52cf8824f77c1ac993d
7
+ data.tar.gz: 75642d5fb4aa28807e9fc32e77e4de90cf1527da81ddccdcd4b2bf0c233240c2642acb7b100e7931bc2a169429289626c962ab88bc305a2cf016446e2ac229b5
data/lib/authie/config.rb CHANGED
@@ -8,6 +8,8 @@ module Authie
8
8
  attr_accessor :persistent_session_length
9
9
  attr_accessor :sudo_session_timeout
10
10
  attr_accessor :browser_id_cookie_name
11
+ attr_accessor :session_token_length
12
+ attr_accessor :extend_session_expiry_on_touch
11
13
  attr_accessor :events
12
14
 
13
15
  def initialize
@@ -15,6 +17,8 @@ module Authie
15
17
  @persistent_session_length = 2.months
16
18
  @sudo_session_timeout = 10.minutes
17
19
  @browser_id_cookie_name = :browser_id
20
+ @session_token_length = 64
21
+ @extend_session_expiry_on_touch = false
18
22
  @events = EventManager.new
19
23
  end
20
24
  end
@@ -9,10 +9,13 @@ module Authie
9
9
  # The controller delegate implements methods that can be used by a controller. These are then
10
10
  # extended into controllers as needed (see ControllerExtension).
11
11
  class ControllerDelegate
12
+ attr_accessor :touch_auth_session_enabled
13
+
12
14
  # @param controller [ActionController::Base]
13
15
  # @return [Authie::ControllerDelegate]
14
16
  def initialize(controller)
15
17
  @controller = controller
18
+ @touch_auth_session_enabled = true
16
19
  end
17
20
 
18
21
  # Sets a browser ID. This must be performed on any page request where AUthie will be used.
@@ -36,18 +39,25 @@ module Authie
36
39
  proposed_browser_id
37
40
  end
38
41
 
39
- # Touch the session on each request to ensure that it is validated and all last activity
40
- # information is updated. This will return the session if one has been touched otherwise
41
- # it will reteurn false if there is no session/not logged in. It is safe to run this on
42
- # all requests even if there is no session.
42
+ # Validate the auth session to ensure that it is current validate and raise an error if it
43
+ # is not suitable for use.
43
44
  #
44
45
  # @return [Authie::Session, false]
45
- def touch_auth_session
46
- return auth_session.touch if logged_in?
46
+ def validate_auth_session
47
+ return auth_session.validate if logged_in?
47
48
 
48
49
  false
49
50
  end
50
51
 
52
+ # Touch the session to update details on the latest activity.
53
+ #
54
+ # @return [Authie::Session, false]
55
+ def touch_auth_session
56
+ yield if block_given?
57
+ ensure
58
+ auth_session.touch if @touch_auth_session_enabled && logged_in?
59
+ end
60
+
51
61
  # Return the user for the currently logged in user or nil if no user is logged in
52
62
  #
53
63
  # @return [ActiveRecord::Base, nil]
@@ -7,9 +7,11 @@ module Authie
7
7
  class << self
8
8
  def included(base)
9
9
  base.helper_method :logged_in?, :current_user, :auth_session
10
- base.before_action :set_browser_id, :touch_auth_session
10
+ base.before_action :set_browser_id, :validate_auth_session
11
+ base.around_action :touch_auth_session
11
12
 
12
13
  base.delegate :set_browser_id, to: :auth_session_delegate
14
+ base.delegate :validate_auth_session, to: :auth_session_delegate
13
15
  base.delegate :touch_auth_session, to: :auth_session_delegate
14
16
  base.delegate :current_user, to: :auth_session_delegate
15
17
  base.delegate :create_auth_session, to: :auth_session_delegate
@@ -24,5 +26,9 @@ module Authie
24
26
  def auth_session_delegate
25
27
  @auth_session_delegate ||= Authie::ControllerDelegate.new(self)
26
28
  end
29
+
30
+ def skip_touch_auth_session!
31
+ auth_session_delegate.touch_auth_session_enabled = false
32
+ end
27
33
  end
28
34
  end
@@ -88,11 +88,11 @@ module Authie
88
88
  # @raises [ActiveRecord::RecordInvalid]
89
89
  # @return [Authie::Session]
90
90
  def touch
91
- validate
92
91
  @session.last_activity_at = Time.now
93
92
  @session.last_activity_ip = @controller.request.ip
94
93
  @session.last_activity_path = @controller.request.path
95
94
  @session.requests += 1
95
+ extend_session_expiry_if_appropriate
96
96
  @session.save!
97
97
  Authie.config.events.dispatch(:session_touched, self)
98
98
  self
@@ -133,6 +133,15 @@ module Authie
133
133
  self
134
134
  end
135
135
 
136
+ # Resets the token for the currently active session to a new string
137
+ #
138
+ # @return [Authie::Session]
139
+ def reset_token
140
+ @session.reset_token
141
+ set_cookie
142
+ self
143
+ end
144
+
136
145
  private
137
146
 
138
147
  # rubocop:disable Naming/AccessorMethodName
@@ -202,6 +211,16 @@ module Authie
202
211
  self
203
212
  end
204
213
 
214
+ def extend_session_expiry_if_appropriate
215
+ return if @session.expires_at.nil?
216
+ return unless Authie.config.extend_session_expiry_on_touch
217
+
218
+ # If enabled, sessions with an expiry time will automatiaclly be incremented
219
+ # whenever a page is touched. The cookie will also be updated as appropriate.
220
+ @session.expires_at = Authie.config.persistent_session_length.from_now
221
+ set_cookie
222
+ end
223
+
205
224
  class << self
206
225
  # Create a new session within the given controller for the
207
226
  #
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_record/base'
4
- require 'secure_random_string'
4
+ require 'securerandom'
5
5
  require 'authie/config'
6
6
 
7
7
  module Authie
@@ -19,15 +19,8 @@ module Authie
19
19
  # Attributes
20
20
  serialize :data, Hash
21
21
 
22
- before_validation do
23
- self.user_agent = user_agent[0, 255] if user_agent.is_a?(String)
24
- self.last_activity_path = last_activity_path[0, 255] if last_activity_path.is_a?(String)
25
- end
26
-
27
- before_create do
28
- self.temporary_token = SecureRandomString.new(44)
29
- self.token_hash = self.class.hash_token(temporary_token)
30
- end
22
+ before_validation :shorten_strings
23
+ before_create :set_new_token
31
24
 
32
25
  # Return the user that
33
26
  def user
@@ -109,6 +102,27 @@ module Authie
109
102
  self.class.where('id < ?', id).for_user(user).where(login_ip: login_ip).empty?
110
103
  end
111
104
 
105
+ # Reset a new token for the session and return the new token
106
+ #
107
+ # @return [String]
108
+ def reset_token
109
+ set_new_token
110
+ save!
111
+ temporary_token
112
+ end
113
+
114
+ private
115
+
116
+ def shorten_strings
117
+ self.user_agent = user_agent[0, 255] if user_agent.is_a?(String)
118
+ self.last_activity_path = last_activity_path[0, 255] if last_activity_path.is_a?(String)
119
+ end
120
+
121
+ def set_new_token
122
+ self.temporary_token = SecureRandom.alphanumeric(Authie.config.session_token_length)
123
+ self.token_hash = self.class.hash_token(temporary_token)
124
+ end
125
+
112
126
  class << self
113
127
  # Find a session from the database for the given controller instance.
114
128
  # Returns a session object or :none if no session is found.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authie
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0.rc4
4
+ version: 4.0.0.rc7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-29 00:00:00.000000000 Z
11
+ date: 2022-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -30,20 +30,6 @@ dependencies:
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '8.0'
33
- - !ruby/object:Gem::Dependency
34
- name: secure_random_string
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - ">="
38
- - !ruby/object:Gem::Version
39
- version: '0'
40
- type: :runtime
41
- prerelease: false
42
- version_requirements: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: '0'
47
33
  - !ruby/object:Gem::Dependency
48
34
  name: appraisal
49
35
  requirement: !ruby/object:Gem::Requirement