Class: LightJWT::JWS

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

Constant Summary collapse

NUM_OF_SEGMENTS =
3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(signing_data, alg, key, signature = nil) ⇒ JWS

Returns a new instance of JWS.



36
37
38
39
40
41
42
# File 'lib/light_jwt/jws.rb', line 36

def initialize(signing_data, alg, key, signature = nil)
  @signing_data = signing_data
  @alg = alg
  @key = key
  @signature = signature
  @header, @payload = extract_header_and_payload
end

Instance Attribute Details

#algObject (readonly)

Returns the value of attribute alg.



5
6
7
# File 'lib/light_jwt/jws.rb', line 5

def alg
  @alg
end

#headerObject (readonly)

Returns the value of attribute header.



5
6
7
# File 'lib/light_jwt/jws.rb', line 5

def header
  @header
end

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/light_jwt/jws.rb', line 5

def key
  @key
end

#payloadObject (readonly)

Returns the value of attribute payload.



5
6
7
# File 'lib/light_jwt/jws.rb', line 5

def payload
  @payload
end

#signatureObject (readonly)

Returns the value of attribute signature.



5
6
7
# File 'lib/light_jwt/jws.rb', line 5

def signature
  @signature
end

Class Method Details

.decode_compact_serialized(jwt_token, key) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/light_jwt/jws.rb', line 10

def decode_compact_serialized(jwt_token, key)
  segments = jwt_token.split('.')
  validate_segment_count(segments)

  header, payload, signature = segments
  parsed_header = parse_segment(header)

  new("#{header}.#{payload}", parsed_header[:alg], key, signature)
end

Instance Method Details

#as_jsonObject



63
64
65
# File 'lib/light_jwt/jws.rb', line 63

def as_json
  { header:, payload: }
end

#sign!Object



44
45
46
47
48
49
# File 'lib/light_jwt/jws.rb', line 44

def sign!
  raw_signature = JWA::JWS.sign(alg, key, signing_data)
  @signature = encode_segment(raw_signature)

  self
end

#to_sObject

Raises:

  • (ArgumentError)


57
58
59
60
61
# File 'lib/light_jwt/jws.rb', line 57

def to_s
  raise ArgumentError, 'Signature is missing' unless signature

  [encoded_header, encoded_payload, signature].join('.')
end

#verify!Object



51
52
53
54
55
# File 'lib/light_jwt/jws.rb', line 51

def verify!
  raise Error::VerificationError, 'Signature verification failed' unless valid_signature?

  true
end