Module: Softwear::Auth::TokenAuthentication

Extended by:
ActiveSupport::Concern
Defined in:
lib/softwear/auth/token_authentication.rb

Instance Method Summary collapse

Instance Method Details

#token_authenticate_user!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/softwear/auth/token_authentication.rb', line 11

def token_authenticate_user!
  user_class = self.class.user_class || base_class.user_class || User
  options    = (self.class.token_auth_options || base_class.token_auth_options || {}).with_indifferent_access
  params_options  = (options[:params]  || {}).with_indifferent_access
  headers_options = (options[:headers] || {}).with_indifferent_access

  email_param  = params_options[:email]                 || 'user_email'
  token_param  = params_options[:authentication_token]  || 'user_token'
  email_header = headers_options[:email]                || 'X-User-Email'
  token_header = headers_options[:authentication_token] || 'X-User-Token'

  email = params[email_param] || request.headers[email_header]
  token = params[token_param] || request.headers[token_header]

  return render_unauthorized if email.blank? || token.blank?

  case user_class.query "token #{Figaro.env.hub_app_name} #{email} #{token}"
  when 'no'      then render_unauthorized
  when 'invaild' then render_unauthorized
  when 'sorry'   then render_internal_server_error
  when 'yes'     then true
  end
end