Class: Userbin::JWT
- Inherits:
-
Object
- Object
- Userbin::JWT
- Defined in:
- lib/userbin/jwt.rb
Instance Attribute Summary collapse
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
Instance Method Summary collapse
- #app_id ⇒ Object
- #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.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/userbin/jwt.rb', line 8 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 (readonly)
Returns the value of attribute header.
5 6 7 |
# File 'lib/userbin/jwt.rb', line 5 def header @header end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
6 7 8 |
# File 'lib/userbin/jwt.rb', line 6 def payload @payload end |
Instance Method Details
#app_id ⇒ Object
32 33 34 |
# File 'lib/userbin/jwt.rb', line 32 def app_id @header['aud'] end |
#expired? ⇒ Boolean
20 21 22 |
# File 'lib/userbin/jwt.rb', line 20 def expired? Time.now.utc > Time.at(@header['exp']).utc end |
#merge!(payload = {}) ⇒ Object
36 37 38 |
# File 'lib/userbin/jwt.rb', line 36 def merge!(payload = {}) @payload.merge!(payload) end |
#to_json ⇒ Object
24 25 26 |
# File 'lib/userbin/jwt.rb', line 24 def to_json @payload end |