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
VALID_DASHBOARD_CONTENT_SECURITY_POLICIES =
[:default, :strict, false, nil].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 ==



545
546
547
# File 'lib/api_keys/configuration.rb', line 545

def initialize
  set_defaults
end

Instance Attribute Details

#after_authenticationObject

Callbacks (Placeholders for future extension)



62
63
64
# File 'lib/api_keys/configuration.rb', line 62

def after_authentication
  @after_authentication
end

#authenticate_owner_methodObject

Owner Context Configuration



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

def authenticate_owner_method
  @authenticate_owner_method
end

#before_authenticationObject

Callbacks (Placeholders for future extension)



62
63
64
# File 'lib/api_keys/configuration.rb', line 62

def before_authentication
  @before_authentication
end

#cache_ttlObject

Performance



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

def cache_ttl
  @cache_ttl
end

#callbacks_job_queueObject

Background Job Queues



65
66
67
# File 'lib/api_keys/configuration.rb', line 65

def callbacks_job_queue
  @callbacks_job_queue
end

#client_ip_resolverObject

Request Restrictions



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

def client_ip_resolver
  @client_ip_resolver
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



136
137
# File 'lib/api_keys/configuration.rb', line 136

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

#current_owner_methodObject

Owner Context Configuration



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

def current_owner_method
  @current_owner_method
end

#dashboard_allow_cross_environmentObject

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



136
137
# File 'lib/api_keys/configuration.rb', line 136

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

#dashboard_content_security_policyObject

Dashboard Content Security Policy



85
86
87
# File 'lib/api_keys/configuration.rb', line 85

def dashboard_content_security_policy
  @dashboard_content_security_policy
end

#debug_loggingObject (readonly)

Debugging



88
89
90
# File 'lib/api_keys/configuration.rb', line 88

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!



136
137
# File 'lib/api_keys/configuration.rb', line 136

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

#default_max_keys_per_ownerObject

Optional Behaviors



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

def default_max_keys_per_owner
  @default_max_keys_per_owner
end

#default_scopesObject

Returns the value of attribute default_scopes.



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

def default_scopes
  @default_scopes
end

#enable_async_operationsObject (readonly)

Global Async Toggle



68
69
70
# File 'lib/api_keys/configuration.rb', line 68

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)


136
137
138
# File 'lib/api_keys/configuration.rb', line 136

def environments
  @environments
end

#expire_afterObject

Returns the value of attribute expire_after.



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

def expire_after
  @expire_after
end

#hash_strategyObject

Storage & Verification



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

def hash_strategy
  @hash_strategy
end

#headerObject

Core Authentication



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

def header
  @header
end

#https_only_productionObject (readonly)

Security



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

def https_only_production
  @https_only_production
end

#https_strict_modeObject (readonly)

Security



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

def https_strict_mode
  @https_strict_mode
end

#key_store_adapterObject

Returns the value of attribute key_store_adapter.



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

def key_store_adapter
  @key_store_adapter
end

#key_typesObject

Custom writer for key_types that validates prefix uniqueness



136
137
# File 'lib/api_keys/configuration.rb', line 136

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

#parent_controllerObject

Engine Configuration



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

def parent_controller
  @parent_controller
end

#policy_providerObject

Returns the value of attribute policy_provider.



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

def policy_provider
  @policy_provider
end

#query_paramObject

Core Authentication



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

def query_param
  @query_param
end

#require_key_nameObject (readonly)

Optional Behaviors



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

def require_key_name
  @require_key_name
end

#return_textObject

Engine UI Configuration



71
72
73
# File 'lib/api_keys/configuration.rb', line 71

def return_text
  @return_text
end

#return_urlObject

Engine UI Configuration



71
72
73
# File 'lib/api_keys/configuration.rb', line 71

def return_url
  @return_url
end

#secure_compare_procObject

Returns the value of attribute secure_compare_proc.



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

def secure_compare_proc
  @secure_compare_proc
end

#stats_job_queueObject

Background Job Queues



65
66
67
# File 'lib/api_keys/configuration.rb', line 65

def stats_job_queue
  @stats_job_queue
end

#stats_update_intervalObject

Performance



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

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



136
137
# File 'lib/api_keys/configuration.rb', line 136

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

#tenant_resolverObject

Tenant Resolution



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

def tenant_resolver
  @tenant_resolver
end

#token_alphabetObject

Token Generation



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

def token_alphabet
  @token_alphabet
end

#token_lengthObject

Token Generation



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

def token_length
  @token_length
end

#token_prefixObject

Token Generation



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

def token_prefix
  @token_prefix
end

#track_requests_countObject (readonly)

Returns the value of attribute track_requests_count.



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

def track_requests_count
  @track_requests_count
end

Instance Method Details

#parent_controller_classObject



228
229
230
231
232
233
234
235
236
# File 'lib/api_keys/configuration.rb', line 228

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



315
316
317
318
319
# File 'lib/api_keys/configuration.rb', line 315

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