Module: DeviseTokenAuth::Concerns::SetUserByToken

Extended by:
ActiveSupport::Concern
Includes:
DeviseTokenAuth::Controllers::Helpers
Included in:
ApplicationController
Defined in:
app/controllers/devise_token_auth/concerns/set_user_by_token.rb

Instance Method Summary collapse

Methods included from DeviseTokenAuth::Controllers::Helpers

define_helpers

Instance Method Details

#mappingObject



86
87
88
# File 'app/controllers/devise_token_auth/concerns/set_user_by_token.rb', line 86

def mapping
  @mapping ||= request.env['devise.mapping'] || Devise.mappings.values.first
end

#mapping=(m) ⇒ Object



90
91
92
# File 'app/controllers/devise_token_auth/concerns/set_user_by_token.rb', line 90

def mapping=(m)
  @mapping = Devise.mappings[m]
end

#resource_classObject



94
95
96
# File 'app/controllers/devise_token_auth/concerns/set_user_by_token.rb', line 94

def resource_class
  mapping.to
end

#resource_nameObject



98
99
100
# File 'app/controllers/devise_token_auth/concerns/set_user_by_token.rb', line 98

def resource_name
  mapping.name
end

#set_request_startObject

keep track of request duration



11
12
13
# File 'app/controllers/devise_token_auth/concerns/set_user_by_token.rb', line 11

def set_request_start
  @request_started_at = Time.now
end

#set_user_by_token(mapping = nil) ⇒ Object

user auth



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/devise_token_auth/concerns/set_user_by_token.rb', line 16

def set_user_by_token(mapping=nil)

  # determine target authentication class
  self.mapping = mapping
  rc = resource_class

  # no default user defined
  return unless rc

  # user has already been found and authenticated
  return @user if @user and @user.class == rc

  # parse header for values necessary for authentication
  uid        = request.headers['uid']
  @token     = request.headers['access-token']
  @client_id = request.headers['client']

  return false unless @token

  # client_id isn't required, set to 'default' if absent
  @client_id ||= 'default'

  # mitigate timing attacks by finding by uid instead of auth token
  user = uid && rc.find_by_uid(uid)

  if user && user.valid_token?(@token, @client_id)
    (resource_name, user, store: false)
    return @user = user
  else
    # zero all values previously set values
    return @user = nil
  end
end

#update_auth_headerObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/devise_token_auth/concerns/set_user_by_token.rb', line 51

def update_auth_header

  # cannot save object if model has invalid params
  return unless @user and @user.valid? and @client_id

  # Lock the user record during any auth_header updates to ensure 
  # we don't have write contention from multiple threads
  @user.with_lock do
    
    # determine batch request status after request processing, in case 
    # another processes has updated it during that processing
    @is_batch_request = is_batch_request?(@user, @client_id)

    auth_header = {}

    if not DeviseTokenAuth.change_headers_on_each_request
      auth_header = @user.build_auth_header(@token, @client_id)

    # extend expiration of batch buffer to account for the duration of
    # this request
    elsif @is_batch_request
      auth_header = @user.extend_batch_buffer(@token, @client_id)

    # update Authorization response header with new token
    else
      auth_header = @user.create_new_auth_token(@client_id)
    end

    # update the response header
    response.headers.merge!(auth_header)

  end # end lock

end