Sha256: 236f63980fb2703016bb0634191f0002a3acc5c6c32ef2e4e50383e2dae88a93

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

module DeviseOam
  module Devise
    module Strategies
      class HeaderAuthenticatable < ::Devise::Strategies::Base
        attr_reader :authenticatable
        
        def valid?
          # this strategy is only valid if there is a DeviseOam.oam_header header in the request
          request.headers[DeviseOam.oam_header]
        end

        def authenticate!         
          failure_message = "OAM authentication failed"
          
          oam_data = request.headers[DeviseOam.oam_header]
          if DeviseOam.ldap_header
            ldap_data = request.headers[DeviseOam.ldap_header] || ""
          end

          if oam_data.blank?
            fail!(failure_message)
          else
            @authenticatable = AuthenticatableEntity.new(oam_data, ldap_data)
            
            user = find_or_create_user
            success!(user)
          end
        end
        
        def set_roles?
          !DeviseOam.ldap_header.blank? && @authenticatable.ldap_roles
        end
        
        private
        
        def find_or_create_user
          user = DeviseOam.user_class.where({ DeviseOam.user_login_field.to_sym => @authenticatable.login }).first
          
          if user.nil? && DeviseOam.create_user_if_not_found
            user = DeviseOam.user_class.send(DeviseOam.create_user_method, { DeviseOam.user_login_field.to_sym => @authenticatable.login, :roles => @authenticatable.ldap_roles })
          elsif user && set_roles?
            user.send(DeviseOam.roles_setter, @authenticatable.ldap_roles)
          end
          
          user
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
devise_oam-0.0.2 lib/devise_oam/strategies/header_authenticatable.rb
devise_oam-0.0.1 lib/devise_oam/strategies/header_authenticatable.rb