Class: EncodedToken::Configuration

Inherits:
Object
  • Object
show all
Includes:
ErrorMessages, Utils
Defined in:
lib/encoded_token/configuration.rb

Overview

EncodedToken::Configuration

This class manages the single configuration instance for EncodedToken.

Constant Summary collapse

DEFAULT_CIPHER_COUNT =

======================================================================

Constants

======================================================================

16
DEFAULT_MIN_TOKEN_SIZE =
55
DEFAULT_MAX_INPUT_SIZE =
1000

Constants included from ErrorMessages

ErrorMessages::CIPHER_ERRORS, ErrorMessages::CONFIG_ERRORS, ErrorMessages::ENCODED_TOKEN_ERRORS, ErrorMessages::ENCODER_ERRORS, ErrorMessages::ENCRYPTOR_ERRORS, ErrorMessages::ERROR_MESSAGES, ErrorMessages::V1_ENCODER_ERRORS

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

======================================================================

Instance Methods

======================================================================



87
88
89
90
91
92
93
# File 'lib/encoded_token/configuration.rb', line 87

def initialize
  @cipher_count       = DEFAULT_CIPHER_COUNT
  @expiring_seeds     = {}
  @locked             = false
  @max_input_size     = DEFAULT_MAX_INPUT_SIZE
  @min_token_size     = DEFAULT_MIN_TOKEN_SIZE
end

Class Method Details

.instanceConfiguration

Returns the singleton instance of the Configuration.

Returns:



39
40
41
# File 'lib/encoded_token/configuration.rb', line 39

def instance
  @instance ||= new
end

.lock!true

Locks the configuration to prevent further changes.

Returns:

  • (true)

    the lock status



67
68
69
# File 'lib/encoded_token/configuration.rb', line 67

def lock!
  instance.lock!
end

.locked?Boolean

Checks if the configuration is locked.

Returns:

  • (Boolean)


48
49
50
# File 'lib/encoded_token/configuration.rb', line 48

def locked?
  instance.locked?
end

.unlock!false

Unlocks the configuration.

Returns:

  • (false)

    the lock status



77
78
79
# File 'lib/encoded_token/configuration.rb', line 77

def unlock!
  instance.unlock!
end

.unlocked?Boolean

Checks if the configuration is unlocked.

Returns:

  • (Boolean)


57
58
59
# File 'lib/encoded_token/configuration.rb', line 57

def unlocked?
  instance.unlocked?
end

Instance Method Details

#add_expiring_seed(expiring_seed, expiry_date, cipher_count = DEFAULT_CIPHER_COUNT) ⇒ Hash

Adds an expiring seed to the configuration.

Parameters:

  • expiring_seed (Integer, String)

    the seed for the expiring cipher.

  • expiry_date (Date, Time)

    the date when the seed expires.

  • cipher_count (Integer) (defaults to: DEFAULT_CIPHER_COUNT)

    the number of keys for the expiring cipher.

Returns:

  • (Hash)

    the details of the added expiring seed.

Raises:

  • (ArgumentError)

    if the arguments are invalid.



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/encoded_token/configuration.rb', line 147

def add_expiring_seed(expiring_seed, expiry_date, cipher_count = DEFAULT_CIPHER_COUNT)
  assert_configuration_can_change!

  key     = Integer(expiring_seed).abs rescue nil
  c_count = Integer(cipher_count).abs rescue nil

  if key && valid_date?(expiry_date) && valid_cipher_count?(c_count)
    @expiring_seeds[key] = {expires_on: expiry_date, cipher_count: c_count}
  else
    fail_with(:invalid_expiry_argument)
  end
end

#cipher_countInteger

Returns the default cipher count.

Returns:

  • (Integer)


165
166
167
# File 'lib/encoded_token/configuration.rb', line 165

def cipher_count
  @cipher_count
end

#cipher_count=(new_count) ⇒ Object

Sets the default cipher count.

Parameters:

  • new_count (Integer)

    the new cipher count.

Raises:

  • (ArgumentError, RuntimeError)

    if the count is invalid or configuration is locked.



178
179
180
181
182
183
184
185
# File 'lib/encoded_token/configuration.rb', line 178

def cipher_count=(new_count)
  assert_configuration_can_change!

  value = Integer(new_count).abs rescue nil
  fail_with(:invalid_cipher_count) unless valid_cipher_count?(value)

  @cipher_count = value
end

#expiring_seedsHash<Integer, Hash>

Returns the map of expiring seeds.

Returns:

  • (Hash<Integer, Hash>)


192
193
194
# File 'lib/encoded_token/configuration.rb', line 192

def expiring_seeds
  @expiring_seeds || {}
end

#lock!true

Locks the configuration.

Returns:

  • (true)

    the lock status



118
119
120
# File 'lib/encoded_token/configuration.rb', line 118

def lock!
  @locked = true
end

#locked?Boolean

Checks if the configuration is locked.

Returns:

  • (Boolean)


100
101
102
# File 'lib/encoded_token/configuration.rb', line 100

def locked?
  !!@locked
end

#max_input_sizeInteger

Returns the maximum input size.

Returns:

  • (Integer)


201
202
203
# File 'lib/encoded_token/configuration.rb', line 201

def max_input_size
  @max_input_size
end

#max_input_size=(max_size) ⇒ Object

Sets the maximum input size.

Parameters:

  • max_size (Integer)

Raises:

  • (ArgumentError, RuntimeError)

    if the size is invalid or configuration is locked.



213
214
215
216
217
218
219
220
# File 'lib/encoded_token/configuration.rb', line 213

def max_input_size=(max_size)
  assert_configuration_can_change!

  value = Integer(max_size).abs rescue nil
  fail_with(:invalid_max_input_size) if value.nil? || !value.positive?

  @max_input_size = value
end

#min_token_sizeInteger

Returns the minimum token size.

Returns:

  • (Integer)


227
228
229
# File 'lib/encoded_token/configuration.rb', line 227

def min_token_size
  @min_token_size
end

#min_token_size=(min_size) ⇒ Object

Sets the minimum token size.

Parameters:

  • min_size (Integer)

Raises:

  • (ArgumentError, RuntimeError)

    if the size is invalid or configuration is locked.



239
240
241
242
243
244
245
246
# File 'lib/encoded_token/configuration.rb', line 239

def min_token_size=(min_size)
  assert_configuration_can_change!

  value = Integer(min_size).abs rescue nil
  fail_with(:invalid_min_token_size) if value.nil? || !value.positive?

  @min_token_size = value
end

#seedInteger

Returns the master seed.

Returns:

  • (Integer)


253
254
255
# File 'lib/encoded_token/configuration.rb', line 253

def seed
  @seed
end

#seed=(new_seed) ⇒ Object

Sets the master seed.

Parameters:

  • new_seed (Integer)

Raises:

  • (ArgumentError, RuntimeError)

    if the seed is invalid or configuration is locked.



265
266
267
268
269
270
271
272
# File 'lib/encoded_token/configuration.rb', line 265

def seed=(new_seed)
  assert_configuration_can_change!

  value = Integer(new_seed).abs rescue nil
  fail_with(:invalid_seed) if value.nil?

  @seed = value
end

#unlock!false

Unlocks the configuration.

Returns:

  • (false)

    the lock status



127
128
129
# File 'lib/encoded_token/configuration.rb', line 127

def unlock!
  @locked = false
end

#unlocked?Boolean

Checks if the configuration is unlocked.

Returns:

  • (Boolean)


109
110
111
# File 'lib/encoded_token/configuration.rb', line 109

def unlocked?
  !locked?
end