|
3 | 3 | require 'omniauth/cul' |
4 | 4 |
|
5 | 5 | class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController |
6 | | - # Adding the line below so that if the auth endpoint POSTs to our cas endpoint, it won't |
7 | | - # be rejected by authenticity token verification. |
8 | 6 | # See https://github.com/omniauth/omniauth/wiki/FAQ#rails-session-is-clobbered-after-callback-on-developer-strategy |
9 | | - skip_before_action :verify_authenticity_token, only: :cas |
| 7 | + # The CAS login redirect to the columbia_cas callback endpoint AND the developer form submission to the |
| 8 | + # developer_uid callback do not send authenticity tokens, so we'll skip token verification for these actions. |
| 9 | + skip_before_action :verify_authenticity_token, only: [:columbia_cas, :developer_uid] |
10 | 10 |
|
11 | | - def app_cas_callback_endpoint |
12 | | - "#{request.base_url}/users/auth/cas/callback" |
13 | | - end |
| 11 | + # POST /users/auth/developer_uid/callback |
| 12 | + def developer_uid |
| 13 | + return unless Rails.env.development? # Only allow this action to run in the development environment |
14 | 14 |
|
15 | | - # In local development, use devise's controller action. In deployed env, use CAS server |
16 | | - def passthru |
17 | | - if Rails.env.development? |
18 | | - super |
19 | | - else |
20 | | - redirect_to Omniauth::Cul::Cas3.passthru_redirect_url(app_cas_callback_endpoint), allow_other_host: true |
21 | | - end |
22 | | - end |
| 15 | + uid = params[:uid] |
| 16 | + user = User.find_by(uid: uid) |
23 | 17 |
|
24 | | - def developer |
25 | | - current_user ||= User.find_or_create_by( |
26 | | - uid: request.env['omniauth.auth'][:uid], provider: :developer |
27 | | - ) |
| 18 | + unless user |
| 19 | + flash[:alert] = "Login attempt failed. User #{uid} does not have an account." |
| 20 | + redirect_to root_path |
| 21 | + return |
| 22 | + end |
28 | 23 |
|
29 | | - sign_in_and_redirect current_user, event: :authentication |
| 24 | + flash[:success] = 'You have succesfully logged in.' |
| 25 | + sign_in_and_redirect user, event: :authentication # this will throw if user is not activated |
30 | 26 | end |
31 | 27 |
|
32 | | - def cas |
33 | | - user_id, _affils = Omniauth::Cul::Cas3.validation_callback(request.params['ticket'], app_cas_callback_endpoint) |
| 28 | + # POST /users/auth/columbia_cas/callback |
| 29 | + def columbia_cas # rubocop:disable Metrics/AbcSize |
| 30 | + callback_url = user_columbia_cas_omniauth_callback_url # The columbia_cas callback route in this application |
| 31 | + uid, _affils = Omniauth::Cul::ColumbiaCas.validation_callback(request.params['ticket'], callback_url) |
34 | 32 |
|
35 | | - user = User.find_by(uid: user_id) || User.create!( |
36 | | - uid: user_id, |
37 | | - email: "#{user_id}@columbia.edu" |
38 | | - ) |
| 33 | + user = User.find_by(uid: uid) || User.create(uid: uid, email: "#{uid}@columbia.edu") |
| 34 | + flash[:success] = 'You have succesfully logged in.' |
39 | 35 | sign_in_and_redirect user, event: :authentication |
| 36 | + rescue Omniauth::Cul::Exceptions::Error => e |
| 37 | + # If an unexpected CAS ticket validation occurs, log the error message and ask the user to try |
| 38 | + # logging in again. Do not display the exception object's original message to the user because it may |
| 39 | + # contain information that only a developer should see. |
| 40 | + error_message = 'CAS login validation failed. Please try again.' |
| 41 | + Rails.logger.debug(error_message + " #{e.class.name}: #{e.message}") |
| 42 | + flash[:alert] = error_message |
| 43 | + redirect_to root_path |
40 | 44 | end |
41 | 45 | end |
0 commit comments