Class: JWT::Auth::Token

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

Overview

In-memory representation of JWT

Direct Known Subclasses

AccessToken, RefreshToken

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Token

Returns a new instance of Token.



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

def initialize(params = {})
  params.each { |key, value| send "#{key}=", value }
end

Instance Attribute Details

#issued_atObject

Returns the value of attribute issued_at.



11
12
13
# File 'lib/jwt/auth/token.rb', line 11

def issued_at
  @issued_at
end

#subjectObject

Returns the value of attribute subject.



11
12
13
# File 'lib/jwt/auth/token.rb', line 11

def subject
  @subject
end

#versionObject

Returns the value of attribute version.



11
12
13
# File 'lib/jwt/auth/token.rb', line 11

def version
  @version
end

Class Method Details

.from_jwt(token) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/jwt/auth/token.rb', line 52

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

  token = token_for payload['typ']

  token ? token.new(parse payload) : nil
rescue JWT::DecodeError
  nil
end

Instance Method Details

#lifetimeObject

Override this method in subclasses

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/jwt/auth/token.rb', line 47

def lifetime
  raise NotImplementedError
end

#to_jwtObject



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

def to_jwt
  JWT.encode payload, JWT::Auth.secret
end

#typeObject

Override this method in subclasses

Raises:

  • (NotImplementedError)


40
41
42
# File 'lib/jwt/auth/token.rb', line 40

def type
  raise NotImplementedError
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  # Reload subject to prevent caching the old token_version
  subject&.reload

  return false if subject.nil? || issued_at.nil? || version.nil?
  return false if Time.at(issued_at + lifetime.to_i).past?
  return false if Time.at(issued_at).future?
  return false if version != subject.token_version

  true
rescue ActiveRecord::RecordNotFound
  false
end