-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10203 from DFE-Digital/cv/one-login-db-session
One login db session
- Loading branch information
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.