From faeb05e7f5823d7d390063e7463a0dd65ef8bed8 Mon Sep 17 00:00:00 2001 From: Macartur Sousa Date: Wed, 22 Jul 2015 11:56:14 -0300 Subject: [PATCH] Added redirect to last request before login --- config.ru | 14 +++++--------- lib/omniauth/strategies/remote_user.rb | 39 ++++++++++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/config.ru b/config.ru index 98035b4..1606dfb 100644 --- a/config.ru +++ b/config.ru @@ -1,18 +1,15 @@ require 'sinatra' require 'omniauth' require 'json' +require_relative 'lib/omniauth/strategies/remote_user' class MyApplication < Sinatra::Base - use Rack::Session::Cookie, secret: '123' - STRATEGY = 'RemoteUser' - #use OmniAuth::Strategies::RemoteUser - #STRATEGY = 'developer' - use OmniAuth::Strategies::Developer - + use Rack::Session::Cookie, secret: '123' + use OmniAuth::Strategies::RemoteUser get '/login' do - redirect '/gitlab/auth/%s' % STRATEGY + redirect '/auth/%s' % STRATEGY end get '/logout' do @@ -20,7 +17,7 @@ class MyApplication < Sinatra::Base redirect '/' end - post '/auth/:provider/callback' do + get '/auth/:provider/callback' do session[:current_user] = request.env['omniauth.auth']['uid'] session[:current_user_email] = request.env['omniauth.auth']['info']['email'] session[:current_user_nickname] = request.env['omniauth.auth']['info']['nickname'] @@ -40,4 +37,3 @@ class MyApplication < Sinatra::Base end run MyApplication - diff --git a/lib/omniauth/strategies/remote_user.rb b/lib/omniauth/strategies/remote_user.rb index 96a7014..faf4d66 100644 --- a/lib/omniauth/strategies/remote_user.rb +++ b/lib/omniauth/strategies/remote_user.rb @@ -3,17 +3,19 @@ module OmniAuth class RemoteUser include OmniAuth::Strategy - option :internal_cookie, '_remote_user' + option :remote_user_cookie, '_remote_user' + option :last_path_cookie, '_last_path' 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) + __return_last_path(env) || super(env) else __logout(env) end @@ -31,7 +33,32 @@ module OmniAuth def __current_user(env) request = Rack::Request.new(env) - request.cookies.has_key?(options.internal_cookie) && request.cookies[options.internal_cookie] + request.cookies.has_key?(options.remote_user_cookie) && request.cookies[options.remote_user_cookie] + end + + def __last_path(env) + request = Rack::Request.new(env) + request.cookies.has_key?(options.last_path_cookie) && request.cookies[options.last_path_cookie] + end + + def __set_last_path(env,response) + request = Rack::Request.new(env) + puts "---#{request.path_info}--#{__last_path(env)}" + if not __last_path(env) + response.set_cookie(options.last_path_cookie, {value: request.path_info , path: "#{request.script_name}", httponly: true}) + end + response + end + + def __return_last_path(env) + last_path = __last_path(env) + request = Rack::Request.new(env) + response = Rack::Response.new + if last_path + response.delete_cookie(options.last_path_cookie , path: "#{request.script_name}" ) + response.redirect last_path + response.finish + end end def __logout(env) @@ -39,7 +66,8 @@ module OmniAuth request.session.clear response = redirect_if_not_logging_in(request, request.path ) if response - response.delete_cookie(options.internal_cookie , path: "#{request.script_name}" ) + response.delete_cookie(options.remote_user_cookie , path: "#{request.script_name}" ) + response = __set_last_path(env,response) response.finish end end @@ -48,7 +76,8 @@ module OmniAuth request = Rack::Request.new(env) response = redirect_if_not_logging_in(request,_auth_path(request) ) if response - response.set_cookie(options.internal_cookie, {value: uid, path: "#{request.script_name}", httponly: true}) + response.set_cookie(options.remote_user_cookie, {value: uid, path: "#{request.script_name}", httponly: true}) + response = __set_last_path(env,response) response.finish end end -- libgit2 0.21.2