Skip to content

Commit

Permalink
deprecate ActiveSupport::Base64
Browse files Browse the repository at this point in the history
extend and define ::Base64 if needed
  • Loading branch information
lest committed Jan 2, 2012
1 parent 6e9cd38 commit 5f09414
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 63 deletions.
10 changes: 5 additions & 5 deletions actionpack/lib/action_controller/metal/http_authentication.rb
@@ -1,4 +1,4 @@
require 'active_support/base64'
require 'base64'
require 'active_support/core_ext/object/blank'

module ActionController
Expand Down Expand Up @@ -141,11 +141,11 @@ def user_name_and_password(request)
end

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

def encode_credentials(user_name, password)
"Basic #{ActiveSupport::Base64.encode64s("#{user_name}:#{password}")}"
"Basic #{::Base64.strict_encode64("#{user_name}:#{password}")}"
end

def authentication_request(controller, realm)
Expand Down Expand Up @@ -286,7 +286,7 @@ def nonce(secret_key, time = Time.now)
t = time.to_i
hashed = [t, secret_key]
digest = ::Digest::MD5.hexdigest(hashed.join(":"))
ActiveSupport::Base64.encode64("#{t}:#{digest}").gsub("\n", '')
::Base64.encode64("#{t}:#{digest}").gsub("\n", '')
end

# Might want a shorter timeout depending on whether the request
Expand All @@ -295,7 +295,7 @@ def nonce(secret_key, time = Time.now)
# allow a user to use new nonce without prompting user again for their
# username and password.
def validate_nonce(secret_key, request, value, seconds_to_timeout=5*60)
t = ActiveSupport::Base64.decode64(value).split(":").first.to_i
t = ::Base64.decode64(value).split(":").first.to_i
nonce(secret_key, t) == value && (t - Time.now.to_i).abs <= seconds_to_timeout
end

Expand Down
Expand Up @@ -132,6 +132,6 @@ def test_encode_credentials_has_no_newline
private

def encode_credentials(username, password)
"Basic #{ActiveSupport::Base64.encode64("#{username}:#{password}")}"
"Basic #{::Base64.encode64("#{username}:#{password}")}"
end
end
8 changes: 4 additions & 4 deletions actionpack/test/dispatch/request/xml_params_parsing_test.rb
Expand Up @@ -41,7 +41,7 @@ def call(env)

test "parses single file" do
with_test_routing do
xml = "<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{ActiveSupport::Base64.encode64('ABC')}</avatar></person>"
xml = "<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{::Base64.encode64('ABC')}</avatar></person>"
post "/parse", xml, default_headers
assert_response :ok

Expand All @@ -55,7 +55,7 @@ def call(env)
test "logs error if parsing unsuccessful" do
with_test_routing do
output = StringIO.new
xml = "<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{ActiveSupport::Base64.encode64('ABC')}</avatar></pineapple>"
xml = "<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{::Base64.encode64('ABC')}</avatar></pineapple>"
post "/parse", xml, default_headers.merge('action_dispatch.show_exceptions' => true, 'action_dispatch.logger' => Logger.new(output))
assert_response :error
output.rewind && err = output.read
Expand All @@ -80,8 +80,8 @@ def call(env)
<person>
<name>David</name>
<avatars>
<avatar type='file' name='me.jpg' content_type='image/jpg'>#{ActiveSupport::Base64.encode64('ABC')}</avatar>
<avatar type='file' name='you.gif' content_type='image/gif'>#{ActiveSupport::Base64.encode64('DEF')}</avatar>
<avatar type='file' name='me.jpg' content_type='image/jpg'>#{::Base64.encode64('ABC')}</avatar>
<avatar type='file' name='you.gif' content_type='image/gif'>#{::Base64.encode64('DEF')}</avatar>
</avatars>
</person>
end_body
Expand Down
8 changes: 4 additions & 4 deletions activerecord/lib/active_record/session_store.rb
Expand Up @@ -51,11 +51,11 @@ module ActiveRecord
class SessionStore < ActionDispatch::Session::AbstractStore
module ClassMethods # :nodoc:
def marshal(data)
ActiveSupport::Base64.encode64(Marshal.dump(data)) if data
::Base64.encode64(Marshal.dump(data)) if data
end

def unmarshal(data)
Marshal.load(ActiveSupport::Base64.decode64(data)) if data
Marshal.load(::Base64.decode64(data)) if data
end

def drop_table!
Expand Down Expand Up @@ -169,11 +169,11 @@ def raise_on_session_data_overflow!
# are implemented as class methods that you may override. By default,
# marshaling data is
#
# ActiveSupport::Base64.encode64(Marshal.dump(data))
# ::Base64.encode64(Marshal.dump(data))
#
# and unmarshaling data is
#
# Marshal.load(ActiveSupport::Base64.decode64(data))
# Marshal.load(::Base64.decode64(data))
#
# This marshaling behavior is intended to store the widest range of
# binary session data in a +text+ column. For higher performance,
Expand Down
16 changes: 8 additions & 8 deletions activeresource/test/cases/authorization_test.rb
Expand Up @@ -53,7 +53,7 @@ def test_authorization_header
authorization = authorization_header["Authorization"].to_s.split

assert_equal "Basic", authorization[0]
assert_equal ["david", "test123"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1]
assert_equal ["david", "test123"], ::Base64.decode64(authorization[1]).split(":")[0..1]
end

def test_authorization_header_with_username_but_no_password
Expand All @@ -62,7 +62,7 @@ def test_authorization_header_with_username_but_no_password
authorization = authorization_header["Authorization"].to_s.split

assert_equal "Basic", authorization[0]
assert_equal ["david"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1]
assert_equal ["david"], ::Base64.decode64(authorization[1]).split(":")[0..1]
end

def test_authorization_header_with_password_but_no_username
Expand All @@ -71,7 +71,7 @@ def test_authorization_header_with_password_but_no_username
authorization = authorization_header["Authorization"].to_s.split

assert_equal "Basic", authorization[0]
assert_equal ["", "test123"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1]
assert_equal ["", "test123"], ::Base64.decode64(authorization[1]).split(":")[0..1]
end

def test_authorization_header_with_decoded_credentials_from_url
Expand All @@ -80,7 +80,7 @@ def test_authorization_header_with_decoded_credentials_from_url
authorization = authorization_header["Authorization"].to_s.split

assert_equal "Basic", authorization[0]
assert_equal ["my@email.com", "123"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1]
assert_equal ["my@email.com", "123"], ::Base64.decode64(authorization[1]).split(":")[0..1]
end

def test_authorization_header_explicitly_setting_username_and_password
Expand All @@ -92,7 +92,7 @@ def test_authorization_header_explicitly_setting_username_and_password
authorization = authorization_header["Authorization"].to_s.split

assert_equal "Basic", authorization[0]
assert_equal ["david", "test123"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1]
assert_equal ["david", "test123"], ::Base64.decode64(authorization[1]).split(":")[0..1]
end

def test_authorization_header_explicitly_setting_username_but_no_password
Expand All @@ -102,7 +102,7 @@ def test_authorization_header_explicitly_setting_username_but_no_password
authorization = authorization_header["Authorization"].to_s.split

assert_equal "Basic", authorization[0]
assert_equal ["david"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1]
assert_equal ["david"], ::Base64.decode64(authorization[1]).split(":")[0..1]
end

def test_authorization_header_explicitly_setting_password_but_no_username
Expand All @@ -112,7 +112,7 @@ def test_authorization_header_explicitly_setting_password_but_no_username
authorization = authorization_header["Authorization"].to_s.split

assert_equal "Basic", authorization[0]
assert_equal ["", "test123"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1]
assert_equal ["", "test123"], ::Base64.decode64(authorization[1]).split(":")[0..1]
end

def test_authorization_header_if_credentials_supplied_and_auth_type_is_basic
Expand All @@ -122,7 +122,7 @@ def test_authorization_header_if_credentials_supplied_and_auth_type_is_basic
authorization = authorization_header["Authorization"].to_s.split

assert_equal "Basic", authorization[0]
assert_equal ["david", "test123"], ActiveSupport::Base64.decode64(authorization[1]).split(":")[0..1]
assert_equal ["david", "test123"], ::Base64.decode64(authorization[1]).split(":")[0..1]
end

def test_authorization_header_if_credentials_supplied_and_auth_type_is_digest
Expand Down
57 changes: 31 additions & 26 deletions activesupport/lib/active_support/base64.rb
@@ -1,42 +1,47 @@
begin
require 'base64'
rescue LoadError
end

module ActiveSupport
if defined? ::Base64
Base64 = ::Base64
else
# Base64 provides utility methods for encoding and de-coding binary data
# using a base 64 representation. A base 64 representation of binary data
# consists entirely of printable US-ASCII characters. The Base64 module
# is included in Ruby 1.8, but has been removed in Ruby 1.9.
module Base64
# Encodes a string to its base 64 representation. Each 60 characters of
# output is separated by a newline character.
#
# ActiveSupport::Base64.encode64("Original unencoded string")
# # => "T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw==\n"
def self.encode64(data)
[data].pack("m")
end
# The Base64 module isn't available in ealier versions of Ruby 1.9.
module Base64
# Encodes a string to its base 64 representation. Each 60 characters of
# output is separated by a newline character.
#
# ActiveSupport::Base64.encode64("Original unencoded string")
# # => "T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw==\n"
def self.encode64(data)
[data].pack("m")
end

# Decodes a base 64 encoded string to its original representation.
#
# ActiveSupport::Base64.decode64("T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw==")
# # => "Original unencoded string"
def self.decode64(data)
data.unpack("m").first
end
# Decodes a base 64 encoded string to its original representation.
#
# ActiveSupport::Base64.decode64("T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw==")
# # => "Original unencoded string"
def self.decode64(data)
data.unpack("m").first
end
end
end

unless Base64.respond_to?(:strict_encode64)
# Included in Ruby 1.9
def Base64.strict_encode64(value)
encode64(value).gsub(/\n/, '')
end
end

module ActiveSupport
Base64 = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('ActiveSupport::Base64', '::Base64')

# *DEPRECATED*: Use +Base64.strict_encode64+ instead.
#
# Encodes the value as base64 without the newline breaks. This makes the base64 encoding readily usable as URL parameters
# or memcache keys without further processing.
#
# ActiveSupport::Base64.encode64s("Original unencoded string")
# # => "T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw=="
def Base64.encode64s(value)
ActiveSupport::Deprecation.warn "encode64s " \
"is deprecated. Use Base64.strict_encode64 instead", caller
encode64(value).gsub(/\n/, '')
end
end
6 changes: 3 additions & 3 deletions activesupport/lib/active_support/message_encryptor.rb
@@ -1,5 +1,5 @@
require 'openssl'
require 'active_support/base64'
require 'base64'

module ActiveSupport
# MessageEncryptor is a simple way to encrypt values which get stored somewhere
Expand Down Expand Up @@ -73,12 +73,12 @@ def _encrypt(value)
encrypted_data = cipher.update(@serializer.dump(value))
encrypted_data << cipher.final

[encrypted_data, iv].map {|v| ActiveSupport::Base64.encode64s(v)}.join("--")
[encrypted_data, iv].map {|v| ::Base64.strict_encode64(v)}.join("--")
end

def _decrypt(encrypted_message)
cipher = new_cipher
encrypted_data, iv = encrypted_message.split("--").map {|v| ActiveSupport::Base64.decode64(v)}
encrypted_data, iv = encrypted_message.split("--").map {|v| ::Base64.decode64(v)}

cipher.decrypt
cipher.key = @secret
Expand Down
6 changes: 3 additions & 3 deletions activesupport/lib/active_support/message_verifier.rb
@@ -1,4 +1,4 @@
require 'active_support/base64'
require 'base64'
require 'active_support/core_ext/object/blank'

module ActiveSupport
Expand Down Expand Up @@ -42,14 +42,14 @@ def verify(signed_message)

data, digest = signed_message.split("--")
if data.present? && digest.present? && secure_compare(digest, generate_digest(data))
@serializer.load(ActiveSupport::Base64.decode64(data))
@serializer.load(::Base64.decode64(data))
else
raise InvalidSignature
end
end

def generate(value)
data = ActiveSupport::Base64.encode64s(@serializer.dump(value))
data = ::Base64.strict_encode64(@serializer.dump(value))
"#{data}--#{generate_digest(data)}"
end

Expand Down
8 changes: 4 additions & 4 deletions activesupport/lib/active_support/xml_mini.rb
Expand Up @@ -48,7 +48,7 @@ def content_type
"symbol" => Proc.new { |symbol| symbol.to_s },
"date" => Proc.new { |date| date.to_s(:db) },
"datetime" => Proc.new { |time| time.xmlschema },
"binary" => Proc.new { |binary| ActiveSupport::Base64.encode64(binary) },
"binary" => Proc.new { |binary| ::Base64.encode64(binary) },
"yaml" => Proc.new { |yaml| yaml.to_yaml }
} unless defined?(FORMATTING)

Expand All @@ -64,7 +64,7 @@ def content_type
"boolean" => Proc.new { |boolean| %w(1 true).include?(boolean.strip) },
"string" => Proc.new { |string| string.to_s },
"yaml" => Proc.new { |yaml| YAML::load(yaml) rescue yaml },
"base64Binary" => Proc.new { |bin| ActiveSupport::Base64.decode64(bin) },
"base64Binary" => Proc.new { |bin| ::Base64.decode64(bin) },
"binary" => Proc.new { |bin, entity| _parse_binary(bin, entity) },
"file" => Proc.new { |file, entity| _parse_file(file, entity) }
}
Expand Down Expand Up @@ -148,14 +148,14 @@ def _dasherize(key)
def _parse_binary(bin, entity) #:nodoc:
case entity['encoding']
when 'base64'
ActiveSupport::Base64.decode64(bin)
::Base64.decode64(bin)
else
bin
end
end

def _parse_file(file, entity)
f = StringIO.new(ActiveSupport::Base64.decode64(file))
f = StringIO.new(::Base64.decode64(file))
f.extend(FileLike)
f.original_filename = entity['name']
f.content_type = entity['content_type']
Expand Down
6 changes: 4 additions & 2 deletions activesupport/test/core_ext/base64_ext_test.rb
Expand Up @@ -2,7 +2,9 @@

class Base64Test < Test::Unit::TestCase
def test_no_newline_in_encoded_value
assert_match(/\n/, ActiveSupport::Base64.encode64("oneverylongstringthatwouldnormallybesplitupbynewlinesbytheregularbase64"))
assert_no_match(/\n/, ActiveSupport::Base64.encode64s("oneverylongstringthatwouldnormallybesplitupbynewlinesbytheregularbase64"))
ActiveSupport::Deprecation.silence do
assert_match(/\n/, ActiveSupport::Base64.encode64("oneverylongstringthatwouldnormallybesplitupbynewlinesbytheregularbase64"))
assert_no_match(/\n/, ActiveSupport::Base64.encode64s("oneverylongstringthatwouldnormallybesplitupbynewlinesbytheregularbase64"))
end
end
end
6 changes: 3 additions & 3 deletions activesupport/test/message_encryptor_test.rb
Expand Up @@ -82,10 +82,10 @@ def assert_not_verified(value)
end

def munge(base64_string)
bits = ActiveSupport::Base64.decode64(base64_string)
bits = ::Base64.decode64(base64_string)
bits.reverse!
ActiveSupport::Base64.encode64s(bits)
::Base64.strict_encode64(bits)
end
end

end
end

0 comments on commit 5f09414

Please sign in to comment.