2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module TokenAuthenticatableStrategies
|
|
|
|
class Encrypted < Base
|
2022-05-07 20:08:51 +05:30
|
|
|
def token_fields
|
|
|
|
super + [encrypted_field]
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def find_token_authenticatable(token, unscoped = false)
|
|
|
|
return if token.blank?
|
|
|
|
|
2022-02-27 12:50:16 +05:30
|
|
|
instance = if required?
|
|
|
|
find_by_encrypted_token(token, unscoped)
|
|
|
|
elsif optional?
|
|
|
|
find_by_encrypted_token(token, unscoped) ||
|
|
|
|
find_by_plaintext_token(token, unscoped)
|
|
|
|
elsif migrating?
|
|
|
|
find_by_plaintext_token(token, unscoped)
|
|
|
|
else
|
|
|
|
raise ArgumentError, _("Unknown encryption strategy: %{encrypted_strategy}!") % { encrypted_strategy: encrypted_strategy }
|
|
|
|
end
|
|
|
|
|
|
|
|
instance if instance && matches_prefix?(instance, token)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_token(instance)
|
|
|
|
# TODO, tech debt, because some specs are testing migrations, but are still
|
|
|
|
# using factory bot to create resources, it might happen that a database
|
|
|
|
# schema does not have "#{token_name}_encrypted" field yet, however a bunch
|
|
|
|
# of models call `ensure_#{token_name}` in `before_save`.
|
|
|
|
#
|
|
|
|
# In that case we are using insecure strategy, but this should only happen
|
|
|
|
# in tests, because otherwise `encrypted_field` is going to exist.
|
|
|
|
#
|
|
|
|
# Another use case is when we are caching resources / columns, like we do
|
|
|
|
# in case of ApplicationSetting.
|
|
|
|
|
|
|
|
return super if instance.has_attribute?(encrypted_field)
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
if required?
|
2019-07-31 22:56:46 +05:30
|
|
|
raise ArgumentError, _('Using required encryption strategy when encrypted field is missing!')
|
2019-02-15 15:39:39 +05:30
|
|
|
else
|
|
|
|
insecure_strategy.ensure_token(instance)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_token(instance)
|
|
|
|
return insecure_strategy.get_token(instance) if migrating?
|
|
|
|
|
2022-02-27 12:50:16 +05:30
|
|
|
get_encrypted_token(instance)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def set_token(instance, token)
|
|
|
|
raise ArgumentError unless token.present?
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
instance[encrypted_field] = EncryptionHelper.encrypt_token(token)
|
2019-02-15 15:39:39 +05:30
|
|
|
instance[token_field] = token if migrating?
|
2019-07-07 11:18:12 +05:30
|
|
|
instance[token_field] = nil if optional?
|
2019-02-15 15:39:39 +05:30
|
|
|
token
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def required?
|
|
|
|
encrypted_strategy == :required
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrating?
|
|
|
|
encrypted_strategy == :migrating
|
|
|
|
end
|
|
|
|
|
|
|
|
def optional?
|
|
|
|
encrypted_strategy == :optional
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2022-02-27 12:50:16 +05:30
|
|
|
def get_encrypted_token(instance)
|
|
|
|
encrypted_token = instance.read_attribute(encrypted_field)
|
|
|
|
token = EncryptionHelper.decrypt_token(encrypted_token)
|
|
|
|
token || (insecure_strategy.get_token(instance) if optional?)
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def encrypted_strategy
|
|
|
|
value = options[:encrypted]
|
|
|
|
value = value.call if value.is_a?(Proc)
|
|
|
|
|
|
|
|
unless value.in?([:required, :optional, :migrating])
|
2019-07-31 22:56:46 +05:30
|
|
|
raise ArgumentError, _('encrypted: needs to be a :required, :optional or :migrating!')
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def find_by_plaintext_token(token, unscoped)
|
|
|
|
insecure_strategy.find_token_authenticatable(token, unscoped)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_by_encrypted_token(token, unscoped)
|
2021-04-29 21:17:54 +05:30
|
|
|
encrypted_value = EncryptionHelper.encrypt_token(token)
|
|
|
|
token_encrypted_with_static_iv = Gitlab::CryptoHelper.aes256_gcm_encrypt(token)
|
|
|
|
relation(unscoped).find_by(encrypted_field => [encrypted_value, token_encrypted_with_static_iv])
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def insecure_strategy
|
|
|
|
@insecure_strategy ||= TokenAuthenticatableStrategies::Insecure
|
|
|
|
.new(klass, token_field, options)
|
|
|
|
end
|
|
|
|
|
2022-02-27 12:50:16 +05:30
|
|
|
def matches_prefix?(instance, token)
|
2023-05-27 22:25:52 +05:30
|
|
|
!options[:require_prefix_for_validation] || token.start_with?(prefix_for(instance))
|
2022-02-27 12:50:16 +05:30
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def token_set?(instance)
|
2022-02-27 12:50:16 +05:30
|
|
|
token = get_encrypted_token(instance)
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
unless required?
|
2022-02-27 12:50:16 +05:30
|
|
|
token ||= insecure_strategy.get_token(instance)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
2022-02-27 12:50:16 +05:30
|
|
|
token.present? && matches_prefix?(instance, token)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def encrypted_field
|
|
|
|
@encrypted_field ||= "#{@token_field}_encrypted"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|