Improve token authenticable tests and exceptions

This commit is contained in:
Grzegorz Bizon 2018-11-27 14:34:05 +01:00
parent 3dfbfa4e4f
commit 37add27a00
4 changed files with 10 additions and 8 deletions

View File

@ -47,17 +47,17 @@ module TokenAuthenticatableStrategies
options[:fallback] == true
end
def self.fabricate(instance, field, options)
def self.fabricate(model, field, options)
if options[:digest] && options[:encrypted]
raise ArgumentError, 'Incompatible options set!'
end
if options[:digest]
TokenAuthenticatableStrategies::Digest.new(instance, field, options)
TokenAuthenticatableStrategies::Digest.new(model, field, options)
elsif options[:encrypted]
TokenAuthenticatableStrategies::Encrypted.new(instance, field, options)
TokenAuthenticatableStrategies::Encrypted.new(model, field, options)
else
TokenAuthenticatableStrategies::Insecure.new(instance, field, options)
TokenAuthenticatableStrategies::Insecure.new(model, field, options)
end
end

View File

@ -46,7 +46,7 @@ module TokenAuthenticatableStrategies
raise ArgumentError unless token.present?
instance[encrypted_field] = Gitlab::CryptoHelper.aes256_gcm_encrypt(token)
instance[token_field] = nil
fallback_strategy.set_token(instance, nil) if fallback?
token
end

View File

@ -17,7 +17,8 @@ module Gitlab
end
def ensure_utf8_size(str, bytes:)
raise ArgumentError if str.empty? || bytes.negative?
raise ArgumentError, 'Empty string provided!' if str.empty?
raise ArgumentError, 'Negative string size provided!' if bytes.negative?
truncated = str.each_char.each_with_object(+'') do |char, object|
if object.bytesize + char.bytesize > bytes

View File

@ -5,7 +5,7 @@ describe Gitlab::CryptoHelper do
it 'generates SHA256 digest Base46 encoded' do
digest = described_class.sha256('some-value')
expect(digest).to match %r{^[A-Za-z0-9+/=]+$}
expect(digest).to match %r{\A[A-Za-z0-9+/=]+\z}
expect(digest).to eq digest.strip
end
end
@ -14,7 +14,8 @@ describe Gitlab::CryptoHelper do
it 'is Base64 encoded string without new line character' do
encrypted = described_class.aes256_gcm_encrypt('some-value')
expect(encrypted).to match %r{^[A-Za-z0-9+/=]+$}
expect(encrypted).to match %r{\A[A-Za-z0-9+/=]+\z}
expect(encrypted).not_to include "\n"
end
end