This repository has been archived by the owner on Oct 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
multi_scope_app.rb
84 lines (73 loc) · 2.04 KB
/
multi_scope_app.rb
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
require File.expand_path('../setup', __FILE__)
module Example
class MultiScopeApp < BaseApp
enable :inline_templates
GITHUB_CONFIG = {
client_id: ENV['GITHUB_CLIENT_ID'] || 'test_client_id',
client_secret: ENV['GITHUB_CLIENT_SECRET'] || 'test_client_secret'
}
use Warden::Manager do |config|
config.failure_app = BadAuthentication
config.default_strategies :github
config.scope_defaults :default, config: GITHUB_CONFIG
config.scope_defaults :admin, config: GITHUB_CONFIG.merge(scope: 'user,notifications')
end
get '/' do
erb :index
end
get '/login' do
env['warden'].authenticate!
redirect '/'
end
get '/admin/login' do
env['warden'].authenticate!(scope: :admin)
redirect '/'
end
get '/logout' do
if params.include?('all')
env['warden'].logout
else
env['warden'].logout(:default)
end
redirect '/'
end
get '/admin/logout' do
env['warden'].logout(:admin)
redirect '/'
end
end
def self.app
@app ||= Rack::Builder.new do
run MultiScopeApp
end
end
end
__END__
@@ index
<html>
<body>
<h1>Multi Scope App Example</h1>
<ul>
<% if env['warden'].authenticated? %>
<li><a href='/logout'>[User] sign out</a></li>
<% else %>
<li><a href='/login'>[User] sign in</a></li>
<% end %>
<% if env['warden'].authenticated?(:admin) %>
<li><a href='/admin/logout'>[Admin] sign out</a></li>
<% else %>
<li><a href='/admin/login'>[Admin] sign in</a></li>
<% end %>
<% if env['warden'].authenticated? && env['warden'].authenticated?(:admin) %>
<li><a href='/logout?all=1'>[User & Admin] sign out</a></li>
<% end %>
</ul>
<hr />
<dl>
<dt>User:</dt>
<dd><%= env['warden'].authenticated? ? env['warden'].user.name : 'Not signed in' %></dd>
<dt>Admin:</dt>
<dd><%= env['warden'].authenticated?(:admin) ? env['warden'].user(:admin).name : 'Not signed in' %></dd>
</dl>
</body>
</html>