Class: JWTear::JWS

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

Overview

JWS

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.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def alg
  @alg
end

#at_hashObject

Returns the value of attribute at_hash.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def at_hash
  @at_hash
end

#audObject

Returns the value of attribute aud.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def aud
  @aud
end

#azpObject

Returns the value of attribute azp.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def azp
  @azp
end

#ctyObject

Returns the value of attribute cty.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def cty
  @cty
end

#emailObject

Returns the value of attribute email.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def email
  @email
end

#email_verifiedObject

Returns the value of attribute email_verified.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def email_verified
  @email_verified
end

#expObject

Returns the value of attribute exp.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def exp
  @exp
end

#hdObject

Returns the value of attribute hd.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def hd
  @hd
end

#headerObject

Returns the value of attribute header.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def header
  @header
end

#iatObject

Returns the value of attribute iat.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def iat
  @iat
end

#issObject

Returns the value of attribute iss.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def iss
  @iss
end

#jkuObject

Returns the value of attribute jku.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def jku
  @jku
end

#kidObject

Returns the value of attribute kid.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def kid
  @kid
end

#payloadObject

Returns the value of attribute payload.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def payload
  @payload
end

#signatureObject

Returns the value of attribute signature.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def signature
  @signature
end

#subObject

Returns the value of attribute sub.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def sub
  @sub
end

#typObject

Returns the value of attribute typ.



8
9
10
# File 'lib/jwtear/jws.rb', line 8

def typ
  @typ
end

Instance Method Details

#generate_jws(header:, payload:, key:) ⇒ String

generate_jws

generate JWS token

Parameters:

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

Returns:

  • (String)

    the generated token



54
55
56
57
58
59
60
61
62
# File 'lib/jwtear/jws.rb', line 54

def generate_jws(header:, payload:, key:)
  jwt = JSON::JWT.new(JSON.parse(payload, symbolize_names: true))
  jwt.header = JSON.parse(header, symbolize_names: true)
  handle_signing(jwt, key)
rescue JSON::JWS::UnexpectedAlgorithm => e
  puts "Unexpected algorithm '#{jwt.header[:alg]}'."
  puts e.message
  exit!
end

#parse(token) ⇒ Self

parse

is a basic parser for JWS token

Parameters:

  • token (String)

    parsed token string

Returns:

  • (Self)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jwtear/jws.rb', line 21

def parse(token)
  jwt = JSON::JWT.decode(token, :skip_verification)
  @header    = jwt.header
  @payload   = jwt.to_h
  @signature = jwt.signature
  @alg       = @header["alg"]
  @typ       = @header["typ"]
  @cty       = @header["cty"]
  @kid       = @header["kid"]
  @jku       = @header["jku"]
  @iat       = @payload["iat"]
  self
rescue JSON::JWT::InvalidFormat => e
  print_error e.message
  puts token
  exit!
rescue Exception => e
  puts e.full_message
end

#to_json_presentationObject



41
42
43
# File 'lib/jwtear/jws.rb', line 41

def to_json_presentation
  "#{@header.to_json}" + "" + "#{@payload.to_json}" + "" + "#{Base64.urlsafe_encode64(@signature, padding: false)}"
end