module Slots::JWT::Tokens

Public Instance Methods

create_token(have_session) click to toggle source
# File lib/slots/jwt/tokens.rb, line 15
def create_token(have_session)
  session = ''
  if have_session && Slots::JWT.configuration.session_lifetime
    @new_session = self.sessions.new(jwt_iat: 0)
    # Session should never be invalid since its all programmed
    raise 'Session not valid' unless @new_session.valid?
    session = @new_session.session
  end
  @slots_jwt = Slots::JWT::Slokens.encode(self, session, extra_payload)
  if @new_session
    @new_session.jwt_iat = @slots_jwt.iat
    @new_session.save!
  end
  @new_token = true
  run_token_created_callback
  token
end
extra_payload() click to toggle source
# File lib/slots/jwt/tokens.rb, line 33
def extra_payload
  @extra_payload || {}
end
jwt() click to toggle source
# File lib/slots/jwt/tokens.rb, line 41
def jwt
  @slots_jwt
end
jwt_identifier() click to toggle source
# File lib/slots/jwt/tokens.rb, line 11
def jwt_identifier
  send(self.class.jwt_identifier_column)
end
new_token?() click to toggle source
# File lib/slots/jwt/tokens.rb, line 79
def new_token?
  @new_token
end
set_token!(slots_jwt) click to toggle source
# File lib/slots/jwt/tokens.rb, line 44
def set_token!(slots_jwt)
  @slots_jwt = slots_jwt
  @extra_payload = slots_jwt.extra_payload
  self
end
token() click to toggle source
# File lib/slots/jwt/tokens.rb, line 37
def token
  @slots_jwt&.token
end
update_session() click to toggle source
# File lib/slots/jwt/tokens.rb, line 50
def update_session
  return false unless valid_in_database?
  return false unless allowed_new_token?
  # Need to check if allowed new token after loading
  session = self.sessions.matches_jwt(jwt)
  return false unless session
  old_iat = jwt.iat
  jwt.update_token(self, extra_payload)
  if session.jwt_iat == old_iat
    # if old_iat == previous_jwt_iat dont update and return token
    session.update(previous_jwt_iat: old_iat, jwt_iat: jwt.iat)
    @new_token = true
  end
end
update_token() click to toggle source
# File lib/slots/jwt/tokens.rb, line 65
def update_token
  # This will only update the data in the token
  # not the experation data or anything else
  return false unless valid_in_database?
  return false unless allowed_new_token?

  session = self.sessions.matches_jwt(jwt)
  old_iat = jwt.iat
  jwt.update_token_data(self, extra_payload)
  # Dont worry if session isnt there because exp not updated
  session&.update(previous_jwt_iat: old_iat, jwt_iat: jwt.iat)
  @new_token = true
end
valid_in_database?() click to toggle source
# File lib/slots/jwt/tokens.rb, line 83
def valid_in_database?
  begin
    jwt_identifier_was = self.jwt_identifier
    self.reload
    return false if jwt_identifier_was != self.jwt_identifier
  rescue ActiveRecord::RecordNotFound
    return false
  end
  true
end