Skip to content

Commit

Permalink
Support JWKs for pre 2.3 rubies
Browse files Browse the repository at this point in the history
  • Loading branch information
anakinj committed Oct 26, 2020
1 parent 98352ba commit 699dfaa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/jwt/base64.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ module JWT
class Base64
class << self
def url_encode(str)
::Base64.encode64(str).tr('+/', '-_').gsub(/[\n=]/, '')
encoded = ::Base64.encode64(str)
encoded.tr!('+/', '-_')
encoded.gsub!(/[\n=]/, '')
encoded
end

def url_decode(str)
str += '=' * (4 - str.length.modulo(4))
::Base64.decode64(str.tr('-_', '+/'))
str.tr!('-_', '+/')
::Base64.decode64(str)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/jwt/jwk/rsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def append_private_parts(the_hash)
end

def encode_open_ssl_bn(key_part)
::Base64.urlsafe_encode64(key_part.to_s(BINARY), padding: false)
::JWT::Base64.url_encode(key_part.to_s(BINARY))
end

class << self
Expand Down Expand Up @@ -107,7 +107,7 @@ def populate_key(rsa_key, rsa_parameters) # rubocop:disable Metrics/CyclomaticCo
def decode_open_ssl_bn(jwk_data)
return nil unless jwk_data

OpenSSL::BN.new(::Base64.urlsafe_decode64(jwk_data), BINARY)
OpenSSL::BN.new(::JWT::Base64.url_decode(jwk_data), BINARY)
end
end
end
Expand Down

0 comments on commit 699dfaa

Please sign in to comment.