From b9b3ec83540a82fc96ddd64715a51d7adeb7312c Mon Sep 17 00:00:00 2001 From: Imre Farkas Date: Fri, 5 Jul 2019 08:12:29 +0200 Subject: CE port of "Require session with smartcard login for Git access" --- lib/gitlab/namespaced_session_store.rb | 15 ++++++++---- spec/lib/gitlab/namespaced_session_store_spec.rb | 30 +++++++++++++++++------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/lib/gitlab/namespaced_session_store.rb b/lib/gitlab/namespaced_session_store.rb index 34520078bfb..f0f24c081c3 100644 --- a/lib/gitlab/namespaced_session_store.rb +++ b/lib/gitlab/namespaced_session_store.rb @@ -4,19 +4,24 @@ module Gitlab class NamespacedSessionStore delegate :[], :[]=, to: :store - def initialize(key) + def initialize(key, session = Session.current) @key = key + @session = session end def initiated? - !Session.current.nil? + !session.nil? end def store - return unless Session.current + return unless session - Session.current[@key] ||= {} - Session.current[@key] + session[@key] ||= {} + session[@key] end + + private + + attr_reader :session end end diff --git a/spec/lib/gitlab/namespaced_session_store_spec.rb b/spec/lib/gitlab/namespaced_session_store_spec.rb index c0af2ede32a..e177c44ad67 100644 --- a/spec/lib/gitlab/namespaced_session_store_spec.rb +++ b/spec/lib/gitlab/namespaced_session_store_spec.rb @@ -4,19 +4,33 @@ require 'spec_helper' describe Gitlab::NamespacedSessionStore do let(:key) { :some_key } - subject { described_class.new(key) } - it 'stores data under the specified key' do - Gitlab::Session.with_session({}) do - subject[:new_data] = 123 + context 'current session' do + subject { described_class.new(key) } - expect(Thread.current[:session_storage][key]).to eq(new_data: 123) + it 'stores data under the specified key' do + Gitlab::Session.with_session({}) do + subject[:new_data] = 123 + + expect(Thread.current[:session_storage][key]).to eq(new_data: 123) + end + end + + it 'retrieves data from the given key' do + Thread.current[:session_storage] = { key => { existing_data: 123 } } + + expect(subject[:existing_data]).to eq 123 end end - it 'retrieves data from the given key' do - Thread.current[:session_storage] = { key => { existing_data: 123 } } + context 'passed in session' do + let(:data) { { 'data' => 42 } } + let(:session) { { 'some_key' => data } } + + subject { described_class.new(key, session.with_indifferent_access) } - expect(subject[:existing_data]).to eq 123 + it 'retrieves data from the given key' do + expect(subject['data']).to eq 42 + end end end -- cgit v1.2.1