Class: JSON::JWE

Inherits:
JOSE
  • Object
show all
Defined in:
lib/json/jwe.rb

Defined Under Namespace

Classes: DecryptionFailed, InvalidFormat, UnexpectedAlgorithm

Constant Summary collapse

NUM_OF_SEGMENTS =
5

Instance Attribute Summary collapse

Attributes inherited from JWT

#header, #signature

Instance Method Summary collapse

Methods inherited from JWT

decode, #encrypt, register_header_keys, #sign, #verify

Constructor Details

#initialize(input) ⇒ JWE

Returns a new instance of JWE.



21
22
23
# File 'lib/json/jwe.rb', line 21

def initialize(input)
  self.input = input.to_s
end

Instance Attribute Details

#authentication_tag=(value) ⇒ Object

Sets the attribute authentication_tag

Parameters:

  • value

    the value to set the attribute authentication_tag to.



12
13
14
# File 'lib/json/jwe.rb', line 12

def authentication_tag=(value)
  @authentication_tag = value
end

#cipher_textObject

Returns the value of attribute cipher_text.



12
13
14
# File 'lib/json/jwe.rb', line 12

def cipher_text
  @cipher_text
end

#content_encryption_keyObject

Returns the value of attribute content_encryption_key.



12
13
14
# File 'lib/json/jwe.rb', line 12

def content_encryption_key
  @content_encryption_key
end

#encryption_keyObject

Returns the value of attribute encryption_key.



12
13
14
# File 'lib/json/jwe.rb', line 12

def encryption_key
  @encryption_key
end

#inputObject

Returns the value of attribute input.



12
13
14
# File 'lib/json/jwe.rb', line 12

def input
  @input
end

#ivObject

Returns the value of attribute iv.



12
13
14
# File 'lib/json/jwe.rb', line 12

def iv
  @iv
end

#jwe_encrypted_key=(value) ⇒ Object

Sets the attribute jwe_encrypted_key

Parameters:

  • value

    the value to set the attribute jwe_encrypted_key to.



12
13
14
# File 'lib/json/jwe.rb', line 12

def jwe_encrypted_key=(value)
  @jwe_encrypted_key = value
end

#mac_keyObject

Returns the value of attribute mac_key.



12
13
14
# File 'lib/json/jwe.rb', line 12

def mac_key
  @mac_key
end

#modeObject

Returns the value of attribute mode.



12
13
14
# File 'lib/json/jwe.rb', line 12

def mode
  @mode
end

#plain_textObject

Returns the value of attribute plain_text.



12
13
14
# File 'lib/json/jwe.rb', line 12

def plain_text
  @plain_text
end

#private_key_or_secretObject

Returns the value of attribute private_key_or_secret.



12
13
14
# File 'lib/json/jwe.rb', line 12

def private_key_or_secret
  @private_key_or_secret
end

#public_key_or_secretObject

Returns the value of attribute public_key_or_secret.



12
13
14
# File 'lib/json/jwe.rb', line 12

def public_key_or_secret
  @public_key_or_secret
end

Instance Method Details

#content_typeObject



25
26
27
# File 'lib/json/jwe.rb', line 25

def content_type
  'application/jose'
end

#decrypt!(private_key_or_secret) ⇒ Object



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

def decrypt!(private_key_or_secret)
  self.mode = :decryption
  self.private_key_or_secret = private_key_or_secret
  decode_segments!
  cipher.decrypt
  restore_cipher_keys!
  self.plain_text = cipher.update(cipher_text) + cipher.final
  verify_cbc_authentication_tag! if cbc?
  self
end

#encrypt!(public_key_or_secret) ⇒ Object



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

def encrypt!(public_key_or_secret)
  self.mode = :encryption
  self.plain_text = input
  self.public_key_or_secret = public_key_or_secret
  cipher.encrypt
  generate_cipher_keys!
  self.cipher_text = cipher.update(plain_text) + cipher.final
  self
end

#to_sObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/json/jwe.rb', line 50

def to_s
  if mode == :encryption
    [
      header.to_json,
      jwe_encrypted_key,
      iv,
      cipher_text,
      authentication_tag
    ].collect do |segment|
      UrlSafeBase64.encode64 segment.to_s
    end.join('.')
  else
    plain_text
  end
end