Sha256: 6c1d134a23fcea7c991c4d12e8e33dd7e11e3e6e70be045840bdb667a2291b28

Contents?: true

Size: 1.85 KB

Versions: 6

Compression:

Stored size: 1.85 KB

Contents

module Devise
  module Models
    # Extends your User class with support for CAS ticket authentication.
    module CasAuthenticatable
      def self.included(base)
        base.extend ClassMethods
      end
      
      module ClassMethods
        # Authenticate a CAS ticket and return the resulting user object.  Behavior is as follows:
        # 
        # * Check ticket validity using RubyCAS::Client.  Return nil if the ticket is invalid.
        # * Find a matching user by username (will use find_for_authentication if available).
        # * If the user does not exist, but Devise.cas_create_user is set, attempt to create the
        #   user object in the database.  If cas_extra_attributes= is defined, this will also
        #   pass in the ticket's extra_attributes hash.
        # * Return the resulting user object.
        def authenticate_with_cas_ticket(ticket)
          ::Devise.cas_client.validate_service_ticket(ticket) unless ticket.has_been_validated?
          
          if ticket.is_valid?
           conditions = {::Devise.cas_username_column => ticket.respond_to?(:user) ? ticket.user : ticket.response.user} 
            # We don't want to override Devise 1.1's find_for_authentication
            resource = if respond_to?(:find_for_authentication)
              find_for_authentication(conditions)
            else
              find(:first, :conditions => conditions)
            end
            
            resource = new(conditions) if (resource.nil? and ::Devise.cas_create_user?)
            return nil unless resource
            
            if resource.respond_to? :cas_extra_attributes=
              resource.cas_extra_attributes = ticket.respond_to?(:extra_attributes) ? ticket.extra_attributes : ticket.response.extra_attributes
            end
            resource.save
            resource
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
devise_cas_authenticatable-1.0.1 lib/devise_cas_authenticatable/model.rb
devise_cas_authenticatable-1.1.2 lib/devise_cas_authenticatable/model.rb
devise_cas_authenticatable-1.1.1 lib/devise_cas_authenticatable/model.rb
devise_cas_authenticatable-1.1.0 lib/devise_cas_authenticatable/model.rb
devise_cas_authenticatable-1.0.0 lib/devise_cas_authenticatable/model.rb
devise_cas_authenticatable-1.0.0.alpha13 lib/devise_cas_authenticatable/model.rb