Class: ApiKeys::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/api_keys/configuration.rb

Overview

Defines the configuration options for the ApiKeys gem. These options can be set in an initializer, e.g., config/initializers/api_keys.rb

Constant Summary collapse

DEFAULT_CALLBACK =

Default empty callback proc

->(_context){}.freeze
DEFAULT_PARENT_CONTROLLER =
"::ApplicationController"
VALID_HASH_STRATEGIES =
%i[sha256 bcrypt].freeze
VALID_TOKEN_ALPHABETS =
%i[base58 hex].freeze
TOKEN_LENGTH_RANGE =
(16..64)
MAX_CONFIGURED_SCOPES =
100
CONFIG_NAME_PATTERN =
/\A[a-zA-Z0-9_-]{1,64}\z/
HTTP_HEADER_PATTERN =
/\A[!#$%&'*+\-.^_`|~0-9A-Za-z]{1,128}\z/
QUERY_PARAM_PATTERN =
/\A[a-zA-Z0-9_.~-]{1,128}\z/
METHOD_NAME_PATTERN =
/\A[a-zA-Z_]\w*[!?]?\z/
CONSTANT_NAME_PATTERN =
/\A(?:::)?[A-Z]\w*(?:::[A-Z]\w*)*\z/
BOOLEAN_SETTINGS =
%i[
  require_key_name track_requests_count https_only_production https_strict_mode
  enable_async_operations debug_logging strict_environment_isolation
  dashboard_allow_cross_environment
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Initialization ==



477
478
479
# File 'lib/api_keys/configuration.rb', line 477

def initialize
  set_defaults
end

Instance Attribute Details

#after_authenticationObject

Callbacks (Placeholders for future extension)



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

def after_authentication
  @after_authentication
end

#authenticate_owner_methodObject

Owner Context Configuration



32
33
34
# File 'lib/api_keys/configuration.rb', line 32

def authenticate_owner_method
  @authenticate_owner_method
end

#before_authenticationObject

Callbacks (Placeholders for future extension)



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

def before_authentication
  @before_authentication
end

#cache_ttlObject

Performance



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

def cache_ttl
  @cache_ttl
end

#callbacks_job_queueObject

Background Job Queues



51
52
53
# File 'lib/api_keys/configuration.rb', line 51

def callbacks_job_queue
  @callbacks_job_queue
end

#current_environmentProc, Symbol

Returns Lambda or symbol returning current environment.

Examples:

config.current_environment = -> { Rails.env.production? ? :live : :test }

Returns:

  • (Proc, Symbol)

    Lambda or symbol returning current environment



103
104
# File 'lib/api_keys/configuration.rb', line 103

attr_reader :environments, :current_environment, :strict_environment_isolation,
:default_key_type, :dashboard_allow_cross_environment

#current_owner_methodObject

Owner Context Configuration



32
33
34
# File 'lib/api_keys/configuration.rb', line 32

def current_owner_method
  @current_owner_method
end

#dashboard_allow_cross_environmentObject

Key Types & Environments (Stripe-style publishable/secret keys)



103
104
# File 'lib/api_keys/configuration.rb', line 103

attr_reader :environments, :current_environment, :strict_environment_isolation,
:default_key_type, :dashboard_allow_cross_environment

#debug_loggingObject (readonly)

Debugging



60
61
62
# File 'lib/api_keys/configuration.rb', line 60

def debug_logging
  @debug_logging
end

#default_key_typeSymbol?

Returns Default key type when not specified in create_api_key!.

Examples:

config.default_key_type = :secret

Returns:

  • (Symbol, nil)

    Default key type when not specified in create_api_key!



103
104
# File 'lib/api_keys/configuration.rb', line 103

attr_reader :environments, :current_environment, :strict_environment_isolation,
:default_key_type, :dashboard_allow_cross_environment

#default_max_keys_per_ownerObject

Optional Behaviors



35
36
37
# File 'lib/api_keys/configuration.rb', line 35

def default_max_keys_per_owner
  @default_max_keys_per_owner
end

#default_scopesObject

Returns the value of attribute default_scopes.



36
37
38
# File 'lib/api_keys/configuration.rb', line 36

def default_scopes
  @default_scopes
end

#enable_async_operationsObject (readonly)

Global Async Toggle



54
55
56
# File 'lib/api_keys/configuration.rb', line 54

def enable_async_operations
  @enable_async_operations
end

#environmentsHash

Returns Environment definitions. Each environment has:

  • :prefix_segment [String, nil] Middle part of prefix (e.g., "test" → pk_test_).

Examples:

config.environments = { test: { prefix_segment: "test" }, live: { prefix_segment: "live" } }

Returns:

  • (Hash)

    Environment definitions. Each environment has:

    • :prefix_segment [String, nil] Middle part of prefix (e.g., "test" → pk_test_)

Raises:

  • (ArgumentError)


103
104
105
# File 'lib/api_keys/configuration.rb', line 103

def environments
  @environments
end

#expire_afterObject

Returns the value of attribute expire_after.



36
37
38
# File 'lib/api_keys/configuration.rb', line 36

def expire_after
  @expire_after
end

#hash_strategyObject

Storage & Verification



24
25
26
# File 'lib/api_keys/configuration.rb', line 24

def hash_strategy
  @hash_strategy
end

#headerObject

Core Authentication



18
19
20
# File 'lib/api_keys/configuration.rb', line 18

def header
  @header
end

#https_only_productionObject (readonly)

Security



42
43
44
# File 'lib/api_keys/configuration.rb', line 42

def https_only_production
  @https_only_production
end

#https_strict_modeObject (readonly)

Security



42
43
44
# File 'lib/api_keys/configuration.rb', line 42

def https_strict_mode
  @https_strict_mode
end

#key_store_adapterObject

Returns the value of attribute key_store_adapter.



26
27
28
# File 'lib/api_keys/configuration.rb', line 26

def key_store_adapter
  @key_store_adapter
end

#key_typesObject

Custom writer for key_types that validates prefix uniqueness



103
104
# File 'lib/api_keys/configuration.rb', line 103

attr_reader :environments, :current_environment, :strict_environment_isolation,
:default_key_type, :dashboard_allow_cross_environment

#parent_controllerObject

Engine Configuration



29
30
31
# File 'lib/api_keys/configuration.rb', line 29

def parent_controller
  @parent_controller
end

#policy_providerObject

Returns the value of attribute policy_provider.



26
27
28
# File 'lib/api_keys/configuration.rb', line 26

def policy_provider
  @policy_provider
end

#query_paramObject

Core Authentication



18
19
20
# File 'lib/api_keys/configuration.rb', line 18

def query_param
  @query_param
end

#require_key_nameObject (readonly)

Optional Behaviors



35
36
37
# File 'lib/api_keys/configuration.rb', line 35

def require_key_name
  @require_key_name
end

#return_textObject

Engine UI Configuration



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

def return_text
  @return_text
end

#return_urlObject

Engine UI Configuration



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

def return_url
  @return_url
end

#secure_compare_procObject

Returns the value of attribute secure_compare_proc.



25
26
27
# File 'lib/api_keys/configuration.rb', line 25

def secure_compare_proc
  @secure_compare_proc
end

#stats_job_queueObject

Background Job Queues



51
52
53
# File 'lib/api_keys/configuration.rb', line 51

def stats_job_queue
  @stats_job_queue
end

#stats_update_intervalObject

Performance



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

def stats_update_interval
  @stats_update_interval
end

#strict_environment_isolationBoolean

Returns If true, keys only work in their matching environment.

Returns:

  • (Boolean)

    If true, keys only work in their matching environment



103
104
# File 'lib/api_keys/configuration.rb', line 103

attr_reader :environments, :current_environment, :strict_environment_isolation,
:default_key_type, :dashboard_allow_cross_environment

#tenant_resolverObject

Tenant Resolution



45
46
47
# File 'lib/api_keys/configuration.rb', line 45

def tenant_resolver
  @tenant_resolver
end

#token_alphabetObject

Token Generation



21
22
23
# File 'lib/api_keys/configuration.rb', line 21

def token_alphabet
  @token_alphabet
end

#token_lengthObject

Token Generation



21
22
23
# File 'lib/api_keys/configuration.rb', line 21

def token_length
  @token_length
end

#token_prefixObject

Token Generation



21
22
23
# File 'lib/api_keys/configuration.rb', line 21

def token_prefix
  @token_prefix
end

#track_requests_countObject (readonly)

Returns the value of attribute track_requests_count.



36
37
38
# File 'lib/api_keys/configuration.rb', line 36

def track_requests_count
  @track_requests_count
end

Instance Method Details

#parent_controller_classObject



194
195
196
197
198
199
200
201
202
# File 'lib/api_keys/configuration.rb', line 194

def parent_controller_class
  candidate = if !@parent_controller_explicitly_configured && defined?(ApiKeys::Engine) &&
                 ApiKeys::Engine.config.parent_controller.present?
                ApiKeys::Engine.config.parent_controller
              else
                parent_controller
              end
  candidate.is_a?(Class) ? candidate : candidate.constantize
end

#resolved_token_prefixObject



275
276
277
278
279
# File 'lib/api_keys/configuration.rb', line 275

def resolved_token_prefix
  value = @token_prefix.respond_to?(:call) ? @token_prefix.call : @token_prefix
  validate_resolved_prefix!(value)
  value
end