Skip to content

Commit e2a323d

Browse files
committed
Merge pull request rails#17186 from tgxworld/header_authentication_token
Allow authentication header to not have to specify 'token=' key. Conflicts: actionpack/CHANGELOG.md
1 parent 1e442ef commit e2a323d

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

actionpack/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Restore handling of a bare `Authorization` header, without `token=`
2+
prefix.
3+
4+
Fixes #17108.
5+
6+
*Guo Xiang Tan*
7+
8+
19
## Rails 4.0.12 (November 16, 2014) ##
210

311
* Fix a bug where malformed query strings lead to 500.

actionpack/lib/action_controller/metal/http_authentication.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ def opaque(secret_key)
385385
#
386386
# RewriteRule ^(.*)$ dispatch.fcgi [E=X-HTTP_AUTHORIZATION:%{HTTP:Authorization},QSA,L]
387387
module Token
388+
TOKEN_KEY = 'token='
388389
TOKEN_REGEX = /^Token /
389390
AUTHN_PAIR_DELIMITERS = /(?:,|;|\t+)/
390391
extend self
@@ -459,7 +460,13 @@ def rewrite_param_values(array_params)
459460
# pairs by the standardized `:`, `;`, or `\t` delimiters defined in
460461
# `AUTHN_PAIR_DELIMITERS`.
461462
def raw_params(auth)
462-
auth.sub(TOKEN_REGEX, '').split(/\s*#{AUTHN_PAIR_DELIMITERS}\s*/)
463+
_raw_params = auth.sub(TOKEN_REGEX, '').split(/\s*#{AUTHN_PAIR_DELIMITERS}\s*/)
464+
465+
if !(_raw_params.first =~ %r{\A#{TOKEN_KEY}})
466+
_raw_params[0] = "#{TOKEN_KEY}#{_raw_params.first}"
467+
end
468+
469+
_raw_params
463470
end
464471

465472
# Encodes the given token and options into an Authorization header value.
@@ -469,7 +476,7 @@ def raw_params(auth)
469476
#
470477
# Returns String.
471478
def encode_credentials(token, options = {})
472-
values = ["token=#{token.to_s.inspect}"] + options.map do |key, value|
479+
values = ["#{TOKEN_KEY}#{token.to_s.inspect}"] + options.map do |key, value|
473480
"#{key}=#{value.to_s.inspect}"
474481
end
475482
"Token #{values * ", "}"

actionpack/test/controller/http_token_authentication_test.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,36 @@ def authenticate_long_credentials
162162
assert_equal(expected, actual)
163163
end
164164

165+
test "token_and_options returns right token when token key is not specified in header" do
166+
token = "rcHu+HzSFw89Ypyhn/896A="
167+
168+
actual = ActionController::HttpAuthentication::Token.token_and_options(
169+
sample_request_without_token_key(token)
170+
).first
171+
172+
expected = token
173+
assert_equal(expected, actual)
174+
end
175+
165176
private
166177

167178
def sample_request(token, options = {nonce: "def"})
168179
authorization = options.inject([%{Token token="#{token}"}]) do |arr, (k, v)|
169180
arr << "#{k}=\"#{v}\""
170181
end.join(", ")
171-
@sample_request ||= OpenStruct.new authorization: authorization
182+
mock_authorization_request(authorization)
172183
end
173184

174185
def malformed_request
175-
@malformed_request ||= OpenStruct.new authorization: %{Token token=}
186+
mock_authorization_request(%{Token token=})
187+
end
188+
189+
def sample_request_without_token_key(token)
190+
mock_authorization_request(%{Token #{token}})
191+
end
192+
193+
def mock_authorization_request(authorization)
194+
OpenStruct.new(authorization: authorization)
176195
end
177196

178197
def encode_credentials(token, options = {})

0 commit comments

Comments
 (0)