Skip to content

Commit 1e442ef

Browse files
rafaelfrancamatthewd
authored andcommitted
Merge pull request rails#16011 from xjlu/token_and_options
Improve token_and_options regex and test
1 parent 65121a9 commit 1e442ef

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

actionpack/lib/action_controller/metal/http_authentication.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ def rewrite_param_values(array_params)
459459
# pairs by the standardized `:`, `;`, or `\t` delimiters defined in
460460
# `AUTHN_PAIR_DELIMITERS`.
461461
def raw_params(auth)
462-
auth.sub(TOKEN_REGEX, '').split(/"\s*#{AUTHN_PAIR_DELIMITERS}\s*/)
462+
auth.sub(TOKEN_REGEX, '').split(/\s*#{AUTHN_PAIR_DELIMITERS}\s*/)
463463
end
464464

465465
# Encodes the given token and options into an Authorization header value.

actionpack/test/controller/http_token_authentication_test.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,36 @@ def authenticate_long_credentials
139139
assert_equal(expected, actual)
140140
end
141141

142+
test "token_and_options returns correct token with nounce option" do
143+
token = "rcHu+HzSFw89Ypyhn/896A="
144+
nonce_hash = {nonce: "123abc"}
145+
actual = ActionController::HttpAuthentication::Token.token_and_options(sample_request(token, nonce_hash))
146+
expected_token = token
147+
expected_nonce = {"nonce" => nonce_hash[:nonce]}
148+
assert_equal(expected_token, actual.first)
149+
assert_equal(expected_nonce, actual.last)
150+
end
151+
142152
test "token_and_options returns nil with no value after the equal sign" do
143153
actual = ActionController::HttpAuthentication::Token.token_and_options(malformed_request).first
144154
expected = nil
145155
assert_equal(expected, actual)
146156
end
147157

158+
test "raw_params returns a tuple of two key value pair strings" do
159+
auth = sample_request("rcHu+HzSFw89Ypyhn/896A=").authorization.to_s
160+
actual = ActionController::HttpAuthentication::Token.raw_params(auth)
161+
expected = ["token=\"rcHu+HzSFw89Ypyhn/896A=\"", "nonce=\"def\""]
162+
assert_equal(expected, actual)
163+
end
164+
148165
private
149166

150-
def sample_request(token)
151-
@sample_request ||= OpenStruct.new authorization: %{Token token="#{token}", nonce="def"}
167+
def sample_request(token, options = {nonce: "def"})
168+
authorization = options.inject([%{Token token="#{token}"}]) do |arr, (k, v)|
169+
arr << "#{k}=\"#{v}\""
170+
end.join(", ")
171+
@sample_request ||= OpenStruct.new authorization: authorization
152172
end
153173

154174
def malformed_request

0 commit comments

Comments
 (0)