Class: LightJWT::JWE

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

Constant Summary collapse

NUM_OF_SEGMENTS =
5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil, jwt_token = nil) ⇒ JWE

Returns a new instance of JWE.



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

def initialize(key = nil, jwt_token = nil)
  @key = key
  @jwt_token = jwt_token
end

Instance Attribute Details

#algObject

Returns the value of attribute alg.



8
9
10
# File 'lib/light_jwt/jwe.rb', line 8

def alg
  @alg
end

#auth_tagObject (readonly)

Returns the value of attribute auth_tag.



9
10
11
# File 'lib/light_jwt/jwe.rb', line 9

def auth_tag
  @auth_tag
end

#ciphertextObject (readonly)

Returns the value of attribute ciphertext.



9
10
11
# File 'lib/light_jwt/jwe.rb', line 9

def ciphertext
  @ciphertext
end

#encObject

Returns the value of attribute enc.



8
9
10
# File 'lib/light_jwt/jwe.rb', line 8

def enc
  @enc
end

#encrypted_keyObject (readonly)

Returns the value of attribute encrypted_key.



9
10
11
# File 'lib/light_jwt/jwe.rb', line 9

def encrypted_key
  @encrypted_key
end

#headerObject (readonly)

Returns the value of attribute header.



9
10
11
# File 'lib/light_jwt/jwe.rb', line 9

def header
  @header
end

#ivObject (readonly)

Returns the value of attribute iv.



9
10
11
# File 'lib/light_jwt/jwe.rb', line 9

def iv
  @iv
end

#jwt_tokenObject (readonly)

Returns the value of attribute jwt_token.



9
10
11
# File 'lib/light_jwt/jwe.rb', line 9

def jwt_token
  @jwt_token
end

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/light_jwt/jwe.rb', line 9

def key
  @key
end

#payloadObject

Returns the value of attribute payload.



8
9
10
# File 'lib/light_jwt/jwe.rb', line 8

def payload
  @payload
end

Class Method Details

.decode_compact_serialized(jwt_token, private_key) ⇒ Object



14
15
16
17
# File 'lib/light_jwt/jwe.rb', line 14

def decode_compact_serialized(jwt_token, private_key)
  jwe = new(private_key, jwt_token)
  jwe.extract!
end

Instance Method Details

#as_jsonObject



52
53
54
# File 'lib/light_jwt/jwe.rb', line 52

def as_json
  { payload: }
end

#decrypt!Object



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

def decrypt!
  validate_decrypt_requirements!

  plaintext = JWA::JWE.decrypt(alg, enc, encrypted_key, iv, ciphertext, auth_tag, key)
  @payload = JSON.parse(plaintext, symbolize_names: true)

  self
end

#encrypt!Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/light_jwt/jwe.rb', line 25

def encrypt!
  validate_encrypt_requirements!

  result = JWA::JWE.encrypt(alg, enc, payload.to_json, key)

  @header = { alg:, enc: }
  @encrypted_key = result[:encrypted_key]
  @iv = result[:iv]
  @ciphertext = result[:ciphertext]
  @auth_tag = result[:auth_tag]

  self
end

#extract!Object



56
57
58
59
60
61
62
63
64
# File 'lib/light_jwt/jwe.rb', line 56

def extract!
  segments = split_and_decode_segments
  @encrypted_key, @iv, @ciphertext, @auth_tag = segments[1..]
  @header = parse_header(segments[0])

  @alg, @enc = header.values_at(:alg, :enc)

  self
end

#to_sObject



48
49
50
# File 'lib/light_jwt/jwe.rb', line 48

def to_s
  serialize_compact_format
end