This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.ru
95 lines (78 loc) · 1.69 KB
/
config.ru
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
require 'bundler'
Bundler.require
require './lib/ext/gollum'
require './lib/configuration.rb'
require './lib/wiki.rb'
class App < Sinatra::Base
configure :development do
use Rack::Reloader
Sinatra::Application.reset!
end
use Rack::Session::Cookie, key: CONFIG[:session][:key],
secret: CONFIG[:session][:secret]
use OmniAuth::Builder do
provider :google_apps, store: OpenID::Store::Filesystem.new('/tmp'),
domain: CONFIG[:google][:domain]
end
helpers do
def auth_hash
request.env['omniauth.auth']
end
end
# Callback for OpenID login.
post '/auth/google_apps/callback' do
unless auth_hash[:provider] == 'google_apps'
403
end
user = session[:user] = auth_hash['info']
if user['email']
redirect '/'
else
session.clear
403
end
end
post '/auth/google_oauth2/callback' do
unless auth_hash[:provider] == 'google_oauth2'
403
end
user = auth_hash['info']
if email = user['email'] && email.end_with?("@#{CONFIG[:google][:domain]}")
session[:user] = user
redirect '/'
else
session.clear
403
end
end
get '/' do
if not session[:user]
erb :login
else
redirect '/wiki'
end
end
get '/logout' do
session.clear
redirect '/'
end
not_found do
if not request.path_info.start_with? '/auth'
redirect "/wiki#{request.fullpath}"
else
"Not Found"
end
end
error 403 do
"Forbidden"
end
end
use Rack::Rewrite do
rewrite %r{/(javascript|css|edit|create|preview|compare)(.*)}, '/wiki/$1$2'
end
map '/wiki' do
run Wiki.new
end
map '/' do
run App.new
end