Module: DeviseTokenAuth::Concerns::SetUserByToken

Extended by:
ActiveSupport::Concern
Included in:
ApplicationController, PasswordsController, SessionsController
Defined in:
app/controllers/devise_token_auth/concerns/set_user_by_token.rb

Instance Method Summary collapse

Instance Method Details

#resource_class ⇒ Object



68
69
70
71
# File 'app/controllers/devise_token_auth/concerns/set_user_by_token.rb', line 68

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

#set_user_by_token ⇒ Object

user auth



10
11
12
13
14
15
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
# File 'app/controllers/devise_token_auth/concerns/set_user_by_token.rb', line 10

def set_user_by_token
  auth_header = request.headers["Authorization"]

  #binding.pry_remote

  # missing auth token
  return false unless auth_header

  # no default user defined
  return false unless resource_class

  # parse header for values necessary for authentication
  uid        = auth_header[/uid=(.*?)$/,1]
  @token     = auth_header[/token=(.*?) /,1]
  @client_id = auth_header[/client=(.*?) /,1]

  # 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 = @current_user = uid && resource_class.find_by_uid(uid)

  if @user && @user.valid_token?(@token, @client_id)
    (:user, @user, store: false, bypass: true)

    # check this now so that the duration of the request itself doesn't eat
    # away the buffer
    @is_batch_request = is_batch_request?(@user, @client_id)

  else
    # zero all values previously set values
    @user = @current_user = @is_batch_request = nil
  end
end

#update_auth_header ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/devise_token_auth/concerns/set_user_by_token.rb', line 46

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

  auth_header = nil

  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

  response.headers["Authorization"] = auth_header if auth_header
end