Class: LightJWT::JWE
- Inherits:
-
Object
- Object
- LightJWT::JWE
- Defined in:
- lib/light_jwt/jwe.rb
Constant Summary collapse
- NUM_OF_SEGMENTS =
5
Instance Attribute Summary collapse
-
#alg ⇒ Object
Returns the value of attribute alg.
-
#auth_tag ⇒ Object
readonly
Returns the value of attribute auth_tag.
-
#ciphertext ⇒ Object
readonly
Returns the value of attribute ciphertext.
-
#enc ⇒ Object
Returns the value of attribute enc.
-
#encrypted_key ⇒ Object
readonly
Returns the value of attribute encrypted_key.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#iv ⇒ Object
readonly
Returns the value of attribute iv.
-
#jwt_token ⇒ Object
readonly
Returns the value of attribute jwt_token.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#payload ⇒ Object
Returns the value of attribute payload.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
- #decrypt! ⇒ Object
- #encrypt! ⇒ Object
- #extract! ⇒ Object
-
#initialize(key = nil, jwt_token = nil) ⇒ JWE
constructor
A new instance of JWE.
- #to_s ⇒ Object
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
#alg ⇒ Object
Returns the value of attribute alg.
8 9 10 |
# File 'lib/light_jwt/jwe.rb', line 8 def alg @alg end |
#auth_tag ⇒ Object (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 |
#ciphertext ⇒ Object (readonly)
Returns the value of attribute ciphertext.
9 10 11 |
# File 'lib/light_jwt/jwe.rb', line 9 def ciphertext @ciphertext end |
#enc ⇒ Object
Returns the value of attribute enc.
8 9 10 |
# File 'lib/light_jwt/jwe.rb', line 8 def enc @enc end |
#encrypted_key ⇒ Object (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 |
#header ⇒ Object (readonly)
Returns the value of attribute header.
9 10 11 |
# File 'lib/light_jwt/jwe.rb', line 9 def header @header end |
#iv ⇒ Object (readonly)
Returns the value of attribute iv.
9 10 11 |
# File 'lib/light_jwt/jwe.rb', line 9 def iv @iv end |
#jwt_token ⇒ Object (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 |
#key ⇒ Object (readonly)
Returns the value of attribute key.
9 10 11 |
# File 'lib/light_jwt/jwe.rb', line 9 def key @key end |
#payload ⇒ Object
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_json ⇒ Object
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_s ⇒ Object
48 49 50 |
# File 'lib/light_jwt/jwe.rb', line 48 def to_s serialize_compact_format end |