Class: Landline::Util::JWT
- Inherits:
-
Object
- Object
- Landline::Util::JWT
- Defined in:
- lib/landline/util/jwt.rb
Overview
JSON Web Token construction class
Constant Summary collapse
- ALGO =
{ "HS256" => proc do |data, secret| Base64.urlsafe_encode64( OpenSSL::HMAC.digest("SHA256", secret, data) ).gsub('=', '') end, "HS384" => proc do |data, secret| Base64.urlsafe_encode64( OpenSSL::HMAC.digest("SHA384", secret, data) ).gsub('=', '') end, "HS512" => proc do |data, secret| Base64.urlsafe_encode64( OpenSSL::HMAC.digest("SHA512", secret, data) ).gsub('=', '') end }.freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Class Method Summary collapse
-
.from_string(input, key) ⇒ JWT?
Construct an object from string.
Instance Method Summary collapse
-
#initialize(data, halgo = "HS256") ⇒ JWT
constructor
Create a new JWT token wrapper.
-
#make(key) ⇒ String
Construct a string representation of the current token.
Constructor Details
#initialize(data, halgo = "HS256") ⇒ JWT
Create a new JWT token wrapper
38 39 40 41 42 43 44 45 |
# File 'lib/landline/util/jwt.rb', line 38 def initialize(data, halgo = "HS256") unless ALGO.include? halgo raise StandardError, "hash algorithm #{halgo} not supported" end @halgo = halgo @data = data end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
73 74 75 |
# File 'lib/landline/util/jwt.rb', line 73 def data @data end |
Class Method Details
.from_string(input, key) ⇒ JWT?
Construct an object from string
64 65 66 67 68 69 70 71 |
# File 'lib/landline/util/jwt.rb', line 64 def self.from_string(input, key) halgoj, dataj, sig = input.split(".") halgo = JSON.parse(Base64.urlsafe_decode64(halgoj))["alg"] return nil unless ALGO.include? halgo return nil if ALGO[halgo].call("#{halgoj}.#{dataj}", key) != sig new(JSON.parse(Base64.urlsafe_decode64(dataj)), halgo) end |
Instance Method Details
#make(key) ⇒ String
Construct a string representation of the current token
50 51 52 53 54 55 56 57 58 |
# File 'lib/landline/util/jwt.rb', line 50 def make(key) jsonheader = { "alg": @halgo, "typ": "JWT" }.to_json jsondata = @data.to_json data = "#{base64(jsonheader)}.#{base64(jsondata)}" "#{data}.#{ALGO[@halgo].call(data, key)}" end |