Commit e9abaced authored by Grzegorz Bizon's avatar Grzegorz Bizon

Refactor encrypted token strategy class

parent fe4b5c98
...@@ -11,26 +11,18 @@ module TokenAuthenticatableStrategies ...@@ -11,26 +11,18 @@ module TokenAuthenticatableStrategies
end end
def find_token_authenticatable(token, unscoped = false) def find_token_authenticatable(token, unscoped = false)
return unless token return if token.blank?
return find_by_encrypted_token(token, unscoped) if fully_encrypted?
unless migrating? if fallback?
encrypted_value = Gitlab::CryptoHelper.aes256_gcm_encrypt(token) find_by_encrypted_token(token, unscoped) ||
token_authenticatable = relation(unscoped) find_by_plaintext_token(token, unscoped)
.find_by(encrypted_field => encrypted_value) elsif migrating?
end find_by_plaintext_token(token, unscoped) ||
find_by_encrypted_token(token, unscoped)
if fallback? || migrating? else
token_authenticatable ||= fallback_strategy raise ArgumentError, 'Unknown encryption strategy!'
.find_token_authenticatable(token)
end
if migrating?
encrypted_value = Gitlab::CryptoHelper.aes256_gcm_encrypt(token)
token_authenticatable ||= relation(unscoped)
.find_by(encrypted_field => encrypted_value)
end end
token_authenticatable
end end
def ensure_token(instance) def ensure_token(instance)
...@@ -47,20 +39,20 @@ module TokenAuthenticatableStrategies ...@@ -47,20 +39,20 @@ module TokenAuthenticatableStrategies
return super if instance.has_attribute?(encrypted_field) return super if instance.has_attribute?(encrypted_field)
if fallback? if fully_encrypted?
fallback_strategy.ensure_token(instance) raise ArgumentError, 'Using encrypted strategy when encrypted field is missing!'
else else
raise ArgumentError, 'No fallback defined when encrypted field is missing!' insecure_strategy.ensure_token(instance)
end end
end end
def get_token(instance) def get_token(instance)
return fallback_strategy.get_token(instance) if migrating? return insecure_strategy.get_token(instance) if migrating?
encrypted_token = instance.read_attribute(encrypted_field) encrypted_token = instance.read_attribute(encrypted_field)
token = Gitlab::CryptoHelper.aes256_gcm_decrypt(encrypted_token) token = Gitlab::CryptoHelper.aes256_gcm_decrypt(encrypted_token)
token || (fallback_strategy.get_token(instance) if fallback?) token || (insecure_strategy.get_token(instance) if fallback?)
end end
def set_token(instance, token) def set_token(instance, token)
...@@ -72,16 +64,29 @@ module TokenAuthenticatableStrategies ...@@ -72,16 +64,29 @@ module TokenAuthenticatableStrategies
token token
end end
def fully_encrypted?
!migrating? && !fallback?
end
protected protected
def fallback_strategy def find_by_plaintext_token(token, unscoped)
@fallback_strategy ||= TokenAuthenticatableStrategies::Insecure 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
.new(klass, token_field, options) .new(klass, token_field, options)
end end
def token_set?(instance) def token_set?(instance)
raw_token = instance.read_attribute(encrypted_field) raw_token = instance.read_attribute(encrypted_field)
raw_token ||= (fallback_strategy.get_token(instance) if fallback?) raw_token ||= (insecure_strategy.get_token(instance) if fallback?)
raw_token.present? raw_token.present?
end end
......
...@@ -35,8 +35,8 @@ describe TokenAuthenticatableStrategies::Encrypted do ...@@ -35,8 +35,8 @@ describe TokenAuthenticatableStrategies::Encrypted do
.to eq 'encrypted resource' .to eq 'encrypted resource'
end end
it 'uses fallback strategy when encrypted token cannot be found' do it 'uses insecure strategy when encrypted token cannot be found' do
allow(subject.send(:fallback_strategy)) allow(subject.send(:insecure_strategy))
.to receive(:find_token_authenticatable) .to receive(:find_token_authenticatable)
.and_return('plaintext resource') .and_return('plaintext resource')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment