Skip to content

Commit

Permalink
Merge pull request #596 from jmax-fearless/new-SRCH-1515-logout
Browse files Browse the repository at this point in the history
SRCH-1515 logout
  • Loading branch information
lsamuels-fearless authored Jul 9, 2020
2 parents d5090cc + e8258a2 commit 0718819
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 25 deletions.
47 changes: 36 additions & 11 deletions app/controllers/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
# frozen_string_literal: true

class OmniauthCallbacksController < ApplicationController
class LoginError < StandardError
end

def login_dot_gov
@user = User.from_omniauth(request.env['omniauth.auth'])

if @user.persisted? && @user.approval_status != 'not_approved'
reset_session
set_user_session
redirect_to(admin_home_page_path)
else
redirect_to('https://search.gov/access-denied')
end
reset_session
set_id_token
set_user_session
redirect_to(admin_home_page_path)
rescue LoginError => e
flash[:error] = "login internal error: #{e.message}"
redirect_to('/login')
end

def user
@user ||= User.from_omniauth(omniauth_data)

raise LoginError, "can't find user #{omniauth_data.info.email}" unless @user

raise LoginError, "login not allowed for #{@user.email}" unless @user.login_allowed?

@user
end

private
def omniauth_data
raise LoginError, 'no omniauth data' unless request.env['omniauth.auth']

request.env['omniauth.auth']
end

def credentials
raise LoginError, 'no user credentials' unless omniauth_data['credentials']

omniauth_data['credentials']
end

def set_id_token
session[:id_token] = credentials['id_token']
end

def set_user_session
user_session = UserSession.create(@user)
user_session = UserSession.create(user)
user_session.secure = Rails.application.config.ssl_options[:secure_cookies]
end
end
24 changes: 22 additions & 2 deletions app/controllers/user_sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
# frozen_string_literal: true

class UserSessionsController < ApplicationController
before_action :reset_session, only: [:destroy]
before_action :require_user, only: :destroy

def security_notification
redirect_to(account_path) if current_user && current_user&.complete?
end

def destroy
id_token = session[:id_token]
reset_session
current_user_session.destroy
redirect_to(login_path)
redirect_to(logout_redirect_uri(id_token))
end

def login_uri
"#{request.protocol}#{request.host_with_port}/login"
end

def logout_redirect_uri(id_token)
base_uri = URI(Rails.application.secrets.login_dot_gov[:idp_base_url])
URI::HTTPS.build(
host: base_uri.host,
path: '/openid_connect/logout',
query: {
id_token_hint: id_token,
post_logout_redirect_uri: login_uri,
state: '1234567890123456789012'
}.to_query
).to_s
end
end
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ def self.from_omniauth(auth)
end
end

def login_allowed?
persisted? && approved?
end

private

def ping_admin
Expand Down
7 changes: 5 additions & 2 deletions features/admin.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ Feature: Administration
When I follow "Super Admin" in the main navigation bar
Then I should be on the admin home page

When I follow "Sign Out"
Then I should be on the login page
# SRCH-1552
# Commented out until we figure out how to get login.gov out of
# the loop during testing.
# When I follow "Sign Out"
# Then I should be on the login page

Scenario: Visiting the admin home page as an admin who is also an affiliate
Given "[email protected]" is an affiliate
Expand Down
7 changes: 5 additions & 2 deletions features/step_definitions/user_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
uid: 'test_123',
info: {
email: email
}
},
credentials: {
id_token: 'fake_id_token',
},
}

OmniAuth.config.add_mock('logindotgov', omniauth_hash)
Expand Down Expand Up @@ -33,4 +36,4 @@

When(/^I visit the login page/) do
visit login_path
end
end
5 changes: 4 additions & 1 deletion features/user_sessions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ Feature: User sessions
And I go to the login page
Then I should see "Contact Information"
When I sign out
Then I should be on the login page
# SRCH-1552
# Commented out until we figure out how to handle login.gov sign
# out properly
# Then I should be on the login page

# to be updated in SRCH-947 for login.gov
@wip
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/omniauth_callbacks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
end

it 'redirects to access-denied page' do
expect(get_login_dot_gov).to redirect_to('https://search.gov/access-denied')
expect(get_login_dot_gov).to redirect_to('http://test.host/login')
end
end

Expand All @@ -83,8 +83,8 @@
let(:auth) { mock_user_auth(user.email, 'notapproved12345') }

it 'redirects to access-denied page' do
expect(get_login_dot_gov).to redirect_to('https://search.gov/access-denied')
expect(get_login_dot_gov).to redirect_to('http://test.host/login')
end
end
end
end
end
2 changes: 0 additions & 2 deletions spec/controllers/user_sessions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
describe UserSessionsController do
fixtures :users

it { is_expected.to use_before_action(:reset_session) }

describe '#security_notification' do
context 'when a user is not logged in' do
before { get :security_notification }
Expand Down
9 changes: 7 additions & 2 deletions spec/support/omniauth_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
module OmniauthHelpers
OmniAuth.config.test_mode = true

def mock_user_auth(email = '[email protected]', uid = '12345')
def mock_user_auth(email = '[email protected]',
uid = '12345',
id_token = 'mock_id_token')
omniauth_hash = {
'provider': 'logindotgov',
'uid': uid,
'info': {
'email': email
}
},
'credentials': {
'id_token': id_token
},
}

OmniAuth.config.add_mock(:login_dot_gov, omniauth_hash)
Expand Down

0 comments on commit 0718819

Please sign in to comment.