remote_user.rb
2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
module OmniAuth
module Strategies
class RemoteUser
include OmniAuth::Strategy
#option :cookie, 'rack.session'
option :cookie, '_gitlab_session'
option :internal_cookie, '_remote_user'
def __write_file message
file = File.open("/home/git/gitlab/log/remote_user.log",'a')
file.write message
file.close
end
def call(env)
__write_file "Call \n"
remote_user = env['HTTP_REMOTE_USER']
__write_file "#{remote_user}\n"
session_user = __current_user(env)
if remote_user
if session_user
if remote_user == session_user
super(env)
else
__login(env, remote_user) || super(env)
end
else
__login(env, remote_user) || super(env)
end
else
if session_user
__logout(env) || super(env)
else
super(env)
end
end
end
def __current_user(env)
__write_file "__CURRENT_USER"
request = Rack::Request.new(env)
request.cookies.has_key?(options.internal_cookie) && request.cookies[options.internal_cookie]
end
def __logout(env)
__write_file "__LOGOUT"
request = Rack::Request.new(env)
response = redirect_if_not_logging_in(request, request.path)
if response
response.delete_cookie(options.cookie)
response.delete_cookie(options.internal_cookie)
response
end
end
def __login(env, uid)
__write_file "__LOGIN"
request = Rack::Request.new(env)
response = redirect_if_not_logging_in(request, '/users/auth/RemoteUser')
if response
response.set_cookie(options.internal_cookie, uid)
response
end
end
def redirect_if_not_logging_in(request, url)
puts "__redirect_if_not_loggin_in"
if ! [
'/users/auth/RemoteUser',
'/users/auth/RemoteUser/callback'
].include?(request.path_info)
response = Rack::Response.new
response.redirect url
response
end
end
uid do
request.env['HTTP_REMOTE_USER']
end
info do
user_data = request.env['HTTP_REMOTE_USER_DATA']
__write_file "#{user_data} \n"
if user_data
data = JSON.parse(user_data)
data['nickname'] = data['name']
data
else
{}
end
end
def request_phase
__write_file "request phase\n"
form = OmniAuth::Form.new(:url => "RemoteUser/callback")
form.html '<script type="text/javascript"> document.forms[0].submit(); </script>'
form.to_response
end
end
end
end