Class: JSON::JWT
- Inherits:
-
ActiveSupport::HashWithIndifferentAccess
- Object
- ActiveSupport::HashWithIndifferentAccess
- JSON::JWT
show all
- Defined in:
- lib/json/jwt.rb
Direct Known Subclasses
JOSE
Defined Under Namespace
Classes: Exception, InvalidFormat, UnexpectedAlgorithm, VerificationFailed
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(claims = {}) ⇒ JWT
Returns a new instance of JWT.
31
32
33
34
35
36
37
38
|
# File 'lib/json/jwt.rb', line 31
def initialize(claims = {})
self.typ = :JWT
self.alg = :none
[:exp, :nbf, :iat].each do |key|
claims[key] = claims[key].to_i if claims[key]
end
update claims
end
|
Instance Attribute Details
Returns the value of attribute header.
9
10
11
|
# File 'lib/json/jwt.rb', line 9
def
@header
end
|
#signature ⇒ Object
Returns the value of attribute signature.
9
10
11
|
# File 'lib/json/jwt.rb', line 9
def signature
@signature
end
|
Class Method Details
.decode(input, key_or_secret = nil) ⇒ Object
100
101
102
103
104
105
106
107
108
|
# File 'lib/json/jwt.rb', line 100
def decode(input, key_or_secret = nil)
if input.is_a? Hash
decode_json_serialized input, key_or_secret
else
decode_compact_serialized input, key_or_secret
end
rescue MultiJson::DecodeError
raise InvalidFormat.new("Invalid JSON Format")
end
|
.decode_compact_serialized(jwt_string, key_or_secret) ⇒ Object
.decode_json_serialized(input, key_or_secret) ⇒ Object
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/json/jwt.rb', line 121
def decode_json_serialized(input, key_or_secret)
input = input.with_indifferent_access
if (input[:signatures] || input[:signature]).present?
JWS.decode_json_serialized input, key_or_secret
elsif input[:ciphertext].present?
JWE.decode_json_serialized input, key_or_secret
else
raise InvalidFormat.new("Unexpected JOSE JSON Serialization Format.")
end
end
|
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/json/jwt.rb', line 17
def (*keys)
keys.each do ||
define_method do
self.[]
end
define_method "#{}=" do |value|
self.[] = value
end
end
end
|
Instance Method Details
#as_json(options = {}) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/json/jwt.rb', line 78
def as_json(options = {})
case options[:syntax]
when :general
{
payload: UrlSafeBase64.encode64(self.to_json),
signatures: [{
protected: UrlSafeBase64.encode64(.to_json),
signature: UrlSafeBase64.encode64(signature.to_s)
}]
}
when :flattened
{
protected: UrlSafeBase64.encode64(.to_json),
payload: UrlSafeBase64.encode64(self.to_json),
signature: UrlSafeBase64.encode64(signature.to_s)
}
else
super
end
end
|
#content_type ⇒ Object
40
41
42
|
# File 'lib/json/jwt.rb', line 40
def content_type
'application/jwt'
end
|
#encrypt(public_key_or_secret, algorithm = :RSA1_5, encryption_method = :'A128CBC-HS256') ⇒ Object
61
62
63
64
65
66
|
# File 'lib/json/jwt.rb', line 61
def encrypt(public_key_or_secret, algorithm = :RSA1_5, encryption_method = :'A128CBC-HS256')
jwe = JWE.new self
jwe.alg = algorithm
jwe.enc = encryption_method
jwe.encrypt! public_key_or_secret
end
|
#sign(private_key_or_secret, algorithm = :HS256) ⇒ Object
48
49
50
51
52
|
# File 'lib/json/jwt.rb', line 48
def sign(private_key_or_secret, algorithm = :HS256)
jws = JWS.new self
jws.alg = algorithm
jws.sign! private_key_or_secret
end
|
#to_s ⇒ Object
68
69
70
71
72
73
74
75
76
|
# File 'lib/json/jwt.rb', line 68
def to_s
[
.to_json,
self.to_json,
signature
].collect do |segment|
UrlSafeBase64.encode64 segment.to_s
end.join('.')
end
|
#verify(signature_base_string, public_key_or_secret = nil) ⇒ Object
NOTE: keeping for backward compatibility
55
56
57
58
59
|
# File 'lib/json/jwt.rb', line 55
def verify(signature_base_string, public_key_or_secret = nil)
jws = JWS.new self
jws.signature_base_string = signature_base_string
jws.verify! public_key_or_secret
end
|