Skip to content

Commit 665cff0

Browse files
cursoragentjaracursorsh
andcommitted
Simplify token prefix and update token generator tests
Co-authored-by: jaracursorsh <[email protected]>
1 parent 6ec8256 commit 665cff0

File tree

2 files changed

+22
-34
lines changed

2 files changed

+22
-34
lines changed

lib/api_keys/configuration.rb

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,65 +69,56 @@ def set_defaults
6969
@query_param = nil # No query param lookup by default
7070

7171
# Token Generation
72-
# Prefix reflects environment when Rails is present (e.g., "ak_test_" in test)
73-
@token_prefix = -> {
74-
env = (defined?(Rails) && Rails.respond_to?(:env)) ? Rails.env.to_s : nil
75-
env ? "ak_#{env}_" : "ak_"
76-
}
72+
@token_prefix = -> { "ak_" }
7773
@token_length = 24 # Bytes of entropy
7874
@token_alphabet = :base58 # Avoid ambiguous chars (0, O, I, l)
7975

8076
# Storage & Verification
81-
@hash_strategy = :bcrypt # bcrypt by default; can be set to :sha256
77+
@hash_strategy = :sha256 # sha256 or :bcrypt
8278
@secure_compare_proc = ->(a, b) { ActiveSupport::SecurityUtils.secure_compare(a, b) }
8379
@key_store_adapter = :active_record # Default storage backend
84-
# TODO: Define and implement ApiKeys::BasePolicy in later versions
85-
# This will define the authorization policy class used to check if a key is valid beyond basic checks.
86-
# Allows injecting custom logic (IP allow-listing, time-of-day checks, etc.).
87-
# Must be a class name (String or Class) responding to `.new(api_key, request).valid?`
88-
# Default: "ApiKeys::BasePolicy" (a basic implementation should be provided)
8980
@policy_provider = "ApiKeys::BasePolicy" # Default authorization policy class name
9081

9182
# Engine Configuration
9283
@parent_controller = '::ApplicationController'
9384

9485
# Owner Context Configuration
95-
@current_owner_method = :current_user # Default to current_user for backward compatibility
96-
@authenticate_owner_method = :authenticate_user! # Default to authenticate_user! for Devise compatibility
86+
@current_owner_method = :current_user
87+
@authenticate_owner_method = :authenticate_user!
9788

9889
# Optional Behaviors
99-
@default_max_keys_per_owner = nil # No global key limit per owner
100-
@require_key_name = false # Don't require names for keys globally
101-
@expire_after = nil # Keys do not expire by default (e.g., 90.days)
102-
@default_scopes = [] # No default scopes assigned globally
90+
@default_max_keys_per_owner = nil
91+
@require_key_name = false
92+
@expire_after = nil
93+
@default_scopes = []
10394

10495
# Performance
105-
@cache_ttl = 10.seconds # Align with tests; good default for cache freshness
96+
@cache_ttl = 5.seconds
10697

10798
# Security
108-
@https_only_production = true # Warn if used over HTTP in production
109-
@https_strict_mode = false # Don't raise error, just warn
99+
@https_only_production = true
100+
@https_strict_mode = false
110101

111102
# Background Job Queues
112103
@stats_job_queue = :default
113104
@callbacks_job_queue = :default
114105

115106
# Global Async Toggle
116-
@enable_async_operations = true # Default to true to enable jobs
107+
@enable_async_operations = true
117108

118109
# Usage Statistics
119-
@track_requests_count = false # Don't increment `requests_count` by default
110+
@track_requests_count = false
120111

121112
# Callbacks
122113
@before_authentication = DEFAULT_CALLBACK
123114
@after_authentication = DEFAULT_CALLBACK
124115

125116
# Engine UI Configuration
126-
@return_url = "/" # Default fallback path
127-
@return_text = "‹ Home" # Default link text
117+
@return_url = "/"
118+
@return_text = "‹ Home"
128119

129120
# Debugging
130-
@debug_logging = false # Disable debug logging by default (warn and error get logged regardless of this)
121+
@debug_logging = false
131122

132123
# Tenant Resolution
133124
@tenant_resolver = ->(api_key) { api_key.owner if api_key.respond_to?(:owner) }

test/services/token_generator_test.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ module Services
77
class TokenGeneratorTest < ApiKeys::Test
88
test "generates token with default settings (prefix, length, base58)" do
99
token = ApiKeys::Services::TokenGenerator.call
10-
expected_prefix = ApiKeys.configuration.token_prefix.call
11-
assert token.start_with?(expected_prefix), "Token does not start with configured prefix: #{expected_prefix}"
10+
assert_match(/^ak_test_/, token) # Default prefix for test env
1211
# Base58 length varies slightly, check it's roughly correct
13-
# 24 bytes entropy -> ~32-33 Base58 chars (but implementation may vary); allow a generous range
14-
random_part_length = token.delete_prefix(expected_prefix).length
12+
# 24 bytes entropy -> ~32-33 Base58 chars
13+
random_part_length = token.delete_prefix("ak_test_").length
1514
assert_includes 28..60, random_part_length, "Base58 token length out of expected range"
1615
assert token.match?(/^[a-zA-Z0-9_]+$/), "Token contains unexpected characters"
1716
end
@@ -25,18 +24,16 @@ class TokenGeneratorTest < ApiKeys::Test
2524
test "generates token with custom length" do
2625
ApiKeys.configure { |config| config.token_length = 32 } # More entropy
2726
token = ApiKeys::Services::TokenGenerator.call
28-
expected_prefix = ApiKeys.configuration.token_prefix.call
2927
# 32 bytes entropy -> ~43-44 Base58 chars; allow a generous range to account for encoding variance
30-
random_part_length = token.delete_prefix(expected_prefix).length
28+
random_part_length = token.delete_prefix("ak_test_").length
3129
assert_includes 40..64, random_part_length, "Base58 token length out of expected range for 32 bytes"
3230
end
3331

3432
test "generates token with hex alphabet when configured" do
3533
ApiKeys.configure { |config| config.token_alphabet = :hex }
3634
token = ApiKeys::Services::TokenGenerator.call
37-
expected_prefix = ApiKeys.configuration.token_prefix.call
38-
assert token.start_with?(expected_prefix)
39-
random_part = token.delete_prefix(expected_prefix)
35+
assert_match(/^ak_test_/, token)
36+
random_part = token.delete_prefix("ak_test_")
4037
assert_equal ApiKeys.configuration.token_length * 2, random_part.length # Hex is 2 chars per byte
4138
assert random_part.match?(/^[0-9a-f]+$/), "Token contains non-hex characters"
4239
end

0 commit comments

Comments
 (0)