module DeviseTokenAuth::TokenFactory

def self.client

=> "zNf0pNP5iGfuBItZJGCseQ"
DeviseTokenAuth::TokenFactory.client
Example:
Generates a random URL-safe client.
def self.client
  secure_string
end

def self.create(client: nil, lifespan: nil, cost: nil)

=> #
DeviseTokenAuth::TokenFactory.create(lifespan: 10, cost: 4)

=> #
DeviseTokenAuth::TokenFactory.create
Example:
Creates a token instance. Takes an optional client, lifespan and cost options.
def self.create(client: nil, lifespan: nil, cost: nil)
  # obj_client  = client.nil? ? client() : client
  obj_client  = client || client()
  obj_token      = token
  obj_token_hash = token_hash(obj_token, cost)
  obj_expiry     = expiry(lifespan)
  Token.new(obj_client, obj_token, obj_token_hash, obj_expiry)
end

def self.expiry(lifespan = nil)

=> 1517773781
DeviseTokenAuth::TokenFactory.expiry(10)
=> 1518983359
DeviseTokenAuth::TokenFactory.expiry
Example:
Returns the value of time as an integer number of seconds. Takes one argument.
def self.expiry(lifespan = nil)
  lifespan ||= DeviseTokenAuth.token_lifespan
  (Time.zone.now + lifespan).to_i
end

def self.new

=> #
DeviseTokenAuth::TokenFactory.new
Example:
Creates a token instance with instance variables equal nil.
def self.new
  Token.new
end

def self.secure_string

=> "ADBoIaqXsEDnxIpOuumrTA"
DeviseTokenAuth::TokenFactory.secure_string
Example:
Generates a random URL-safe string.
def self.secure_string
  # https://ruby-doc.org/stdlib-2.5.0/libdoc/securerandom/rdoc/Random/Formatter.html#method-i-urlsafe_base64
  SecureRandom.urlsafe_base64
end

def self.token

=> "6Bqs4K9x8ChLmZogvruF3A"
DeviseTokenAuth::TokenFactory.token
Example:
Generates a random URL-safe token.
def self.token
  secure_string
end

def self.token_hash(token, cost = nil)

=> "$2a$04$RkIrosbdRtuet2eUk3si8eS4ufeNpiPc/rSSsfpniRK8ogM5YFOWS"
DeviseTokenAuth::TokenFactory.token_hash("_qxAxmc-biQLiYRHsmwd5Q", 4)

=> "$2a$10$6/cTAtQ3CBLfpkeHW7dlt.PD2aVCbFRN5vDDJUUhGsZ6pzYFlh4Me"
DeviseTokenAuth::TokenFactory.token_hash("_qxAxmc-biQLiYRHsmwd5Q")
Example:
It is recommended to not use a value more than 10.
the default value is used. The possible cost value is within range from 4 to 31.
Returns token hash for a token with given cost. If no cost value is specified,
def self.token_hash(token, cost = nil)
  cost ||= DeviseTokenAuth.token_cost
  BCrypt::Password.create(token, cost: cost)
end

def self.token_hash_is_token?(token_hash, token)

=> true
DeviseTokenAuth::TokenFactory.token_hash_is_token?(token_hash, token)
token_hash = "$2a$10$ArjX0tskRIa5Z/Tmapy59OCiAXLStfhrCiaDz.8fCb6hnX1gJ0p/2"
token = "4wZ9gcc900rMQD1McpcSNA"
Example:
Compares a potential token against the token hash. Returns true if the token is the original token, false otherwise.
def self.token_hash_is_token?(token_hash, token)
  BCrypt::Password.new(token_hash).is_password?(token)
rescue StandardError
  false
end

def self.valid_token_hash?(token_hash)

=> true
DeviseTokenAuth::TokenFactory.valid_token_hash?(token_hash)
token_hash = "$2a$10$ArjX0tskRIa5Z/Tmapy59OCiAXLStfhrCiaDz.8fCb6hnX1gJ0p/2"
Example:
Returns true if token hash is a valid token hash.
def self.valid_token_hash?(token_hash)
  !!BCrypt::Password.valid_hash?(token_hash)
end