class SimpleTokenAuthentication::Entity

Public Class Methods

new(model, model_alias=nil) click to toggle source
# File lib/simple_token_authentication/entity.rb, line 3
def initialize(model, model_alias=nil)
  @model = model
  @name = model.name
  @name_underscore = model_alias.to_s unless model_alias.nil?
end

Public Instance Methods

get_identifier_from_params_or_headers(controller) click to toggle source
# File lib/simple_token_authentication/entity.rb, line 65
def get_identifier_from_params_or_headers controller
  # if the identifier is not present among params, get it from headers
  if identifer_param = controller.params[identifier_param_name].blank? && controller.request.headers[identifier_header_name]
    controller.params[identifier_param_name] = identifer_param
  end
  controller.params[identifier_param_name]
end
get_token_from_params_or_headers(controller) click to toggle source
# File lib/simple_token_authentication/entity.rb, line 57
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
identifier() click to toggle source
# File lib/simple_token_authentication/entity.rb, line 49
def identifier
  if custom_identifier = SimpleTokenAuthentication.identifiers["#{name_underscore}".to_sym]
    custom_identifier.to_sym
  else
    :email
  end
end
identifier_header_name() click to toggle source

Private: Return the name of the header to watch for the email param

# File lib/simple_token_authentication/entity.rb, line 32
def identifier_header_name
  if SimpleTokenAuthentication.header_names["#{name_underscore}".to_sym].presence \
    && identifier_header_name = SimpleTokenAuthentication.header_names["#{name_underscore}".to_sym][identifier]
    identifier_header_name
  else
    "X-#{name_underscore.camelize}-#{identifier.to_s.camelize}"
  end
end
identifier_param_name() click to toggle source
# File lib/simple_token_authentication/entity.rb, line 45
def identifier_param_name
  "#{name_underscore}_#{identifier}".to_sym
end
model() click to toggle source
# File lib/simple_token_authentication/entity.rb, line 9
def model
  @model
end
name() click to toggle source
# File lib/simple_token_authentication/entity.rb, line 13
def name
  @name
end
name_underscore() click to toggle source
# File lib/simple_token_authentication/entity.rb, line 17
def name_underscore
  @name_underscore || name.underscore
end
token_header_name() click to toggle source

Private: Return the name of the header to watch for the token authentication param

# File lib/simple_token_authentication/entity.rb, line 22
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_underscore.camelize}-Token"
  end
end
token_param_name() click to toggle source
# File lib/simple_token_authentication/entity.rb, line 41
def token_param_name
  "#{name_underscore}_token".to_sym
end