2018-11-13 09:11:36 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module TokenAuthenticatableStrategies
|
|
|
|
class Encrypted < Base
|
|
|
|
def find_token_authenticatable(token, unscoped = false)
|
2018-12-03 08:12:51 -05:00
|
|
|
return if token.blank?
|
2018-12-03 08:29:51 -05:00
|
|
|
|
2019-03-06 07:18:53 -05:00
|
|
|
if required?
|
|
|
|
find_by_encrypted_token(token, unscoped)
|
|
|
|
elsif optional?
|
2018-12-03 08:12:51 -05:00
|
|
|
find_by_encrypted_token(token, unscoped) ||
|
|
|
|
find_by_plaintext_token(token, unscoped)
|
|
|
|
elsif migrating?
|
2018-12-03 08:29:51 -05:00
|
|
|
find_by_plaintext_token(token, unscoped)
|
2018-12-03 08:12:51 -05:00
|
|
|
else
|
2019-03-06 07:18:53 -05:00
|
|
|
raise ArgumentError, "Unknown encryption strategy: #{encrypted_strategy}!"
|
2018-12-03 07:40:45 -05:00
|
|
|
end
|
2018-11-19 09:01:37 -05:00
|
|
|
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`.
|
2018-11-20 09:25:45 -05:00
|
|
|
#
|
|
|
|
# In that case we are using insecure strategy, but this should only happen
|
|
|
|
# in tests, because otherwise `encrypted_field` is going to exist.
|
2018-11-28 05:43:17 -05:00
|
|
|
#
|
|
|
|
# Another use case is when we are caching resources / columns, like we do
|
|
|
|
# in case of ApplicationSetting.
|
2018-11-20 08:29:46 -05:00
|
|
|
|
|
|
|
return super if instance.has_attribute?(encrypted_field)
|
|
|
|
|
2019-03-06 07:18:53 -05:00
|
|
|
if required?
|
|
|
|
raise ArgumentError, 'Using required encryption strategy when encrypted field is missing!'
|
2018-11-20 08:29:46 -05:00
|
|
|
else
|
2018-12-03 08:12:51 -05:00
|
|
|
insecure_strategy.ensure_token(instance)
|
2018-11-20 08:29:46 -05:00
|
|
|
end
|
2018-11-13 09:11:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_token(instance)
|
2018-12-03 08:12:51 -05:00
|
|
|
return insecure_strategy.get_token(instance) if migrating?
|
2018-12-03 06:35:49 -05:00
|
|
|
|
2018-11-23 04:43:47 -05:00
|
|
|
encrypted_token = instance.read_attribute(encrypted_field)
|
|
|
|
token = Gitlab::CryptoHelper.aes256_gcm_decrypt(encrypted_token)
|
2019-03-06 07:18:53 -05:00
|
|
|
token || (insecure_strategy.get_token(instance) if optional?)
|
2018-11-13 09:11:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_token(instance, token)
|
2018-11-14 08:46:46 -05:00
|
|
|
raise ArgumentError unless token.present?
|
2018-11-13 09:11:36 -05:00
|
|
|
|
2018-11-14 08:46:46 -05:00
|
|
|
instance[encrypted_field] = Gitlab::CryptoHelper.aes256_gcm_encrypt(token)
|
2018-12-03 06:35:49 -05:00
|
|
|
instance[token_field] = token if migrating?
|
2019-03-06 07:18:53 -05:00
|
|
|
instance[token_field] = nil if optional?
|
2018-11-15 09:17:32 -05:00
|
|
|
token
|
2018-11-13 09:11:36 -05:00
|
|
|
end
|
|
|
|
|
2019-03-06 07:18:53 -05:00
|
|
|
def required?
|
|
|
|
encrypted_strategy == :required
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrating?
|
|
|
|
encrypted_strategy == :migrating
|
|
|
|
end
|
|
|
|
|
|
|
|
def optional?
|
|
|
|
encrypted_strategy == :optional
|
2018-12-03 08:12:51 -05:00
|
|
|
end
|
|
|
|
|
2018-11-13 09:11:36 -05:00
|
|
|
protected
|
|
|
|
|
2019-03-06 07:18:53 -05:00
|
|
|
def encrypted_strategy
|
|
|
|
value = options[:encrypted]
|
|
|
|
value = value.call if value.is_a?(Proc)
|
|
|
|
|
|
|
|
unless value.in?([:required, :optional, :migrating])
|
|
|
|
raise ArgumentError, 'encrypted: needs to be a :required, :optional or :migrating!'
|
|
|
|
end
|
|
|
|
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
2018-12-03 08:12:51 -05:00
|
|
|
def find_by_plaintext_token(token, unscoped)
|
|
|
|
insecure_strategy.find_token_authenticatable(token, unscoped)
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_by_encrypted_token(token, unscoped)
|
|
|
|
encrypted_value = Gitlab::CryptoHelper.aes256_gcm_encrypt(token)
|
|
|
|
relation(unscoped).find_by(encrypted_field => encrypted_value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def insecure_strategy
|
|
|
|
@insecure_strategy ||= TokenAuthenticatableStrategies::Insecure
|
2018-11-14 08:46:46 -05:00
|
|
|
.new(klass, token_field, options)
|
2018-11-13 09:11:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def token_set?(instance)
|
2018-11-14 08:46:46 -05:00
|
|
|
raw_token = instance.read_attribute(encrypted_field)
|
2018-12-04 03:10:57 -05:00
|
|
|
|
2019-03-06 07:18:53 -05:00
|
|
|
unless required?
|
2018-12-04 03:10:57 -05:00
|
|
|
raw_token ||= insecure_strategy.get_token(instance)
|
|
|
|
end
|
2018-11-13 09:11:36 -05:00
|
|
|
|
2018-11-14 08:40:37 -05:00
|
|
|
raw_token.present?
|
2018-11-13 09:11:36 -05:00
|
|
|
end
|
|
|
|
|
2018-11-14 08:46:46 -05:00
|
|
|
def encrypted_field
|
|
|
|
@encrypted_field ||= "#{@token_field}_encrypted"
|
2018-11-13 09:11:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|