Update TokenAuthenticatable so methods can be overridden

This commit is contained in:
Lin Jen-Shin 2019-03-08 23:28:59 +08:00
parent 6b0d493350
commit 364791c9f0
2 changed files with 19 additions and 8 deletions

View file

@ -7,11 +7,15 @@ class ApplicationSetting < ActiveRecord::Base
include IgnorableColumn
include ChronicDurationAttribute
include ApplicationSettingImplementation
add_authentication_token_field :runners_registration_token, encrypted: -> { Feature.enabled?(:application_settings_tokens_optional_encryption) ? :optional : :required }
add_authentication_token_field :health_check_access_token
# Include here so it can override methods from
# `add_authentication_token_field`
# We don't prepend for now because otherwise we'll need to
# fix a lot of tests using allow_any_instance_of
include ApplicationSettingImplementation
serialize :restricted_visibility_levels # rubocop:disable Cop/ActiveRecordSerialize
serialize :import_sources # rubocop:disable Cop/ActiveRecordSerialize
serialize :disabled_oauth_sign_in_sources, Array # rubocop:disable Cop/ActiveRecordSerialize

View file

@ -26,34 +26,41 @@ module TokenAuthenticatable
end
end
define_method(token_field) do
mod = token_authenticatable_module
mod.define_method(token_field) do
strategy.get_token(self)
end
define_method("set_#{token_field}") do |token|
mod.define_method("set_#{token_field}") do |token|
strategy.set_token(self, token)
end
define_method("ensure_#{token_field}") do
mod.define_method("ensure_#{token_field}") do
strategy.ensure_token(self)
end
# Returns a token, but only saves when the database is in read & write mode
define_method("ensure_#{token_field}!") do
mod.define_method("ensure_#{token_field}!") do
strategy.ensure_token!(self)
end
# Resets the token, but only saves when the database is in read & write mode
define_method("reset_#{token_field}!") do
mod.define_method("reset_#{token_field}!") do
strategy.reset_token!(self)
end
define_method("#{token_field}_matches?") do |other_token|
mod.define_method("#{token_field}_matches?") do |other_token|
token = read_attribute(token_field)
token.present? && ActiveSupport::SecurityUtils.variable_size_secure_compare(other_token, token)
end
end
def token_authenticatable_module
@token_authenticatable_module ||=
const_set(:TokenAuthenticatable, Module.new).tap(&method(:include))
end
def token_authenticatable_fields
@token_authenticatable_fields ||= []
end