class CasAuthentication

Authenticate User and Create Session

Constants

USER_CAS_MAP

Attributes

session[R]

Public Class Methods

new(session) click to toggle source
# File lib/components/cas_authentication.rb, line 4
def initialize(session)
  @session = session
end

Public Instance Methods

perform() click to toggle source
# File lib/components/cas_authentication.rb, line 13
def perform
  return true if authenticated?
  return unless session['cas'].present?

  if new_user?
    authenticate! if create_user!
  elsif unauthenticated?
    authenticate!
    update_extra_attributes!
  end
end
user() click to toggle source
# File lib/components/cas_authentication.rb, line 8
def user
  return unless username.present?
  @user ||= User.find_or_initialize_by(username: username)
end

Private Instance Methods

attr_value(value, type = nil) click to toggle source
# File lib/components/cas_authentication.rb, line 83
def attr_value(value, type = nil)
  return Array(value).compact if type == Array
  return Integer(value) if type == Integer &&
                           /\A\d+\z/ =~ value.to_s
  String(value)
end
attrs() click to toggle source
# File lib/components/cas_authentication.rb, line 75
def attrs
  @attrs ||= (session[:cas] || {}).with_indifferent_access
end
authenticate!() click to toggle source
# File lib/components/cas_authentication.rb, line 56
def authenticate!
  session[:username] = username
end
authenticated?() click to toggle source
# File lib/components/cas_authentication.rb, line 48
def authenticated?
  session[:username].present?
end
create_user!()
extra_attrs() click to toggle source
# File lib/components/cas_authentication.rb, line 79
def extra_attrs
  @extra_attrs ||= (attrs[:extra_attributes] || {}).with_indifferent_access
end
new_user?() click to toggle source
# File lib/components/cas_authentication.rb, line 43
def new_user?
  return unless user.present?
  user.new_record?
end
present?() click to toggle source
# File lib/components/cas_authentication.rb, line 39
def present?
  session['cas'].present?
end
unauthenticated?() click to toggle source
# File lib/components/cas_authentication.rb, line 52
def unauthenticated?
  !authenticated?
end
update_extra_attributes!() click to toggle source
# File lib/components/cas_authentication.rb, line 60
def update_extra_attributes!
  USER_CAS_MAP.each do |attr_key, opt|
    next unless extra_attrs.key?(attr_key)
    key, type = user_key_type(opt)
    user[key] = attr_value(extra_attrs[attr_key], type)
  end

  user.save
end
Also aliased as: create_user!
user_key_type(opt) click to toggle source
# File lib/components/cas_authentication.rb, line 90
def user_key_type(opt)
  return opt if opt.is_a? Symbol
  opt.first
end
username() click to toggle source
# File lib/components/cas_authentication.rb, line 71
def username
  (session[:username] || attrs[:user]).try(:downcase)
end