Skip to content

Commit

Permalink
Adding mongoid support
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Aguilar authored and Carlos Aguilar committed Feb 13, 2015
1 parent 6e6476b commit 2390908
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
42 changes: 21 additions & 21 deletions app/controllers/devise_token_auth/concerns/set_user_by_token.rb
Expand Up @@ -54,34 +54,34 @@ def update_auth_header

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

auth_header = {}
auth_header = {}

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

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

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

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

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

end # end lock
#end # end lock

end

Expand All @@ -102,6 +102,6 @@ def resource_class(m=nil)
def is_batch_request?(user, client_id)
user.tokens[client_id] and
user.tokens[client_id]['updated_at'] and
Time.parse(user.tokens[client_id]['updated_at']) > @request_started_at - DeviseTokenAuth.batch_request_buffer_throttle
user.tokens[client_id]['updated_at'] > @request_started_at - DeviseTokenAuth.batch_request_buffer_throttle
end
end
15 changes: 9 additions & 6 deletions app/models/devise_token_auth/concerns/user.rb
Expand Up @@ -10,7 +10,7 @@ module DeviseTokenAuth::Concerns::User
self.devise_modules.delete(:omniauthable)
end

if self.method_defined?(:serialize)
if respond_to?(:serialize)
serialize :tokens, JSON
end

Expand All @@ -20,10 +20,13 @@ module DeviseTokenAuth::Concerns::User
# only validate unique emails among email registration users
validate :unique_email_user, on: :create

if self.method_defined?(:serialize)
# can't set default on text fields in mysql, simulate here instead.
after_save :set_empty_token_hash
after_initialize :set_empty_token_hash

# can't set default on text fields in mysql, simulate here instead.
if ActiveRecord::Base.connected?
if ['mysql', 'sqlite', 'postgresql'].include?(ActiveRecord::Base.connection.adapter_name.downcase)
after_save :set_empty_token_hash
after_initialize :set_empty_token_hash
end
end

# keep uid in sync with email
Expand Down Expand Up @@ -132,7 +135,7 @@ def token_can_be_reused?(token, client_id)
updated_at and last_token and

# ensure that previous token falls within the batch buffer throttle time of the last request
Time.parse(updated_at) > Time.now - DeviseTokenAuth.batch_request_buffer_throttle and
updated_at > Time.now - DeviseTokenAuth.batch_request_buffer_throttle and

# ensure that the token is valid
BCrypt::Password.new(last_token) == token
Expand Down

0 comments on commit 2390908

Please sign in to comment.