-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
163 lines (137 loc) · 4.33 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
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
require 'sinatra/reloader' if development?
require 'pry' if development?
require 'json'
class App < Sinatra::Base
set :sessions, true
set :inline_templates, true
set :logging, true
configure :development do
register Sinatra::Reloader
end
def client(token_method = :post)
OAuth2::Client.new(
ENV.fetch('PROCORE_CLIENT_ID', 'proauth-local'),
ENV.fetch('PROCORE_CLIENT_SECRET', 'pleaseUseA4RealSecret.'),
site: ENV.fetch('PROCORE_API_URL') {"http://localhost:3000"},
)
end
def access_token
OAuth2::AccessToken.new(client, session[:access_token], refresh_token: session[:refresh_token])
end
def redirect_uri
ENV.fetch('PROCORE_OAUTH2_REDIRECT_URI') {'http://localhost:5000/callback'}
end
def authorized_api_request(path, query_string=nil)
HTTParty.get("#{client.site}/#{path}?#{query_string}",
headers: {
'Authorization' => "Bearer #{session[:access_token]}",
'Accept' => 'application/json',
})
end
get '/' do
erb :home
end
get '/sign_in' do
redirect client.auth_code.authorize_url(redirect_uri: redirect_uri)
end
get '/sign_out' do
session[:access_token] = nil
session[:refresh_token] = nil
erb :sign_out
end
get '/callback' do
token = client.auth_code.get_token(params[:code], redirect_uri: redirect_uri)
session[:access_token] = token.token
session[:refresh_token] = token.refresh_token
erb :callback
end
get '/refresh' do
token = access_token.refresh!
session[:access_token] = token.token
session[:refresh_token] = token.refresh_token
erb :refresh
end
get '/api/*' do
result = authorized_api_request(params[:splat].join('/'), request.query_string)
json JSON.parse(result.body)
end
end
__END__
@@layout
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Oauth Sample Client</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap.min.css" media="all" charset="utf-8">
<% if session[:access_token] %>
<script type="text/javascript">
var apiUrl = '<%= "#{ENV['PROCORE_API_URL']}" %>'
var authHeader = 'Bearer <%= session[:access_token] %>'
</script>
<!-- <script src="xmlhttp_request_example.js" charset="utf-8"></script> -->
<script src="fetch_example.js" charset="utf-8"></script>
<% end %>
</head>
<body>
<div class="container">
<div class="page-header clearfix">
<h3 class="text-muted">Procore Oauth Client App</h3>
</div>
<div>
<%= yield %>
</div>
</div>
</body>
</html>
@@callback
<script type="text/javascript">
localStorage.setItem('accessToken', '<%= session[:access_token] %>')
localStorage.setItem('refreshToken', '<%= session[:refresh_token] %>')
localStorage.setItem('apiUrl', '<%= ENV['PROCORE_API_URL'] %>')
window.location = "/";
</script>
@@refresh
<script type="text/javascript">
localStorage.setItem('accessToken', '<%= session[:access_token] %>')
localStorage.removeItem('refreshToken')
window.location = "/";
</script>
@@sign_out
<script type="text/javascript">
console.log('signing out');
localStorage.removeItem('accessToken')
localStorage.removeItem('refreshToken')
window.location = "/";
</script>
@@home
<div class="grid">
<% if session[:access_token] %>
access token <pre><%= session[:access_token] %></pre>
refresh token <pre><a href='/refresh'><%= session[:refresh_token] %></a></pre>
<a href='/sign_out' class='button btn btn-large btn-block btn-warning'>Sign Out</a>
<br>
<fieldset>
<legend>Initate a CORS api request from this browser</legend>
<%= client.site %>/vapid/companies:
<pre id='api-log'>
<code class='lang-js'></code>
</pre>
</fieldset>
<fieldset>
<legend>Initiate an authorized api calls from server:</legend>
<ul class='list'>
<li>
<a href='/api/vapid/companies'>/api/vapid/companies</a>
</li>
<li>
<a href='/api/oauth/token/info'>/api/oauth/tokens/info</a>
</li>
</ul>
</fieldset>
<% else %>
<a href='/sign_in' class='button btn btn-large btn-block btn-primary'>Sign In</a>
<% end %>
</div>