Class: JSON::JWT

Inherits:
Hash
  • Object
show all
Defined in:
lib/json/jwt.rb

Direct Known Subclasses

JWE, JWS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(claim) ⇒ JWT

Returns a new instance of JWT.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/json/jwt.rb', line 9

def initialize(claim)
  @header = {
    :typ => :JWT,
    :alg => :none
  }
  [:exp, :nbf, :iat].each do |key|
    if claim[key]
      claim[key] = claim[key].to_i
    end
  end
  replace claim
end

Instance Attribute Details

#headerObject

Returns the value of attribute header.



7
8
9
# File 'lib/json/jwt.rb', line 7

def header
  @header
end

#signatureObject

Returns the value of attribute signature.



7
8
9
# File 'lib/json/jwt.rb', line 7

def signature
  @signature
end

Instance Method Details

#sign(private_key_or_secret, algorithm = :RS256) ⇒ Object



22
23
24
# File 'lib/json/jwt.rb', line 22

def sign(private_key_or_secret, algorithm = :RS256)
  JWS.new(self).sign(private_key_or_secret, algorithm)
end

#to_sObject



26
27
28
29
30
31
32
33
34
# File 'lib/json/jwt.rb', line 26

def to_s
  [
    header.to_json,
    self.to_json,
    signature
  ].collect do |segment|
    UrlSafeBase64.encode64 segment.to_s
  end.join('.')
end