Class: LpTokenAuth::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/lp_token_auth/config.rb

Overview

LpTokenAuth::Config manages the configuration options for the token. These can be set with the initializer provided with the generator.

Constant Summary collapse

DEFAULT_VALUES =

Provides default values to token options ENV defaults defined as procs to ensure they return their latest value at call time (else they return nil, since ENV values may not be initialized before gem code)

{
  algorithm: 'HS512',
  expires: (7 * 24),
  token_transport: [:cookie],
  jwe_private_key: -> { ENV['JWE_PRIVATE_KEY'] },
  jwe_encryption: -> { ENV['JWE_ENCRYPTION'] || 'A256GCM' }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#algorithmObject

Creates virtual attributes for configuration options:

  • algorithm is a string corresponding to token encryption algorithm to use
  • expires is an integer corresponding to the number of hours that the token is active
  • secret is a string corresponding to the secret key used when encrypting the token
  • token_transport is a string indicating where to include the token in the HTTP response


12
13
14
# File 'lib/lp_token_auth/config.rb', line 12

def algorithm
  @algorithm
end

#expiresObject

Creates virtual attributes for configuration options:

  • algorithm is a string corresponding to token encryption algorithm to use
  • expires is an integer corresponding to the number of hours that the token is active
  • secret is a string corresponding to the secret key used when encrypting the token
  • token_transport is a string indicating where to include the token in the HTTP response


12
13
14
# File 'lib/lp_token_auth/config.rb', line 12

def expires
  @expires
end

#jwe_encryptionObject

Creates virtual attributes for configuration options:

  • algorithm is a string corresponding to token encryption algorithm to use
  • expires is an integer corresponding to the number of hours that the token is active
  • secret is a string corresponding to the secret key used when encrypting the token
  • token_transport is a string indicating where to include the token in the HTTP response


12
13
14
# File 'lib/lp_token_auth/config.rb', line 12

def jwe_encryption
  @jwe_encryption
end

#jwe_private_keyObject

Creates virtual attributes for configuration options:

  • algorithm is a string corresponding to token encryption algorithm to use
  • expires is an integer corresponding to the number of hours that the token is active
  • secret is a string corresponding to the secret key used when encrypting the token
  • token_transport is a string indicating where to include the token in the HTTP response


12
13
14
# File 'lib/lp_token_auth/config.rb', line 12

def jwe_private_key
  @jwe_private_key
end

#secretObject

Creates virtual attributes for configuration options:

  • algorithm is a string corresponding to token encryption algorithm to use
  • expires is an integer corresponding to the number of hours that the token is active
  • secret is a string corresponding to the secret key used when encrypting the token
  • token_transport is a string indicating where to include the token in the HTTP response


12
13
14
# File 'lib/lp_token_auth/config.rb', line 12

def secret
  @secret
end

#token_transportObject

Creates virtual attributes for configuration options:

  • algorithm is a string corresponding to token encryption algorithm to use
  • expires is an integer corresponding to the number of hours that the token is active
  • secret is a string corresponding to the secret key used when encrypting the token
  • token_transport is a string indicating where to include the token in the HTTP response


12
13
14
# File 'lib/lp_token_auth/config.rb', line 12

def token_transport
  @token_transport
end

Instance Method Details

#get_default_value(key) ⇒ String, Integer

Retrieves default value for a token option

Parameters:

  • the token option name

Returns:

  • the value of the token option



39
40
41
42
# File 'lib/lp_token_auth/config.rb', line 39

def get_default_value(key)
  default = DEFAULT_VALUES[key]
  default.is_a?(Proc) ? default.call : default
end

#get_option(key) ⇒ String, Integer

Retrieves value for token option, either as set by the application, or the default

Parameters:

  • the token option name

Returns:

  • the value of the token option

Raises:

  • if the option has not been set by the application and a default value does not exist



30
31
32
33
34
# File 'lib/lp_token_auth/config.rb', line 30

def get_option(key)
  option = send(key) || get_default_value(key)
  raise LpTokenAuth::Error, "Missing config option value: #{key}" unless option
  option
end