Module: BetterAuth::APIKey::Routes
- Defined in:
- lib/better_auth/api_key/routes/index.rb,
lib/better_auth/api_key/routes/get_api_key.rb,
lib/better_auth/api_key/routes/list_api_keys.rb,
lib/better_auth/api_key/routes/create_api_key.rb,
lib/better_auth/api_key/routes/delete_api_key.rb,
lib/better_auth/api_key/routes/update_api_key.rb,
lib/better_auth/api_key/routes/verify_api_key.rb,
lib/better_auth/api_key/routes/delete_all_expired_api_keys.rb
Defined Under Namespace
Modules: CreateAPIKey, DeleteAPIKey, DeleteAllExpiredAPIKeys, GetAPIKey, ListAPIKeys, UpdateAPIKey, VerifyAPIKey
Constant Summary
collapse
- ROUTE_NAMES =
%i[
create_api_key
verify_api_key
get_api_key
update_api_key
delete_api_key
list_api_keys
delete_all_expired_api_keys
].freeze
Class Method Summary
collapse
Class Method Details
.config_id_matches?(record_config_id, expected_config_id) ⇒ Boolean
37
38
39
40
41
|
# File 'lib/better_auth/api_key/routes/index.rb', line 37
def config_id_matches?(record_config_id, expected_config_id)
return true if default_config_id?(record_config_id) && default_config_id?(expected_config_id)
record_config_id.to_s == expected_config_id.to_s
end
|
.default_config_id?(value) ⇒ Boolean
33
34
35
|
# File 'lib/better_auth/api_key/routes/index.rb', line 33
def default_config_id?(value)
value.nil? || value.to_s.empty? || value.to_s == "default"
end
|
.delete_expired(context, config, bypass_last_check: false) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/better_auth/api_key/routes/index.rb', line 45
def delete_expired(context, config, bypass_last_check: false)
return unless config[:storage] == "database" || config[:fallback_to_database]
unless bypass_last_check
now = Time.now
return if @last_expired_check && ((now - @last_expired_check) * 1000) < 10_000
@last_expired_check = now
end
expired = context.adapter.find_many(model: BetterAuth::Plugins::API_KEY_TABLE_NAME).select do |record|
record["expiresAt"] && record["expiresAt"] < Time.now
end
expired.each do |record|
context.adapter.delete(model: BetterAuth::Plugins::API_KEY_TABLE_NAME, where: [{field: "id", value: record["id"]}])
end
end
|
.resolve_config(context, config, config_id = nil) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/better_auth/api_key/routes/index.rb', line 18
def resolve_config(context, config, config_id = nil)
configurations = config.fetch(:configurations, [config])
return configurations.find { |entry| default_config_id?(entry[:config_id]) } || configurations.first if config_id.to_s.empty?
configurations.find { |entry| entry[:config_id].to_s == config_id.to_s } ||
begin
default = configurations.find { |entry| default_config_id?(entry[:config_id]) }
unless default
context.logger.error(BetterAuth::Plugins::API_KEY_ERROR_CODES["NO_DEFAULT_API_KEY_CONFIGURATION_FOUND"]) if context.respond_to?(:logger) && context.logger.respond_to?(:error)
raise BetterAuth::APIError.new("BAD_REQUEST", message: BetterAuth::Plugins::API_KEY_ERROR_CODES["NO_DEFAULT_API_KEY_CONFIGURATION_FOUND"])
end
default
end
end
|
.schedule_cleanup(ctx, config) ⇒ Object
62
63
64
65
66
67
68
69
|
# File 'lib/better_auth/api_key/routes/index.rb', line 62
def schedule_cleanup(ctx, config)
task = -> { delete_expired(ctx.context, config) }
if config[:defer_updates] && BetterAuth::APIKey::Utils.background_tasks?(ctx)
ctx.context.run_in_background(task)
else
task.call
end
end
|