Skip to content

Commit

Permalink
Remove deprecated base64 in favour of pack/unpack
Browse files Browse the repository at this point in the history
The base64 library is external as of Ruby 3.4 and is no longer available
by default in Ruby. The pack/unpack methods support encoding as base64
and can be used in it's place.

String#unpack.first was preferred here to support Ruby 2.3, which didn't
yet have String#unpack1 introduced in Ruby 2.4.
  • Loading branch information
adam12 committed Apr 24, 2024
1 parent b1deebd commit 8ec1ce5
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
3 changes: 1 addition & 2 deletions lib/rack/auth/basic.rb
Expand Up @@ -2,7 +2,6 @@

require_relative 'abstract/handler'
require_relative 'abstract/request'
require 'base64'

module Rack
module Auth
Expand Down Expand Up @@ -48,7 +47,7 @@ def basic?
end

def credentials
@credentials ||= Base64.decode64(params).split(':', 2)
@credentials ||= params.unpack("m").first.split(':', 2)
end

def username
Expand Down
5 changes: 2 additions & 3 deletions lib/rack/auth/digest/nonce.rb
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require 'digest/md5'
require 'base64'

module Rack
module Auth
Expand All @@ -21,15 +20,15 @@ class << self
end

def self.parse(string)
new(*Base64.decode64(string).split(' ', 2))
new(*string.unpack("m").first.split(' ', 2))
end

def initialize(timestamp = Time.now, given_digest = nil)
@timestamp, @given_digest = timestamp.to_i, given_digest
end

def to_s
Base64.encode64("#{@timestamp} #{digest}").strip
["#{@timestamp} #{digest}"].pack("m0")
end

def digest
Expand Down
5 changes: 2 additions & 3 deletions lib/rack/session/cookie.rb
Expand Up @@ -4,7 +4,6 @@
require 'zlib'
require_relative 'abstract/id'
require 'json'
require 'base64'
require 'delegate'

module Rack
Expand Down Expand Up @@ -51,11 +50,11 @@ class Cookie < Abstract::PersistedSecure
# Encode session cookies as Base64
class Base64
def encode(str)
::Base64.strict_encode64(str)
[str].pack("m0")
end

def decode(str)
::Base64.decode64(str)
str.unpack("m").first
end

# Encode session cookies as Marshaled Base64 data
Expand Down

0 comments on commit 8ec1ce5

Please sign in to comment.