lib/json/jwt.rb in json-jwt-0.0.3 vs lib/json/jwt.rb in json-jwt-0.0.4
- old
+ new
@@ -1,8 +1,9 @@
require 'openssl'
require 'url_safe_base64'
require 'json'
+require 'active_support/core_ext'
module JSON
class JWT < Hash
attr_accessor :header, :signature
@@ -25,12 +26,11 @@
header[:alg] = algorithm
JWS.new(self).sign!(private_key_or_secret)
end
def verify(signature_base_string, signature = '', public_key_or_secret = nil)
- case header[:alg]
- when :none
+ if header[:alg].to_s == 'none'
signature == '' or raise VerificationFailed
else
JWS.new(self).verify(signature_base_string, signature, public_key_or_secret)
end
end
@@ -43,19 +43,28 @@
].collect do |segment|
UrlSafeBase64.encode64 segment.to_s
end.join('.')
end
+ def [](key)
+ if key.respond_to? :to_sym
+ super key.to_sym or
+ super key.to_s
+ else
+ super
+ end
+ end
+
class << self
def decode(jwt_string, public_key_or_secret = nil)
raise InvalidFormat.new('Invalid JWT Format. JWT should include 2 dots.') unless jwt_string.count('.') == 2
- header, claims, signature = jwt_string.split('.').collect do |segment|
+ header, claims, signature = jwt_string.split('.', 3).collect do |segment|
UrlSafeBase64.decode64 segment.to_s
end
- signature_base_string = jwt_string.split('.')[0,2].join('.')
+ signature_base_string = jwt_string.split('.')[0, 2].join('.')
jwt = new JSON.parse(claims)
- jwt.header = JSON.parse header
+ jwt.header = JSON.parse(header).with_indifferent_access
jwt.verify signature_base_string, signature, public_key_or_secret
jwt
rescue JSON::ParserError
raise InvalidFormat.new("Invalid JSON Format")
end
@@ -73,6 +82,6 @@
end
end
end
require 'json/jws'
-require 'json/jwe'
\ No newline at end of file
+require 'json/jwe'