Sha256: 6ddd8b577455f1b55558d153bccac4a32b2188614fd4a4f4c5fcffc836fc8e33

Contents?: true

Size: 1.89 KB

Versions: 3

Compression:

Stored size: 1.89 KB

Contents

module SimpleTokenAuthentication
  class Entity
    def initialize model
      @model = model
      @name = model.name
    end

    def model
      @model
    end

    def name
      @name
    end

    def name_underscore
      name.underscore
    end

    # Private: Return the name of the header to watch for the token authentication param
    def token_header_name
      if SimpleTokenAuthentication.header_names["#{name_underscore}".to_sym].presence \
        && token_header_name = SimpleTokenAuthentication.header_names["#{name_underscore}".to_sym][:authentication_token]
        token_header_name
      else
        "X-#{name}-Token"
      end
    end

    # Private: Return the name of the header to watch for the email param
    def identifier_header_name
      if SimpleTokenAuthentication.header_names["#{name_underscore}".to_sym].presence \
        && identifier_header_name = SimpleTokenAuthentication.header_names["#{name_underscore}".to_sym][:email]
        identifier_header_name
      else
        "X-#{name}-Email"
      end
    end

    def token_param_name
      "#{name_underscore}_token".to_sym
    end

    def identifier_param_name
      "#{name_underscore}_email".to_sym
    end

    def get_token_from_params_or_headers controller
      # if the token is not present among params, get it from headers
      if token = controller.params[token_param_name].blank? && controller.request.headers[token_header_name]
        controller.params[token_param_name] = token
      end
      controller.params[token_param_name]
    end

    def get_identifier_from_params_or_headers controller
      # if the identifier (email) is not present among params, get it from headers
      if email = controller.params[identifier_param_name].blank? && controller.request.headers[identifier_header_name]
        controller.params[identifier_param_name] = email
      end
      controller.params[identifier_param_name]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
simple_token_authentication-1.7.0 lib/simple_token_authentication/entity.rb
simple_token_authentication-1.6.0 lib/simple_token_authentication/entity.rb
simple_token_authentication-1.5.2 lib/simple_token_authentication/entity.rb