Class: JSON::JWS

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

Instance Attribute Summary

Attributes inherited from JWT

#header, #signature

Instance Method Summary collapse

Methods inherited from JWT

#to_s

Constructor Details

#initialize(jwt) ⇒ JWS

Returns a new instance of JWS.



3
4
5
6
# File 'lib/json/jws.rb', line 3

def initialize(jwt)
  @header = jwt.header
  replace jwt
end

Instance Method Details

#sign(private_key_or_secret, algorithm) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/json/jws.rb', line 8

def sign(private_key_or_secret, algorithm)
  header[:alg] = algorithm
  digest = OpenSSL::Digest::Digest.new "SHA#{algorithm.to_s[2, 3]}"
  self.signature = case algorithm
  when :HS256, :HS384, :HS512
    secret = private_key_or_secret
    OpenSSL::HMAC.digest(
      digest,
      secret,
      signature_base_string
    )
  when :RS256, :RS384, :RS512
    private_key = private_key_or_secret
    private_key.sign(
      digest,
      signature_base_string
    )
  when :ES256, :ES384, :ES512
    # TODO
  end
  self
end