Class: Userbin::JWT
- Inherits:
-
Object
- Object
- Userbin::JWT
- Defined in:
- lib/userbin/jwt.rb
Instance Attribute Summary collapse
-
#header ⇒ Object
Returns the value of attribute header.
-
#payload ⇒ Object
Returns the value of attribute payload.
Instance Method Summary collapse
- #expired? ⇒ Boolean
-
#initialize(jwt) ⇒ JWT
constructor
A new instance of JWT.
- #merge!(payload = {}) ⇒ Object
- #to_json ⇒ Object
- #to_token ⇒ Object
Constructor Details
#initialize(jwt) ⇒ JWT
Returns a new instance of JWT.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/userbin/jwt.rb', line 7 def initialize(jwt) begin raise Userbin::SecurityError, 'Empty JWT' unless jwt @payload = ::JWT.decode(jwt, Userbin.config.api_secret, true) do |header| @header = header.with_indifferent_access Userbin.config.api_secret # used by the 'key finder' in the JWT gem end.with_indifferent_access rescue ::JWT::DecodeError => e raise Userbin::SecurityError.new(e) end end |
Instance Attribute Details
#header ⇒ Object
Returns the value of attribute header.
5 6 7 |
# File 'lib/userbin/jwt.rb', line 5 def header @header end |
#payload ⇒ Object
Returns the value of attribute payload.
5 6 7 |
# File 'lib/userbin/jwt.rb', line 5 def payload @payload end |
Instance Method Details
#expired? ⇒ Boolean
19 20 21 |
# File 'lib/userbin/jwt.rb', line 19 def expired? Time.now.utc > Time.at(@header['exp']).utc end |
#merge!(payload = {}) ⇒ Object
23 24 25 |
# File 'lib/userbin/jwt.rb', line 23 def merge!(payload = {}) @payload.merge!(payload) end |
#to_json ⇒ Object
27 28 29 |
# File 'lib/userbin/jwt.rb', line 27 def to_json @payload end |