-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
79 lines (60 loc) · 1.39 KB
/
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
require 'bundler/setup'
require 'dotenv/load'
require 'sinatra'
require 'rack-flash'
require 'erubi'
require 'oauth2'
require 'json'
require 'sinatra/reloader' if development?
set :erb, escape: true
enable :sessions
use Rack::Flash
get '/' do
if authenticated?
@output = get_test_resource
end
erb :index
end
get '/auth' do
redirect client.auth_code.authorize_url(redirect_uri: url('/auth/callback'))
end
get '/auth/callback' do
if params[:error]
flash[:error] = params[:error_description]
else
token = client.auth_code.get_token(params[:code], redirect_uri: url('/auth/callback'))
session['token'] = token.to_hash
end
redirect to('/')
end
get '/logout' do
session.destroy
redirect to('/')
end
helpers do
def authenticated?
!!session['token']
end
end
def get_test_resource
return unless session['token']
token = OAuth2::AccessToken.from_hash(client, session['token'])
begin
response = token.get(ENV["TEST_RESOURCE"])
rescue OAuth2::Error
session.destroy
return
end
s = "Status: #{response.status}\n"
s += response.headers.map { |k, v| "#{k}: #{v}" }.join("\n")
s += "\n\n"
if response.content_type == 'application/json'
s += JSON.pretty_generate(JSON.parse(response.body))
else
s += response.body
end
s
end
def client
OAuth2::Client.new(ENV["CLIENT_ID"], ENV["CLIENT_SECRET"], site: ENV["AUTHORIZATION_SERVER"])
end