Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple authentication_keys on login #1004

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions app/controllers/devise_token_auth/concerns/resource_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,26 @@ def get_case_insensitive_field_from_resource_params(field)
q_value
end

def find_resource(field, value)
# fix for mysql default case insensitivity
q = "#{field.to_s} = ? AND provider='#{provider.to_s}'"
if ActiveRecord::Base.connection.adapter_name.downcase.starts_with? 'mysql'
q = "BINARY " + q
def find_resource

fields = (resource_params.keys.map(&:to_sym) & resource_class.authentication_keys)

conditions = []
values = {}
fields.each do |field|
condition = " #{field.to_s} = :#{field.to_s} "
# fix for mysql default case insensitivity
if ActiveRecord::Base.connection.adapter_name.downcase.starts_with? 'mysql'
condition = "BINARY " + condition
end
conditions.push(condition)
values[field.to_sym] = get_case_insensitive_field_from_resource_params(field)
end

@resource = resource_class.where(q, value).first
conditions.push(' provider = :provider')
values[:provider] = provider.to_s

@resource = resource_class.where([conditions.join(' AND '), values]).first
end

def resource_class(m=nil)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/devise_token_auth/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def create
end

@email = get_case_insensitive_field_from_resource_params(:email)
@resource = find_resource(:uid, @email)
@resource = find_resource

@errors = nil
@error_status = 400
Expand Down
10 changes: 2 additions & 8 deletions app/controllers/devise_token_auth/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@ def new

def create
# Check
field = (resource_params.keys.map(&:to_sym) & resource_class.authentication_keys).first

@resource = nil
if field
q_value = get_case_insensitive_field_from_resource_params(field)
@resource = find_resource

@resource = find_resource(field, q_value)
end

if @resource && valid_params?(field, q_value) && ([email protected]_to?(:active_for_authentication?) || @resource.active_for_authentication?)
if @resource && ([email protected]_to?(:active_for_authentication?) || @resource.active_for_authentication?)
valid_password = @resource.valid_password?(resource_params[:password])
if (@resource.respond_to?(:valid_for_authentication?) && [email protected]_for_authentication? { valid_password }) || !valid_password
render_create_error_bad_credentials
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/devise_token_auth/unlocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def create
end

@email = get_case_insensitive_field_from_resource_params(:email)
@resource = find_resource(:email, @email)
@resource = find_resource

@errors = nil
@error_status = 400
Expand Down