Sha256: b7c2704a2e953b24c345f7a14268f0d551b8177bd12a4a47a94169db8272a682

Contents?: true

Size: 1.22 KB

Versions: 19

Compression:

Stored size: 1.22 KB

Contents

module ScimRails
  class AuthorizeApiRequest

    def initialize(searchable_attribute:, authentication_attribute:)
      @searchable_attribute = searchable_attribute
      @authentication_attribute = authentication_attribute

      raise ScimRails::ExceptionHandler::InvalidCredentials if searchable_attribute.blank? || authentication_attribute.blank?

      @search_parameter = { ScimRails.config.basic_auth_model_searchable_attribute => @searchable_attribute }
    end

    def company
      company = find_company
      authorize(company)
      company
    end

    private

    attr_reader :authentication_attribute
    attr_reader :search_parameter
    attr_reader :searchable_attribute

    def find_company
      @company ||= ScimRails.config.basic_auth_model.find_by!(search_parameter)

    rescue ActiveRecord::RecordNotFound
      raise ScimRails::ExceptionHandler::InvalidCredentials
    end

    def authorize(authentication_model)
      authorized = ActiveSupport::SecurityUtils.secure_compare(
        authentication_model.public_send(ScimRails.config.basic_auth_model_authenticatable_attribute),
        authentication_attribute
      )
      raise ScimRails::ExceptionHandler::InvalidCredentials unless authorized
    end
  end
end

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
scimaenaga-0.9.0 app/models/scim_rails/authorize_api_request.rb
scimaenaga-0.8.0 app/models/scim_rails/authorize_api_request.rb
scimaenaga-0.7.0 app/models/scim_rails/authorize_api_request.rb
scimaenaga-0.6.2 app/models/scim_rails/authorize_api_request.rb
scimaenaga-0.6.1 app/models/scim_rails/authorize_api_request.rb
scimaenaga-0.6.0 app/models/scim_rails/authorize_api_request.rb
scimaenaga-0.5.0 app/models/scim_rails/authorize_api_request.rb
scimaenaga-0.4.1 app/models/scim_rails/authorize_api_request.rb
scim_rails-0.4.0 app/models/scim_rails/authorize_api_request.rb
scim_rails-0.3.1 app/models/scim_rails/authorize_api_request.rb
scim_rails-0.3.0 app/models/scim_rails/authorize_api_request.rb
scim_rails-0.2.2 app/models/scim_rails/authorize_api_request.rb
scim_rails-0.2.1 app/models/scim_rails/authorize_api_request.rb
scim_rails-0.2.0 app/models/scim_rails/authorize_api_request.rb
scim_rails-0.1.4 app/models/scim_rails/authorize_api_request.rb
scim_rails-0.1.3 app/models/scim_rails/authorize_api_request.rb
scim_rails-0.1.2 app/models/scim_rails/authorize_api_request.rb
scim_rails-0.1.1 app/models/scim_rails/authorize_api_request.rb
scim_rails-0.1.0 app/models/scim_rails/authorize_api_request.rb