Commit faeb05e7f5823d7d390063e7463a0dd65ef8bed8

Authored by Macartur Sousa
1 parent d07e692f
Exists in fix_last_path

Added redirect to last request before login

Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
1 require 'sinatra' 1 require 'sinatra'
2 require 'omniauth' 2 require 'omniauth'
3 require 'json' 3 require 'json'
  4 +require_relative 'lib/omniauth/strategies/remote_user'
4 5
5 class MyApplication < Sinatra::Base 6 class MyApplication < Sinatra::Base
6 - use Rack::Session::Cookie, secret: '123'  
7 -  
8 STRATEGY = 'RemoteUser' 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 get '/login' do 11 get '/login' do
15 - redirect '/gitlab/auth/%s' % STRATEGY 12 + redirect '/auth/%s' % STRATEGY
16 end 13 end
17 14
18 get '/logout' do 15 get '/logout' do
@@ -20,7 +17,7 @@ class MyApplication &lt; Sinatra::Base @@ -20,7 +17,7 @@ class MyApplication &lt; Sinatra::Base
20 redirect '/' 17 redirect '/'
21 end 18 end
22 19
23 - post '/auth/:provider/callback' do 20 + get '/auth/:provider/callback' do
24 session[:current_user] = request.env['omniauth.auth']['uid'] 21 session[:current_user] = request.env['omniauth.auth']['uid']
25 session[:current_user_email] = request.env['omniauth.auth']['info']['email'] 22 session[:current_user_email] = request.env['omniauth.auth']['info']['email']
26 session[:current_user_nickname] = request.env['omniauth.auth']['info']['nickname'] 23 session[:current_user_nickname] = request.env['omniauth.auth']['info']['nickname']
@@ -40,4 +37,3 @@ class MyApplication &lt; Sinatra::Base @@ -40,4 +37,3 @@ class MyApplication &lt; Sinatra::Base
40 end 37 end
41 38
42 run MyApplication 39 run MyApplication
43 -  
lib/omniauth/strategies/remote_user.rb
@@ -3,17 +3,19 @@ module OmniAuth @@ -3,17 +3,19 @@ module OmniAuth
3 class RemoteUser 3 class RemoteUser
4 include OmniAuth::Strategy 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 def call(env) 9 def call(env)
9 10
10 remote_user = env['HTTP_REMOTE_USER'] 11 remote_user = env['HTTP_REMOTE_USER']
  12 +
11 session_user = __current_user(env) 13 session_user = __current_user(env)
12 14
13 if remote_user 15 if remote_user
14 if session_user 16 if session_user
15 if remote_user == session_user 17 if remote_user == session_user
16 - super(env) 18 + __return_last_path(env) || super(env)
17 else 19 else
18 __logout(env) 20 __logout(env)
19 end 21 end
@@ -31,7 +33,32 @@ module OmniAuth @@ -31,7 +33,32 @@ module OmniAuth
31 33
32 def __current_user(env) 34 def __current_user(env)
33 request = Rack::Request.new(env) 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 end 62 end
36 63
37 def __logout(env) 64 def __logout(env)
@@ -39,7 +66,8 @@ module OmniAuth @@ -39,7 +66,8 @@ module OmniAuth
39 request.session.clear 66 request.session.clear
40 response = redirect_if_not_logging_in(request, request.path ) 67 response = redirect_if_not_logging_in(request, request.path )
41 if response 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 response.finish 71 response.finish
44 end 72 end
45 end 73 end
@@ -48,7 +76,8 @@ module OmniAuth @@ -48,7 +76,8 @@ module OmniAuth
48 request = Rack::Request.new(env) 76 request = Rack::Request.new(env)
49 response = redirect_if_not_logging_in(request,_auth_path(request) ) 77 response = redirect_if_not_logging_in(request,_auth_path(request) )
50 if response 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 response.finish 81 response.finish
53 end 82 end
54 end 83 end