Skip to content

Commit 00e0590

Browse files
committed
Wrap auth logic in AuthService
This avoids having to keep track of the multiple store classes we now have in the controllers, and removes some logic from the controllers. Hopefully this makes it a bit easier to see what's involved in the auth process.
1 parent 5d65390 commit 00e0590

11 files changed

Lines changed: 150 additions & 68 deletions

app/controllers/application_controller.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,7 @@ def default_locale?(locale)
119119
false
120120
end
121121

122-
def auth_store
123-
@auth_store ||= Store::AuthStore.new(session)
124-
end
125-
126-
def return_from_one_login_store
127-
@return_from_one_login_store ||= Store::ReturnFromOneLoginStore.new(session)
122+
def auth_service
123+
@auth_service ||= AuthService.new(session)
128124
end
129125
end

app/controllers/forms/check_your_answers_controller.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ def submit_answers
4343

4444
current_context.save_submission_details(submission_reference, requested_email_confirmation)
4545

46-
if auth_store.logged_in?
47-
return_from_one_login_store.store_return_params(form: current_context.form, mode: mode, locale: locale_param)
48-
redirect_to logout_request.redirect_uri, allow_other_host: true
46+
if auth_service.logged_in?
47+
auth_service.store_return_params(form: current_context.form, mode: mode, locale: locale_param)
48+
redirect_to auth_service.logout_redirect_uri(omniauth_logged_out_url), allow_other_host: true
4949
else
5050
redirect_to :form_submitted
5151
end
@@ -87,10 +87,5 @@ def back_link
8787
end
8888
end
8989
end
90-
91-
def logout_request
92-
token = auth_store.get_token
93-
AuthService.new.logout_request(token, omniauth_logged_out_url)
94-
end
9590
end
9691
end

app/controllers/forms/continue_to_one_login_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class ContinueToOneLoginController < BaseController
33
before_action :redirect_if_feature_disabled, :redirect_if_form_incomplete
44

55
def show
6-
return_from_one_login_store.store_return_params(
6+
auth_service.store_return_params(
77
form: current_context.form,
88
mode: mode,
99
locale: locale_param,

app/controllers/users/omniauth_controller.rb

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
module Users
22
class OmniauthController < ApplicationController
3-
class OmniAuthLoggedInDataMissingError < StandardError; end
4-
53
class OmniAuthFailure < StandardError; end
64

75
def callback
86
auth_hash = request.env["omniauth.auth"]
9-
raise OmniAuthLoggedInDataMissingError, "Auth hash is missing on request" if auth_hash.blank?
10-
11-
email = auth_hash.dig("info", "email")
12-
raise OmniAuthLoggedInDataMissingError, "Email is missing in OmniAuth auth hash" if email.blank?
13-
14-
token = auth_hash.dig("credentials", "id_token")
15-
raise OmniAuthLoggedInDataMissingError, "Token is missing in OmniAuth auth hash" if token.blank?
16-
17-
auth_store.store_token(token)
18-
19-
form_id = return_from_one_login_store.form_id
20-
path_params = return_from_one_login_store.get_path_params
7+
auth_service.store_auth_details(auth_hash)
218

22-
Store::ConfirmationDetailsStore.new(session, form_id).save_copy_of_answers_email_address(email)
9+
path_params = auth_service.form_path_params
2310

2411
redirect_to check_your_answers_path(**path_params)
2512
rescue Store::ReturnFromOneLoginStore::MissingReturnParamsError
@@ -33,9 +20,9 @@ def failure
3320
end
3421

3522
def logged_out
36-
auth_store.clear
23+
auth_service.clear_auth_session
3724

38-
path_params = return_from_one_login_store.get_path_params
25+
path_params = auth_service.form_path_params
3926
redirect_to form_submitted_path(**path_params)
4027
end
4128
end

app/lib/store/return_from_one_login_store.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def store_return_params(form:, mode:, locale:)
2020
@store[RETURN_FROM_ONE_LOGIN_KEY][LAST_LOCALE_KEY] = locale
2121
end
2222

23-
def get_path_params
23+
def form_path_params
2424
raise MissingReturnParamsError if @store[RETURN_FROM_ONE_LOGIN_KEY].blank?
2525

2626
{

app/services/auth_service.rb

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,43 @@
11
class AuthService
2-
def logout_request(token, post_logout_redirect_uri)
3-
logout_utility.build_request(
2+
class DataMissingError < StandardError; end
3+
4+
attr_reader :auth_store, :return_from_one_login_store
5+
6+
def initialize(store)
7+
@store = store
8+
@return_from_one_login_store = Store::ReturnFromOneLoginStore.new(store)
9+
@auth_store = Store::AuthStore.new(store)
10+
end
11+
12+
delegate :logged_in?, to: :auth_store
13+
delegate :store_return_params, :form_path_params, to: :return_from_one_login_store
14+
15+
def store_auth_details(auth_hash)
16+
form_id = @return_from_one_login_store.form_id
17+
18+
raise DataMissingError, "Auth hash is missing on request" if auth_hash.blank?
19+
20+
email = auth_hash.dig("info", "email")
21+
raise DataMissingError, "Email is missing in OmniAuth auth hash" if email.blank?
22+
23+
token = auth_hash.dig("credentials", "id_token")
24+
raise DataMissingError, "Token is missing in OmniAuth auth hash" if token.blank?
25+
26+
@auth_store.store_token(token)
27+
Store::ConfirmationDetailsStore.new(@store, form_id).save_copy_of_answers_email_address(email)
28+
end
29+
30+
def logout_redirect_uri(post_logout_redirect_uri)
31+
token = @auth_store.get_token
32+
logout_request = logout_utility.build_request(
433
id_token_hint: token,
534
post_logout_redirect_uri: url_without_params(post_logout_redirect_uri),
635
)
36+
logout_request.redirect_uri
37+
end
38+
39+
def clear_auth_session
40+
@auth_store.clear
741
end
842

943
private

spec/lib/store/return_from_one_login_store_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
it "stores and return the path params" do
1313
return_from_one_login_store.store_return_params(form:, mode:, locale:)
1414

15-
expect(return_from_one_login_store.get_path_params).to eq({
15+
expect(return_from_one_login_store.form_path_params).to eq({
1616
mode: mode.to_s,
1717
form_id: form.id,
1818
form_slug: form.form_slug,
@@ -21,7 +21,7 @@
2121
end
2222

2323
it "raises an error if the return params have not been stored" do
24-
expect { return_from_one_login_store.get_path_params }.to raise_error(Store::ReturnFromOneLoginStore::MissingReturnParamsError)
24+
expect { return_from_one_login_store.form_path_params }.to raise_error(Store::ReturnFromOneLoginStore::MissingReturnParamsError)
2525
end
2626
end
2727

spec/requests/forms/check_your_answers_controller_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@
327327
let(:end_session_endpoint) { "http://example.com/one-login-mock/logout" }
328328

329329
before do
330-
allow(Store::ReturnFromOneLoginStore).to receive(:new).and_wrap_original do |original_method, *_args|
330+
allow(AuthService).to receive(:new).and_wrap_original do |original_method, *_args|
331331
original_method.call(store)
332332
end
333333

spec/requests/forms/continue_to_one_login_controller_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
allow(Flow::Context).to receive(:new).and_wrap_original do |original_method, *args|
3838
original_method.call(form: args[0][:form], form_document: args[0][:form_document], store:)
3939
end
40-
allow(Store::ReturnFromOneLoginStore).to receive(:new).and_wrap_original do |original_method, *_args|
40+
allow(AuthService).to receive(:new).and_wrap_original do |original_method, *_args|
4141
original_method.call(store)
4242
end
4343
end

spec/requests/users/omniauth_controller_spec.rb

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,7 @@
4040
OmniAuth.config.test_mode = true
4141
OmniAuth.config.mock_auth[:default] = auth_hash
4242

43-
allow(Store::ReturnFromOneLoginStore).to receive(:new).and_wrap_original do |original_method, *_args|
44-
original_method.call(store)
45-
end
46-
allow(Store::ConfirmationDetailsStore).to receive(:new).and_wrap_original do |original_method, *args|
47-
original_method.call(store, args[1])
48-
end
49-
allow(Store::AuthStore).to receive(:new).and_wrap_original do |original_method, *_args|
43+
allow(AuthService).to receive(:new).and_wrap_original do |original_method, *_args|
5044
original_method.call(store)
5145
end
5246
end
@@ -69,19 +63,11 @@
6963
end
7064
end
7165

72-
context "when the auth details are not present on the request" do
66+
context "when data is missing on the auth details on the request" do
7367
let(:auth_hash) { {} }
7468

7569
it "raises an OmniAuthLoggedInDataMissingError" do
76-
expect { get omniauth_callback_path }.to raise_error(Users::OmniauthController::OmniAuthLoggedInDataMissingError)
77-
end
78-
end
79-
80-
context "when the email on the auth hash is blank" do
81-
let(:email) { "" }
82-
83-
it "raises a OmniAuthLoggedInDataMissingError" do
84-
expect { get omniauth_callback_path }.to raise_error(Users::OmniauthController::OmniAuthLoggedInDataMissingError)
70+
expect { get omniauth_callback_path }.to raise_error(AuthService::DataMissingError)
8571
end
8672
end
8773

@@ -107,10 +93,7 @@
10793
let(:token) { Faker::Alphanumeric.alphanumeric }
10894

10995
before do
110-
allow(Store::ReturnFromOneLoginStore).to receive(:new).and_wrap_original do |original_method, *_args|
111-
original_method.call(store)
112-
end
113-
allow(Store::AuthStore).to receive(:new).and_wrap_original do |original_method, *_args|
96+
allow(AuthService).to receive(:new).and_wrap_original do |original_method, *_args|
11497
original_method.call(store)
11598
end
11699
end

0 commit comments

Comments
 (0)