forked from bmmiller/hadashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ru
90 lines (76 loc) · 2.28 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
require 'omniauth-heroku'
require 'dashing'
configure do
# The auth token used by external clients to get API access to the
# dashing widgets.
set :auth_token, ENV["DASHING_AUTH_TOKEN"]
# Email used for signing up to Heroku. This is used for authentication.
set :user, ENV["HEROKU_OAUTH_EMAIL"]
helpers do
# Protects access to pages and redirects to the autentication page
# if not already authenticated.
def protected!
# Ignore authentication in development
if not production?
return
end
# Authenticate in production
if settings.user
redirect '/auth/heroku' unless session[:user_id] == settings.user
else
# The HEROKU_OAUTH_EMAIL env var has not been set!
redirect '/auth/notset'
end
end
end
# Store the authenticated user name in session state
use Rack::Session::Cookie, :secret => ENV["SESSION_SECRET"]
# Authenticate with Heroku
use OmniAuth::Builder do
provider :heroku,
ENV["HEROKU_OAUTH_ID"],
ENV["HEROKU_OAUTH_SECRET"],
fetch_info: true
end
# Heroku authentication callback.
get '/auth/heroku/callback' do
if auth = request.env['omniauth.auth']
if auth['info']['email'] == settings.user
session[:user_id] = settings.user
redirect '/'
else
redirect '/auth/bad'
end
else
redirect '/auth/failure'
end
end
# Authentication failure. Indicates a configuration problem.
get '/auth/failure' do
"Authentication failure."
end
# Bad credentials. Indicates that the username, password or two-factor
# auth code (if enabled) is incorrect.
get '/auth/bad' do
"Access denied."
end
# No authentication credentials have been set. HEROKU_OAUTH_EMAIL set?
get '/auth/notset' do
"Credentials not set."
end
# Restore the event history on load
savedHistory = Setting.get('history')
if savedHistory
set :history, JSON.parse(savedHistory.value)
end
# Upong exiting, write the event history to persistent storage
at_exit do
savedHistory = Setting.first_or_create(:name => 'history')
savedHistory.value = JSON.generate(settings.history)
savedHistory.save
end
end
map Sinatra::Application.assets_prefix do
run Sinatra::Application.sprockets
end
run Sinatra::Application