Class: JWT::JWK::RSA

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/jwt/jwk/rsa.rb

Constant Summary collapse

BINARY =
2
KTY =
'RSA'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keypair) ⇒ RSA

Returns a new instance of RSA.

Raises:

  • (ArgumentError)


15
16
17
18
19
# File 'lib/jwt/jwk/rsa.rb', line 15

def initialize(keypair)
  raise ArgumentError, 'keypair must be of type OpenSSL::PKey::RSA' unless keypair.is_a?(OpenSSL::PKey::RSA)

  @keypair = keypair
end

Instance Attribute Details

#keypairObject (readonly)

Returns the value of attribute keypair.



8
9
10
# File 'lib/jwt/jwk/rsa.rb', line 8

def keypair
  @keypair
end

Class Method Details

.import(jwk_data) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/jwt/jwk/rsa.rb', line 36

def self.import(jwk_data)
  imported_key = OpenSSL::PKey::RSA.new
  imported_key.set_key(OpenSSL::BN.new(::Base64.urlsafe_decode64(jwk_data[:n]), BINARY),
    OpenSSL::BN.new(::Base64.urlsafe_decode64(jwk_data[:e]), BINARY),
    nil)
  self.new(imported_key)
end

Instance Method Details

#exportObject



27
28
29
30
31
32
33
34
# File 'lib/jwt/jwk/rsa.rb', line 27

def export
  {
    kty: KTY,
    n: ::Base64.urlsafe_encode64(public_key.n.to_s(BINARY), padding: false),
    e: ::Base64.urlsafe_encode64(public_key.e.to_s(BINARY), padding: false),
    kid: kid
  }
end

#kidObject



21
22
23
24
25
# File 'lib/jwt/jwk/rsa.rb', line 21

def kid
  sequence = OpenSSL::ASN1::Sequence([OpenSSL::ASN1::Integer.new(public_key.n),
                                      OpenSSL::ASN1::Integer.new(public_key.e)])
  OpenSSL::Digest::SHA256.hexdigest(sequence.to_der)
end