Class: JSON::JWS

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

Defined Under Namespace

Classes: InvalidFormat, UnexpectedAlgorithm, VerificationFailed

Constant Summary collapse

NUM_OF_SEGMENTS =
3

Instance Attribute Summary

Attributes inherited from JWT

#header, #signature

Instance Method Summary collapse

Methods inherited from JOSE

#content_type

Methods inherited from JWT

#content_type, decode, #encrypt, register_header_keys, #to_s

Constructor Details

#initialize(jwt) ⇒ JWS

Returns a new instance of JWS.

Raises:



9
10
11
12
# File 'lib/json/jws.rb', line 9

def initialize(jwt)
  update jwt
  raise InvalidFormat.new('Signature Algorithm Required') unless algorithm
end

Instance Method Details

#as_json(options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/json/jws.rb', line 24

def as_json(options = {})
  case options[:syntax]
  when :general
    {
      payload: UrlSafeBase64.encode64(self.to_json),
      signatures: {
        protected: UrlSafeBase64.encode64(header.to_json),
        signature: UrlSafeBase64.encode64(signature.to_s)
      }
    }
  when :flattened
    {
      protected: UrlSafeBase64.encode64(header.to_json),
      payload:   UrlSafeBase64.encode64(self.to_json),
      signature: UrlSafeBase64.encode64(signature.to_s)
    }
  else
    super
  end
end

#sign!(private_key_or_secret) ⇒ Object



14
15
16
17
# File 'lib/json/jws.rb', line 14

def sign!(private_key_or_secret)
  self.signature = sign signature_base_string, private_key_or_secret
  self
end

#update_with_jose_attributes(hash_or_jwt) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/json/jws.rb', line 45

def update_with_jose_attributes(hash_or_jwt)
  update_without_jose_attributes hash_or_jwt
  if hash_or_jwt.is_a? JSON::JWT
    self.header = hash_or_jwt.header
    self.signature = hash_or_jwt.signature
  end
  self
end

#verify(signature_base_string, public_key_or_secret) ⇒ Object



19
20
21
22
# File 'lib/json/jws.rb', line 19

def verify(signature_base_string, public_key_or_secret)
  public_key_or_secret && valid?(signature_base_string, public_key_or_secret) or
  raise VerificationFailed
end