Class: JSON::JWT

Inherits:
ActiveSupport::HashWithIndifferentAccess
  • Object
show all
Includes:
JOSE
Defined in:
lib/json/jwt.rb

Direct Known Subclasses

JWS

Defined Under Namespace

Classes: Exception, InvalidFormat, UnexpectedAlgorithm, VerificationFailed

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JOSE

#secure_compare, #with_jwk_support

Constructor Details

#initialize(claims = {}) ⇒ JWT

Returns a new instance of JWT.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/json/jwt.rb', line 19

def initialize(claims = {})
  @content_type = 'application/jwt'
  self.typ = :JWT
  self.alg = :none
  unless claims.nil?
    [:exp, :nbf, :iat].each do |key|
      claims[key] = claims[key].to_i if claims[key]
    end
  end
  update claims
end

Instance Attribute Details

#blank_payloadObject

Returns the value of attribute blank_payload.



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

def blank_payload
  @blank_payload
end

#signatureObject

Returns the value of attribute signature.



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

def signature
  @signature
end

Class Method Details

.decode_compact_serialized(jwt_string, key_or_secret, algorithms = nil, encryption_methods = nil, allow_blank_payload = false) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/json/jwt.rb', line 101

def decode_compact_serialized(jwt_string, key_or_secret, algorithms = nil, encryption_methods = nil, allow_blank_payload = false)
  case jwt_string.count('.') + 1
  when JWS::NUM_OF_SEGMENTS
    JWS.decode_compact_serialized jwt_string, key_or_secret, algorithms, allow_blank_payload
  when JWE::NUM_OF_SEGMENTS
    JWE.decode_compact_serialized jwt_string, key_or_secret, algorithms, encryption_methods
  else
    raise InvalidFormat.new("Invalid JWT Format. JWT should include #{JWS::NUM_OF_SEGMENTS} or #{JWE::NUM_OF_SEGMENTS} segments.")
  end
end

.decode_json_serialized(input, key_or_secret, algorithms = nil, encryption_methods = nil, allow_blank_payload = false) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/json/jwt.rb', line 112

def decode_json_serialized(input, key_or_secret, algorithms = nil, encryption_methods = nil, allow_blank_payload = false)
  input = input.with_indifferent_access
  if (input[:signatures] || input[:signature]).present?
    JWS.decode_json_serialized input, key_or_secret, algorithms, allow_blank_payload
  elsif input[:ciphertext].present?
    JWE.decode_json_serialized input, key_or_secret, algorithms, encryption_methods
  else
    raise InvalidFormat.new("Unexpected JOSE JSON Serialization Format.")
  end
end

.pretty_generate(jwt_string) ⇒ Object



123
124
125
# File 'lib/json/jwt.rb', line 123

def pretty_generate(jwt_string)
  decode(jwt_string, :skip_verification).pretty_generate
end

Instance Method Details

#as_json(options = {}) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/json/jwt.rb', line 56

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

#encrypt(public_key_or_secret, algorithm = :RSA1_5, encryption_method = :'A128CBC-HS256') ⇒ Object



38
39
40
41
42
43
44
# File 'lib/json/jwt.rb', line 38

def encrypt(public_key_or_secret, algorithm = :RSA1_5, encryption_method = :'A128CBC-HS256')
  jwe = JWE.new self
  jwe.kid ||= public_key_or_secret[:kid] if public_key_or_secret.is_a? JSON::JWK
  jwe.alg = algorithm
  jwe.enc = encryption_method
  jwe.encrypt! public_key_or_secret
end

#pretty_generateObject



93
94
95
96
97
98
# File 'lib/json/jwt.rb', line 93

def pretty_generate
  [
    JSON.pretty_generate(header),
    JSON.pretty_generate(self)
  ]
end

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



31
32
33
34
35
36
# File 'lib/json/jwt.rb', line 31

def sign(private_key_or_secret, algorithm = :autodetect)
  jws = JWS.new self
  jws.kid ||= private_key_or_secret[:kid] if private_key_or_secret.is_a? JSON::JWK
  jws.alg = algorithm
  jws.sign! private_key_or_secret
end

#to_json(*args) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/json/jwt.rb', line 77

def to_json *args
  if @blank_payload && args.empty?
    ''
  else
    super
  end
end

#to_sObject



46
47
48
49
50
51
52
53
54
# File 'lib/json/jwt.rb', line 46

def to_s
  [
    header.to_json,
    self.to_json,
    signature
  ].collect do |segment|
    Base64.urlsafe_encode64 segment.to_s, padding: false
  end.join('.')
end

#update(claims) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/json/jwt.rb', line 85

def update claims
  if claims.nil?
    @blank_payload = true
  else
    super
  end
end