better_auth-api-key 0.2.0 → 0.2.1

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: df93f92692f0e4ce8a929b7c13ead11567450cb00052eae52b3d3505c82e0930
4
- data.tar.gz: df7adda82f9f74ac01760f30ecd8013693a18f91c2fb799d961a9acc168cf78b
3
+ metadata.gz: 2ac9db4e9d87c466c1bf8ecb7be2978d8ffc17fa168b5a626e82b29ddf36c4c2
4
+ data.tar.gz: 90f8b579bdf6fd84d7d0d2b0360f479c50800bc5ac724b0fdde6c721a8290e3a
5
5
  SHA512:
6
- metadata.gz: 0c592f30fac4c14c801eeedb43884c66b6ffac714e407e10e41ede39eab3c382341dd1143dc246ade4ebee9dc63ff9b7f13302da1291b4b4180434739df3d314
7
- data.tar.gz: '087f99ab7f1e18be8afa5b3c48d64b579580b5468be4d3c387918c9a19f899087b1c695ae766144f05965050ba9cb9430ad02d64d30e53f6fae41270ed41e6b1'
6
+ metadata.gz: 970bf80895c95174c91e269560260f6b90281dcc5f3a1be4cd708bcdb53a12dd5d567874a3b046b86867b069000009238982bbd22b980edfa990ed7c68a04b6b
7
+ data.tar.gz: c52b6d7e9d2b98e77754e9105681bd1c1f8e619b1a0819ab93798a7eacc62cd10a40e11a32debe1a119578de868e76f3982dd366f745d4eb7abb60eab97a4332
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.1 - 2026-04-30
4
+
5
+ - Fixed API key metadata normalization so symbol and string metadata keys preserve nested metadata payloads.
6
+ - Added upstream parity coverage for API key behavior and error-code response details.
7
+
3
8
  ## 0.2.0 - 2026-04-29
4
9
 
5
10
  - Aligned API key behavior with upstream Better Auth v1.6.9, including key verification, permission checks, metadata updates, expiration, rate limiting, prefix handling, and route response shapes.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module BetterAuth
4
4
  module APIKey
5
- VERSION = "0.2.0"
5
+ VERSION = "0.2.1"
6
6
  end
7
7
  end
@@ -181,7 +181,7 @@ module BetterAuth
181
181
 
182
182
  def api_key_create_endpoint(config)
183
183
  Endpoint.new(path: "/api-key/create", method: "POST") do |ctx|
184
- body = normalize_hash(ctx.body)
184
+ body = api_key_normalize_body(ctx.body)
185
185
  resolved_config = api_key_resolve_config(ctx.context, config, body[:config_id])
186
186
  session = Routes.current_session(ctx, allow_nil: true)
187
187
  reference_id = api_key_create_reference_id!(ctx, body, session, resolved_config)
@@ -262,7 +262,7 @@ module BetterAuth
262
262
 
263
263
  def api_key_update_endpoint(config)
264
264
  Endpoint.new(path: "/api-key/update", method: "POST") do |ctx|
265
- body = normalize_hash(ctx.body)
265
+ body = api_key_normalize_body(ctx.body)
266
266
  resolved_config = api_key_resolve_config(ctx.context, config, body[:config_id])
267
267
  session = Routes.current_session(ctx, allow_nil: true)
268
268
  user_id = session&.dig(:user, "id") || body[:user_id]
@@ -379,7 +379,13 @@ module BetterAuth
379
379
  def api_key_create_reference_id!(ctx, body, session, config)
380
380
  if config[:references].to_s == "organization"
381
381
  organization_id = body[:organization_id]
382
- raise APIError.new("BAD_REQUEST", message: API_KEY_ERROR_CODES["ORGANIZATION_ID_REQUIRED"]) if organization_id.to_s.empty?
382
+ if organization_id.to_s.empty?
383
+ raise APIError.new(
384
+ "BAD_REQUEST",
385
+ message: API_KEY_ERROR_CODES["ORGANIZATION_ID_REQUIRED"],
386
+ code: "ORGANIZATION_ID_REQUIRED"
387
+ )
388
+ end
383
389
 
384
390
  user_id = session&.dig(:user, "id") || body[:user_id]
385
391
  raise APIError.new("UNAUTHORIZED", message: API_KEY_ERROR_CODES["UNAUTHORIZED_SESSION"]) if user_id.to_s.empty?
@@ -515,11 +521,21 @@ module BetterAuth
515
521
  record = api_key_validate!(ctx, key, config)
516
522
  api_key_schedule_cleanup(ctx, config)
517
523
  if config[:references].to_s != "user"
518
- raise APIError.new("UNAUTHORIZED", message: API_KEY_ERROR_CODES["INVALID_REFERENCE_ID_FROM_API_KEY"])
524
+ raise APIError.new(
525
+ "UNAUTHORIZED",
526
+ message: API_KEY_ERROR_CODES["INVALID_REFERENCE_ID_FROM_API_KEY"],
527
+ code: "INVALID_REFERENCE_ID_FROM_API_KEY"
528
+ )
519
529
  end
520
530
  reference_id = api_key_record_reference_id(record)
521
531
  user = ctx.context.internal_adapter.find_user_by_id(reference_id)
522
- raise APIError.new("UNAUTHORIZED", message: API_KEY_ERROR_CODES["INVALID_REFERENCE_ID_FROM_API_KEY"]) unless user
532
+ unless user
533
+ raise APIError.new(
534
+ "UNAUTHORIZED",
535
+ message: API_KEY_ERROR_CODES["INVALID_REFERENCE_ID_FROM_API_KEY"],
536
+ code: "INVALID_REFERENCE_ID_FROM_API_KEY"
537
+ )
538
+ end
523
539
 
524
540
  session = {
525
541
  user: user,
@@ -699,6 +715,15 @@ module BetterAuth
699
715
  config[:disable_key_hashing] ? key.to_s : default_api_key_hasher(key)
700
716
  end
701
717
 
718
+ def api_key_normalize_body(raw)
719
+ body = normalize_hash(raw)
720
+ return body unless raw.is_a?(Hash)
721
+
722
+ metadata_key = raw.key?(:metadata) ? :metadata : ("metadata" if raw.key?("metadata"))
723
+ body[:metadata] = raw[metadata_key] if metadata_key
724
+ body
725
+ end
726
+
702
727
  def api_key_expires_at(body, config)
703
728
  if body.key?(:expires_in)
704
729
  Time.now + body[:expires_in].to_i unless body[:expires_in].nil?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_auth-api-key
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Sala