api_keys 0.8.1 → 0.10.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 +4 -4
- data/CHANGELOG.md +34 -0
- data/README.md +92 -22
- data/api_keys_expiration.webp +0 -0
- data/api_keys_expiration_custom.webp +0 -0
- data/api_keys_expiration_list.webp +0 -0
- data/app/controllers/api_keys/application_controller.rb +21 -1
- data/app/controllers/api_keys/keys_controller.rb +36 -15
- data/app/views/api_keys/keys/_form.html.erb +70 -16
- data/app/views/api_keys/keys/_key_row.html.erb +7 -5
- data/app/views/api_keys/keys/_show_token.html.erb +8 -0
- data/app/views/layouts/api_keys/application.html.erb +16 -2
- data/lib/api_keys/configuration.rb +50 -0
- data/lib/api_keys/errors.rb +6 -0
- data/lib/api_keys/form_builder_extensions.rb +41 -13
- data/lib/api_keys/helpers/expiration_options.rb +226 -84
- data/lib/api_keys/helpers/view_helpers.rb +15 -0
- data/lib/api_keys/models/api_key.rb +3 -1
- data/lib/api_keys/models/concerns/has_api_keys.rb +42 -9
- data/lib/api_keys/version.rb +1 -1
- data/lib/generators/api_keys/templates/initializer.rb +17 -0
- metadata +4 -1
|
@@ -212,8 +212,16 @@ module ApiKeys
|
|
|
212
212
|
# When key_type is specified, scopes are filtered to only include those allowed
|
|
213
213
|
# by the key type's permissions ceiling. Blank values are automatically removed.
|
|
214
214
|
# @param expires_at [Time, nil] Optional expiration timestamp.
|
|
215
|
-
# @param
|
|
216
|
-
#
|
|
215
|
+
# @param expires_in [ActiveSupport::Duration, nil] How long the key lives:
|
|
216
|
+
# `expires_in: 30.days`. Whole hours, days, weeks, months or years, up
|
|
217
|
+
# to ten years. Pass it or expires_at, not both.
|
|
218
|
+
# @param expires_at_preset [String, nil] What the expiration picker
|
|
219
|
+
# submitted: "1_day", "30_days", "1_year", "never", "custom"... (see
|
|
220
|
+
# ApiKeys::ExpirationOptions). Takes precedence over expires_at if both given.
|
|
221
|
+
# @param expires_on [String, Date, nil] The day the key expires, at
|
|
222
|
+
# 23:59:59 ("2027-01-31"): the picker's "Custom" date. Used alone or
|
|
223
|
+
# with expires_at_preset: "custom"; beside another preset it is
|
|
224
|
+
# ignored, as a form submits the field whatever was chosen.
|
|
217
225
|
# @param metadata [Hash, nil] Optional metadata hash.
|
|
218
226
|
# @param key_type [Symbol, nil] The key type (e.g., :publishable, :secret).
|
|
219
227
|
# Must be defined in ApiKeys.configuration.key_types if provided.
|
|
@@ -230,19 +238,16 @@ module ApiKeys
|
|
|
230
238
|
# @return [ApiKeys::ApiKey] The newly created ApiKey instance. The plaintext token
|
|
231
239
|
# is available via the `#token` attribute on this instance
|
|
232
240
|
# *only until it's reloaded*.
|
|
233
|
-
def create_api_key!(name: nil, scopes: nil, expires_at: nil,
|
|
234
|
-
|
|
235
|
-
created_by: nil)
|
|
241
|
+
def create_api_key!(name: nil, scopes: nil, expires_at: nil, expires_in: nil, expires_at_preset: nil,
|
|
242
|
+
expires_on: nil, metadata: nil, key_type: nil, environment: nil, restrictions: nil,
|
|
243
|
+
allowed_origins: nil, allowed_ips: nil, created_by: nil)
|
|
236
244
|
config = ApiKeys.configuration
|
|
237
245
|
|
|
238
246
|
if created_by && !ApiKeys::ApiKey.created_by_columns?
|
|
239
247
|
raise ApiKeys::Errors::CreatedByMigrationRequiredError
|
|
240
248
|
end
|
|
241
249
|
|
|
242
|
-
|
|
243
|
-
if expires_at_preset.present?
|
|
244
|
-
expires_at = ApiKeys::Helpers::ExpirationOptions.parse(expires_at_preset)
|
|
245
|
-
end
|
|
250
|
+
expires_at = resolve_expiration(expires_at, expires_in, expires_at_preset, expires_on)
|
|
246
251
|
|
|
247
252
|
# Auto-clean scopes: remove blank values from arrays (common when using form checkboxes)
|
|
248
253
|
if scopes.is_a?(Array)
|
|
@@ -334,6 +339,34 @@ module ApiKeys
|
|
|
334
339
|
config.key_types.present? && config.key_types.any?
|
|
335
340
|
end
|
|
336
341
|
|
|
342
|
+
# The one expiration the caller asked for. expires_at_preset wins over
|
|
343
|
+
# expires_at, as it always has. expires_on: is the picker's Custom
|
|
344
|
+
# date: a form submits it ("" or a date) whatever was chosen, so it
|
|
345
|
+
# counts only with the "custom" preset or alone, and a blank one is
|
|
346
|
+
# no date at all. expires_in: combines with nothing, and expires_on:
|
|
347
|
+
# alone combines with nothing, so a key never quietly gets an
|
|
348
|
+
# expiration its caller did not mean.
|
|
349
|
+
def resolve_expiration(expires_at, expires_in, preset, expires_on)
|
|
350
|
+
custom = ApiKeys::Helpers::ExpirationOptions::CUSTOM
|
|
351
|
+
preset = preset.presence
|
|
352
|
+
expires_on = nil if expires_on.respond_to?(:empty?) && expires_on.to_s.strip.empty?
|
|
353
|
+
expires_on = nil unless preset.nil? || preset == custom
|
|
354
|
+
preset ||= custom unless expires_on.nil?
|
|
355
|
+
|
|
356
|
+
conflicting = (!expires_in.nil? && !(preset.nil? && expires_at.nil?)) ||
|
|
357
|
+
(!expires_on.nil? && !expires_at.nil?)
|
|
358
|
+
raise ArgumentError, "Pass one expiration: expires_at, expires_in, expires_at_preset, or expires_on" if conflicting
|
|
359
|
+
|
|
360
|
+
return ApiKeys::Helpers::ExpirationOptions.parse(preset, on: expires_on) if preset
|
|
361
|
+
return expires_at if expires_in.nil?
|
|
362
|
+
|
|
363
|
+
unless ApiKeys::Helpers::ExpirationOptions.preset_for(expires_in)
|
|
364
|
+
raise ArgumentError, "expires_in must be whole hours, days, weeks, months or years, up to ten years (e.g. 30.days)"
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
expires_in.from_now
|
|
368
|
+
end
|
|
369
|
+
|
|
337
370
|
def validate_key_type!(key_type, config)
|
|
338
371
|
return unless key_types_feature_enabled?(config)
|
|
339
372
|
|
data/lib/api_keys/version.rb
CHANGED
|
@@ -48,6 +48,15 @@ ApiKeys.configure do |config|
|
|
|
48
48
|
# Example for Organization-owned keys:
|
|
49
49
|
# config.authenticate_owner_method = :authenticate_organization!
|
|
50
50
|
|
|
51
|
+
# The method that returns the signed-in person, recorded as a new key's
|
|
52
|
+
# created_by when the dashboard mints it. Set it when keys belong to an
|
|
53
|
+
# organization, so each key says who created it. Needs the created_by
|
|
54
|
+
# columns (rails g api_keys:add_created_by).
|
|
55
|
+
# Default: nil (records nobody)
|
|
56
|
+
#
|
|
57
|
+
# Example for Organization-owned keys:
|
|
58
|
+
# config.current_creator_method = :current_user
|
|
59
|
+
|
|
51
60
|
# Dashboard environment filtering (only applies when key_types is configured).
|
|
52
61
|
# When false (default), dashboard only shows keys matching current_environment.
|
|
53
62
|
# When true, dashboard shows keys from all environments.
|
|
@@ -239,6 +248,14 @@ ApiKeys.configure do |config|
|
|
|
239
248
|
# Default: nil
|
|
240
249
|
# config.expire_after = 90.days
|
|
241
250
|
|
|
251
|
+
# What the expiration picker offers when someone creates a key, in order:
|
|
252
|
+
# durations (whole hours, days, weeks, months or years, up to ten years),
|
|
253
|
+
# :never, and :custom (a date picker). Leave out :never to make every key
|
|
254
|
+
# created from the dashboard expire. While expire_after is set, :never shows
|
|
255
|
+
# that lifetime instead ("90 days (default)").
|
|
256
|
+
# Default: [1.day, 7.days, 30.days, 1.year, :never, :custom]
|
|
257
|
+
# config.expiration_options = [1.day, 7.days, 30.days, 90.days, :custom]
|
|
258
|
+
|
|
242
259
|
# Default scopes to assign to newly created keys if none are specified.
|
|
243
260
|
# Can be overridden per-model with `default_scopes` in the `has_api_keys` block.
|
|
244
261
|
# Default: []
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: api_keys
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.10.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- rameerez
|
|
@@ -71,6 +71,9 @@ files:
|
|
|
71
71
|
- README.md
|
|
72
72
|
- SECURITY.md
|
|
73
73
|
- api_keys_dashboard.webp
|
|
74
|
+
- api_keys_expiration.webp
|
|
75
|
+
- api_keys_expiration_custom.webp
|
|
76
|
+
- api_keys_expiration_list.webp
|
|
74
77
|
- api_keys_permissions.webp
|
|
75
78
|
- api_keys_token.webp
|
|
76
79
|
- app/controllers/api_keys/application_controller.rb
|