Class: JWTear::JWE

Inherits:
Object
  • Object
show all
Includes:
Helpers::Extensions::Print
Defined in:
lib/jwtear/jwe.rb

Overview

JWE

Takes a parsed token from JSON::JWT#decode

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#algObject

Returns the value of attribute alg.



10
11
12
# File 'lib/jwtear/jwe.rb', line 10

def alg
  @alg
end

#auth_dataObject

Returns the value of attribute auth_data.



10
11
12
# File 'lib/jwtear/jwe.rb', line 10

def auth_data
  @auth_data
end

#authentication_tagObject

Returns the value of attribute authentication_tag.



10
11
12
# File 'lib/jwtear/jwe.rb', line 10

def authentication_tag
  @authentication_tag
end

#cekObject

Returns the value of attribute cek.



10
11
12
# File 'lib/jwtear/jwe.rb', line 10

def cek
  @cek
end

#cipher_textObject

Returns the value of attribute cipher_text.



10
11
12
# File 'lib/jwtear/jwe.rb', line 10

def cipher_text
  @cipher_text
end

#encObject

Returns the value of attribute enc.



10
11
12
# File 'lib/jwtear/jwe.rb', line 10

def enc
  @enc
end

#encrypted_keyObject

Returns the value of attribute encrypted_key.



10
11
12
# File 'lib/jwtear/jwe.rb', line 10

def encrypted_key
  @encrypted_key
end

#headerObject

Returns the value of attribute header.



10
11
12
# File 'lib/jwtear/jwe.rb', line 10

def header
  @header
end

#iatObject

Returns the value of attribute iat.



10
11
12
# File 'lib/jwtear/jwe.rb', line 10

def iat
  @iat
end

#issObject

Returns the value of attribute iss.



10
11
12
# File 'lib/jwtear/jwe.rb', line 10

def iss
  @iss
end

#ivObject

Returns the value of attribute iv.



10
11
12
# File 'lib/jwtear/jwe.rb', line 10

def iv
  @iv
end

#kidObject

Returns the value of attribute kid.



10
11
12
# File 'lib/jwtear/jwe.rb', line 10

def kid
  @kid
end

#plaintextObject

Returns the value of attribute plaintext.



10
11
12
# File 'lib/jwtear/jwe.rb', line 10

def plaintext
  @plaintext
end

#subObject

Returns the value of attribute sub.



10
11
12
# File 'lib/jwtear/jwe.rb', line 10

def sub
  @sub
end

#zipObject

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

Parameters:

  • header (JSON)
  • payload (JSON)
  • key (String)

Returns:

  • (String)

    the generated 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 jwe 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.message
  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.

Parameters:

  • item (JSON|STRING)

Returns:

  • (Boolean)


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

Parameters:

  • token (String)

    parsed token string

Returns:

  • (Self)


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.full_message
rescue JSON::JWT::InvalidFormat => e
  print_error e.message
  puts token
  exit!
end

#to_json_presentationObject



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}" + ".".bold +
  "#{encrypted_key}"  + ".".bold +
  "#{iv}"             + ".".bold +
  "#{cipher_text}"    + ".".bold +
  "#{authentication_tag}"
end