-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
86 lines (66 loc) · 1.86 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
80
81
82
83
84
85
86
require 'dotenv'
Dotenv.load('.env.local', '.env')
require 'json'
require 'net/http'
require 'oauth2'
require "sinatra/reloader"
require 'sinatra/base'
require 'pry'
class App < Sinatra::Base
configure :development do
register Sinatra::Reloader
enable :sessions
end
def client_id
ENV['STATUSGATOR_OAUTH_ID']
end
def client_secret
ENV['STATUSGATOR_OAUTH_SECRET']
end
def oauth_url
ENV['STATUSGATOR_OAUTH_URL']
end
def client
@client ||= OAuth2::Client.new(client_id, client_secret, :site => oauth_url)
end
def authorize_url
client.auth_code.authorize_url(:redirect_uri => 'http://localhost:9292/oauth/callback')
end
def token
client.auth_code.get_token(params[:code], :redirect_uri => 'http://localhost:9292/oauth/callback').token
end
def ping
uri = URI("#{ENV['STATUSGATOR_API_ROOT']}/api/v1/ping")
req = Net::HTTP::Get.new(uri)
req['Authorization'] = "Bearer #{session['token']}"
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
http.request(req)
end
if res.code == "200"
res.body
end
end
get '/' do
if text = ping
text
else
redirect authorize_url
end
end
get '/oauth/callback' do
session['token'] = token
redirect '/'
end
run! if app_file == $0
end
__END__
# client.auth_code.authorize_url(:redirect_uri => 'http://localhost:8080/oauth2/callback')
# =>
# "https://example.org/oauth/authorization?response_type=code&client_id=client_id&redirect_uri=http://localhost:8080/oauth2/callback"
#
# token = client.auth_code.get_token('authorization_code_value', :redirect_uri
# => 'http://localhost:8080/oauth2/callback', :headers => {'Authorization' =>
# 'Basic some_password'})
# response = token.get('/api/resource', :params => { 'query_foo' => 'bar' })
# response.class.name
# # => OAuth2::Response