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.
30
31
32
33
34
35
36
37
|
# File 'lib/json/jwt.rb', line 30
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
replace claims
end
|
Instance Attribute Details
Returns the value of attribute header.
8
9
10
|
# File 'lib/json/jwt.rb', line 8
def
@header
end
|
#signature ⇒ Object
Returns the value of attribute signature.
8
9
10
|
# File 'lib/json/jwt.rb', line 8
def signature
@signature
end
|
Class Method Details
.decode(jwt_string, key_or_secret = nil) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/json/jwt.rb', line 80
def decode(jwt_string, key_or_secret = nil)
case jwt_string.count('.') + 1
when JWS::NUM_OF_SEGMENTS , claims, signature = jwt_string.split('.', JWS::NUM_OF_SEGMENTS).collect do |segment|
UrlSafeBase64.decode64 segment.to_s
end
, claims = [, claims].collect do |json|
MultiJson.load(json).with_indifferent_access
end
signature_base_string = jwt_string.split('.')[0, JWS::NUM_OF_SEGMENTS - 1].join('.')
jwt = new claims
jwt. =
jwt.signature = signature
jwt.verify signature_base_string, key_or_secret unless key_or_secret == :skip_verification
jwt
when JWE::NUM_OF_SEGMENTS
jwe = JWE.new jwt_string
jwe. = MultiJson.load(
UrlSafeBase64.decode64 jwt_string.split('.').first
).with_indifferent_access
jwe.decrypt! key_or_secret unless key_or_secret == :skip_decryption
jwe
else
raise InvalidFormat.new("Invalid JWT Format. JWT should include #{JWS::NUM_OF_SEGMENTS} or #{JWE::NUM_OF_SEGMENTS} segments.")
end
rescue MultiJson::DecodeError
raise InvalidFormat.new("Invalid JSON Format")
end
|
.new_from_hash_copying_default(hash) ⇒ Object
NOTE: Ugly hack to avoid this ActiveSupport 4.0 bug.
https://github.com/rails/rails/issues/11087
115
116
117
|
# File 'lib/json/jwt.rb', line 115
def new_from_hash_copying_default(hash)
superclass.new_from_hash_copying_default hash
end
|
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/json/jwt.rb', line 16
def (*keys)
keys.each do ||
define_method do
self.[]
end
define_method "#{}=" do |value|
self.[] = value
end
end
end
|
Instance Method Details
#content_type ⇒ Object
39
40
41
|
# File 'lib/json/jwt.rb', line 39
def content_type
'application/jwt'
end
|
#encrypt(public_key_or_secret, algorithm = :RSA1_5, encryption_method = :'A128CBC-HS256') ⇒ Object
62
63
64
65
66
67
|
# File 'lib/json/jwt.rb', line 62
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
47
48
49
50
51
|
# File 'lib/json/jwt.rb', line 47
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
69
70
71
72
73
74
75
76
77
|
# File 'lib/json/jwt.rb', line 69
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
53
54
55
56
57
58
59
60
|
# File 'lib/json/jwt.rb', line 53
def verify(signature_base_string, public_key_or_secret = nil)
if alg.to_s == 'none'
raise UnexpectedAlgorithm if public_key_or_secret
signature == '' or raise VerificationFailed
else
JWS.new(self).verify(signature_base_string, public_key_or_secret)
end
end
|