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
|
@@ -38,10 +38,40 @@ module ApiKeys
|
|
|
38
38
|
# Owner Context Configuration
|
|
39
39
|
attr_reader :current_owner_method, :authenticate_owner_method
|
|
40
40
|
|
|
41
|
+
# The person behind a dashboard-created key
|
|
42
|
+
#
|
|
43
|
+
# @!attribute [rw] current_creator_method
|
|
44
|
+
# @return [Symbol, String, nil] Controller method that returns the signed-in
|
|
45
|
+
# person (e.g. :current_user). When set, the dashboard records it as the
|
|
46
|
+
# new key's `created_by`, so an organization-owned key says who minted it
|
|
47
|
+
# and `config.trusted_key_check` can judge dashboard-created keys. Needs
|
|
48
|
+
# the created_by columns (`rails g api_keys:add_created_by`). nil (the
|
|
49
|
+
# default) records nobody, as before.
|
|
50
|
+
# @example Organization-owned keys
|
|
51
|
+
# config.current_owner_method = :current_organization
|
|
52
|
+
# config.current_creator_method = :current_user
|
|
53
|
+
attr_reader :current_creator_method
|
|
54
|
+
|
|
41
55
|
# Optional Behaviors
|
|
42
56
|
attr_reader :default_max_keys_per_owner, :require_key_name
|
|
43
57
|
attr_reader :expire_after, :default_scopes, :track_requests_count
|
|
44
58
|
|
|
59
|
+
# Expiration choices
|
|
60
|
+
#
|
|
61
|
+
# @!attribute [rw] expiration_options
|
|
62
|
+
# @return [Array<ActiveSupport::Duration, Symbol>] What the expiration
|
|
63
|
+
# picker offers when someone creates a key, in order: durations (whole
|
|
64
|
+
# hours, days, weeks, months or years, up to ten years), `:never`, and
|
|
65
|
+
# `:custom` (a date picker). Take `:never` out to make every key expire.
|
|
66
|
+
# While `config.expire_after` is set, `:never` is offered under that
|
|
67
|
+
# lifetime's name ("90 days (default)"), because such a key expires anyway.
|
|
68
|
+
# The stock dashboard enforces this list; a controller of your own
|
|
69
|
+
# checks a submitted preset with `ApiKeys::ExpirationOptions.offered?`.
|
|
70
|
+
# Default: `[1.day, 7.days, 30.days, 1.year, :never, :custom]`.
|
|
71
|
+
# @example Every key expires within 90 days
|
|
72
|
+
# config.expiration_options = [1.day, 7.days, 30.days, 90.days, :custom]
|
|
73
|
+
attr_reader :expiration_options
|
|
74
|
+
|
|
45
75
|
# Performance
|
|
46
76
|
attr_reader :cache_ttl, :stats_update_interval
|
|
47
77
|
|
|
@@ -235,6 +265,20 @@ module ApiKeys
|
|
|
235
265
|
@expire_after = value
|
|
236
266
|
end
|
|
237
267
|
|
|
268
|
+
def expiration_options=(value)
|
|
269
|
+
choices = Array(value).map { |choice| choice.is_a?(String) ? choice.to_sym : choice } if value.is_a?(Array)
|
|
270
|
+
options = ApiKeys::Helpers::ExpirationOptions
|
|
271
|
+
valid = choices.present? && choices.uniq.length == choices.length && choices.all? do |choice|
|
|
272
|
+
options::CHOICES.include?(choice) || options.preset_for(choice)
|
|
273
|
+
end
|
|
274
|
+
unless valid
|
|
275
|
+
raise ArgumentError, "expiration_options must list durations (whole hours, days, weeks, months or years, " \
|
|
276
|
+
"up to ten years), :never and :custom, each once"
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
@expiration_options = choices.freeze
|
|
280
|
+
end
|
|
281
|
+
|
|
238
282
|
def default_scopes=(value)
|
|
239
283
|
unless value.is_a?(Array) && value.length <= MAX_CONFIGURED_SCOPES && value.all? { |scope| valid_scope_name?(scope) }
|
|
240
284
|
raise ArgumentError, "default_scopes must be a bounded Array of safe scope strings"
|
|
@@ -354,6 +398,10 @@ module ApiKeys
|
|
|
354
398
|
@authenticate_owner_method = validate_method_name!(value, :authenticate_owner_method)
|
|
355
399
|
end
|
|
356
400
|
|
|
401
|
+
def current_creator_method=(value)
|
|
402
|
+
@current_creator_method = validate_method_name!(value, :current_creator_method)
|
|
403
|
+
end
|
|
404
|
+
|
|
357
405
|
def current_environment=(value)
|
|
358
406
|
unless value.nil? || value.respond_to?(:call) || valid_config_name?(value)
|
|
359
407
|
raise ArgumentError, "current_environment must be callable, a safe String/Symbol, or nil"
|
|
@@ -660,11 +708,13 @@ module ApiKeys
|
|
|
660
708
|
# Owner Context Configuration
|
|
661
709
|
@current_owner_method = :current_user # Default to current_user for backward compatibility
|
|
662
710
|
@authenticate_owner_method = :authenticate_user! # Default to authenticate_user! for Devise compatibility
|
|
711
|
+
@current_creator_method = nil
|
|
663
712
|
|
|
664
713
|
# Optional Behaviors
|
|
665
714
|
@default_max_keys_per_owner = nil # No global key limit per owner
|
|
666
715
|
@require_key_name = false # Don't require names for keys globally
|
|
667
716
|
@expire_after = nil # Keys do not expire by default (e.g., 90.days)
|
|
717
|
+
@expiration_options = [1.day, 7.days, 30.days, 1.year, :never, :custom].freeze # The picker, like OpenAI's
|
|
668
718
|
@default_scopes = [].freeze # No default scopes assigned globally
|
|
669
719
|
|
|
670
720
|
# Performance
|
data/lib/api_keys/errors.rb
CHANGED
|
@@ -5,6 +5,12 @@ module ApiKeys
|
|
|
5
5
|
# Base error class for all ApiKeys errors
|
|
6
6
|
class BaseError < StandardError; end
|
|
7
7
|
|
|
8
|
+
# Raised when a key's expiration cannot be understood: "Custom" without a
|
|
9
|
+
# readable date, or a date past the ten-year limit. An ArgumentError, as
|
|
10
|
+
# every invalid expiration preset has always been; its message is written
|
|
11
|
+
# for the person filling in the form.
|
|
12
|
+
class InvalidExpirationError < ArgumentError; end
|
|
13
|
+
|
|
8
14
|
# Raised when attempting to revoke or destroy a non-revocable key
|
|
9
15
|
class KeyNotRevocableError < BaseError
|
|
10
16
|
def initialize(message = "This API key cannot be revoked or deleted")
|
|
@@ -11,8 +11,8 @@ module ApiKeys
|
|
|
11
11
|
# ActionView::Helpers::FormBuilder.include(ApiKeys::FormBuilderExtensions)
|
|
12
12
|
# end
|
|
13
13
|
#
|
|
14
|
-
# @example Expiration
|
|
15
|
-
# <%= form.api_key_expiration_select(class: "my-select-class") %>
|
|
14
|
+
# @example Expiration picker
|
|
15
|
+
# <%= form.api_key_expiration_select({}, class: "my-select-class") %>
|
|
16
16
|
#
|
|
17
17
|
# @example Scopes checkboxes with block for custom rendering
|
|
18
18
|
# <%= form.api_key_scopes_checkboxes(@scopes) do |scope, checked| %>
|
|
@@ -25,34 +25,62 @@ module ApiKeys
|
|
|
25
25
|
# clearing every box submits "no scopes" instead of nothing.)
|
|
26
26
|
#
|
|
27
27
|
module FormBuilderExtensions
|
|
28
|
-
# Renders
|
|
28
|
+
# Renders the expiration picker: 1 day, 7 days, 30 days, 1 year, Never
|
|
29
|
+
# (config.expiration_options). Like OpenAI's, it asks for a choice
|
|
30
|
+
# ("Select an expiration", required) instead of defaulting to one, and
|
|
31
|
+
# each option carries the date it means (`data-api-keys-expires-on`).
|
|
29
32
|
#
|
|
30
|
-
# @param options [Hash] Options passed to the select helper
|
|
31
|
-
#
|
|
33
|
+
# @param options [Hash] Options passed to the select helper, plus:
|
|
34
|
+
# - :selected [String] Preselect a preset ("30_days"); no prompt then.
|
|
35
|
+
# - :prompt [String, false] The placeholder (default "Select an expiration").
|
|
36
|
+
# - :custom [Boolean] Offer "Custom" too (default false). Render
|
|
37
|
+
# `api_key_expiration_date_field` with it, and show that field when
|
|
38
|
+
# Custom is chosen.
|
|
39
|
+
# @param html_options [Hash] HTML attributes for the select element.
|
|
40
|
+
# `required: true` unless you pass `required: false`.
|
|
32
41
|
# @return [String] HTML select element
|
|
33
42
|
#
|
|
34
43
|
# @example Basic usage
|
|
35
44
|
# <%= form.api_key_expiration_select %>
|
|
36
45
|
#
|
|
37
46
|
# @example With Tailwind classes
|
|
38
|
-
# <%= form.api_key_expiration_select(class: "w-full px-4 py-3 border rounded-lg") %>
|
|
47
|
+
# <%= form.api_key_expiration_select({}, class: "w-full px-4 py-3 border rounded-lg") %>
|
|
39
48
|
#
|
|
40
|
-
# @example With custom
|
|
41
|
-
# <%= form.api_key_expiration_select(
|
|
49
|
+
# @example With a custom date
|
|
50
|
+
# <%= form.api_key_expiration_select(custom: true) %>
|
|
51
|
+
# <%= form.api_key_expiration_date_field %>
|
|
42
52
|
#
|
|
43
53
|
def api_key_expiration_select(options = {}, html_options = {})
|
|
44
|
-
# expires_at_preset is a form-only param, not a model attribute
|
|
45
|
-
#
|
|
46
|
-
|
|
54
|
+
# expires_at_preset is a form-only param, not a model attribute, so the
|
|
55
|
+
# selection comes from :selected, never from the object.
|
|
56
|
+
options = options.dup
|
|
57
|
+
custom = options.delete(:custom) || false
|
|
58
|
+
selected = options.delete(:selected)
|
|
59
|
+
prompt = options.key?(:prompt) ? options.delete(:prompt) : "Select an expiration"
|
|
60
|
+
options[:prompt] = prompt if prompt && selected.nil?
|
|
47
61
|
|
|
48
62
|
select(
|
|
49
63
|
:expires_at_preset,
|
|
50
|
-
@template.options_for_select(ApiKeys::ExpirationOptions.
|
|
64
|
+
@template.options_for_select(ApiKeys::ExpirationOptions.choices(include_custom: custom, preview: true), selected),
|
|
51
65
|
options,
|
|
52
|
-
html_options
|
|
66
|
+
{ required: true }.merge(html_options)
|
|
53
67
|
)
|
|
54
68
|
end
|
|
55
69
|
|
|
70
|
+
# Renders the date field for the picker's "Custom" choice
|
|
71
|
+
# (`api_key[expires_on]`), from tomorrow up to ten years out. The key
|
|
72
|
+
# expires at the end of the chosen day.
|
|
73
|
+
#
|
|
74
|
+
# @param html_options [Hash] HTML attributes for the input.
|
|
75
|
+
# @return [String] HTML date input
|
|
76
|
+
#
|
|
77
|
+
# @example
|
|
78
|
+
# <%= form.api_key_expiration_date_field(class: "form-input") %>
|
|
79
|
+
#
|
|
80
|
+
def api_key_expiration_date_field(html_options = {})
|
|
81
|
+
date_field(:expires_on, ApiKeys::ExpirationOptions.custom_date_range.merge(html_options))
|
|
82
|
+
end
|
|
83
|
+
|
|
56
84
|
# Renders checkboxes for API key scopes.
|
|
57
85
|
#
|
|
58
86
|
# If a block is given, yields each scope and its checked state for custom rendering.
|
|
@@ -2,136 +2,278 @@
|
|
|
2
2
|
|
|
3
3
|
module ApiKeys
|
|
4
4
|
module Helpers
|
|
5
|
-
#
|
|
5
|
+
# The expiration picker: what a new key's form offers, and how each choice
|
|
6
|
+
# becomes an `expires_at`.
|
|
6
7
|
#
|
|
7
|
-
#
|
|
8
|
-
#
|
|
8
|
+
# The choices come from `config.expiration_options`, which defaults to the
|
|
9
|
+
# picker OpenAI and most API dashboards use:
|
|
9
10
|
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
11
|
+
# 1 day · 7 days · 30 days · 1 year · Never · Custom
|
|
12
|
+
#
|
|
13
|
+
# A choice travels through the form as a preset string: "1_day",
|
|
14
|
+
# "30_days", "1_year", "12_hours", "no_expiration" (or "never"), and
|
|
15
|
+
# "custom", which takes the date the key expires.
|
|
16
|
+
#
|
|
17
|
+
# @example In your view
|
|
18
|
+
# <%= form.select :expires_at_preset, ApiKeys::ExpirationOptions.for_select %>
|
|
13
19
|
#
|
|
14
20
|
# @example In your controller
|
|
15
|
-
# expires_at = ApiKeys::
|
|
16
|
-
#
|
|
21
|
+
# expires_at = ApiKeys::ExpirationOptions.parse(params[:expires_at_preset])
|
|
22
|
+
# ApiKeys::ExpirationOptions.parse("custom", on: "2027-01-31") # => end of that day
|
|
17
23
|
#
|
|
18
24
|
class ExpirationOptions
|
|
19
|
-
#
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
# A for_select preset given in whole days, as before 0.10.
|
|
26
|
+
LegacyDays = Struct.new(:days)
|
|
27
|
+
private_constant :LegacyDays
|
|
28
|
+
|
|
29
|
+
# The longest a key can be set to live, in days (about ten years).
|
|
30
|
+
MAX_DAYS = 3650
|
|
31
|
+
# The symbols config.expiration_options takes beside durations.
|
|
32
|
+
CHOICES = %i[never custom].freeze
|
|
33
|
+
NEVER = "no_expiration"
|
|
34
|
+
CUSTOM = "custom"
|
|
35
|
+
# The units a preset can name, smallest first.
|
|
36
|
+
UNITS = %w[hour day week month year].freeze
|
|
37
|
+
PRESET_FORMAT = /\A(\d{1,6})_(#{UNITS.join('|')})s?\z/
|
|
38
|
+
DATE_FORMAT = /\A\d{4}-\d{2}-\d{2}\z/
|
|
39
|
+
DATE_LABEL = "%B %-d, %Y"
|
|
40
|
+
# What for_select offered before 0.10, in whole days.
|
|
41
|
+
LEGACY_PRESETS = [7, 30, 60, 90, 365].freeze
|
|
28
42
|
|
|
29
43
|
class << self
|
|
30
|
-
#
|
|
44
|
+
# The picker's options, in the order `config.expiration_options`
|
|
45
|
+
# lists them, as [label, value] pairs for a Rails select helper. Ask
|
|
46
|
+
# for a choice with a prompt (see `form.api_key_expiration_select`),
|
|
47
|
+
# since a browser preselects the first option.
|
|
31
48
|
#
|
|
32
|
-
# @param include_no_expiration [Boolean]
|
|
33
|
-
#
|
|
34
|
-
#
|
|
35
|
-
#
|
|
49
|
+
# @param include_no_expiration [Boolean] Offer the never option
|
|
50
|
+
# (default: true). While `config.expire_after` is set it names that
|
|
51
|
+
# lifetime ("90 days (default)") instead of promising "Never": its
|
|
52
|
+
# value, "no_expiration", has always meant "no expiration of my own".
|
|
53
|
+
# @param include_custom [Boolean] Offer "Custom" (default: false). Only
|
|
54
|
+
# a form that also renders the date field (`expires_on`) can use it.
|
|
55
|
+
# @param preview [Boolean] Add each choice's expiration date as a
|
|
56
|
+
# `data-api-keys-expires-on` attribute, so the form can show it.
|
|
57
|
+
# @return [Array<Array>]
|
|
36
58
|
#
|
|
37
|
-
# @example
|
|
38
|
-
# ExpirationOptions.
|
|
39
|
-
# # => [["
|
|
59
|
+
# @example
|
|
60
|
+
# ExpirationOptions.choices
|
|
61
|
+
# # => [["1 day", "1_day"], ["7 days", "7_days"], ["30 days", "30_days"],
|
|
62
|
+
# # ["1 year", "1_year"], ["Never", "no_expiration"]]
|
|
63
|
+
#
|
|
64
|
+
def choices(include_no_expiration: true, include_custom: false, preview: false)
|
|
65
|
+
build_options(ApiKeys.configuration.expiration_options, include_no_expiration, include_custom, preview)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# The options forms were built on before 0.10, unchanged: No
|
|
69
|
+
# Expiration first, then 7, 30, 60 and 90 days and 1 year. It does not
|
|
70
|
+
# follow `config.expiration_options` (see .choices), so a form that
|
|
71
|
+
# preselects "365_days" or "90_days", or relies on the first option,
|
|
72
|
+
# keeps working exactly as it did.
|
|
40
73
|
#
|
|
41
|
-
# @
|
|
42
|
-
#
|
|
43
|
-
#
|
|
74
|
+
# @param include_no_expiration [Boolean] Offer "No Expiration" (default: true).
|
|
75
|
+
# @param presets [Array<Integer, ActiveSupport::Duration>, nil] Offer
|
|
76
|
+
# these instead: whole days ("30_days") or durations ("1_year").
|
|
77
|
+
# @param include_custom [Boolean] Add "Custom" at the end (default: false).
|
|
78
|
+
# @param preview [Boolean] Add each choice's date (see .choices).
|
|
79
|
+
# @return [Array<Array>]
|
|
44
80
|
#
|
|
45
|
-
# @example
|
|
46
|
-
# ExpirationOptions.for_select
|
|
47
|
-
# # => [["7 days", "7_days"], ["30 days", "30_days"],
|
|
81
|
+
# @example
|
|
82
|
+
# ExpirationOptions.for_select
|
|
83
|
+
# # => [["No Expiration", "no_expiration"], ["7 days", "7_days"], ["30 days", "30_days"],
|
|
84
|
+
# # ["60 days", "60_days"], ["90 days", "90_days"], ["1 year", "365_days"]]
|
|
48
85
|
#
|
|
49
|
-
def for_select(include_no_expiration: true, presets: nil)
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
filter_default_presets(include_no_expiration)
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
options.map { |opt| [opt[:label], opt[:value]] }
|
|
86
|
+
def for_select(include_no_expiration: true, presets: nil, include_custom: false, preview: false)
|
|
87
|
+
configured = choices_from_presets(presets || LEGACY_PRESETS)
|
|
88
|
+
configured += [:custom] if include_custom
|
|
89
|
+
build_options(configured, include_no_expiration, include_custom, preview, never_label: "No Expiration")
|
|
57
90
|
end
|
|
58
91
|
|
|
59
|
-
#
|
|
92
|
+
# Turns a preset into the moment the key expires.
|
|
60
93
|
#
|
|
61
|
-
# @param preset [String, nil]
|
|
62
|
-
# @
|
|
94
|
+
# @param preset [String, nil] "30_days", "1_year", "no_expiration", "custom"...
|
|
95
|
+
# @param on [String, Date, nil] With "custom": the day the key expires
|
|
96
|
+
# ("2027-01-31"). The key expires at 23:59:59 that day, in the
|
|
97
|
+
# app's time zone.
|
|
98
|
+
# @return [ActiveSupport::TimeWithZone, nil] nil means the key never expires.
|
|
99
|
+
# @raise [ArgumentError] for anything else, so a typo never makes a
|
|
100
|
+
# key permanent. A missing or out-of-range custom date raises
|
|
101
|
+
# ApiKeys::Errors::InvalidExpirationError (an ArgumentError) with a
|
|
102
|
+
# message a person can act on.
|
|
63
103
|
#
|
|
64
104
|
# @example
|
|
65
|
-
# ExpirationOptions.parse("30_days")
|
|
66
|
-
# ExpirationOptions.parse("
|
|
67
|
-
# ExpirationOptions.parse(
|
|
68
|
-
# ExpirationOptions.parse("invalid") # => raises ArgumentError
|
|
105
|
+
# ExpirationOptions.parse("30_days") # => 30.days.from_now
|
|
106
|
+
# ExpirationOptions.parse("never") # => nil
|
|
107
|
+
# ExpirationOptions.parse("custom", on: "2027-01-31") # => 2027-01-31 23:59:59
|
|
69
108
|
#
|
|
70
|
-
def parse(preset)
|
|
109
|
+
def parse(preset, on: nil)
|
|
71
110
|
return nil if preset.nil? || preset == ""
|
|
72
111
|
unless preset.is_a?(String) && preset.valid_encoding? && preset.bytesize <= 32
|
|
73
112
|
raise ArgumentError, "Invalid API key expiration preset"
|
|
74
113
|
end
|
|
75
|
-
return nil if preset
|
|
114
|
+
return nil if preset == NEVER || preset == "never"
|
|
115
|
+
return custom_date(on) if preset == CUSTOM
|
|
76
116
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
117
|
+
duration = duration_for(preset)
|
|
118
|
+
raise ArgumentError, "Invalid API key expiration preset" unless duration
|
|
119
|
+
|
|
120
|
+
duration.from_now
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Whether the form offers this preset ("never" counts as "no_expiration").
|
|
124
|
+
#
|
|
125
|
+
# @param preset [String]
|
|
126
|
+
# @param include_custom [Boolean] Whether the form offers "Custom".
|
|
127
|
+
# @return [Boolean]
|
|
128
|
+
def offered?(preset, include_custom: false)
|
|
129
|
+
return false unless preset.is_a?(String)
|
|
130
|
+
|
|
131
|
+
preset = NEVER if preset == "never"
|
|
132
|
+
choices(include_custom: include_custom).any? { |_label, value| value == preset }
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# Whether `config.expiration_options` offers a custom date.
|
|
136
|
+
def custom_offered?
|
|
137
|
+
ApiKeys.configuration.expiration_options.include?(:custom)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Whether `config.expiration_options` offers the never option (named
|
|
141
|
+
# after `config.expire_after` while that is set).
|
|
142
|
+
def never_offered?
|
|
143
|
+
ApiKeys.configuration.expiration_options.include?(:never)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# The earliest and latest day a custom date can be, as the date
|
|
147
|
+
# field's min and max.
|
|
148
|
+
#
|
|
149
|
+
# @return [Hash] { min: "2026-11-14", max: "2036-11-10" }
|
|
150
|
+
def custom_date_range
|
|
151
|
+
{ min: (Date.current + 1).iso8601, max: (Date.current + MAX_DAYS).iso8601 }
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
# The preset string for a duration, or nil when the picker cannot
|
|
155
|
+
# offer it (not a whole number of one unit, not positive, or longer
|
|
156
|
+
# than ten years).
|
|
157
|
+
#
|
|
158
|
+
# @param duration [ActiveSupport::Duration]
|
|
159
|
+
# @return [String, nil] "1_day", "2_weeks", "1_year"...
|
|
160
|
+
def preset_for(duration)
|
|
161
|
+
return nil unless duration.is_a?(ActiveSupport::Duration) && duration.parts.size == 1
|
|
83
162
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return
|
|
163
|
+
unit, amount = duration.parts.first
|
|
164
|
+
unit = unit.to_s.delete_suffix("s")
|
|
165
|
+
return nil unless UNITS.include?(unit) && amount.is_a?(Integer)
|
|
87
166
|
|
|
88
|
-
|
|
167
|
+
preset = "#{amount}_#{unit.pluralize(amount)}"
|
|
168
|
+
preset if duration_for(preset)
|
|
89
169
|
end
|
|
90
170
|
|
|
91
|
-
#
|
|
171
|
+
# The default selection of forms written before 0.10.
|
|
92
172
|
#
|
|
93
|
-
# @return [String]
|
|
173
|
+
# @return [String] "no_expiration"
|
|
94
174
|
def default_value
|
|
95
|
-
|
|
175
|
+
NEVER
|
|
96
176
|
end
|
|
97
177
|
|
|
98
178
|
private
|
|
99
179
|
|
|
100
|
-
def
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
180
|
+
def build_options(configured, include_no_expiration, include_custom, preview, never_label: nil)
|
|
181
|
+
configured = configured.reject { |choice| choice == :never } unless include_no_expiration
|
|
182
|
+
configured = configured.reject { |choice| choice == :custom } unless include_custom
|
|
183
|
+
|
|
184
|
+
configured.map do |choice|
|
|
185
|
+
option = [choice == :never && never_label ? never_label : label_for(choice), value_for(choice)]
|
|
186
|
+
expires_at = preview_time(choice)
|
|
187
|
+
option << { "data-api-keys-expires-on" => expires_at.strftime(DATE_LABEL) } if preview && expires_at
|
|
188
|
+
option
|
|
105
189
|
end
|
|
106
190
|
end
|
|
107
191
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
192
|
+
# When a choice expires a key made now, or nil (Never, Custom).
|
|
193
|
+
def preview_time(choice)
|
|
194
|
+
return choice.days.days.from_now if choice.is_a?(LegacyDays)
|
|
195
|
+
return choice.from_now if choice.is_a?(ActiveSupport::Duration)
|
|
112
196
|
|
|
113
|
-
|
|
197
|
+
ApiKeys.configuration.expire_after&.from_now if choice == :never
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def duration_for(preset)
|
|
201
|
+
match = PRESET_FORMAT.match(preset)
|
|
202
|
+
return nil unless match
|
|
203
|
+
|
|
204
|
+
amount = match[1].to_i
|
|
205
|
+
duration = amount.public_send(match[2].pluralize)
|
|
206
|
+
duration if amount.positive? && duration.to_i <= MAX_DAYS.days.to_i
|
|
207
|
+
end
|
|
114
208
|
|
|
115
|
-
|
|
116
|
-
|
|
209
|
+
def custom_date(on)
|
|
210
|
+
date = case on
|
|
211
|
+
when Date then on
|
|
212
|
+
when String then parse_date(on.strip)
|
|
213
|
+
end
|
|
214
|
+
raise ApiKeys::Errors::InvalidExpirationError, "Pick the date the key expires." unless date
|
|
215
|
+
if date > Date.current + MAX_DAYS
|
|
216
|
+
raise ApiKeys::Errors::InvalidExpirationError, "A key can expire at most 10 years from now."
|
|
117
217
|
end
|
|
118
218
|
|
|
119
|
-
|
|
120
|
-
|
|
219
|
+
# 23:59:59 sharp: a fractional end_of_day would round up to the next
|
|
220
|
+
# midnight in a datetime column without sub-second precision (MySQL).
|
|
221
|
+
(Time.zone || Time.find_zone!("UTC")).local(date.year, date.month, date.day, 23, 59, 59)
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def parse_date(text)
|
|
225
|
+
Date.iso8601(text) if text.valid_encoding? && text.match?(DATE_FORMAT)
|
|
226
|
+
rescue Date::Error
|
|
227
|
+
nil
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def label_for(choice)
|
|
231
|
+
case choice
|
|
232
|
+
when :never then never_label
|
|
233
|
+
when :custom then "Custom"
|
|
234
|
+
when LegacyDays then legacy_days_label(choice.days)
|
|
235
|
+
else
|
|
236
|
+
unit, amount = choice.parts.first
|
|
237
|
+
unit = unit.to_s.delete_suffix("s")
|
|
238
|
+
"#{amount} #{unit.pluralize(amount)}"
|
|
121
239
|
end
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# config.expire_after gives a key without an expiration of its own
|
|
243
|
+
# that lifetime, so the never option names it.
|
|
244
|
+
def never_label
|
|
245
|
+
expire_after = ApiKeys.configuration.expire_after
|
|
246
|
+
return "Never" if expire_after.nil?
|
|
247
|
+
|
|
248
|
+
lifetime = expire_after.is_a?(ActiveSupport::Duration) ? expire_after : expire_after.seconds
|
|
249
|
+
"#{lifetime.inspect} (default)"
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def legacy_days_label(days)
|
|
253
|
+
return "#{days / 365} #{'year'.pluralize(days / 365)}" if (days % 365).zero?
|
|
254
|
+
|
|
255
|
+
"#{days} #{'day'.pluralize(days)}"
|
|
256
|
+
end
|
|
122
257
|
|
|
123
|
-
|
|
258
|
+
def value_for(choice)
|
|
259
|
+
case choice
|
|
260
|
+
when :never then NEVER
|
|
261
|
+
when :custom then CUSTOM
|
|
262
|
+
when LegacyDays then "#{choice.days}_days"
|
|
263
|
+
else preset_for(choice)
|
|
264
|
+
end
|
|
124
265
|
end
|
|
125
266
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
267
|
+
# Whole days (as before 0.10) or durations, with "Never" first.
|
|
268
|
+
def choices_from_presets(presets)
|
|
269
|
+
# A Duration answers is_a?(Integer) with true, so ask about it first.
|
|
270
|
+
# Whole days stay LegacyDays, so they keep 0.9's "N_days" values.
|
|
271
|
+
durations = presets.map { |days| days.is_a?(ActiveSupport::Duration) || !days.is_a?(Integer) ? days : LegacyDays.new(days) } if presets.is_a?(Array)
|
|
272
|
+
unless durations&.all? { |duration| duration.is_a?(LegacyDays) ? duration_for("#{duration.days}_days") : preset_for(duration) }
|
|
273
|
+
raise ArgumentError, "Expiration presets must be whole days or durations, from 1 day up to ten years"
|
|
274
|
+
end
|
|
133
275
|
|
|
134
|
-
|
|
276
|
+
[:never, *durations]
|
|
135
277
|
end
|
|
136
278
|
end
|
|
137
279
|
end
|
|
@@ -53,6 +53,21 @@ module ApiKeys
|
|
|
53
53
|
REFUSAL_REASONS.fetch(api_key.last_refusal_code.to_s, UNKNOWN_REFUSAL_REASON)
|
|
54
54
|
end
|
|
55
55
|
|
|
56
|
+
# Says when a key expires, in plain English.
|
|
57
|
+
#
|
|
58
|
+
# @param api_key [ApiKeys::ApiKey] The API key
|
|
59
|
+
# @return [String] "Expires on December 13, 2026", "Expired on
|
|
60
|
+
# November 1, 2026", or "Never expires"
|
|
61
|
+
#
|
|
62
|
+
# @example
|
|
63
|
+
# api_key_expiration_label(@key) # => "Expires on December 13, 2026"
|
|
64
|
+
#
|
|
65
|
+
def api_key_expiration_label(api_key)
|
|
66
|
+
return "Never expires" if api_key.expires_at.nil?
|
|
67
|
+
|
|
68
|
+
"#{api_key.expired? ? 'Expired' : 'Expires'} on #{api_key.expires_at.strftime(ApiKeys::Helpers::ExpirationOptions::DATE_LABEL)}"
|
|
69
|
+
end
|
|
70
|
+
|
|
56
71
|
# Returns the status of an API key as a symbol.
|
|
57
72
|
#
|
|
58
73
|
# @param api_key [ApiKeys::ApiKey] The API key
|
|
@@ -70,7 +70,9 @@ module ApiKeys
|
|
|
70
70
|
# validations) and no update_column can name another one. A backfill of
|
|
71
71
|
# keys made before the columns existed writes them with SQL (update_all).
|
|
72
72
|
attr_readonly :created_by_type, :created_by_id
|
|
73
|
-
|
|
73
|
+
# Form-only: what the expiration picker submitted, kept so a form that
|
|
74
|
+
# failed to save shows the same choice again.
|
|
75
|
+
attr_accessor :expires_at_preset, :expires_on
|
|
74
76
|
|
|
75
77
|
# JSON attributes (:scopes, :metadata) are defined in the engine initializer
|
|
76
78
|
# using ActiveSupport.on_load(:active_record) to ensure DB connection is ready.
|