Skip to content

Commit

Permalink
backport the padding option to ruby 2.2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
gfx committed Jul 24, 2018
1 parent 540f3e9 commit 89b98b0
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/graphql/schema/base_64_encoder.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
# frozen_string_literal: true

require 'base64'

module GraphQL
class Schema
# @api private
module Base64Encoder
def self.encode(plaintext, nonce: false)
Base64.urlsafe_encode64(plaintext, padding: false)
Base64bp.urlsafe_encode64(plaintext, padding: false)
end

def self.decode(ciphertext, nonce: false)
Base64.urlsafe_decode64(ciphertext)
Base64bp.urlsafe_decode64(ciphertext)
end

# @api private
module Base64bp # backport from ruby v2.5 to v2.2 that has no `padding` option
extend Base64

module_function

def urlsafe_encode64(bin, padding:)
str = strict_encode64(bin).tr("+/", "-_")
str = str.delete("=") unless padding
str
end

def urlsafe_decode64(str)
str = str.tr("-_", "+/")
if !str.end_with?("=") && str.length % 4 != 0
str = str.ljust((str.length + 3) & ~3, "=")
end
strict_decode64(str)
end
end
end
end
Expand Down

0 comments on commit 89b98b0

Please sign in to comment.