Class: Clavis::Configuration

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

Constant Summary collapse

SUPPORTED_PROVIDERS =
i[google github facebook apple microsoft].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/clavis/configuration.rb', line 16

def initialize
  @providers = {}
  @default_callback_path = "/auth/:provider/callback"
  @default_scopes = nil
  @verbose_logging = false
  @claims_processor = nil

  # Security-related defaults
  @encrypt_tokens = false
  @encryption_key = nil
  @use_rails_credentials = defined?(Rails)
  @parameter_filter_enabled = true

  # Redirect URI validation defaults
  @allowed_redirect_hosts = []
  @exact_redirect_uri_matching = false
  @allow_localhost_in_development = true
  @raise_on_invalid_redirect = true

  # HTTPS enforcement defaults
  @enforce_https = true
  @allow_http_localhost = true
  @verify_ssl = true
  @minimum_tls_version = :TLS1_2

  # Input validation configuration
  @validate_inputs = true
  @sanitize_inputs = true

  # Session management configuration
   = true
  @session_key_prefix = "clavis"

  # Additional configuration options
  @logger = nil
  @log_level = :info
  @token_encryption_key = nil
  @csrf_protection_enabled = true
  @valid_redirect_schemes = %w[http https]
  @view_helpers_auto_include = true

  # User creation configuration
  @user_class = "User"
  @user_finder_method = :find_or_create_from_clavis

  # Rate limiting configuration
  @rate_limiting_enabled = true
  @custom_throttles = {}
end

Instance Attribute Details

#allow_http_localhostObject

Returns the value of attribute allow_http_localhost.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def allow_http_localhost
  @allow_http_localhost
end

#allow_localhost_in_developmentObject

Returns the value of attribute allow_localhost_in_development.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def allow_localhost_in_development
  @allow_localhost_in_development
end

#allowed_redirect_hostsObject

Returns the value of attribute allowed_redirect_hosts.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def allowed_redirect_hosts
  @allowed_redirect_hosts
end

#claims_processorObject

Returns the value of attribute claims_processor.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def claims_processor
  @claims_processor
end

#csrf_protection_enabledObject

Returns the value of attribute csrf_protection_enabled.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def csrf_protection_enabled
  @csrf_protection_enabled
end

#custom_throttlesObject

Returns the value of attribute custom_throttles.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def custom_throttles
  @custom_throttles
end

#default_callback_pathObject

Returns the value of attribute default_callback_path.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def default_callback_path
  @default_callback_path
end

#default_scopesObject

Returns the value of attribute default_scopes.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def default_scopes
  @default_scopes
end

#encrypt_tokensObject

Returns the value of attribute encrypt_tokens.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def encrypt_tokens
  @encrypt_tokens
end

#encryption_keyObject

Returns the value of attribute encryption_key.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def encryption_key
  @encryption_key
end

#enforce_httpsObject

Returns the value of attribute enforce_https.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def enforce_https
  @enforce_https
end

#exact_redirect_uri_matchingObject

Returns the value of attribute exact_redirect_uri_matching.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def exact_redirect_uri_matching
  @exact_redirect_uri_matching
end

#log_levelObject

Returns the value of attribute log_level.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def log_level
  @log_level
end

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def logger
  @logger
end

#minimum_tls_versionObject

Returns the value of attribute minimum_tls_version.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def minimum_tls_version
  @minimum_tls_version
end

#parameter_filter_enabledObject

Returns the value of attribute parameter_filter_enabled.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def parameter_filter_enabled
  @parameter_filter_enabled
end

#providersObject

Returns the value of attribute providers.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def providers
  @providers
end

#raise_on_invalid_redirectObject

Returns the value of attribute raise_on_invalid_redirect.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def raise_on_invalid_redirect
  @raise_on_invalid_redirect
end

#rate_limiting_enabledObject

Returns the value of attribute rate_limiting_enabled.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def rate_limiting_enabled
  @rate_limiting_enabled
end

#rotate_session_after_loginObject

Returns the value of attribute rotate_session_after_login.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def 
  
end

#sanitize_inputsObject

Returns the value of attribute sanitize_inputs.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def sanitize_inputs
  @sanitize_inputs
end

#session_key_prefixObject

Returns the value of attribute session_key_prefix.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def session_key_prefix
  @session_key_prefix
end

#token_encryption_keyObject

Returns the value of attribute token_encryption_key.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def token_encryption_key
  @token_encryption_key
end

#use_rails_credentialsObject

Returns the value of attribute use_rails_credentials.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def use_rails_credentials
  @use_rails_credentials
end

#user_classObject

Returns the value of attribute user_class.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def user_class
  @user_class
end

#user_finder_methodObject

Returns the value of attribute user_finder_method.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def user_finder_method
  @user_finder_method
end

#valid_redirect_schemesObject

Returns the value of attribute valid_redirect_schemes.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def valid_redirect_schemes
  @valid_redirect_schemes
end

#validate_inputsObject

Returns the value of attribute validate_inputs.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def validate_inputs
  @validate_inputs
end

#verbose_loggingObject

Returns the value of attribute verbose_logging.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def verbose_logging
  @verbose_logging
end

#verify_sslObject

Returns the value of attribute verify_ssl.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def verify_ssl
  @verify_ssl
end

#view_helpers_auto_includeObject

Returns the value of attribute view_helpers_auto_include.



7
8
9
# File 'lib/clavis/configuration.rb', line 7

def view_helpers_auto_include
  @view_helpers_auto_include
end

Class Method Details

.supported_providersArray<Symbol>

Returns the list of supported providers

Returns:

  • (Array<Symbol>)

    List of supported provider symbols



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

def self.supported_providers
  SUPPORTED_PROVIDERS
end

Instance Method Details

#callback_path(provider_name) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/clavis/configuration.rb', line 162

def callback_path(provider_name)
  provider_name = provider_name.to_sym if provider_name.is_a?(String)

  # Ensure the provider is configured properly first
  provider_cfg = provider_config(provider_name)

  # Use provider's redirect_uri if specified, otherwise use default
  path = provider_cfg[:redirect_uri] || default_callback_path

  # Replace :provider placeholder with the actual provider name
  path = path.gsub(":provider", provider_name.to_s) if path.include?(":provider")

  path
end

#configured_providersArray<Symbol>

Returns the list of configured providers

Returns:

  • (Array<Symbol>)

    List of configured provider symbols



74
75
76
# File 'lib/clavis/configuration.rb', line 74

def configured_providers
  providers.keys
end

#effective_encryption_keyObject



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

def effective_encryption_key
  return nil unless encrypt_tokens

  if use_rails_credentials && defined?(Rails) && Rails.application.respond_to?(:credentials)
    rails_key = Rails.application.credentials.dig(:clavis, :encryption_key)
    return rails_key if rails_key && !rails_key.to_s.empty?
  end

  encryption_key
end

#post_initializeObject



78
79
80
81
# File 'lib/clavis/configuration.rb', line 78

def post_initialize
  # Set up engine view helpers based on configuration
  Clavis::Engine.include_view_helpers = @view_helpers_auto_include if defined?(Clavis::Engine)
end

#provider_config(provider_name) ⇒ Object



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

def provider_config(provider_name)
  validate_provider!(provider_name)

  # If Rails credentials are enabled, merge with provider config
  if use_rails_credentials && defined?(Rails) && Rails.application.respond_to?(:credentials)
    credentials_config = Rails.application.credentials.dig(:clavis, :providers, provider_name.to_sym)
    if credentials_config && !credentials_config.to_h.empty?
      return providers[provider_name.to_sym].merge(credentials_config.to_h)
    end
  end

  providers[provider_name.to_sym]
end

#provider_configured?(provider_name) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/clavis/configuration.rb', line 83

def provider_configured?(provider_name)
  provider_sym = provider_name.to_sym

  # Check if the provider is defined in the configuration
  return false unless providers&.key?(provider_sym)

  provider_config = providers[provider_sym]

  # Handle empty provider config or non-hash values
  return false unless provider_config.is_a?(Hash)

  # Check for required credentials
  if provider_config[:client_id].nil? || provider_config[:client_id].to_s.strip.empty?
    Clavis::Logging.log_error("Provider '#{provider_name}' is missing client_id")
    return false
  end

  if provider_config[:client_secret].nil? || provider_config[:client_secret].to_s.strip.empty?
    Clavis::Logging.log_error("Provider '#{provider_name}' is missing client_secret")
    return false
  end

  # Apple doesn't always require a redirect_uri
  if provider_sym != :apple &&
     (provider_config[:redirect_uri].nil? ||
      provider_config[:redirect_uri].to_s.strip.empty?)
    Clavis::Logging.log_error("Provider '#{provider_name}' is missing redirect_uri")
    return false
  end

  # All checks passed
  true
end

#should_verify_ssl?Boolean

Returns:

  • (Boolean)


188
189
190
191
192
193
194
# File 'lib/clavis/configuration.rb', line 188

def should_verify_ssl?
  # Always verify SSL in production
  return true if defined?(Rails) && Rails.env.production?

  # Otherwise, use the configured value
  verify_ssl
end

#validate_provider!(provider_name) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/clavis/configuration.rb', line 117

def validate_provider!(provider_name)
  provider_sym = provider_name.to_sym

  # Check if the provider is defined in the configuration
  unless providers&.key?(provider_sym)
    raise Clavis::ProviderNotConfigured, "Provider '#{provider_name}' is not defined in the configuration"
  end

  provider_config = providers[provider_sym]

  # Handle empty provider config or non-hash values
  unless provider_config.is_a?(Hash)
    raise Clavis::ProviderNotConfigured, "Provider '#{provider_name}' has invalid configuration"
  end

  # Check for required credentials
  if provider_config[:client_id].nil? || provider_config[:client_id].to_s.strip.empty?
    raise Clavis::ProviderNotConfigured, "Provider '#{provider_name}' is missing client_id"
  end

  if provider_config[:client_secret].nil? || provider_config[:client_secret].to_s.strip.empty?
    raise Clavis::ProviderNotConfigured, "Provider '#{provider_name}' is missing client_secret"
  end

  # Apple doesn't always require a redirect_uri
  return if provider_sym == :apple
  return unless provider_config[:redirect_uri].nil? || provider_config[:redirect_uri].to_s.strip.empty?

  raise Clavis::ProviderNotConfigured, "Provider '#{provider_name}' is missing redirect_uri"
end