Commit faeb05e7f5823d7d390063e7463a0dd65ef8bed8
1 parent
d07e692f
Exists in
fix_last_path
Added redirect to last request before login
Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
Showing
2 changed files
with
39 additions
and
14 deletions
Show diff stats
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 < 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 < 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,32 @@ 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 __set_last_path(env,response) | |
| 45 | + request = Rack::Request.new(env) | |
| 46 | + puts "---#{request.path_info}--#{__last_path(env)}" | |
| 47 | + if not __last_path(env) | |
| 48 | + response.set_cookie(options.last_path_cookie, {value: request.path_info , path: "#{request.script_name}", httponly: true}) | |
| 49 | + end | |
| 50 | + response | |
| 51 | + end | |
| 52 | + | |
| 53 | + def __return_last_path(env) | |
| 54 | + last_path = __last_path(env) | |
| 55 | + request = Rack::Request.new(env) | |
| 56 | + response = Rack::Response.new | |
| 57 | + if last_path | |
| 58 | + response.delete_cookie(options.last_path_cookie , path: "#{request.script_name}" ) | |
| 59 | + response.redirect last_path | |
| 60 | + response.finish | |
| 61 | + end | |
| 35 | 62 | end |
| 36 | 63 | |
| 37 | 64 | def __logout(env) |
| ... | ... | @@ -39,7 +66,8 @@ module OmniAuth |
| 39 | 66 | request.session.clear |
| 40 | 67 | response = redirect_if_not_logging_in(request, request.path ) |
| 41 | 68 | if response |
| 42 | - response.delete_cookie(options.internal_cookie , path: "#{request.script_name}" ) | |
| 69 | + response.delete_cookie(options.remote_user_cookie , path: "#{request.script_name}" ) | |
| 70 | + response = __set_last_path(env,response) | |
| 43 | 71 | response.finish |
| 44 | 72 | end |
| 45 | 73 | end |
| ... | ... | @@ -48,7 +76,8 @@ module OmniAuth |
| 48 | 76 | request = Rack::Request.new(env) |
| 49 | 77 | response = redirect_if_not_logging_in(request,_auth_path(request) ) |
| 50 | 78 | if response |
| 51 | - response.set_cookie(options.internal_cookie, {value: uid, path: "#{request.script_name}", httponly: true}) | |
| 79 | + response.set_cookie(options.remote_user_cookie, {value: uid, path: "#{request.script_name}", httponly: true}) | |
| 80 | + response = __set_last_path(env,response) | |
| 52 | 81 | response.finish |
| 53 | 82 | end |
| 54 | 83 | end | ... | ... |