Commit 4b0971749ef5e92862b69f672ef68251f084fb9b
Exists in
master
and in
2 other branches
Merge branch 'gitlab_integration'
See !1
Showing
10 changed files
with
193 additions
and
265 deletions
Show diff stats
Gemfile
1 | 1 | source "http://rubygems.org" |
2 | 2 | |
3 | -group :development, :test do | |
4 | - gem 'guard' | |
5 | - gem 'guard-rspec' | |
6 | - gem 'guard-bundler' | |
7 | - gem 'rb-fsevent' | |
8 | - gem 'simplecov' | |
9 | - gem 'rspec' | |
10 | - gem 'rake' | |
11 | - gem 'coveralls' | |
12 | - gem 'rack-test' | |
3 | +gemspec | |
4 | + | |
5 | +group :development do | |
6 | + gem 'guard' | |
7 | + gem 'guard-bundler' | |
8 | + gem 'guard-rspec' | |
9 | + gem 'rake' | |
10 | + gem 'bundler' | |
11 | + gem 'sinatra' | |
13 | 12 | end |
14 | 13 | |
15 | -gemspec | |
14 | +group :test do | |
15 | + gem 'coveralls' | |
16 | + gem 'rack-test' | |
17 | + gem 'simplecov' | |
18 | + gem 'rspec' | |
19 | +end | ... | ... |
README.md
1 | 1 | # Omniath Remote User |
2 | 2 | |
3 | 3 | The Omniauth Remote User gem provides a way for application to utilize a authentication with remote user HTTP header. |
4 | + | |
5 | +# Instalation | |
6 | + | |
7 | +Include in your Gemfile | |
8 | + | |
9 | +```ruby | |
10 | +gem "omniauth-remote-user" | |
11 | +``` | |
12 | +Then run `bundle install` from the command line: | |
13 | + | |
14 | + bundle install | ... | ... |
Rakefile
config.ru
... | ... | @@ -2,109 +2,17 @@ require 'sinatra' |
2 | 2 | require 'omniauth' |
3 | 3 | require 'json' |
4 | 4 | |
5 | -module OmniAuth | |
6 | - module Strategies | |
7 | - class RemoteUser | |
8 | - | |
9 | - include OmniAuth::Strategy | |
10 | - | |
11 | - option :cookie, 'rack.session' | |
12 | - option :internal_cookie, '_remote_user' | |
13 | - | |
14 | - def call(env) | |
15 | - remote_user = env['HTTP_REMOTE_USER'] | |
16 | - $stderr.puts('Remote-User: %s' % (remote_user || '(none')) | |
17 | - session_user = __current_user(env) | |
18 | - if remote_user | |
19 | - if session_user | |
20 | - if remote_user == session_user | |
21 | - super(env) | |
22 | - else | |
23 | - __login(env, remote_user) || super(env) | |
24 | - end | |
25 | - else | |
26 | - __login(env, remote_user) || super(env) | |
27 | - end | |
28 | - else | |
29 | - if session_user | |
30 | - __logout(env) || super(env) | |
31 | - else | |
32 | - super(env) | |
33 | - end | |
34 | - end | |
35 | - end | |
36 | - | |
37 | - def __current_user(env) | |
38 | - request = Rack::Request.new(env) | |
39 | - request.cookies.has_key?(options.internal_cookie) && request.cookies[options.internal_cookie] | |
40 | - end | |
41 | - | |
42 | - def __logout(env) | |
43 | - $stderr.puts 'LOGOUT' | |
44 | - request = Rack::Request.new(env) | |
45 | - response = redirect_if_not_logging_in(request, request.path) | |
46 | - if response | |
47 | - response.delete_cookie(options.cookie) | |
48 | - response.delete_cookie(options.internal_cookie) | |
49 | - response | |
50 | - end | |
51 | - end | |
52 | - | |
53 | - def __login(env, uid) | |
54 | - $stderr.puts 'LOGIN (%s)' % uid | |
55 | - request = Rack::Request.new(env) | |
56 | - response = redirect_if_not_logging_in(request, '/auth/remoteuser') | |
57 | - if response | |
58 | - response.set_cookie(options.internal_cookie, uid) | |
59 | - response | |
60 | - end | |
61 | - end | |
62 | - | |
63 | - def redirect_if_not_logging_in(request, url) | |
64 | - if ! [ | |
65 | - '/auth/remoteuser', | |
66 | - '/auth/remoteuser/callback' | |
67 | - ].include?(request.path_info) | |
68 | - response = Rack::Response.new | |
69 | - response.redirect url | |
70 | - response | |
71 | - end | |
72 | - end | |
73 | - | |
74 | - uid do | |
75 | - request.env['HTTP_REMOTE_USER'] | |
76 | - end | |
77 | - | |
78 | - info do | |
79 | - user_data = request.env['HTTP_REMOTE_USER_DATA'] | |
80 | - if user_data | |
81 | - data = JSON.parse(user_data) | |
82 | - data['nickname'] = data['name'] | |
83 | - data | |
84 | - else | |
85 | - {} | |
86 | - end | |
87 | - end | |
88 | - | |
89 | - def request_phase | |
90 | - form = OmniAuth::Form.new(:url => callback_path) | |
91 | - form.html '<script type="text/javascript"> document.forms[0].submit(); </script>' | |
92 | - form.to_response | |
93 | - end | |
94 | - end | |
95 | - end | |
96 | -end | |
97 | - | |
98 | 5 | class MyApplication < Sinatra::Base |
99 | 6 | use Rack::Session::Cookie, secret: '123' |
100 | 7 | |
101 | - STRATEGY = 'remoteuser' | |
102 | - use OmniAuth::Strategies::RemoteUser | |
8 | + STRATEGY = 'RemoteUser' | |
9 | + #use OmniAuth::Strategies::RemoteUser | |
103 | 10 | #STRATEGY = 'developer' |
104 | - #use OmniAuth::Strategies::Developer | |
11 | + use OmniAuth::Strategies::Developer | |
12 | + | |
105 | 13 | |
106 | 14 | get '/login' do |
107 | - redirect '/auth/%s' % STRATEGY | |
15 | + redirect '/gitlab/auth/%s' % STRATEGY | |
108 | 16 | end |
109 | 17 | |
110 | 18 | get '/logout' do | ... | ... |
lib/omniauth-remote-user/version.rb
lib/omniauth/remote-user.rb
lib/omniauth/strategies/remote_user.rb
1 | 1 | module OmniAuth |
2 | 2 | module Strategies |
3 | 3 | class RemoteUser |
4 | - | |
5 | 4 | include OmniAuth::Strategy |
6 | 5 | |
7 | - #option :cookie, 'rack.session' | |
8 | - option :cookie, '_gitlab_session' | |
9 | 6 | option :internal_cookie, '_remote_user' |
10 | 7 | |
11 | 8 | def call(env) |
9 | + | |
12 | 10 | remote_user = env['HTTP_REMOTE_USER'] |
13 | 11 | session_user = __current_user(env) |
12 | + | |
14 | 13 | if remote_user |
15 | 14 | if session_user |
16 | 15 | if remote_user == session_user |
17 | 16 | super(env) |
18 | 17 | else |
19 | - __login(env, remote_user) || super(env) | |
18 | + __logout(env) | |
20 | 19 | end |
21 | 20 | else |
22 | - __login(env, remote_user) || super(env) | |
21 | + __login(env, remote_user) | |
23 | 22 | end |
24 | 23 | else |
25 | 24 | if session_user |
26 | - __logout(env) || super(env) | |
25 | + __logout(env) | |
27 | 26 | else |
28 | 27 | super(env) |
29 | 28 | end |
... | ... | @@ -37,34 +36,35 @@ module OmniAuth |
37 | 36 | |
38 | 37 | def __logout(env) |
39 | 38 | request = Rack::Request.new(env) |
40 | - response = redirect_if_not_logging_in(request, request.path) | |
39 | + request.session.clear | |
40 | + response = redirect_if_not_logging_in(request, request.path ) | |
41 | 41 | if response |
42 | - response.delete_cookie(options.cookie) | |
43 | - response.delete_cookie(options.internal_cookie) | |
44 | - response | |
42 | + response.delete_cookie(options.internal_cookie , path: "#{request.script_name}" ) | |
43 | + response.finish | |
45 | 44 | end |
46 | 45 | end |
47 | 46 | |
48 | 47 | def __login(env, uid) |
49 | 48 | request = Rack::Request.new(env) |
50 | - response = redirect_if_not_logging_in(request, '/auth/remoteuser') | |
49 | + response = redirect_if_not_logging_in(request,_auth_path(request) ) | |
51 | 50 | if response |
52 | - response.set_cookie(options.internal_cookie, uid) | |
53 | - response | |
51 | + response.set_cookie(options.internal_cookie, {value: uid , path: "#{request.script_name}"}) | |
52 | + response.finish | |
54 | 53 | end |
55 | 54 | end |
56 | 55 | |
57 | 56 | def redirect_if_not_logging_in(request, url) |
58 | 57 | if ! [ |
59 | - '/auth/remoteuser', | |
60 | - '/auth/remoteuser/callback' | |
61 | - ].include?(request.path_info) | |
58 | + _auth_path(request), | |
59 | + _callback_path(request) | |
60 | + ].include?(request.path_info) | |
62 | 61 | response = Rack::Response.new |
63 | 62 | response.redirect url |
64 | 63 | response |
65 | 64 | end |
66 | 65 | end |
67 | 66 | |
67 | + | |
68 | 68 | uid do |
69 | 69 | request.env['HTTP_REMOTE_USER'] |
70 | 70 | end |
... | ... | @@ -73,7 +73,9 @@ module OmniAuth |
73 | 73 | user_data = request.env['HTTP_REMOTE_USER_DATA'] |
74 | 74 | if user_data |
75 | 75 | data = JSON.parse(user_data) |
76 | - data['nickname'] = data['name'] | |
76 | + data['nickname'] = uid | |
77 | + data['firstname'] = data['name'].split()[0] | |
78 | + data['lastname'] = data['name'].split()[1] | |
77 | 79 | data |
78 | 80 | else |
79 | 81 | {} |
... | ... | @@ -81,11 +83,17 @@ module OmniAuth |
81 | 83 | end |
82 | 84 | |
83 | 85 | def request_phase |
84 | - form = OmniAuth::Form.new(:url => callback_path) | |
85 | - form.html '<script type="text/javascript"> document.forms[0].submit(); </script>' | |
86 | - form.to_response | |
86 | + redirect _callback_path(request) | |
87 | 87 | end |
88 | + | |
89 | + def _callback_path(request) | |
90 | + "#{_auth_path(request)}/callback" | |
91 | + end | |
92 | + | |
93 | + def _auth_path(request) | |
94 | + "#{request.script_name}#{path_prefix}/RemoteUser" | |
95 | + end | |
96 | + | |
88 | 97 | end |
89 | 98 | end |
90 | 99 | end |
91 | - | ... | ... |
omniauth-remote-user.gemspec
1 | 1 | require File.dirname(__FILE__) + '/lib/omniauth-remote-user/version' |
2 | 2 | |
3 | 3 | Gem::Specification.new do |gem| |
4 | - gem.add_runtime_dependency 'omniauth' | |
5 | - | |
6 | - gem.add_runtime_dependency 'simplecov' | |
7 | - gem.add_runtime_dependency 'bundler' | |
8 | - gem.add_runtime_dependency 'rake' | |
9 | - gem.add_runtime_dependency 'rspec' | |
10 | - gem.add_runtime_dependency 'rack-test' | |
11 | - gem.add_runtime_dependency 'activerecord' | |
12 | - | |
4 | + gem.add_runtime_dependency 'omniauth', '~> 1.0' | |
13 | 5 | gem.name = 'omniauth-remote-user' |
14 | 6 | gem.version = Omniauth::RemoteUser::VERSION |
15 | - gem.description = %q{Authentication with Remote-User HTTP header for Omniauth.} | |
16 | - gem.summary = gem.description | |
17 | - gem.email = ['kanashiro.duarte@gmail.com', 'thiagitosouza@gmail.com', 'rodrigosiqueiramelo@gmail.com'] | |
7 | + gem.description = 'Authentication with Remote-User HTTP header for Omniauth.' | |
8 | + gem.summary = 'Authentication with HTTP Remote User' | |
9 | + gem.email = ['kanashiro.duarte@gmail.com', 'thiagitosouza@gmail.com', 'rodrigosiqueiramelo@gmail.com','macartur.sc@gmail.com','terceiro@softwarelivre.org'] | |
18 | 10 | gem.homepage = 'http://beta.softwarepublico.gov.br/gitlab/softwarepublico/omiauth-remote-user' |
19 | - gem.authors = ['Lucas Kanashiro', 'Thiago Ribeiro', 'Rodrigo Siqueira'] | |
11 | + gem.authors = ['Lucas Kanashiro', 'Thiago Ribeiro', 'Rodrigo Siqueira','Macartur Sousa', 'Antonio Terceiro'] | |
20 | 12 | gem.require_paths = %w(lib) |
21 | 13 | gem.files = `git ls-files -z`.split("\x0").reject {|f| f.start_with?('spec/')} |
22 | 14 | gem.test_files = `git ls-files -- {test,spec,feature}/*`.split("\n") | ... | ... |
spec/omniauth/strategies/remote_user_spec.rb
1 | 1 | require 'spec_helper' |
2 | 2 | |
3 | 3 | describe 'Test Strategy Remote_User' do |
4 | - let(:app) do | |
5 | - Rack::Builder.new do |b| | |
6 | - b.use Rack::Session::Cookie, :secret => 'abc123' | |
7 | - b.use OmniAuth::Strategies::RemoteUser | |
8 | - b.run lambda { |_env| [200, {}, ['My body']] } | |
9 | - end.to_app | |
10 | - end | |
11 | - | |
12 | - context 'Without REMOTE_USER and not logged in' do | |
13 | - before(:each){ | |
14 | - get '/', {}, {} | |
15 | - } | |
16 | - | |
17 | - it 'Do nothing' do | |
18 | - expect(last_response.status).to eq(200) | |
19 | - expect(last_request.cookies['_remote_user']).to eq(nil) | |
20 | - expect(last_request.cookies['_gitlab_session']).to eq(nil) | |
21 | - end | |
22 | - end | |
23 | - | |
24 | - context 'Without REMOTE_USER and logged in' do | |
25 | - before(:each){ | |
26 | - clear_cookies | |
27 | - set_cookie "_gitlab_session=test" | |
28 | - set_cookie "_remote_user=test" | |
29 | - get '/', {}, {} | |
30 | - } | |
31 | - | |
32 | - it 'Logout curreent user' do | |
33 | - cookie_session_str = "_gitlab_session=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 -0000" << | |
34 | - "\n_remote_user=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 -0000" | |
35 | - expect(last_request.cookies['_gitlab_session']).to eq('test') | |
36 | - expect(last_request.cookies['_remote_user']).to eq('test') | |
37 | - expect(last_response.status).to eq(302) | |
38 | - expect(last_response['Set-Cookie']).to eq(cookie_session_str) | |
39 | - end | |
40 | - end | |
41 | - | |
42 | - context 'With REMOTE_USER and not logged in' do | |
43 | - before(:each){ | |
44 | - get '/', {}, { 'HTTP_REMOTE_USER' => 'foobar' } | |
45 | - } | |
46 | - | |
47 | - it 'logs REMOTE_USER in' do | |
48 | - expect(last_response.status).to eq(302) | |
49 | - expect(last_response['Set-Cookie']).to eq('_remote_user=foobar') | |
50 | - end | |
51 | - end | |
52 | - | |
53 | - context 'With REMOTE_USER, logged in and current user equals REMOTE_USER' do | |
54 | - before(:each){ | |
55 | - clear_cookies | |
56 | - set_cookie "_gitlab_session=foobar" | |
57 | - set_cookie "_remote_user=foobar" | |
58 | - get '/', {}, { 'HTTP_REMOTE_USER' => 'foobar' } | |
59 | - } | |
60 | - | |
61 | - it 'Do nothing' do | |
62 | - cookie_session_str = "_gitlab_session=foobar\n_remote_user=foobar" | |
63 | - expect(last_request.cookies['_gitlab_session']).to eq('foobar') | |
64 | - expect(last_request.cookies['_remote_user']).to eq('foobar') | |
65 | - expect(last_response.status).to eq(200) | |
66 | - expect(last_response['Set-Cookie']).to eq(nil) | |
67 | - end | |
68 | - end | |
69 | - | |
70 | - context 'With REMOTE_USER, logged in and current user not equals REMOTE_USER' do | |
71 | - before(:each){ | |
72 | - clear_cookies | |
73 | - set_cookie "_gitlab_session=foobar" | |
74 | - set_cookie "_remote_user=foobar" | |
75 | - get '/', {}, { 'HTTP_REMOTE_USER' => 'foobar2' } | |
76 | - } | |
77 | - | |
78 | - it 'Logout current user and login REMOTE_USER' do | |
79 | - expect(last_request.cookies['_gitlab_session']).to eq('foobar') | |
80 | - expect(last_request.cookies['_remote_user']).to eq('foobar') | |
81 | - expect(last_response.status).to eq(302) | |
82 | - expect(last_response['Set-Cookie']).to eq('_remote_user=foobar2') | |
83 | - end | |
84 | - end | |
85 | - | |
86 | - context 'Verify omniauth hash with REMOTE_USER_DATA' do | |
87 | - before(:each){ | |
88 | - clear_cookies | |
89 | - post '/auth/remoteuser/callback', {}, { 'HTTP_REMOTE_USER' => 'foobar', | |
90 | - 'HTTP_REMOTE_USER_DATA' => JSON.dump({'name' => 'foobar', 'email' => 'foobar@test.com'})} | |
91 | - } | |
92 | - | |
93 | - it 'Verify uid' do | |
94 | - expect(last_request.env['omniauth.auth']['uid']).to eq('foobar') | |
95 | - end | |
96 | - | |
97 | - it 'Verify info' do | |
98 | - expect(last_request.env['omniauth.auth']['info']['nickname']).to eq('foobar') | |
99 | - expect(last_request.env['omniauth.auth']['info']['email']).to eq('foobar@test.com') | |
100 | - end | |
101 | - end | |
102 | - | |
103 | - context 'Verify omniauth.auth info without REMOTE_USER_DATA' do | |
104 | - before(:each){ | |
105 | - clear_cookies | |
106 | - post '/auth/remoteuser/callback', {}, { 'HTTP_REMOTE_USER' => 'foobar' } | |
107 | - } | |
108 | - | |
109 | - it 'Verify uid' do | |
110 | - expect(last_request.env['omniauth.auth']['uid']).to eq('foobar') | |
111 | - end | |
112 | - | |
113 | - it 'Verify info' do | |
114 | - expect(last_request.env['omniauth.auth']['info']).to eq({}) | |
115 | - end | |
116 | - end | |
117 | -end | |
4 | + let(:app) do | |
5 | + Rack::Builder.new do |b| | |
6 | + b.use Rack::Session::Cookie, :secret => 'abc123' | |
7 | + b.use OmniAuth::Strategies::RemoteUser | |
8 | + b.run lambda { |_env| [200, {}, ['My body']] } | |
9 | + end.to_app | |
10 | + end | |
11 | + | |
12 | + context 'Without HTTP_REMOTE_USER and not logged in' do | |
13 | + before(:each){ | |
14 | + get '/', {}, {} | |
15 | + } | |
16 | + | |
17 | + it 'Do nothing' do | |
18 | + expect(last_response.status).to eq(200) | |
19 | + expect(last_request.cookies['_remote_user']).to eq(nil) | |
20 | + end | |
21 | + end | |
22 | + | |
23 | + context 'Without HTTP_REMOTE_USER and logged in' do | |
24 | + before(:each){ | |
25 | + clear_cookies | |
26 | + set_cookie "_remote_user=test" | |
27 | + get '/', {}, {} | |
28 | + } | |
29 | + | |
30 | + it 'Logout curreent user' do | |
31 | + expect(last_request.cookies['_remote_user']).to eq('test') | |
32 | + expect(last_response.status).to eq(302) | |
33 | + expect(last_response['Set-Cookie']).to include("_remote_user=") | |
34 | + expect(last_response['Set-Cookie']).to include("path=") | |
35 | + end | |
36 | + end | |
37 | + | |
38 | + context 'With HTTP_REMOTE_USER and not logged in' do | |
39 | + before(:each){ | |
40 | + get '/', {}, { 'HTTP_REMOTE_USER' => 'foobar' } | |
41 | + } | |
42 | + | |
43 | + it 'logs HTTP_REMOTE_USER in' do | |
44 | + expect(last_response.status).to eq(302) | |
45 | + expect(last_response['Set-Cookie']).to include('_remote_user=foobar') | |
46 | + expect(last_response['Set-Cookie']).to include('path=') | |
47 | + end | |
48 | + end | |
49 | + | |
50 | + context 'With HTTP_REMOTE_USER, logged in and current user equals HTTP_REMOTE_USER' do | |
51 | + before(:each){ | |
52 | + clear_cookies | |
53 | + set_cookie "_remote_user=foobar" | |
54 | + get '/', {}, { 'HTTP_REMOTE_USER' => 'foobar' } | |
55 | + } | |
56 | + | |
57 | + it 'Do nothing' do | |
58 | + expect(last_request.cookies['_remote_user']).to eq('foobar') | |
59 | + expect(last_response.status).to eq(200) | |
60 | + expect(last_response['Set-Cookie']).to eq(nil) | |
61 | + end | |
62 | + end | |
63 | + | |
64 | + context 'With HTTP_REMOTE_USER, logged in and current user not equals HTTP_REMOTE_USER' do | |
65 | + before(:each){ | |
66 | + clear_cookies | |
67 | + set_cookie "_remote_user=foobar" | |
68 | + get '/', {}, { 'HTTP_REMOTE_USER' => 'foobar2' } | |
69 | + } | |
70 | + | |
71 | + it 'Logout current user and login HTTP_REMOTE_USER' do | |
72 | + expect(last_request.cookies['_remote_user']).to eq('foobar') | |
73 | + expect(last_response.status).to eq(302) | |
74 | + end | |
75 | + end | |
76 | + | |
77 | + context 'Verify omniauth hash with HTTP_REMOTE_USER_DATA' do | |
78 | + before(:each){ | |
79 | + clear_cookies | |
80 | + set_cookie "_remote_user=foobar" | |
81 | + post '/auth/RemoteUser/callback', {}, { 'HTTP_REMOTE_USER' => 'foobar', | |
82 | + 'HTTP_REMOTE_USER_DATA' => JSON.dump({'name' => 'foobar barfoo', 'email' => 'foobar@test.com'})} | |
83 | + } | |
84 | + | |
85 | + it 'Verify uid' do | |
86 | + expect(last_request.env['omniauth.auth']['uid']).to eq('foobar') | |
87 | + end | |
88 | + | |
89 | + it 'Verify info' do | |
90 | + expect(last_request.env['omniauth.auth']['info']['nickname']).to eq('foobar') | |
91 | + expect(last_request.env['omniauth.auth']['info']['email']).to eq('foobar@test.com') | |
92 | + expect(last_request.env['omniauth.auth']['info']['lastname']).to eq('barfoo') | |
93 | + expect(last_request.env['omniauth.auth']['info']['firstname']).to eq('foobar') | |
94 | + end | |
95 | + end | |
96 | + | |
97 | + context 'Verify omniauth.auth info without HTTP_REMOTE_USER_DATA' do | |
98 | + before(:each){ | |
99 | + clear_cookies | |
100 | + set_cookie "_remote_user=foobar" | |
101 | + post '/auth/RemoteUser/callback', {}, { 'HTTP_REMOTE_USER' => 'foobar' } | |
102 | + } | |
103 | + | |
104 | + it 'Verify uid' do | |
105 | + expect(last_request.env['omniauth.auth']['uid']).to eq('foobar') | |
106 | + end | |
107 | + | |
108 | + it 'Verify info' do | |
109 | + expect(last_request.env['omniauth.auth']['info']).to eq({}) | |
110 | + end | |
111 | + end | |
112 | + | |
113 | + context 'With HTTP_REMOTE_USER and ' do | |
114 | + before(:each){ | |
115 | + set_cookie "_remote_user=foobar" | |
116 | + get "auth/RemoteUser", {}, { 'HTTP_REMOTE_USER' => 'foobar' } | |
117 | + } | |
118 | + | |
119 | + it 'redirect for callback' do | |
120 | + expect(last_response.status).to eq(302) | |
121 | + expect(last_response.location).to eq("/auth/RemoteUser/callback") | |
122 | + end | |
123 | + end | |
118 | 124 | |
125 | +end | ... | ... |
spec/spec_helper.rb