Commit 37add27a authored by Grzegorz Bizon's avatar Grzegorz Bizon

Improve token authenticable tests and exceptions

parent 3dfbfa4e
...@@ -47,17 +47,17 @@ module TokenAuthenticatableStrategies ...@@ -47,17 +47,17 @@ module TokenAuthenticatableStrategies
options[:fallback] == true options[:fallback] == true
end end
def self.fabricate(instance, field, options) def self.fabricate(model, field, options)
if options[:digest] && options[:encrypted] if options[:digest] && options[:encrypted]
raise ArgumentError, 'Incompatible options set!' raise ArgumentError, 'Incompatible options set!'
end end
if options[:digest] if options[:digest]
TokenAuthenticatableStrategies::Digest.new(instance, field, options) TokenAuthenticatableStrategies::Digest.new(model, field, options)
elsif options[:encrypted] elsif options[:encrypted]
TokenAuthenticatableStrategies::Encrypted.new(instance, field, options) TokenAuthenticatableStrategies::Encrypted.new(model, field, options)
else else
TokenAuthenticatableStrategies::Insecure.new(instance, field, options) TokenAuthenticatableStrategies::Insecure.new(model, field, options)
end end
end end
......
...@@ -46,7 +46,7 @@ module TokenAuthenticatableStrategies ...@@ -46,7 +46,7 @@ module TokenAuthenticatableStrategies
raise ArgumentError unless token.present? raise ArgumentError unless token.present?
instance[encrypted_field] = Gitlab::CryptoHelper.aes256_gcm_encrypt(token) instance[encrypted_field] = Gitlab::CryptoHelper.aes256_gcm_encrypt(token)
instance[token_field] = nil fallback_strategy.set_token(instance, nil) if fallback?
token token
end end
......
...@@ -17,7 +17,8 @@ module Gitlab ...@@ -17,7 +17,8 @@ module Gitlab
end end
def ensure_utf8_size(str, bytes:) 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| truncated = str.each_char.each_with_object(+'') do |char, object|
if object.bytesize + char.bytesize > bytes if object.bytesize + char.bytesize > bytes
......
...@@ -5,7 +5,7 @@ describe Gitlab::CryptoHelper do ...@@ -5,7 +5,7 @@ describe Gitlab::CryptoHelper do
it 'generates SHA256 digest Base46 encoded' do it 'generates SHA256 digest Base46 encoded' do
digest = described_class.sha256('some-value') 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 expect(digest).to eq digest.strip
end end
end end
...@@ -14,7 +14,8 @@ describe Gitlab::CryptoHelper do ...@@ -14,7 +14,8 @@ describe Gitlab::CryptoHelper do
it 'is Base64 encoded string without new line character' do it 'is Base64 encoded string without new line character' do
encrypted = described_class.aes256_gcm_encrypt('some-value') 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
end end
......
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