Merge Request #6

Open
softwarepublico/omniauth-remote-user!6
Created by Macartur Sousa

Fix last path

Added currect redirect using request path before login.

Assignee: Antonio Terceiro
Milestone: None
This can't be merged automatically, even if it could be merged you don't have the permission to do so.
This can be merged automatically but you don't have the permission to do so.
Commits (5)
2 participants
config.ru
1 1 require 'sinatra'
2 2 require 'omniauth'
3 3 require 'json'
  4 +require_relative 'lib/omniauth/strategies/remote_user'
4 5  
5 6 class MyApplication < Sinatra::Base
6   - use Rack::Session::Cookie, secret: '123'
7   -
8 7 STRATEGY = 'RemoteUser'
9   - #use OmniAuth::Strategies::RemoteUser
10   - #STRATEGY = 'developer'
11   - use OmniAuth::Strategies::Developer
12   -
  8 + use Rack::Session::Cookie, secret: '123'
  9 + use OmniAuth::Strategies::RemoteUser
13 10  
14 11 get '/login' do
15   - redirect '/gitlab/auth/%s' % STRATEGY
  12 + redirect '/auth/%s' % STRATEGY
16 13 end
17 14  
18 15 get '/logout' do
... ... @@ -20,7 +17,7 @@ class MyApplication &lt; Sinatra::Base
20 17 redirect '/'
21 18 end
22 19  
23   - post '/auth/:provider/callback' do
  20 + get '/auth/:provider/callback' do
24 21 session[:current_user] = request.env['omniauth.auth']['uid']
25 22 session[:current_user_email] = request.env['omniauth.auth']['info']['email']
26 23 session[:current_user_nickname] = request.env['omniauth.auth']['info']['nickname']
... ... @@ -40,4 +37,3 @@ class MyApplication &lt; Sinatra::Base
40 37 end
41 38  
42 39 run MyApplication
43   -
... ...
lib/omniauth/strategies/remote_user.rb
... ... @@ -3,17 +3,19 @@ module OmniAuth
3 3 class RemoteUser
4 4 include OmniAuth::Strategy
5 5  
6   - option :internal_cookie, '_remote_user'
  6 + option :remote_user_cookie, '_remote_user'
  7 + option :last_path_cookie, '_last_path'
7 8  
8 9 def call(env)
9 10  
10 11 remote_user = env['HTTP_REMOTE_USER']
  12 +
11 13 session_user = __current_user(env)
12 14  
13 15 if remote_user
14 16 if session_user
15 17 if remote_user == session_user
16   - super(env)
  18 + __return_last_path(env) || super(env)
17 19 else
18 20 __logout(env)
19 21 end
... ... @@ -31,7 +33,33 @@ module OmniAuth
31 33  
32 34 def __current_user(env)
33 35 request = Rack::Request.new(env)
34   - request.cookies.has_key?(options.internal_cookie) && request.cookies[options.internal_cookie]
  36 + request.cookies.has_key?(options.remote_user_cookie) && request.cookies[options.remote_user_cookie]
  37 + end
  38 +
  39 + def __last_path(env)
  40 + request = Rack::Request.new(env)
  41 + request.cookies.has_key?(options.last_path_cookie) && request.cookies[options.last_path_cookie]
  42 + end
  43 +
  44 + def __request_path(env)
  45 + env['REQUEST_PATH']
  46 + end
  47 +
  48 + def __set_last_path(env,response)
  49 + request = Rack::Request.new(env)
  50 + if not __last_path(env)
  51 + response.set_cookie(options.last_path_cookie, {value: __request_path(env) , path: "#{request.script_name}"})
  52 + end
  53 + end
  54 +
  55 + def __return_last_path(env)
  56 + request = Rack::Request.new(env)
  57 + if ! [_auth_path(request),_callback_path(request)].include?(__request_path(env)) && __last_path(env)
  58 + response = Rack::Response.new
  59 + response.redirect __last_path(env)
  60 + response.delete_cookie(options.last_path_cookie , path: "#{request.script_name}" )
  61 + response.finish
  62 + end
35 63 end
36 64  
37 65 def __logout(env)
... ... @@ -39,7 +67,8 @@ module OmniAuth
39 67 request.session.clear
40 68 response = redirect_if_not_logging_in(request, request.path )
41 69 if response
42   - response.delete_cookie(options.internal_cookie , path: "#{request.script_name}" )
  70 + response.delete_cookie(options.remote_user_cookie , path: "#{request.script_name}" )
  71 + __set_last_path(env,response)
43 72 response.finish
44 73 end
45 74 end
... ... @@ -48,7 +77,8 @@ module OmniAuth
48 77 request = Rack::Request.new(env)
49 78 response = redirect_if_not_logging_in(request,_auth_path(request) )
50 79 if response
51   - response.set_cookie(options.internal_cookie, {value: uid, path: "#{request.script_name}", httponly: true})
  80 + response.set_cookie(options.remote_user_cookie, {value: uid, path: "#{request.script_name}", httponly: true})
  81 + __set_last_path(env,response)
52 82 response.finish
53 83 end
54 84 end
... ...
spec/omniauth/strategies/remote_user_spec.rb
... ... @@ -122,4 +122,17 @@ describe &#39;Test Strategy Remote_User&#39; do
122 122 end
123 123 end
124 124  
  125 + context 'Redirect After login in' do
  126 + before(:each){
  127 + set_cookie '_remote_user=foobar'
  128 + set_cookie '_last_path=/dashboard'
  129 + get '/', {}, { 'HTTP_REMOTE_USER' => 'foobar' }
  130 + }
  131 +
  132 + it 'redirect to last path before login' do
  133 + expect(last_response.status).to eq(302)
  134 + expect(last_response.location).to eq('/dashboard')
  135 + end
  136 + end
  137 +
125 138 end
... ...
test_notes.txt
1 1 Configurando apache para setar header Remote-User:
2 2  
  3 +--- USING APACHE2 ---
  4 +
3 5 1 - Instalar apache2
4 6 2 - Criar arquivo de configuração (ex: proxy.conf) em /etc/apache2/sites-available
5 7  
... ... @@ -25,3 +27,28 @@ Executando aplicação sinatra:
25 27  
26 28 1 - Entrar no diretório que contem o arquivo conf.ru
27 29 2 - Executar aplicação (rackup)
  30 +
  31 +
  32 +-- USING NGINX --
  33 +
  34 +sudo apt-get install nginx
  35 +
  36 +editar um arquivo de configuração em /etc/nginx/sites-available/proxy.conf
  37 + e criar um link para /etc/nginx/sites-enable/proxy.conf com o conteudo:
  38 +
  39 +server {
  40 + server_name 127.0.0.1;
  41 + listen 80;
  42 + location / {
  43 + proxy_pass http://127.0.0.1:9292;
  44 + proxy_set_header Host $http_host;
  45 + proxy_set_header REMOTE_USER "<usuário>";
  46 + }
  47 +}
  48 +
  49 +Executando aplicação sinatra:
  50 +
  51 +1 - Entrar no diretório que contem o arquivo conf.ru
  52 +2 - Executar aplicação (rackup)
  53 +
  54 +------------------
... ...