Skip to content

Commit

Permalink
Merge pull request #11346 from tomykaira/fix_10257
Browse files Browse the repository at this point in the history
Check authentication scheme in Basic auth
  • Loading branch information
rafaelfranca committed May 20, 2014
2 parents 52b5586 + 15a98a8 commit ef00bb7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
16 changes: 14 additions & 2 deletions actionpack/lib/action_controller/metal/http_authentication.rb
Expand Up @@ -90,17 +90,29 @@ def request_http_basic_authentication(realm = "Application")
end

def authenticate(request, &login_procedure)
unless request.authorization.blank?
if has_basic_credentials?(request)
login_procedure.call(*user_name_and_password(request))
end
end

def has_basic_credentials?(request)
request.authorization.present? && (auth_scheme(request) == 'Basic')
end

def user_name_and_password(request)
decode_credentials(request).split(':', 2)
end

def decode_credentials(request)
::Base64.decode64(request.authorization.split(' ', 2).last || '')
::Base64.decode64(auth_param(request) || '')
end

def auth_scheme(request)
request.authorization.split(' ', 2).first
end

def auth_param(request)
request.authorization.split(' ', 2).second
end

def encode_credentials(user_name, password)
Expand Down
7 changes: 7 additions & 0 deletions actionpack/test/controller/http_basic_authentication_test.rb
Expand Up @@ -129,6 +129,13 @@ def test_encode_credentials_has_no_newline
assert_response :unauthorized
end

test "authentication request with wrong scheme" do
header = 'Bearer ' + encode_credentials('David', 'Goliath').split(' ', 2)[1]
@request.env['HTTP_AUTHORIZATION'] = header
get :search
assert_response :unauthorized
end

private

def encode_credentials(username, password)
Expand Down

0 comments on commit ef00bb7

Please sign in to comment.