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,
lib/json/jwk/set/fetcher.rb

Defined Under Namespace

Modules: JWKizable Classes: Set, UnknownAlgorithm

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
22
# 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
  calculate_default_kid if self[:kid].blank?
end

Instance Method Details

#content_typeObject



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

def content_type
  'application/jwk+json'
end

#ec?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/json/jwk.rb', line 57

def ec?
  self[:kty]&.to_sym == :EC
end

#normalizeObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/json/jwk.rb', line 65

def normalize
  case
  when rsa?
    {
      e:   self[:e],
      kty: self[:kty],
      n:   self[:n]
    }
  when ec?
    {
      crv: self[:crv],
      kty: self[:kty],
      x:   self[:x],
      y:   self[:y]
    }
  when oct?
    {
      k:   self[:k],
      kty: self[:kty]
    }
  else
    raise UnknownAlgorithm.new('Unknown Key Type')
  end
end

#oct?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/json/jwk.rb', line 61

def oct?
  self[:kty]&.to_sym == :oct
end

#rsa?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/json/jwk.rb', line 53

def rsa?
  self[:kty]&.to_sym == :RSA
end

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



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

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
  Base64.urlsafe_encode64 digest.digest(normalize.to_json), padding: false
end

#to_keyObject



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

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