Sha256: 1be2d3e04440dbbb4891ba9f34a659b25aeb0fc4199167a35e8996403905fd55

Contents?: true

Size: 1.98 KB

Versions: 2

Compression:

Stored size: 1.98 KB

Contents

module JSON
  class JWK
    module JWKizable
      module RSA
        def to_jwk(ex_params = {})
          params = {
            kty: :RSA,
            e: UrlSafeBase64.encode64(e.to_s(2)),
            n: UrlSafeBase64.encode64(n.to_s(2))
          }.merge ex_params
          if private?
            params.merge!(
              d: UrlSafeBase64.encode64(d.to_s(2))
            )
          end
          JWK.new params
        end
      end

      module EC
        def to_jwk(ex_params = {})
          # NOTE:
          #  OpenSSL::PKey::EC instance can be both public & private key at the same time.
          #  In such case, is it handled as public key or private key?
          #  For now, this gem handles any OpenSSL::PKey::EC instances as public key.
          unless public_key?
            raise UnknownAlgorithm.new('EC private key is not supported yet')
          end
          params = {
            kty: :EC,
            crv: curve_name,
            x: UrlSafeBase64.encode64(coodinates[:x].to_s),
            y: UrlSafeBase64.encode64(coodinates[:y].to_s)
          }.merge ex_params
          JWK.new params
        end

        private

        def curve_name
          case group.curve_name
          when 'prime256v1'
            :'P-256'
          when 'secp384r1'
            :'P-384'
          when 'secp521r1'
            :'P-521'
          else
            raise UnknownAlgorithm.new('Unknown EC Curve')
          end
        end

        def coodinates
          unless @coodinates
            hex = public_key.to_bn.to_s(16)
            data_len = hex.length - 2
            type = hex[0, 2]
            hex_x = hex[2, data_len / 2]
            hex_y = hex[2 + data_len / 2, data_len / 2]
            @coodinates = {
              x: [hex_x].pack("H*"),
              y: [hex_y].pack("H*")
            }
          end
          @coodinates
        end
      end
    end
  end
end

OpenSSL::PKey::RSA.send :include, JSON::JWK::JWKizable::RSA
OpenSSL::PKey::EC.send :include, JSON::JWK::JWKizable::EC

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
json-jwt-1.2.1 lib/json/jwk/jwkizable.rb
json-jwt-1.2.0 lib/json/jwk/jwkizable.rb