Class: SimpleTokenAuthentication::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_token_authentication/entity.rb

Instance Method Summary collapse

Constructor Details

#initialize(model, model_alias = nil) ⇒ Entity

Returns a new instance of Entity.



3
4
5
6
7
# 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

Instance Method Details

#get_identifier_from_params_or_headers(controller) ⇒ Object



65
66
67
68
69
70
71
# 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) ⇒ Object



57
58
59
60
61
62
63
# 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

#identifierObject



49
50
51
52
53
54
55
# 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_nameObject

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



32
33
34
35
36
37
38
39
# 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_nameObject



45
46
47
# File 'lib/simple_token_authentication/entity.rb', line 45

def identifier_param_name
  "#{name_underscore}_#{identifier}".to_sym
end

#modelObject



9
10
11
# File 'lib/simple_token_authentication/entity.rb', line 9

def model
  @model
end

#nameObject



13
14
15
# File 'lib/simple_token_authentication/entity.rb', line 13

def name
  @name
end

#name_underscoreObject



17
18
19
# File 'lib/simple_token_authentication/entity.rb', line 17

def name_underscore
  @name_underscore || name.underscore
end

#token_header_nameObject

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



22
23
24
25
26
27
28
29
# 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_nameObject



41
42
43
# File 'lib/simple_token_authentication/entity.rb', line 41

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