Class: JWTear::JWE
- Inherits:
-
Object
- Object
- JWTear::JWE
- Includes:
- Helpers::Extensions::Print
- Defined in:
- lib/jwtear/jwe.rb
Overview
JWE
Takes a parsed token from JSON::JWT#decode
Instance Attribute Summary collapse
-
#alg ⇒ Object
Returns the value of attribute alg.
-
#auth_data ⇒ Object
Returns the value of attribute auth_data.
-
#authentication_tag ⇒ Object
Returns the value of attribute authentication_tag.
-
#cek ⇒ Object
Returns the value of attribute cek.
-
#cipher_text ⇒ Object
Returns the value of attribute cipher_text.
-
#enc ⇒ Object
Returns the value of attribute enc.
-
#encrypted_key ⇒ Object
Returns the value of attribute encrypted_key.
-
#header ⇒ Object
Returns the value of attribute header.
-
#iat ⇒ Object
Returns the value of attribute iat.
-
#iss ⇒ Object
Returns the value of attribute iss.
-
#iv ⇒ Object
Returns the value of attribute iv.
-
#kid ⇒ Object
Returns the value of attribute kid.
-
#plaintext ⇒ Object
Returns the value of attribute plaintext.
-
#sub ⇒ Object
Returns the value of attribute sub.
-
#zip ⇒ Object
Returns the value of attribute zip.
Instance Method Summary collapse
-
#generate_jwe(header:, payload:, key:) ⇒ String
generate_jwe generate JWE token.
-
#is_encrypted?(item) ⇒ Boolean
is_encrypted? to check if the given string in a JSON format or its encrypted.
-
#parse(token) ⇒ Self
parse is a basic parser for JWE token.
- #to_json_presentation ⇒ Object
Methods included from Helpers::Extensions::Print
#print_bad, #print_error, #print_good, #print_h1, #print_h2, #print_h3, #print_status, #print_warning
Instance Attribute Details
#alg ⇒ Object
Returns the value of attribute alg.
10 11 12 |
# File 'lib/jwtear/jwe.rb', line 10 def alg @alg end |
#auth_data ⇒ Object
Returns the value of attribute auth_data.
10 11 12 |
# File 'lib/jwtear/jwe.rb', line 10 def auth_data @auth_data end |
#authentication_tag ⇒ Object
Returns the value of attribute authentication_tag.
10 11 12 |
# File 'lib/jwtear/jwe.rb', line 10 def authentication_tag @authentication_tag end |
#cek ⇒ Object
Returns the value of attribute cek.
10 11 12 |
# File 'lib/jwtear/jwe.rb', line 10 def cek @cek end |
#cipher_text ⇒ Object
Returns the value of attribute cipher_text.
10 11 12 |
# File 'lib/jwtear/jwe.rb', line 10 def cipher_text @cipher_text end |
#enc ⇒ Object
Returns the value of attribute enc.
10 11 12 |
# File 'lib/jwtear/jwe.rb', line 10 def enc @enc end |
#encrypted_key ⇒ Object
Returns the value of attribute encrypted_key.
10 11 12 |
# File 'lib/jwtear/jwe.rb', line 10 def encrypted_key @encrypted_key end |
#header ⇒ Object
Returns the value of attribute header.
10 11 12 |
# File 'lib/jwtear/jwe.rb', line 10 def header @header end |
#iat ⇒ Object
Returns the value of attribute iat.
10 11 12 |
# File 'lib/jwtear/jwe.rb', line 10 def iat @iat end |
#iss ⇒ Object
Returns the value of attribute iss.
10 11 12 |
# File 'lib/jwtear/jwe.rb', line 10 def iss @iss end |
#iv ⇒ Object
Returns the value of attribute iv.
10 11 12 |
# File 'lib/jwtear/jwe.rb', line 10 def iv @iv end |
#kid ⇒ Object
Returns the value of attribute kid.
10 11 12 |
# File 'lib/jwtear/jwe.rb', line 10 def kid @kid end |
#plaintext ⇒ Object
Returns the value of attribute plaintext.
10 11 12 |
# File 'lib/jwtear/jwe.rb', line 10 def plaintext @plaintext end |
#sub ⇒ Object
Returns the value of attribute sub.
10 11 12 |
# File 'lib/jwtear/jwe.rb', line 10 def sub @sub end |
#zip ⇒ Object
Returns the value of attribute zip.
10 11 12 |
# File 'lib/jwtear/jwe.rb', line 10 def zip @zip end |
Instance Method Details
#generate_jwe(header:, payload:, key:) ⇒ String
generate_jwe
generate JWE token
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/jwtear/jwe.rb', line 77 def generate_jwe(header:, payload:, key:) key = OpenSSL::PKey::RSA.new(key) jwt = JSON::JWT.new(JSON.parse(payload, symbolize_names: true)) jwt.header = JSON.parse(header, symbolize_names: true) ::JWE.encrypt(payload, key, enc: jwt.header[:enc]) # I had to use this gem as json-jwt does not support A192GCM AFAIK rescue TypeError => e print_bad "Invalid data type." print_warning "Make sure your public/private key file exists." rescue ArgumentError => e print_error e. print_warning "Make sure that you have a proper header." puts jwt.header rescue OpenSSL::PKey::RSAError => e print_error "#{e.message} '#{key}'" print_warning "Make sure your public/private key file exists." exit! end |
#is_encrypted?(item) ⇒ Boolean
is_encrypted?
to check if the given string in a JSON format or its encrypted.
Used mostly with @encrypted_key as it might come in different format.
101 102 103 104 105 106 |
# File 'lib/jwtear/jwe.rb', line 101 def is_encrypted?(item) JSON.parse item false rescue JSON::ParserError true end |
#parse(token) ⇒ Self
parse
is a basic parser for JWE token
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/jwtear/jwe.rb', line 22 def parse(token) jwt = JSON::JWT.decode(token, :skip_decryption, :skip_verification) @header = jwt.header @encrypted_key = jwt.send(:jwe_encrypted_key) @iv = jwt.iv @cipher_text = jwt.cipher_text @authentication_tag = jwt.send(:authentication_tag) @algorithm = jwt.algorithm @auth_data = jwt.auth_data @plaintext = jwt.send(:plain_text) @kid = jwt.kid @alg = @header["alg"] @typ = @header["typ"] @cty = @header["cty"] @enc = @header["enc"] @zip = @header["zip"] @iat = @encrypted_key["iat"] @iss = @encrypted_key["iss"] @cek = @encrypted_key self rescue JSON::JWS::UnexpectedAlgorithm => e puts e. rescue JSON::JWT::InvalidFormat => e print_error e. puts token exit! end |
#to_json_presentation ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/jwtear/jwe.rb', line 50 def to_json_presentation header = @header if is_encrypted?(@encrypted_key) encrypted_key = Base64.urlsafe_encode64(@encrypted_key, padding: false) else encrypted_key = @encrypted_key.to_json end iv = Base64.urlsafe_encode64(@iv) cipher_text = Base64.urlsafe_encode64(@cipher_text, padding: false) authentication_tag = Base64.urlsafe_encode64(@authentication_tag, padding: false) "#{header.to_json}" + "●" + "#{encrypted_key}" + "●" + "#{iv}" + "●" + "#{cipher_text}" + "●" + "#{authentication_tag}" end |