Class: JWK::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/jwk/key.rb

Direct Known Subclasses

ECKey, OctKey, RSAKey

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_json(json) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jwk/key.rb', line 20

def from_json(json)
  key = JSON.parse(json)
  validate_kty!(key['kty'])

  case key['kty']
  when 'EC'
    ECKey.new(key)
  when 'RSA'
    RSAKey.new(key)
  when 'oct'
    OctKey.new(key)
  end
end

.from_openssl(key) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/jwk/key.rb', line 12

def from_openssl(key)
  if key.is_a?(OpenSSL::PKey::RSA)
    RSAKey.from_openssl(key)
  elsif key.is_a?(OpenSSL::PKey::EC) || key.is_a?(OpenSSL::PKey::EC::Point)
    ECKey.from_openssl(key)
  end
end

.from_pem(pem) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/jwk/key.rb', line 4

def from_pem(pem)
  key = OpenSSL::PKey.read(pem)
  if defined?(OpenSSL::PKey::EC) && key.is_a?(OpenSSL::PKey::EC)
    $stderr.puts('WARNING: EC Keys have bugs on jRuby') if defined?(JRUBY_VERSION)
  end
  from_openssl(key)
end

.validate_kty!(kty) ⇒ Object



34
35
36
37
38
# File 'lib/jwk/key.rb', line 34

def validate_kty!(kty)
  unless %w[EC RSA oct].include?(kty)
    raise JWK::InvalidKey, "The provided JWK has an unknown \"kty\" value: #{kty}."
  end
end

Instance Method Details

#to_jsonObject



41
42
43
# File 'lib/jwk/key.rb', line 41

def to_json
  @key.to_json
end

#x5t_s256Object



51
52
53
# File 'lib/jwk/key.rb', line 51

def x5t_s256
  @key['x5t#S256']
end