diff --git a/Gemfile b/Gemfile index d070247..1e8c977 100644 --- a/Gemfile +++ b/Gemfile @@ -3,15 +3,15 @@ source "http://rubygems.org" gemspec group :development do - gem 'guard' - gem 'guard-bundler' - gem 'guard-rspec' - gem 'rake' + gem 'guard' + gem 'guard-bundler' + gem 'guard-rspec' + gem 'rake' end group :test do - gem 'coveralls' - gem 'rack-test' - gem 'simplecov' - gem 'rspec' + gem 'coveralls' + gem 'rack-test' + gem 'simplecov' + gem 'rspec' end diff --git a/Rakefile b/Rakefile index af6b616..30c66a3 100644 --- a/Rakefile +++ b/Rakefile @@ -7,5 +7,3 @@ RSpec::Core::RakeTask.new(:spec) task :default => :spec task :test => :spec - - diff --git a/lib/omniauth-remote-user/version.rb b/lib/omniauth-remote-user/version.rb index 901c6e6..925f665 100644 --- a/lib/omniauth-remote-user/version.rb +++ b/lib/omniauth-remote-user/version.rb @@ -1,5 +1,5 @@ module Omniauth - module RemoteUser - VERSION = '0.0.1' - end + module RemoteUser + VERSION = '0.0.1' + end end diff --git a/lib/omniauth/strategies/remote_user.rb b/lib/omniauth/strategies/remote_user.rb index 140e69a..463bfea 100644 --- a/lib/omniauth/strategies/remote_user.rb +++ b/lib/omniauth/strategies/remote_user.rb @@ -2,38 +2,31 @@ module OmniAuth module Strategies class RemoteUser include OmniAuth::Strategy - - option :internal_cookie, '_remote_user' - - def __write_file message - file = File.open("/home/git/gitlab/log/remote_user.log",'a') - file.write " \n #{message} \n" - file.close - end + option :internal_cookie, '_remote_user' def call(env) remote_user = env['HTTP_REMOTE_USER'] session_user = __current_user(env) - - if remote_user - if session_user - if remote_user == session_user - super(env) - else - __logout(env) - end + + if remote_user + if session_user + if remote_user == session_user + super(env) else - __login(env, remote_user) - end - else - if session_user __logout(env) - else - super(env) end + else + __login(env, remote_user) + end + else + if session_user + __logout(env) + else + super(env) end + end end def __current_user(env) @@ -43,7 +36,7 @@ module OmniAuth def __logout(env) request = Rack::Request.new(env) - request.session.clear + request.session.clear response = redirect_if_not_logging_in(request, request.path ) if response response.delete_cookie(options.internal_cookie , path: "#{request.script_name}" ) @@ -62,15 +55,15 @@ module OmniAuth def redirect_if_not_logging_in(request, url) if ! [ - _auth_path(request), - _callback_path(request) - ].include?(request.path_info) + _auth_path(request), + _callback_path(request) + ].include?(request.path_info) response = Rack::Response.new response.redirect url response end end - + uid do request.env['HTTP_REMOTE_USER'] @@ -80,7 +73,8 @@ module OmniAuth user_data = request.env['HTTP_REMOTE_USER_DATA'] if user_data data = JSON.parse(user_data) - data['nickname'] = data['name'] + data['nickname'] = data['firstname'] = data['name'].split()[0] + data['lastname'] = data['name'].split()[1] data else {} @@ -88,15 +82,15 @@ module OmniAuth end def request_phase - redirect _callback_path(request) + redirect _callback_path(request) end def _callback_path(request) - "#{_auth_path(request)}/callback" + "#{_auth_path(request)}/callback" end def _auth_path(request) - "#{request.script_name}#{path_prefix}/RemoteUser" + "#{request.script_name}#{path_prefix}/RemoteUser" end end diff --git a/spec/omniauth/strategies/remote_user_spec.rb b/spec/omniauth/strategies/remote_user_spec.rb index e37ace9..cf42525 100644 --- a/spec/omniauth/strategies/remote_user_spec.rb +++ b/spec/omniauth/strategies/remote_user_spec.rb @@ -1,116 +1,116 @@ require 'spec_helper' describe 'Test Strategy Remote_User' do - let(:app) do - Rack::Builder.new do |b| - b.use Rack::Session::Cookie, :secret => 'abc123' - b.use OmniAuth::Strategies::RemoteUser - b.run lambda { |_env| [200, {}, ['My body']] } - end.to_app - end - - context 'Without REMOTE_USER and not logged in' do - before(:each){ - get '/', {}, {} - } - - it 'Do nothing' do - expect(last_response.status).to eq(200) - expect(last_request.cookies['_remote_user']).to eq(nil) - expect(last_request.cookies['_gitlab_session']).to eq(nil) - end - end - - context 'Without REMOTE_USER and logged in' do - before(:each){ - clear_cookies - set_cookie "_gitlab_session=test" - set_cookie "_remote_user=test" - get '/', {}, {} - } - - it 'Logout curreent user' do - cookie_session_str = "_gitlab_session=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 -0000" << - "\n_remote_user=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 -0000" - expect(last_request.cookies['_gitlab_session']).to eq('test') - expect(last_request.cookies['_remote_user']).to eq('test') - expect(last_response.status).to eq(302) - expect(last_response['Set-Cookie']).to eq(cookie_session_str) - end - end - - context 'With REMOTE_USER and not logged in' do - before(:each){ - get '/', {}, { 'HTTP_REMOTE_USER' => 'foobar' } - } - - it 'logs REMOTE_USER in' do - expect(last_response.status).to eq(302) - expect(last_response['Set-Cookie']).to eq('_remote_user=foobar') - end - end - - context 'With REMOTE_USER, logged in and current user equals REMOTE_USER' do - before(:each){ - clear_cookies - set_cookie "_gitlab_session=foobar" - set_cookie "_remote_user=foobar" - get '/', {}, { 'HTTP_REMOTE_USER' => 'foobar' } - } - - it 'Do nothing' do - expect(last_request.cookies['_gitlab_session']).to eq('foobar') - expect(last_request.cookies['_remote_user']).to eq('foobar') - expect(last_response.status).to eq(200) - expect(last_response['Set-Cookie']).to eq(nil) - end - end - - context 'With REMOTE_USER, logged in and current user not equals REMOTE_USER' do - before(:each){ - clear_cookies - set_cookie "_gitlab_session=foobar" - set_cookie "_remote_user=foobar" - get '/', {}, { 'HTTP_REMOTE_USER' => 'foobar2' } - } - - it 'Logout current user and login REMOTE_USER and no have _gitlab_session' do - expect(last_request.cookies['_gitlab_session']).to eq('foobar') - expect(last_request.cookies['_remote_user']).to eq('foobar') - expect(last_response.status).to eq(302) - expect(last_response['Set-Cookie']).to include('_gitlab_session=') - end - end - - context 'Verify omniauth hash with REMOTE_USER_DATA' do - before(:each){ - clear_cookies - post '/auth/RemoteUser/callback', {}, { 'HTTP_REMOTE_USER' => 'foobar', - 'HTTP_REMOTE_USER_DATA' => JSON.dump({'name' => 'foobar', 'email' => 'foobar@test.com'})} - } - - it 'Verify uid' do - expect(last_request.env['omniauth.auth']['uid']).to eq('foobar') - end - - it 'Verify info' do - expect(last_request.env['omniauth.auth']['info']['nickname']).to eq('foobar') - expect(last_request.env['omniauth.auth']['info']['email']).to eq('foobar@test.com') - end - end - - context 'Verify omniauth.auth info without REMOTE_USER_DATA' do - before(:each){ - clear_cookies - post '/auth/RemoteUser/callback', {}, { 'HTTP_REMOTE_USER' => 'foobar' } - } - - it 'Verify uid' do - expect(last_request.env['omniauth.auth']['uid']).to eq('foobar') - end - - it 'Verify info' do - expect(last_request.env['omniauth.auth']['info']).to eq({}) - end - end + let(:app) do + Rack::Builder.new do |b| + b.use Rack::Session::Cookie, :secret => 'abc123' + b.use OmniAuth::Strategies::RemoteUser + b.run lambda { |_env| [200, {}, ['My body']] } + end.to_app + end + + context 'Without REMOTE_USER and not logged in' do + before(:each){ + get '/', {}, {} + } + + it 'Do nothing' do + expect(last_response.status).to eq(200) + expect(last_request.cookies['_remote_user']).to eq(nil) + expect(last_request.cookies['_gitlab_session']).to eq(nil) + end + end + + context 'Without REMOTE_USER and logged in' do + before(:each){ + clear_cookies + set_cookie "_gitlab_session=test" + set_cookie "_remote_user=test" + get '/', {}, {} + } + + it 'Logout curreent user' do + cookie_session_str = "_gitlab_session=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 -0000" << + "\n_remote_user=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 -0000" + expect(last_request.cookies['_gitlab_session']).to eq('test') + expect(last_request.cookies['_remote_user']).to eq('test') + expect(last_response.status).to eq(302) + expect(last_response['Set-Cookie']).to eq(cookie_session_str) + end + end + + context 'With REMOTE_USER and not logged in' do + before(:each){ + get '/', {}, { 'HTTP_REMOTE_USER' => 'foobar' } + } + + it 'logs REMOTE_USER in' do + expect(last_response.status).to eq(302) + expect(last_response['Set-Cookie']).to eq('_remote_user=foobar') + end + end + + context 'With REMOTE_USER, logged in and current user equals REMOTE_USER' do + before(:each){ + clear_cookies + set_cookie "_gitlab_session=foobar" + set_cookie "_remote_user=foobar" + get '/', {}, { 'HTTP_REMOTE_USER' => 'foobar' } + } + + it 'Do nothing' do + expect(last_request.cookies['_gitlab_session']).to eq('foobar') + expect(last_request.cookies['_remote_user']).to eq('foobar') + expect(last_response.status).to eq(200) + expect(last_response['Set-Cookie']).to eq(nil) + end + end + + context 'With REMOTE_USER, logged in and current user not equals REMOTE_USER' do + before(:each){ + clear_cookies + set_cookie "_gitlab_session=foobar" + set_cookie "_remote_user=foobar" + get '/', {}, { 'HTTP_REMOTE_USER' => 'foobar2' } + } + + it 'Logout current user and login REMOTE_USER and no have _gitlab_session' do + expect(last_request.cookies['_gitlab_session']).to eq('foobar') + expect(last_request.cookies['_remote_user']).to eq('foobar') + expect(last_response.status).to eq(302) + expect(last_response['Set-Cookie']).to include('_gitlab_session=') + end + end + + context 'Verify omniauth hash with REMOTE_USER_DATA' do + before(:each){ + clear_cookies + post '/auth/RemoteUser/callback', {}, { 'HTTP_REMOTE_USER' => 'foobar', + 'HTTP_REMOTE_USER_DATA' => JSON.dump({'name' => 'foobar', 'email' => 'foobar@test.com'})} + } + + it 'Verify uid' do + expect(last_request.env['omniauth.auth']['uid']).to eq('foobar') + end + + it 'Verify info' do + expect(last_request.env['omniauth.auth']['info']['nickname']).to eq('foobar') + expect(last_request.env['omniauth.auth']['info']['email']).to eq('foobar@test.com') + end + end + + context 'Verify omniauth.auth info without REMOTE_USER_DATA' do + before(:each){ + clear_cookies + post '/auth/RemoteUser/callback', {}, { 'HTTP_REMOTE_USER' => 'foobar' } + } + + it 'Verify uid' do + expect(last_request.env['omniauth.auth']['uid']).to eq('foobar') + end + + it 'Verify info' do + expect(last_request.env['omniauth.auth']['info']).to eq({}) + end + end end -- libgit2 0.21.2