Class: SimpleTokenAuthentication::Entity

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

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Entity

Returns a new instance of Entity



3
4
5
6
# File 'lib/simple_token_authentication/entity.rb', line 3

def initialize model
  @model = model
  @name = model.name
end

Instance Method Details

#get_identifier_from_params_or_headers(controller) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/simple_token_authentication/entity.rb', line 64

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



56
57
58
59
60
61
62
# File 'lib/simple_token_authentication/entity.rb', line 56

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



48
49
50
51
52
53
54
# File 'lib/simple_token_authentication/entity.rb', line 48

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



31
32
33
34
35
36
37
38
# File 'lib/simple_token_authentication/entity.rb', line 31

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}-#{identifier.to_s.camelize}"
  end
end

#identifier_param_nameObject



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

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

#modelObject



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

def model
  @model
end

#nameObject



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

def name
  @name
end

#name_underscoreObject



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

def name_underscore
  name.underscore
end

#token_header_nameObject

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



21
22
23
24
25
26
27
28
# File 'lib/simple_token_authentication/entity.rb', line 21

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

#token_param_nameObject



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

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