better_auth 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b62f1d0f1e0fc4c774ab2d4907f9229f4cbcae46599cf8b72c03a4d5984aafcf
4
- data.tar.gz: a02c36229e3d829a1d3c24d64e9e560fc9f592e9ad4ad915bcd6a962fd0f633a
3
+ metadata.gz: 98014e245459f3b5a7de74fbd1e41ac5449ff223e20e5f9c8ef9500e15c55d9f
4
+ data.tar.gz: 28252ead0ea2f370233bcc73bcc41568604608ff2d4d891a66abf71f49e01497
5
5
  SHA512:
6
- metadata.gz: 50d18b3901b1e7292b3b4089e1eb388dadb16836d88a6d8af35ecb8061005dc062712010865b0f131b768538fbc2e64042edfb5b652feaad979f93d03f6ca7fa
7
- data.tar.gz: ce679e88e9e12dd1d36aab49a20bb6d2a8e55e95de6eeccbdc3b4bd3736924d9496fb5148e874132f6f07efb914035ed6ce0cd200216355390bfe86b016c6ff8
6
+ metadata.gz: 22e4e6488cebfdc63f77d68747df4283ccf27c82381ab6bb7e372485f0c2d93cb4e37dce4748e75020f6556ff08a675ba42d47cf39a57f90c86f2d4a396c09b4
7
+ data.tar.gz: a05af007c3dcd034bc035a37e825991ce76ff8af9199ae33f4a3ecf2250718ac848ae52baa84d2d60c7b5eace5d7f2750de2d40dde779b182640184b50d1a765
data/CHANGELOG.md CHANGED
@@ -50,7 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
50
50
 
51
51
  ### Changed
52
52
 
53
- - Extracted MongoDB adapter support behind the external `better_auth-mongo-adapter` shim while preserving compatibility for existing adapter configuration.
53
+ - Extracted MongoDB adapter support behind the external `better_auth-mongodb` package while preserving compatibility for existing adapter configuration.
54
54
  - Updated auth routes, router behavior, rate limiting, password and email-verification flows, and schema metadata to match upstream semantics more closely.
55
55
 
56
56
  ### Fixed
data/README.md CHANGED
@@ -114,7 +114,7 @@ Custom Better Auth-style password callbacks are still supported through `email_a
114
114
 
115
115
  ### Database Adapters
116
116
 
117
- The core gem ships framework-agnostic adapters for memory, PostgreSQL, MySQL, SQLite, and MSSQL. Driver gems are loaded only when their adapter is instantiated. MongoDB support lives in the external `better_auth-mongo-adapter` package so apps that do not use MongoDB do not install the Mongo driver.
117
+ The core gem ships framework-agnostic adapters for memory, PostgreSQL, MySQL, SQLite, and MSSQL. Driver gems are loaded only when their adapter is instantiated. MongoDB support lives in the external `better_auth-mongodb` package so apps that do not use MongoDB do not install the Mongo driver.
118
118
 
119
119
  ```ruby
120
120
  auth = BetterAuth.auth(
@@ -124,7 +124,7 @@ auth = BetterAuth.auth(
124
124
  ```
125
125
 
126
126
  ```ruby
127
- require "better_auth/mongo_adapter"
127
+ require "better_auth/mongodb"
128
128
 
129
129
  auth = BetterAuth.auth(
130
130
  secret: ENV.fetch("BETTER_AUTH_SECRET"),
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  begin
4
- require "better_auth/mongo_adapter"
4
+ require "better_auth/mongodb"
5
5
  rescue LoadError => error
6
- raise if error.path && error.path != "better_auth/mongo_adapter"
6
+ raise if error.path && error.path != "better_auth/mongodb"
7
7
 
8
- raise LoadError, "BetterAuth::Adapters::MongoDB requires the better_auth-mongo-adapter gem. Add `gem \"better_auth-mongo-adapter\"` and `require \"better_auth/mongo_adapter\"`."
8
+ raise LoadError, "BetterAuth::Adapters::MongoDB requires the better_auth-mongodb gem. Add `gem \"better_auth-mongodb\"` and `require \"better_auth/mongodb\"`."
9
9
  end
@@ -2,7 +2,23 @@
2
2
 
3
3
  module BetterAuth
4
4
  class Auth
5
- attr_reader :handler, :api, :options, :context, :error_codes
5
+ TELEMETRY_ADAPTER_IDS = {
6
+ "BetterAuth::Adapters::Memory" => "memory",
7
+ "BetterAuth::Adapters::Postgres" => "postgres",
8
+ "BetterAuth::Adapters::MySQL" => "mysql",
9
+ "BetterAuth::Adapters::SQLite" => "sqlite",
10
+ "BetterAuth::Adapters::MSSQL" => "mssql"
11
+ }.freeze
12
+
13
+ TELEMETRY_DATABASE_IDS = {
14
+ memory: "memory",
15
+ postgres: "postgres",
16
+ mysql: "mysql",
17
+ sqlite: "sqlite",
18
+ mssql: "mssql"
19
+ }.freeze
20
+
21
+ attr_reader :handler, :api, :options, :context, :error_codes, :telemetry
6
22
 
7
23
  def initialize(options = {})
8
24
  @options = Configuration.new(options)
@@ -16,6 +32,7 @@ module BetterAuth
16
32
  Router.check_endpoint_conflicts(@options, @options.logger)
17
33
  @api = API.new(@context, @endpoints)
18
34
  @handler = Router.new(@context, @endpoints)
35
+ @telemetry = build_telemetry_publisher
19
36
  end
20
37
 
21
38
  def call(env)
@@ -38,5 +55,55 @@ module BetterAuth
38
55
  def build_endpoints
39
56
  Core.base_endpoints.merge(@plugin_registry.endpoints)
40
57
  end
58
+
59
+ def build_telemetry_publisher
60
+ require "better_auth/telemetry"
61
+ BetterAuth::Telemetry.create(@options, telemetry_context)
62
+ rescue LoadError
63
+ noop_telemetry_publisher
64
+ rescue => e
65
+ log_telemetry_error(e)
66
+ noop_telemetry_publisher
67
+ end
68
+
69
+ def telemetry_context
70
+ {
71
+ database: telemetry_database_id,
72
+ adapter: telemetry_adapter_id,
73
+ custom_track: nil,
74
+ skip_test_check: false
75
+ }
76
+ end
77
+
78
+ def telemetry_database_id
79
+ configured = @options.database
80
+ return "memory" if configured.nil?
81
+ return TELEMETRY_DATABASE_IDS[configured] if configured.is_a?(Symbol) && TELEMETRY_DATABASE_IDS.key?(configured)
82
+ return "adapter" if configured.respond_to?(:call)
83
+
84
+ TELEMETRY_ADAPTER_IDS[configured.class.name] || "adapter"
85
+ end
86
+
87
+ def telemetry_adapter_id
88
+ TELEMETRY_ADAPTER_IDS[@context.adapter.class.name] || @context.adapter.class.name
89
+ end
90
+
91
+ def noop_telemetry_publisher
92
+ Class.new do
93
+ def publish(_event) = nil
94
+
95
+ def enabled? = false
96
+ end.new
97
+ end
98
+
99
+ def log_telemetry_error(error)
100
+ logger = @options.logger
101
+ message = "[better-auth] telemetry creation failed: #{error.class}: #{error.message}"
102
+ if logger.respond_to?(:error)
103
+ logger.error(message)
104
+ else
105
+ Kernel.warn(message)
106
+ end
107
+ end
41
108
  end
42
109
  end
@@ -56,6 +56,7 @@ module BetterAuth
56
56
  :on_api_error,
57
57
  :disabled_paths,
58
58
  :trusted_origins_callback,
59
+ :telemetry,
59
60
  :logger
60
61
 
61
62
  def initialize(options = {})
@@ -73,12 +74,13 @@ module BetterAuth
73
74
  @database_hooks = options[:database_hooks]
74
75
  @hooks = options[:hooks]
75
76
  @on_api_error = symbolize_keys(options[:on_api_error] || options[:on_apierror] || {})
77
+ @telemetry = symbolize_keys(options[:telemetry] || {})
76
78
  @social_providers = symbolize_keys(options[:social_providers] || {})
77
79
  @trusted_origins_callbacks = []
78
80
  @trusted_origins_callbacks << options[:trusted_origins] if options[:trusted_origins].respond_to?(:call)
79
81
  @trusted_origins_callback = combined_trusted_origins_callback
80
82
  legacy_secret = resolve_secret(options, allow_test_default: false)
81
- secrets = options.key?(:secrets) ? options[:secrets] : SecretConfig.parse_env(ENV["BETTER_AUTH_SECRETS"])
83
+ secrets = options.key?(:secrets) ? options[:secrets] : SecretConfig.parse_env(Env.get("BETTER_AUTH_SECRETS"))
82
84
  if secrets
83
85
  @secret_config = SecretConfig.build(secrets, legacy_secret, logger: logger)
84
86
  @secret = @secret_config.current_secret
@@ -153,7 +155,8 @@ module BetterAuth
153
155
  database_hooks: database_hooks,
154
156
  hooks: hooks,
155
157
  on_api_error: on_api_error,
156
- disabled_paths: disabled_paths
158
+ disabled_paths: disabled_paths,
159
+ telemetry: telemetry
157
160
  }
158
161
  end
159
162
 
@@ -279,17 +282,13 @@ module BetterAuth
279
282
  def env_base_url
280
283
  base_url = ENV["BASE_URL"]
281
284
  [
282
- ENV["BETTER_AUTH_URL"],
283
- ENV["NEXT_PUBLIC_BETTER_AUTH_URL"],
284
- ENV["PUBLIC_BETTER_AUTH_URL"],
285
- ENV["NUXT_PUBLIC_BETTER_AUTH_URL"],
286
- ENV["NUXT_PUBLIC_AUTH_URL"],
285
+ Env.get("BETTER_AUTH_URL"),
287
286
  (base_url unless base_url == "/")
288
287
  ].find { |value| value && !value.empty? }
289
288
  end
290
289
 
291
290
  def resolve_secret(options, allow_test_default: true)
292
- [options[:secret], ENV["BETTER_AUTH_SECRET"], ENV["AUTH_SECRET"]].find { |value| value && !value.empty? } ||
291
+ [options[:secret], Env.get("BETTER_AUTH_SECRET"), ENV["AUTH_SECRET"]].find { |value| value && !value.empty? } ||
293
292
  ((allow_test_default && test_environment?) ? DEFAULT_SECRET : nil)
294
293
  end
295
294
 
@@ -408,7 +407,7 @@ module BetterAuth
408
407
  end
409
408
 
410
409
  def env_trusted_origins
411
- ENV.fetch("BETTER_AUTH_TRUSTED_ORIGINS", "").split(",").map(&:strip).reject(&:empty?)
410
+ Env.csv("BETTER_AUTH_TRUSTED_ORIGINS")
412
411
  end
413
412
 
414
413
  def symbolize_keys(value)
@@ -321,7 +321,7 @@ module BetterAuth
321
321
  if options.trusted_origins_callback
322
322
  origins.concat(Array(options.trusted_origins_callback.call(request)).compact)
323
323
  end
324
- origins.concat(ENV.fetch("BETTER_AUTH_TRUSTED_ORIGINS", "").split(",").map(&:strip))
324
+ origins.concat(Env.csv("BETTER_AUTH_TRUSTED_ORIGINS"))
325
325
  origins.map(&:to_s).reject(&:empty?).uniq
326
326
  rescue URI::InvalidURIError
327
327
  options.trusted_origins
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BetterAuth
4
+ module Env
5
+ module_function
6
+
7
+ def get(name)
8
+ open_auth_name = open_auth_name(name)
9
+ return nil unless open_auth_name
10
+
11
+ value = ENV[open_auth_name]
12
+ return value if present?(value)
13
+
14
+ value = ENV[name]
15
+ present?(value) ? value : nil
16
+ end
17
+
18
+ def fetch(name, default = nil)
19
+ value = get(name)
20
+ value.nil? ? default : value
21
+ end
22
+
23
+ def csv(name)
24
+ fetch(name, "").split(",").map(&:strip).reject(&:empty?)
25
+ end
26
+
27
+ def open_auth_name(name)
28
+ text = name.to_s
29
+ return nil unless text.start_with?("BETTER_AUTH_")
30
+
31
+ text.sub(/\ABETTER_AUTH_/, "OPEN_AUTH_")
32
+ end
33
+
34
+ def present?(value)
35
+ value && !value.empty?
36
+ end
37
+ end
38
+ end
@@ -178,7 +178,7 @@ module BetterAuth
178
178
  end
179
179
 
180
180
  def env_base_url(base_path)
181
- url = ENV["BETTER_AUTH_URL"] || ENV["NEXT_PUBLIC_BETTER_AUTH_URL"] || ENV["PUBLIC_BETTER_AUTH_URL"] || ENV["NUXT_PUBLIC_BETTER_AUTH_URL"] || ENV["NUXT_PUBLIC_AUTH_URL"]
181
+ url = Env.get("BETTER_AUTH_URL")
182
182
  url ||= ENV["BASE_URL"] if ENV["BASE_URL"] && ENV["BASE_URL"] != "/"
183
183
  url ? with_path(url, base_path) : nil
184
184
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BetterAuth
4
- VERSION = "0.7.0"
4
+ VERSION = "0.8.0"
5
5
  end
data/lib/better_auth.rb CHANGED
@@ -4,6 +4,7 @@ require_relative "better_auth/version"
4
4
  require_relative "better_auth/core"
5
5
  require_relative "better_auth/error"
6
6
  require_relative "better_auth/api_error"
7
+ require_relative "better_auth/env"
7
8
  require_relative "better_auth/secret_config"
8
9
  require_relative "better_auth/crypto"
9
10
  require_relative "better_auth/host"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Sala
@@ -245,8 +245,10 @@ dependencies:
245
245
  - - "~>"
246
246
  - !ruby/object:Gem::Version
247
247
  version: '2.1'
248
- description: Better Auth is a comprehensive, framework-agnostic authentication library
249
- for Ruby. It provides a complete set of features out of the box with a plugin ecosystem.
248
+ description: Better Auth Ruby is an independent, modern authentication framework for
249
+ Ruby, inspired by Better Auth. It is not affiliated with, maintained by, or endorsed
250
+ by the Better Auth project. It provides a framework-agnostic Rack core, auth routes,
251
+ sessions, adapters, and a plugin system.
250
252
  email:
251
253
  - sebastian.sala.tech@gmail.com
252
254
  executables: []
@@ -280,6 +282,7 @@ files:
280
282
  - lib/better_auth/database_hooks.rb
281
283
  - lib/better_auth/deprecate.rb
282
284
  - lib/better_auth/endpoint.rb
285
+ - lib/better_auth/env.rb
283
286
  - lib/better_auth/error.rb
284
287
  - lib/better_auth/host.rb
285
288
  - lib/better_auth/instrumentation.rb