forked from doorkeeper-gem/doorkeeper-sinatra-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doorkeeper_client.rb
206 lines (166 loc) · 4.79 KB
/
doorkeeper_client.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# frozen_string_literal: true
require 'sinatra/base'
require 'securerandom'
require 'singleton'
require 'dotenv/load'
require './lib/html_renderer'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
Rollbar.configure do |config|
config.access_token = ENV['ROLLBAR_ACCESS_TOKEN']
end
class App
include Singleton
attr_accessor :public_client_id,
:public_client_redirect_uri,
:confidential_client_id,
:confidential_client_secret,
:confidential_client_redirect_uri,
:provider_url
end
App.instance.tap do |app|
app.public_client_id = ENV['PUBLIC_CLIENT_ID']
app.public_client_redirect_uri = ENV['PUBLIC_CLIENT_REDIRECT_URI']
app.confidential_client_id = ENV['CONFIDENTIAL_CLIENT_ID']
app.confidential_client_secret = ENV['CONFIDENTIAL_CLIENT_SECRET']
app.confidential_client_redirect_uri = ENV['CONFIDENTIAL_CLIENT_REDIRECT_URI']
app.provider_url = ENV['PROVIDER_URL']
end
class DoorkeeperClient < Sinatra::Base
require 'rollbar/middleware/sinatra'
use Rollbar::Middleware::Sinatra
enable :sessions
helpers do
include Rack::Utils
alias_method :h, :escape_html
def pretty_json(json)
JSON.pretty_generate(json)
end
def signed_in?
!session[:access_token].nil?
end
def state_matches?(prev_state, new_state)
return false if blank?(prev_state)
return false if blank?(new_state)
prev_state == new_state
end
def blank?(string)
return true if string.nil?
/\A[[:space:]]*\z/.match?(string.to_s)
end
def markdown(text)
options = { autolink: true, space_after_headers: true, fenced_code_blocks: true }
markdown = Redcarpet::Markdown.new(HTMLRenderer, options)
markdown.render(text)
end
def markdown_readme
markdown(File.read(File.join(File.dirname(__FILE__), 'README.md')))
end
def site_host
URI.parse(app.provider_url).host
end
end
def app
App.instance
end
def client
public_send("#{session[:client]}_client")
end
def public_client
OAuth2::Client.new(app.public_client_id, nil, site: app.provider_url)
end
def confidential_client
OAuth2::Client.new(app.confidential_client_id, app.confidential_client_secret, site: app.provider_url)
end
def access_token
OAuth2::AccessToken.new(client, session[:access_token], refresh_token: session[:refresh_token])
end
def generate_state!
session[:state] = SecureRandom.hex
end
def generate_code_verifier!
session[:code_verifier] = SecureRandom.uuid
end
def state
session[:state]
end
def code_verifier
session[:code_verifier]
end
def code_challenge_method
'S256'
end
def code_challenge
Base64.urlsafe_encode64(Digest::SHA256.digest(session[:code_verifier])).split('=').first
end
def authorize_url_for_client(type)
session[:client] = type
client.auth_code.authorize_url(
redirect_uri: app.confidential_client_redirect_uri,
scope: 'restaurants bookings people',
state: generate_state!,
code_challenge_method: code_challenge_method,
code_challenge: code_challenge
)
end
get '/' do
erb :home
end
get '/sign_in' do
generate_code_verifier!
redirect authorize_url_for_client(:confidential)
end
get '/public_sign_in' do
generate_code_verifier!
redirect authorize_url_for_client(:public)
end
get '/sign_out' do
session[:access_token] = nil
session[:refresh_token] = nil
redirect '/'
end
get '/callback' do
if params[:error]
erb :callback_error, layout: !request.xhr?
else
unless state_matches?(state, params[:state])
redirect '/'
return
end
new_token =
client
.auth_code
.get_token(
params[:code],
redirect_uri: app.confidential_client_redirect_uri,
code_verifier: code_verifier
)
session[:access_token] = new_token.token
session[:refresh_token] = new_token.refresh_token
redirect '/'
end
end
get '/refresh' do
new_token = access_token.refresh!
session[:access_token] = new_token.token
session[:refresh_token] = new_token.refresh_token
redirect '/'
rescue OAuth2::Error => _e
erb :error, layout: !request.xhr?
rescue StandardError => _e
erb :error, layout: !request.xhr?
end
get '/explore/*' do
path = params['splat'].join
raise 'Please call a valid endpoint' unless path
begin
response = access_token.get "/api/restricted/#{path}", headers: {
'Content-Type' => 'application/json',
'Accept' => 'application/vnd.libro-restricted-v2+json'
}
@json = JSON.parse(response.body)
erb :explore, layout: !request.xhr?
rescue OAuth2::Error => _e
erb :error, layout: !request.xhr?
end
end
end