143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
# File 'lib/omnigollum.rb', line 143
def self.registered(app)
config = Omnigollum::Config.new
options = app.settings.respond_to?(:omnigollum) ?
config.default_options.merge(app.settings.send(:omnigollum)) :
config.default_options
OmniAuth.config.path_prefix = options[:route_prefix] + OmniAuth.config.path_prefix
if options[:dummy_auth]
OmniAuth.config.test_mode = true
OmniAuth.config.mock_auth[:default] = {
'uid' => '12345',
"info" => {
"email" => "[email protected]",
"name" => "example user"
},
'provider' => 'local'
}
end
app.helpers Helpers
app.set :sessions, true
if !options[:providers].nil?
app.use OmniAuth::Builder, &options[:providers]
config.eval_omniauth_config &options[:providers] if options[:provider_names].count == 0
end
options[:protected_routes].each { |route| app.before(route) { user_auth unless user_authed? }}
app.before '/*' do
@user_authed = user_authed?
@user = get_user
end
app.before options[:route_prefix] + '/login' do
kick_back if user_authed?
@auth_params = "?origin=#{CGI.escape(request.referrer)}" if !request.referrer.nil?
user_auth
end
app.before options[:route_prefix] + '/logout' do
user_deauth
kick_back
end
app.before options[:route_prefix] + '/auth/failure' do
user_deauth
@title = 'Authentication failed'
@subtext = 'Provider did not validate your credentials (#{param[:message]}) - please retry or choose another login service'
@auth_params = "?origin=#{CGI.escape(request.env['omniauth.origin'])}" if !request.env['omniauth.origin'].nil?
show_login
end
app.before options[:route_prefix] + '/auth/:name/callback' do
begin
if !request.env['omniauth.auth'].nil?
session[:auth_user] = Omnigollum::Models::OmniauthUser.new(request.env['omniauth.auth'])
redirect request.env['omniauth.origin']
elsif !user_authed?
@title = 'Authentication failed'
@subtext = "Omniauth experienced an error processing your request"
@auth_params = "?origin=#{CGI.escape(request.env['omniauth.origin'])}" if !request.env['omniauth.origin'].nil?
show_login
end
rescue Omnigollum::Models::OmniauthUserInitError => fail_reason
@title = 'Authentication failed'
@subtext = fail_reason
@auth_params = "?origin=#{CGI.escape(request.env['omniauth.origin'])}" if !request.env['omniauth.origin'].nil?
show_login
end
end
app.before options[:route_prefix] + '/images/:image.png' do
content_type :png
send_file options[:path_images] + '/' + params[:image] + '.png'
end
app.before options[:route_prefix] + '/auth/:provider' do
halt 404
end
app.set(:omnigollum, options)
end
|