Class: BetterAuth::Configuration
- Inherits:
-
Object
- Object
- BetterAuth::Configuration
- Defined in:
- lib/better_auth/configuration.rb
Constant Summary collapse
- DEFAULT_BASE_PATH =
"/api/auth"- DEFAULT_SECRET =
"better-auth-secret-12345678901234567890"- DEFAULT_SESSION =
{ update_age: 24 * 60 * 60, expires_in: 60 * 60 * 24 * 7, fresh_age: 60 * 60 * 24 }.freeze
- DEFAULT_EMAIL_AND_PASSWORD =
{ min_password_length: 8, max_password_length: 128 }.freeze
- DEFAULT_PASSWORD_HASHER =
:scrypt- SUPPORTED_PASSWORD_HASHERS =
[:scrypt, :bcrypt].freeze
- DEFAULT_STATELESS_SESSION =
{ cookie_cache: { enabled: true, strategy: "jwe", refresh_cache: true } }.freeze
- DEFAULT_STATELESS_ACCOUNT =
{ store_state_strategy: "cookie", store_account_cookie: true }.freeze
Instance Attribute Summary collapse
-
#account ⇒ Object
readonly
Returns the value of attribute account.
-
#advanced ⇒ Object
readonly
Returns the value of attribute advanced.
-
#app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
#base_path ⇒ Object
readonly
Returns the value of attribute base_path.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#context_base_url ⇒ Object
readonly
Returns the value of attribute context_base_url.
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#database_hooks ⇒ Object
readonly
Returns the value of attribute database_hooks.
-
#disabled_paths ⇒ Object
readonly
Returns the value of attribute disabled_paths.
-
#email_and_password ⇒ Object
readonly
Returns the value of attribute email_and_password.
-
#email_verification ⇒ Object
readonly
Returns the value of attribute email_verification.
-
#experimental ⇒ Object
readonly
Returns the value of attribute experimental.
-
#hooks ⇒ Object
readonly
Returns the value of attribute hooks.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#on_api_error ⇒ Object
readonly
Returns the value of attribute on_api_error.
-
#password_hasher ⇒ Object
readonly
Returns the value of attribute password_hasher.
-
#plugins ⇒ Object
readonly
Returns the value of attribute plugins.
-
#rate_limit ⇒ Object
readonly
Returns the value of attribute rate_limit.
-
#secondary_storage ⇒ Object
readonly
Returns the value of attribute secondary_storage.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#social_providers ⇒ Object
readonly
Returns the value of attribute social_providers.
-
#trusted_origins ⇒ Object
readonly
Returns the value of attribute trusted_origins.
-
#trusted_origins_callback ⇒ Object
readonly
Returns the value of attribute trusted_origins_callback.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
-
#verification ⇒ Object
readonly
Returns the value of attribute verification.
Class Method Summary collapse
- .matches_origin_pattern?(url, pattern, allow_relative_paths: false) ⇒ Boolean
- .origin_for(uri) ⇒ Object
- .parse_uri(url) ⇒ Object
- .relative_path_allowed?(url) ⇒ Boolean
- .wildcard_match?(pattern, value) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Configuration
constructor
A new instance of Configuration.
- #merge_defaults!(defaults) ⇒ Object
- #production? ⇒ Boolean
- #to_h ⇒ Object
- #trusted_origin?(url, allow_relative_paths: false) ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Configuration
Returns a new instance of Configuration.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/better_auth/configuration.rb', line 60 def initialize( = {}) = symbolize_keys() @explicit_options = deep_dup() @logger = [:logger] @app_name = [:app_name] || "Better Auth" @base_path = normalize_base_path(.fetch(:base_path, DEFAULT_BASE_PATH)) @database = [:database] @secondary_storage = [:secondary_storage] @plugins = normalize_plugins([:plugins]) @advanced = deep_merge({}, symbolize_keys([:advanced] || {})) @disabled_paths = Array([:disabled_paths]).compact.map(&:to_s) @database_hooks = [:database_hooks] @hooks = [:hooks] @on_api_error = symbolize_keys([:on_api_error] || [:on_apierror] || {}) @social_providers = symbolize_keys([:social_providers] || {}) @trusted_origins_callback = [:trusted_origins] if [:trusted_origins].respond_to?(:call) @secret = resolve_secret() @base_url, @context_base_url = normalize_base_url([:base_url]) @session = normalize_session([:session]) @account = normalize_account([:account]) @user = symbolize_keys([:user] || {}) @verification = symbolize_keys([:verification] || {}) @email_and_password = normalize_email_and_password([:email_and_password]) @password_hasher = normalize_password_hasher([:password_hasher]) @email_verification = symbolize_keys([:email_verification] || {}) @experimental = normalize_experimental([:experimental]) @rate_limit = normalize_rate_limit([:rate_limit]) @trusted_origins = normalize_trusted_origins([:trusted_origins]) validate_secret end |
Instance Attribute Details
#account ⇒ Object (readonly)
Returns the value of attribute account.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def account @account end |
#advanced ⇒ Object (readonly)
Returns the value of attribute advanced.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def advanced @advanced end |
#app_name ⇒ Object (readonly)
Returns the value of attribute app_name.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def app_name @app_name end |
#base_path ⇒ Object (readonly)
Returns the value of attribute base_path.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def base_path @base_path end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def base_url @base_url end |
#context_base_url ⇒ Object (readonly)
Returns the value of attribute context_base_url.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def context_base_url @context_base_url end |
#database ⇒ Object (readonly)
Returns the value of attribute database.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def database @database end |
#database_hooks ⇒ Object (readonly)
Returns the value of attribute database_hooks.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def database_hooks @database_hooks end |
#disabled_paths ⇒ Object (readonly)
Returns the value of attribute disabled_paths.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def disabled_paths @disabled_paths end |
#email_and_password ⇒ Object (readonly)
Returns the value of attribute email_and_password.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def email_and_password @email_and_password end |
#email_verification ⇒ Object (readonly)
Returns the value of attribute email_verification.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def email_verification @email_verification end |
#experimental ⇒ Object (readonly)
Returns the value of attribute experimental.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def experimental @experimental end |
#hooks ⇒ Object (readonly)
Returns the value of attribute hooks.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def hooks @hooks end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def logger @logger end |
#on_api_error ⇒ Object (readonly)
Returns the value of attribute on_api_error.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def on_api_error @on_api_error end |
#password_hasher ⇒ Object (readonly)
Returns the value of attribute password_hasher.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def password_hasher @password_hasher end |
#plugins ⇒ Object (readonly)
Returns the value of attribute plugins.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def plugins @plugins end |
#rate_limit ⇒ Object (readonly)
Returns the value of attribute rate_limit.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def rate_limit @rate_limit end |
#secondary_storage ⇒ Object (readonly)
Returns the value of attribute secondary_storage.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def secondary_storage @secondary_storage end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def secret @secret end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def session @session end |
#social_providers ⇒ Object (readonly)
Returns the value of attribute social_providers.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def @social_providers end |
#trusted_origins ⇒ Object (readonly)
Returns the value of attribute trusted_origins.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def trusted_origins @trusted_origins end |
#trusted_origins_callback ⇒ Object (readonly)
Returns the value of attribute trusted_origins_callback.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def trusted_origins_callback @trusted_origins_callback end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def user @user end |
#verification ⇒ Object (readonly)
Returns the value of attribute verification.
33 34 35 |
# File 'lib/better_auth/configuration.rb', line 33 def verification @verification end |
Class Method Details
.matches_origin_pattern?(url, pattern, allow_relative_paths: false) ⇒ Boolean
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/better_auth/configuration.rb', line 141 def self.matches_origin_pattern?(url, pattern, allow_relative_paths: false) return relative_path_allowed?(url) if url.start_with?("/") && allow_relative_paths return false if url.start_with?("/") uri = parse_uri(url) return false unless uri if pattern.include?("*") || pattern.include?("?") return wildcard_match?(pattern, origin_for(uri) || url) if pattern.include?("://") return wildcard_match?(pattern, uri.host.to_s) end protocol = uri.scheme&.then { |scheme| "#{scheme}:" } if protocol == "http:" || protocol == "https:" || protocol.nil? pattern == origin_for(uri) else url.start_with?(pattern) end end |
.origin_for(uri) ⇒ Object
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/better_auth/configuration.rb', line 172 def self.origin_for(uri) return nil unless uri.scheme && uri.host port = uri.port default_port = (uri.scheme == "http" && port == 80) || (uri.scheme == "https" && port == 443) host = uri.host host = "[#{host}]" if host.include?(":") && !host.start_with?("[") origin = "#{uri.scheme}://#{host}" default_port ? origin : "#{origin}:#{port}" end |
.parse_uri(url) ⇒ Object
166 167 168 169 170 |
# File 'lib/better_auth/configuration.rb', line 166 def self.parse_uri(url) URI.parse(url) rescue URI::InvalidURIError nil end |
.relative_path_allowed?(url) ⇒ Boolean
162 163 164 |
# File 'lib/better_auth/configuration.rb', line 162 def self.relative_path_allowed?(url) %r{\A/(?!/|\\|%2f|%5c)[\w\-.+/@]*(?:\?[\w\-.+/=&%@]*)?\z}i.match?(url) end |
.wildcard_match?(pattern, value) ⇒ Boolean
183 184 185 186 |
# File 'lib/better_auth/configuration.rb', line 183 def self.wildcard_match?(pattern, value) regex = Regexp.escape(pattern).gsub("\\*", ".*").gsub("\\?", ".") /\A#{regex}\z/.match?(value) end |
Instance Method Details
#merge_defaults!(defaults) ⇒ Object
131 132 133 134 135 136 137 138 139 |
# File 'lib/better_auth/configuration.rb', line 131 def merge_defaults!(defaults) normalized = symbolize_keys(defaults || {}) normalized.each do |key, value| next unless respond_to?(key) next if key == :database_hooks instance_variable_set("@#{key}", merge_default_value([key], public_send(key), value)) end end |
#production? ⇒ Boolean
99 100 101 |
# File 'lib/better_auth/configuration.rb', line 99 def production? production_environment? end |
#to_h ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/better_auth/configuration.rb', line 103 def to_h { app_name: app_name, base_url: base_url, base_path: base_path, secret: secret, database: database, plugins: plugins, trusted_origins: trusted_origins, rate_limit: rate_limit, session: session, account: account, user: user, verification: verification, advanced: advanced, email_and_password: email_and_password, password_hasher: password_hasher, email_verification: email_verification, social_providers: , experimental: experimental, secondary_storage: secondary_storage, database_hooks: database_hooks, hooks: hooks, on_api_error: on_api_error, disabled_paths: disabled_paths } end |
#trusted_origin?(url, allow_relative_paths: false) ⇒ Boolean
93 94 95 96 97 |
# File 'lib/better_auth/configuration.rb', line 93 def trusted_origin?(url, allow_relative_paths: false) trusted_origins.any? do |origin| self.class.matches_origin_pattern?(url, origin, allow_relative_paths: allow_relative_paths) end end |