Class: JSON::JWK

Inherits:
ActiveSupport::HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/json/jwk.rb,
lib/json/jwk/set.rb,
lib/json/jwk/jwkizable.rb

Defined Under Namespace

Modules: JWKizable Classes: Set, UnknownAlgorithm

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, ex_params = {}) ⇒ JWK

Returns a new instance of JWK.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/json/jwk.rb', line 5

def initialize(params = {}, ex_params = {})
  case params
  when OpenSSL::PKey::RSA, OpenSSL::PKey::EC
    super params.to_jwk(ex_params)
  when OpenSSL::PKey::PKey
    raise UnknownAlgorithm.new('Unknown Key Type')
  when String
    super(
      k: params,
      kty: :oct
    )
    merge! ex_params
  else
    super params
    merge! ex_params
  end
end

Class Method Details

.decode(jwk) ⇒ Object



132
133
134
135
136
137
# File 'lib/json/jwk.rb', line 132

def decode(jwk)
  # NOTE:
  #  returning OpenSSL::PKey::RSA/EC instance for backward compatibility.
  #  use `new` if you want JSON::JWK instance.
  new(jwk).to_key
end

Instance Method Details

#content_typeObject



23
24
25
# File 'lib/json/jwk.rb', line 23

def content_type
  'application/jwk+json'
end

#thumbprint(digest = OpenSSL::Digest::SHA256.new) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/json/jwk.rb', line 27

def thumbprint(digest = OpenSSL::Digest::SHA256.new)
  digest = case digest
  when OpenSSL::Digest
    digest
  when String, Symbol
    OpenSSL::Digest.new digest.to_s
  else
    raise UnknownAlgorithm.new('Unknown Digest Algorithm')
  end
  UrlSafeBase64.encode64 digest.digest(normalize.to_json)
end

#to_keyObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/json/jwk.rb', line 39

def to_key
  case
  when rsa?
    to_rsa_key
  when ec?
    to_ec_key
  when oct?
    self[:k]
  else
    raise UnknownAlgorithm.new('Unknown Key Type')
  end
end