-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement DB backed sessions for candidates
In testing the one login integration, we discovered that having session backed login is not ideal. There is a risk that the session gets too big, over 5000 bytes. To fix this, we are switching to Database backed sessions. This is inspired by the rails 8 new db backed sessions. Most of the logic is in this concern `Authentication` using this and before filters in controllers we control the one login mechanism. We will use DB backed session for the normal candidate flow. Impersonation will still use cookie session. This means that deleting the session from the DB logs out the user. Switching between one login and magic link auth system will be seamless as we always check `current_candidate`. We will still save some data in the session but it will be very little, just ids rather than big tokens. The session size decreased from roughly 3224 to 600
- Loading branch information
1 parent
bb3b998
commit 1adb434
Showing
8 changed files
with
215 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module Authentication | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_action :require_authentication, if: -> { one_login_enabled? } | ||
helper_method :authenticated? | ||
end | ||
|
||
class_methods do | ||
def allow_unauthenticated_access(**options) | ||
skip_before_action :require_authentication, **options | ||
end | ||
end | ||
|
||
private | ||
|
||
def authenticated? | ||
resume_session | ||
end | ||
|
||
def require_authentication | ||
current_candidate || resume_session || request_authentication | ||
end | ||
|
||
def resume_session | ||
Current.session ||= find_session_by_cookie | ||
end | ||
|
||
def find_session_by_cookie | ||
Session.find_by(id: cookies.signed[:session_id]) if cookies.signed[:session_id] | ||
end | ||
|
||
def request_authentication | ||
redirect_to candidate_interface_create_account_or_sign_in_path | ||
end | ||
|
||
def start_new_session_for(candidate:, id_token_hint: nil) | ||
ActiveRecord::Base.transaction do | ||
unless authenticated? | ||
candidate.sessions.create!( | ||
user_agent: request.user_agent, | ||
ip_address: request.remote_ip, | ||
id_token_hint:, | ||
).tap do |session| | ||
Current.session = session | ||
cookies.signed.permanent[:session_id] = { value: session.id, httponly: true, same_site: :lax } | ||
end | ||
|
||
candidate.update!(last_signed_in_at: Time.zone.now) | ||
end | ||
end | ||
end | ||
|
||
def terminate_session | ||
Current.session&.destroy | ||
cookies.delete(:session_id) | ||
reset_session | ||
end | ||
|
||
def one_login_enabled? | ||
FeatureFlag.active?(:one_login_candidate_sign_in) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Current < ActiveSupport::CurrentAttributes | ||
attribute :session | ||
delegate :candidate, to: :session, allow_nil: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.