Class: JWT::Auth::Token
- Inherits:
-
Object
- Object
- JWT::Auth::Token
- Defined in:
- lib/jwt/auth/token.rb
Overview
In-memory representation of JWT
Instance Attribute Summary collapse
-
#issued_at ⇒ Object
Returns the value of attribute issued_at.
-
#subject ⇒ Object
Returns the value of attribute subject.
-
#token_version ⇒ Object
Returns the value of attribute token_version.
Class Method Summary collapse
Instance Method Summary collapse
Instance Attribute Details
#issued_at ⇒ Object
Returns the value of attribute issued_at.
13 14 15 |
# File 'lib/jwt/auth/token.rb', line 13 def issued_at @issued_at end |
#subject ⇒ Object
Returns the value of attribute subject.
13 14 15 |
# File 'lib/jwt/auth/token.rb', line 13 def subject @subject end |
#token_version ⇒ Object
Returns the value of attribute token_version.
13 14 15 |
# File 'lib/jwt/auth/token.rb', line 13 def token_version @token_version end |
Class Method Details
.from_token(token) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/jwt/auth/token.rb', line 58 def from_token(token) begin @decoded_payload = JWT.decode(token, JWT::Auth.secret).first rescue JWT::DecodeError @decoded_payload = {} end token = self.new token.issued_at = @decoded_payload['iat'] token.token_version = @decoded_payload['ver'] if @decoded_payload['sub'] find_method = model.respond_to?(:find_by_token) ? :find_by_token : :find_by token.subject = model.send find_method, :id => @decoded_payload['sub'], :token_version => @decoded_payload['ver'] end token end |
.from_user(subject) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/jwt/auth/token.rb', line 38 def self.from_user(subject) token = self.new token.subject = subject token end |
Instance Method Details
#lifetime ⇒ Object
53 54 55 |
# File 'lib/jwt/auth/token.rb', line 53 def lifetime JWT::Auth.token_lifetime end |
#payload ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/jwt/auth/token.rb', line 45 def payload { :iat => issued_at || Time.now.to_i, :sub => subject.id, :ver => token_version || subject.token_version } end |
#renew! ⇒ Object
29 30 31 32 |
# File 'lib/jwt/auth/token.rb', line 29 def renew! self.issued_at = nil self.token_version = nil end |
#to_jwt ⇒ Object
34 35 36 |
# File 'lib/jwt/auth/token.rb', line 34 def to_jwt JWT.encode payload, JWT::Auth.secret end |
#valid? ⇒ Boolean
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/jwt/auth/token.rb', line 15 def valid? # Reload subject to prevent caching the old token_version subject && subject.reload return false if subject.nil? || issued_at.nil? || token_version.nil? return false if Time.at(issued_at + lifetime.to_i).past? return false if Time.at(issued_at).future? return false if token_version != subject.token_version true rescue ActiveRecord::RecordNotFound false end |