Skip to content

Commit

Permalink
Merge pull request lynndylanhurley#650 from JerryGreen/master
Browse files Browse the repository at this point in the history
Tokens count overmuch fixed
  • Loading branch information
booleanbetrayal committed Jun 2, 2016
2 parents 81fc7b4 + 3943071 commit b892e1b
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions app/models/devise_token_auth/concerns/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,37 +177,32 @@ def create_new_auth_token(client_id=nil)
updated_at: Time.now
}

max_clients = DeviseTokenAuth.max_number_of_devices
while self.tokens.keys.length > 0 and max_clients < self.tokens.keys.length
oldest_token = self.tokens.min_by { |cid, v| v[:expiry] || v["expiry"] }
self.tokens.delete(oldest_token.first)
end

self.save!

return build_auth_header(token, client_id)
end


def build_auth_header(token, client_id='default')
client_id ||= 'default'

if !DeviseTokenAuth.change_headers_on_each_request && self.tokens[client_id].nil?
create_new_auth_token(client_id)
else
# client may use expiry to prevent validation request if expired
# must be cast as string or headers will break
expiry = self.tokens[client_id]['expiry'] || self.tokens[client_id][:expiry]

# client may use expiry to prevent validation request if expired
# must be cast as string or headers will break
expiry = self.tokens[client_id]['expiry'] || self.tokens[client_id][:expiry]

return {
DeviseTokenAuth.headers_names[:"access-token"] => token,
DeviseTokenAuth.headers_names[:"token-type"] => "Bearer",
DeviseTokenAuth.headers_names[:"client"] => client_id,
DeviseTokenAuth.headers_names[:"expiry"] => expiry.to_s,
DeviseTokenAuth.headers_names[:"uid"] => self.uid
}
max_clients = DeviseTokenAuth.max_number_of_devices
while self.tokens.keys.length > 0 and max_clients < self.tokens.keys.length
oldest_token = self.tokens.min_by { |cid, v| v[:expiry] || v["expiry"] }
self.tokens.delete(oldest_token.first)
end

self.save!

return {
DeviseTokenAuth.headers_names[:"access-token"] => token,
DeviseTokenAuth.headers_names[:"token-type"] => "Bearer",
DeviseTokenAuth.headers_names[:"client"] => client_id,
DeviseTokenAuth.headers_names[:"expiry"] => expiry.to_s,
DeviseTokenAuth.headers_names[:"uid"] => self.uid
}
end


Expand All @@ -221,7 +216,6 @@ def build_auth_url(base_url, args)

def extend_batch_buffer(token, client_id)
self.tokens[client_id]['updated_at'] = Time.now
self.save!

return build_auth_header(token, client_id)
end
Expand Down

0 comments on commit b892e1b

Please sign in to comment.