Module: DeviseTokenAuth::TokenFactory

Defined in:
lib/devise_token_auth/token_factory.rb

Overview

A token management factory which allow generate token objects and check them.

Defined Under Namespace

Classes: Token

Class Method Summary collapse

Class Method Details

.clientObject

Generates a random URL-safe client. Example:

DeviseTokenAuth::TokenFactory.client
=> "zNf0pNP5iGfuBItZJGCseQ"


30
31
32
# File 'lib/devise_token_auth/token_factory.rb', line 30

def self.client
  secure_string
end

.create(client: nil, lifespan: nil, cost: nil) ⇒ Object

Creates a token instance. Takes an optional client, lifespan and cost options. Example:

DeviseTokenAuth::TokenFactory.create
=> #<struct DeviseTokenAuth::TokenFactory::Token client="tElcgkdZ7f9XEa0unZhrYQ", token="rAMcWOs0-mGHFMnIgJD2cA", token_hash="$2a$10$wrsdlHVRGlYW11wfImxU..jr0Ux3bHo/qbXcSfgp8zmvVUNHosita", expiry=1518982690>

DeviseTokenAuth::TokenFactory.create(lifespan: 10, cost: 4)
=> #<struct DeviseTokenAuth::TokenFactory::Token client="5qleT7_t9JPVcX9xmxkVYA", token="RBXX43u4xXNSO-fr2N_4pA", token_hash="$2a$04$9gpCaoFbu2dUKxU3qiTgluHX7jj9UzS.jq1QW0EkQmoaxARo1WxTy", expiry=1517773268>


16
17
18
19
20
21
22
23
24
# File 'lib/devise_token_auth/token_factory.rb', line 16

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

.expiry(lifespan = nil) ⇒ Object

Returns the value of time as an integer number of seconds. Takes one argument. Example:

DeviseTokenAuth::TokenFactory.expiry
=> 1518983359
DeviseTokenAuth::TokenFactory.expiry(10)
=> 1517773781


62
63
64
65
# File 'lib/devise_token_auth/token_factory.rb', line 62

def self.expiry(lifespan = nil)
  lifespan ||= DeviseTokenAuth.token_lifespan
  (Time.zone.now + lifespan).to_i
end

.newObject

Creates a token instance with instance variables equal nil. Example:

DeviseTokenAuth::TokenFactory.new
=> #<struct DeviseTokenAuth::TokenFactory::Token client=nil, token=nil, token_hash=nil, expiry=nil>


101
102
103
# File 'lib/devise_token_auth/token_factory.rb', line 101

def self.new
  Token.new
end

.secure_stringObject

Generates a random URL-safe string. Example:

DeviseTokenAuth::TokenFactory.secure_string
=> "ADBoIaqXsEDnxIpOuumrTA"


71
72
73
74
# File 'lib/devise_token_auth/token_factory.rb', line 71

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

.tokenObject

Generates a random URL-safe token. Example:

DeviseTokenAuth::TokenFactory.token
=> "6Bqs4K9x8ChLmZogvruF3A"


38
39
40
# File 'lib/devise_token_auth/token_factory.rb', line 38

def self.token
  secure_string
end

.token_hash(token, cost = nil) ⇒ Object

Returns token hash for a token with given cost. If no cost value is specified, the default value is used. The possible cost value is within range from 4 to 31. It is recommended to not use a value more than 10. Example:

DeviseTokenAuth::TokenFactory.token_hash("_qxAxmc-biQLiYRHsmwd5Q")
=> "$2a$10$6/cTAtQ3CBLfpkeHW7dlt.PD2aVCbFRN5vDDJUUhGsZ6pzYFlh4Me"

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


51
52
53
54
# File 'lib/devise_token_auth/token_factory.rb', line 51

def self.token_hash(token, cost = nil)
  cost ||= DeviseTokenAuth.token_cost
  BCrypt::Password.create(token, cost: cost)
end

.token_hash_is_token?(token_hash, token) ⇒ Boolean

Compares a potential token against the token hash. Returns true if the token is the original token, false otherwise. Example:

token = "4wZ9gcc900rMQD1McpcSNA"
token_hash = "$2a$10$ArjX0tskRIa5Z/Tmapy59OCiAXLStfhrCiaDz.8fCb6hnX1gJ0p/2"
DeviseTokenAuth::TokenFactory.token_hash_is_token?(token_hash, token)
=> true

Returns:

  • (Boolean)


91
92
93
94
95
# File 'lib/devise_token_auth/token_factory.rb', line 91

def self.token_hash_is_token?(token_hash, token)
  BCrypt::Password.new(token_hash).is_password?(token)
rescue StandardError
  false
end

.valid_token_hash?(token_hash) ⇒ Boolean

Returns true if token hash is a valid token hash. Example:

token_hash = "$2a$10$ArjX0tskRIa5Z/Tmapy59OCiAXLStfhrCiaDz.8fCb6hnX1gJ0p/2"
DeviseTokenAuth::TokenFactory.valid_token_hash?(token_hash)
=> true

Returns:

  • (Boolean)


81
82
83
# File 'lib/devise_token_auth/token_factory.rb', line 81

def self.valid_token_hash?(token_hash)
  !!BCrypt::Password.valid_hash?(token_hash)
end