Class: JWT::Auth::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/jwt/auth/token.rb

Overview

In-memory representation of JWT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#expirationObject

Returns the value of attribute expiration.



13
14
15
# File 'lib/jwt/auth/token.rb', line 13

def expiration
  @expiration
end

#subjectObject

Returns the value of attribute subject.



13
14
15
# File 'lib/jwt/auth/token.rb', line 13

def subject
  @subject
end

Class Method Details

.from_token(token) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/jwt/auth/token.rb', line 39

def self.from_token(token)
  payload = JWT.decode(token, JWT::Auth.secret).first

  token = JWT::Auth::Token.new
  token.expiration = payload['exp']
  token.subject = JWT::Auth.model.find_by :id => payload['sub'], :token_version => payload['ver']

  token
end

.from_user(subject) ⇒ Object



32
33
34
35
36
37
# File 'lib/jwt/auth/token.rb', line 32

def self.from_user(subject)
  token = JWT::Auth::Token.new
  token.subject = subject

  token
end

Instance Method Details

#renew!Object



19
20
21
# File 'lib/jwt/auth/token.rb', line 19

def renew!
  self.expiration = nil
end

#to_jwtObject



23
24
25
26
27
28
29
30
# File 'lib/jwt/auth/token.rb', line 23

def to_jwt
  payload = {
    :exp => expiration || JWT::Auth.token_lifetime.from_now.to_i,
    :sub => subject.id,
    :ver => subject.token_version
  }
  JWT.encode payload, JWT::Auth.secret
end

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/jwt/auth/token.rb', line 15

def valid?
  subject && Time.at(expiration).future?
end