Skip to content

Commit 092c462

Browse files
committed
Upgrade to OmniAuth CAS 3.x
* Display button on `UnauthorizedError` instead of redirecting to login. * Use `ForbiddenError` instead of redirecting to login when a user that is not an admin tries to access an admin page in the section for ref cards/stack passes. * Update specs.
1 parent 27c9926 commit 092c462

28 files changed

Lines changed: 99 additions & 117 deletions

.idea/altmedia.iml

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gemfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ gem 'lograge', '>=0.11.2'
2424
gem 'netaddr', '~> 1.5', '>= 1.5.1'
2525
gem 'net-ssh'
2626
gem 'okcomputer', '~> 1.19'
27-
gem 'omniauth', '~> 1.9', '>= 1.9.2'
28-
gem 'omniauth-cas', '~> 2.0'
27+
gem 'omniauth', '~> 2.1'
28+
gem 'omniauth-cas', '~> 3.0'
29+
gem 'omniauth-rails_csrf_protection', '~> 1.0'
2930
gem 'pg', '~> 1.2'
3031
gem 'prawn', '~> 2.4'
3132
gem 'puma', '~> 4.3', '>= 4.3.12'

Gemfile.lock

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,18 @@ GEM
276276
ostruct (>= 0.2)
277277
okcomputer (1.19.1)
278278
benchmark
279-
omniauth (1.9.2)
279+
omniauth (2.1.4)
280280
hashie (>= 3.4.6)
281-
rack (>= 1.6.2, < 3)
282-
omniauth-cas (2.0.0)
283-
addressable (~> 2.3)
284-
nokogiri (~> 1.5)
285-
omniauth (~> 1.2)
281+
logger
282+
rack (>= 2.2.3)
283+
rack-protection
284+
omniauth-cas (3.0.2)
285+
addressable (~> 2.8)
286+
nokogiri (~> 1.12)
287+
omniauth (~> 2.1)
288+
omniauth-rails_csrf_protection (1.0.2)
289+
actionpack (>= 4.2)
290+
omniauth (~> 2.0)
286291
ostruct (0.6.3)
287292
ougai (2.0.0)
288293
oj (~> 3.10)
@@ -310,6 +315,9 @@ GEM
310315
raabro (1.4.0)
311316
racc (1.8.1)
312317
rack (2.2.17)
318+
rack-protection (3.2.0)
319+
base64 (>= 0.1.0)
320+
rack (~> 2.2, >= 2.2.4)
313321
rack-session (1.0.2)
314322
rack (< 3)
315323
rack-test (2.2.0)
@@ -533,8 +541,9 @@ DEPENDENCIES
533541
net-ssh
534542
netaddr (~> 1.5, >= 1.5.1)
535543
okcomputer (~> 1.19)
536-
omniauth (~> 1.9, >= 1.9.2)
537-
omniauth-cas (~> 2.0)
544+
omniauth (~> 2.1)
545+
omniauth-cas (~> 3.0)
546+
omniauth-rails_csrf_protection (~> 1.0)
538547
pg (~> 1.2)
539548
prawn (~> 2.4)
540549
puma (~> 4.3, >= 4.3.12)

app/controllers/concerns/exception_handling.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def log_error(error)
6464
# this isn't really an error condition, it just means the user's
6565
# not logged in, so we don't need the full stack trace etc.
6666
logger.info(error.message)
67-
redirect_to main_app.login_path(url: request.fullpath)
67+
render :unauthorized, status: :unauthorized
6868
end
6969
end
7070
# rubocop:enable Metrics/BlockLength

app/controllers/reference_card_forms_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,6 @@ def validate_recaptcha!
8585

8686
def require_admin!
8787
@user_is_admin = current_user.role?(Role.stackpass_admin)
88-
redirect_to login_path(url: request.fullpath) unless @user_is_admin
88+
raise Error::ForbiddenError unless @user_is_admin
8989
end
9090
end

app/controllers/sessions_controller.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010
#
1111
# @see https://github.com/omniauth/omniauth
1212
class SessionsController < ApplicationController
13-
# Redirect the user to Calnet for authentication
14-
def new
15-
redirect_args = { origin: params[:url] || home_path }.to_query
16-
redirect_to "/auth/calnet?#{redirect_args}"
17-
end
18-
1913
# Generate a new user session using data returned from a valid Calnet login
2014
def callback
2115
logger.debug({ msg: 'Received omniauth callback', omniauth: auth_params })

app/controllers/stack_pass_forms_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ def validate_recaptcha!
8787

8888
def require_admin!
8989
@user_is_admin = current_user.role?(Role.stackpass_admin)
90-
redirect_to login_path(url: request.fullpath) unless @user_is_admin
90+
raise Error::ForbiddenError unless @user_is_admin
9191
end
9292
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<h1>Unauthorized</h1>
2+
3+
<p>You need to log in to continue.</p>
4+
5+
<%= form_tag('/auth/calnet', url: request.original_url, method: :post, data: { turbo: false }) do %>
6+
<%= button_tag 'CalNet Login', class: :calnet_login, role: :link %>
7+
<% end %>

config/routes.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474

7575
# Omniauth automatically handles requests to /auth/:provider. We need only
7676
# implement the callback.
77-
get '/login', to: 'sessions#new', as: :login
7877
get '/logout', to: 'sessions#destroy', as: :logout
7978
get '/auth/:provider/callback', to: 'sessions#callback', as: :omniauth_callback
8079
get '/auth/failure', to: 'sessions#failure'

spec/calnet_helper.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ def without_redirects
120120

121121
def log_in_with_omniauth(auth_hash)
122122
OmniAuth.config.mock_auth[:calnet] = auth_hash
123-
do_get login_path
124-
125123
Rails.application.env_config['omniauth.auth'] = auth_hash
126124
do_get omniauth_callback_path(:calnet)
127125
end

0 commit comments

Comments
 (0)