Class: JsonWebToken
- Inherits:
-
Object
- Object
- JsonWebToken
- Defined in:
- lib/jwt_auth/json_web_token.rb
Instance Attribute Summary collapse
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Class Method Summary collapse
- .encode(user_id, expiration = 24.hours.from_now) ⇒ Object
- .find_config {|Rails.application.class.parent_name.underscore, Rails.application.secrets.json_web_token_secret| ... } ⇒ Object
- .secret_and_encoding ⇒ Object
Instance Method Summary collapse
-
#initialize(token) ⇒ JsonWebToken
constructor
A new instance of JsonWebToken.
- #valid? ⇒ Boolean
Constructor Details
#initialize(token) ⇒ JsonWebToken
Returns a new instance of JsonWebToken.
28 29 30 31 32 33 34 35 36 |
# File 'lib/jwt_auth/json_web_token.rb', line 28 def initialize(token) # begin secret, encoding = secret_and_encoding @payload = JWT.decode(token, secret, encoding).first.with_indifferent_access @user_id = @payload[:user_id]["$oid"] # rescue JWT::DecodeError # nil # end end |
Instance Attribute Details
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
6 7 8 |
# File 'lib/jwt_auth/json_web_token.rb', line 6 def payload @payload end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
6 7 8 |
# File 'lib/jwt_auth/json_web_token.rb', line 6 def user_id @user_id end |
Class Method Details
.encode(user_id, expiration = 24.hours.from_now) ⇒ Object
23 24 25 26 |
# File 'lib/jwt_auth/json_web_token.rb', line 23 def self.encode(user_id, expiration = 24.hours.from_now) secret, encoding = secret_and_encoding JWT.encode({user_id: user_id, exp: expiration.to_i}, secret, encoding) end |
.find_config {|Rails.application.class.parent_name.underscore, Rails.application.secrets.json_web_token_secret| ... } ⇒ Object
18 19 20 21 22 |
# File 'lib/jwt_auth/json_web_token.rb', line 18 def self.find_config yield Rails.application.class.parent_name.underscore, Rails.application.secrets.json_web_token_secret Rails.application.secrets.json_web_token_encoding end |
.secret_and_encoding ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/jwt_auth/json_web_token.rb', line 8 def self.secret_and_encoding s = nil e = nil self.find_config do |app, secret, encoding| s = secret e = encoding end [s, e] end |
Instance Method Details
#valid? ⇒ Boolean
38 39 40 |
# File 'lib/jwt_auth/json_web_token.rb', line 38 def valid? user_id.presence && Time.now < Time.at(@payload[:exp].to_i) end |