Class: Clavis::Configuration
- Inherits:
-
Object
- Object
- Clavis::Configuration
- Defined in:
- lib/clavis/configuration.rb
Constant Summary collapse
- SUPPORTED_PROVIDERS =
i[google github facebook apple microsoft].freeze
Instance Attribute Summary collapse
-
#allow_http_localhost ⇒ Object
Returns the value of attribute allow_http_localhost.
-
#allow_localhost_in_development ⇒ Object
Returns the value of attribute allow_localhost_in_development.
-
#allowed_redirect_hosts ⇒ Object
Returns the value of attribute allowed_redirect_hosts.
-
#claims_processor ⇒ Object
Returns the value of attribute claims_processor.
-
#csrf_protection_enabled ⇒ Object
Returns the value of attribute csrf_protection_enabled.
-
#custom_throttles ⇒ Object
Returns the value of attribute custom_throttles.
-
#default_callback_path ⇒ Object
Returns the value of attribute default_callback_path.
-
#default_scopes ⇒ Object
Returns the value of attribute default_scopes.
-
#encrypt_tokens ⇒ Object
Returns the value of attribute encrypt_tokens.
-
#encryption_key ⇒ Object
Returns the value of attribute encryption_key.
-
#enforce_https ⇒ Object
Returns the value of attribute enforce_https.
-
#exact_redirect_uri_matching ⇒ Object
Returns the value of attribute exact_redirect_uri_matching.
-
#log_level ⇒ Object
Returns the value of attribute log_level.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#minimum_tls_version ⇒ Object
Returns the value of attribute minimum_tls_version.
-
#parameter_filter_enabled ⇒ Object
Returns the value of attribute parameter_filter_enabled.
-
#providers ⇒ Object
Returns the value of attribute providers.
-
#raise_on_invalid_redirect ⇒ Object
Returns the value of attribute raise_on_invalid_redirect.
-
#rate_limiting_enabled ⇒ Object
Returns the value of attribute rate_limiting_enabled.
-
#rotate_session_after_login ⇒ Object
Returns the value of attribute rotate_session_after_login.
-
#sanitize_inputs ⇒ Object
Returns the value of attribute sanitize_inputs.
-
#session_key_prefix ⇒ Object
Returns the value of attribute session_key_prefix.
-
#token_encryption_key ⇒ Object
Returns the value of attribute token_encryption_key.
-
#use_rails_credentials ⇒ Object
Returns the value of attribute use_rails_credentials.
-
#user_class ⇒ Object
Returns the value of attribute user_class.
-
#user_finder_method ⇒ Object
Returns the value of attribute user_finder_method.
-
#valid_redirect_schemes ⇒ Object
Returns the value of attribute valid_redirect_schemes.
-
#validate_inputs ⇒ Object
Returns the value of attribute validate_inputs.
-
#verbose_logging ⇒ Object
Returns the value of attribute verbose_logging.
-
#verify_ssl ⇒ Object
Returns the value of attribute verify_ssl.
-
#view_helpers_auto_include ⇒ Object
Returns the value of attribute view_helpers_auto_include.
Class Method Summary collapse
-
.supported_providers ⇒ Array<Symbol>
Returns the list of supported providers.
Instance Method Summary collapse
- #callback_path(provider_name) ⇒ Object
-
#configured_providers ⇒ Array<Symbol>
Returns the list of configured providers.
- #effective_encryption_key ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #post_initialize ⇒ Object
- #provider_config(provider_name) ⇒ Object
- #provider_configured?(provider_name) ⇒ Boolean
- #should_verify_ssl? ⇒ Boolean
- #validate_provider!(provider_name) ⇒ Object
Constructor Details
#initialize ⇒ Configuration
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 @rotate_session_after_login = 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_localhost ⇒ Object
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_development ⇒ Object
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_hosts ⇒ Object
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_processor ⇒ Object
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_enabled ⇒ Object
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_throttles ⇒ Object
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_path ⇒ Object
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_scopes ⇒ Object
Returns the value of attribute default_scopes.
7 8 9 |
# File 'lib/clavis/configuration.rb', line 7 def default_scopes @default_scopes end |
#encrypt_tokens ⇒ Object
Returns the value of attribute encrypt_tokens.
7 8 9 |
# File 'lib/clavis/configuration.rb', line 7 def encrypt_tokens @encrypt_tokens end |
#encryption_key ⇒ Object
Returns the value of attribute encryption_key.
7 8 9 |
# File 'lib/clavis/configuration.rb', line 7 def encryption_key @encryption_key end |
#enforce_https ⇒ Object
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_matching ⇒ Object
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_level ⇒ Object
Returns the value of attribute log_level.
7 8 9 |
# File 'lib/clavis/configuration.rb', line 7 def log_level @log_level end |
#logger ⇒ Object
Returns the value of attribute logger.
7 8 9 |
# File 'lib/clavis/configuration.rb', line 7 def logger @logger end |
#minimum_tls_version ⇒ Object
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_enabled ⇒ Object
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 |
#providers ⇒ Object
Returns the value of attribute providers.
7 8 9 |
# File 'lib/clavis/configuration.rb', line 7 def providers @providers end |
#raise_on_invalid_redirect ⇒ Object
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_enabled ⇒ Object
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_login ⇒ Object
Returns the value of attribute rotate_session_after_login.
7 8 9 |
# File 'lib/clavis/configuration.rb', line 7 def rotate_session_after_login @rotate_session_after_login end |
#sanitize_inputs ⇒ Object
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_prefix ⇒ Object
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_key ⇒ Object
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_credentials ⇒ Object
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_class ⇒ Object
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_method ⇒ Object
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_schemes ⇒ Object
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_inputs ⇒ Object
Returns the value of attribute validate_inputs.
7 8 9 |
# File 'lib/clavis/configuration.rb', line 7 def validate_inputs @validate_inputs end |
#verbose_logging ⇒ Object
Returns the value of attribute verbose_logging.
7 8 9 |
# File 'lib/clavis/configuration.rb', line 7 def verbose_logging @verbose_logging end |
#verify_ssl ⇒ Object
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_include ⇒ Object
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_providers ⇒ Array<Symbol>
Returns the list of supported providers
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_providers ⇒ Array<Symbol>
Returns the list of configured providers
74 75 76 |
# File 'lib/clavis/configuration.rb', line 74 def configured_providers providers.keys end |
#effective_encryption_key ⇒ Object
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_initialize ⇒ Object
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
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
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 |