-
Notifications
You must be signed in to change notification settings - Fork 4
/
authenticate-etsy.r
293 lines (264 loc) · 8.11 KB
/
authenticate-etsy.r
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
Rebol [
Title: "Complete the Etsy User Authentication"
Date: 12-Feb-2017
Author: "Christopher Ross-Gill"
Home: http://re-bol.com/etsy_api_tutorial.html
File: %authenticate-etsy.r
Version: 0.2.0
Purpose: {Obtain permissions to use Etsy API}
Rights: http://opensource.org/licenses/Apache-2.0
History: [
to-do 0.3.0 "Include scope on initiation form"
12-Feb-2017 0.2.0 "Initiates with form requesting credentials"
28-Jan-2017 0.1.0 "Original Version"
]
API: https://openapi.etsy.com/v2/
Port: 8080
Scope: [email_r listings_r]
]
do %verify.r
do %rest.r
do %validate.r
do %httpd.r
do %rsp.r
do %form-error.r
etsy: make object! [
api: system/script/header/api
port: system/script/header/port
root: join http://127.0.0.1: port
request-broker: api/(%oauth/request_token)
access-broker: api/(%oauth/access_token)
callback: root/verify
api-scope: form system/script/header/scope
handshake: credentials: values: params: errors: secret: none
views: make object! [
display: {
<link rel="stylesheet" type="text/css"
href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<div class="container" style="margin-top: 1em;">
<div class="page-header"><h1>Etsy Authentication</h1></div>
<div class="row"><div class="col-sm-8">
<%= content %>
</div></div></div>
}
reject: {
<link rel="stylesheet" type="text/css"
href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<div class="container" style="margin-top: 1em;">
<div class="page-header"><h1>Error</h1></div>
<div class="row"><div class="col-sm-8">
<p class="alert alert-danger"><%= content %></p>
</div></div></div>
}
init: {
<% if find credentials 'error [ %>
<p class="alert alert-danger">The credentials provided were not sufficient.</p>
<% ] %>
<p class="lead">First enter your <a href="https://www.etsy.com/developers/your-apps">application credentials</a>:</p>
<form method="post" action="/request" class="form-horizontal">
<div class="form-group">
<label for="i-key" class="col-sm-3 control-label">Key</label>
<div class="col-sm-9">
<%! ; build-tag notation
input value (credentials/consumer-key)
name "consumer-key" type "text"
id "i-key" placeholder "Keystring" class "form-control"
%>
</div>
</div>
<div class="form-group">
<label for="i-secret" class="col-sm-3 control-label">Secret</label>
<div class="col-sm-9">
<%! ; build-tag notation
input value (credentials/consumer-secret)
name "consumer-secret" type "text"
id "i-secret" placeholder "Shared Secret" class "form-control"
%>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-primary">Start Authentication</button>
</div>
</div>
</form>
}
done: {
<p class="lead">Your request credentials:</p>
<table class="table table-condensed">
<tr>
<th>Consumer-Key:</th>
<td><tt><%== mold credentials/consumer-key %></tt></td>
</tr>
<tr>
<th>Consumer-Secret</th>
<td><tt><%== mold credentials/consumer-secret %></tt></td>
</tr>
<tr>
<th>OAuth-Token:</th>
<td><tt><%== mold params/oauth_token %></tt></td>
</tr>
<tr>
<th>OAuth-Token-Secret</th>
<td><tt><%== mold params/oauth_token_secret %></tt></td>
</tr>
</table>
}
]
foreach name words-of views [
set :name load-rsp trim/auto get :name
]
display: func [response [object!] status [integer!] content [string! binary!]][
response/status: status
response/content: either string? content [
views/display [content]
][
content
]
content
]
reject: func [response [object!] status [integer!] content [string! binary!]][
response/status: status
response/content: views/reject [content]
content
]
redirect: func [response [object!] status [integer!] target [file! url!]][
response/status: status
response/location: target
]
handler: [
handshake: values: params: errors: webform: none
switch/default request/action [
"GET /favicon.ico" [
response/type: "image/png"
display response 200 read/binary %rebol.png
]
"GET /request" [
credentials: [Consumer-Key "" Consumer-Secret ""]
display response 200 views/init [credentials]
]
"POST /request" [
if verify [
all [
request/type = "application/x-www-form-urlencoded"
block? webform: try [load-webform request/content]
][
reject response 400 "Web Form incorrectly submitted."
]
credentials: validate webform [
consumer-key: string! [24 alphanum]
consumer-secret: string! [10 alphanum]
][
credentials: collect [
keep validate/loose webform [
consumer-key: string!
consumer-secret: string!
]
keep [consumer-key "" consumer-secret "" error]
]
; if at first you don't succeed...
display response 400 views/init [credentials]
]
not error? handshake: try [
read/custom [
scheme: 'rest
url: (request-broker)
oauth: (
make credentials: make object! [
consumer-key: credentials/consumer-key
consumer-secret: credentials/consumer-secret
][
oauth-callback: :callback
]
)
][
scope: :api-scope
]
][
print reject response 500 "Bad Request (connection error)"
print form-error :handshake
]
handshake/status = 200 [
print reject response 500 "Bad Request (status error)"
probe handshake/content
]
block? values: try [load-webform handshake/content][
print reject response 500 "Bad Request (encoding error)"
help handshake
]
params: validate/report-to values [
login_url: url! ["https://www.etsy.com/oauth/signin?" to end]
oauth_token: string! [30 32 hex]
oauth_token_secret: string! [10 hex]
oauth_consumer_key: string! [24 alphanum]
oauth_callback: url!
oauth_callback_confirmed: logic! ["true"]
] errors: copy [] [
print reject response 500 "Bad Request (params error)"
probe neaten/pairs values
probe errors
]
][
secret: params/oauth_token_secret
redirect response 303 params/login_url
]
]
"GET /verify" [
if verify [
params: validate load-webform request/query-string [
oauth_token: string! [some hex]
oauth_verifier: string! [some hex]
][
print reject response 500 "Bad Request, <a href='/request'>Try Again</a> (request error)"
help request
]
not error? handshake: try [
read/custom [
scheme: 'rest
url: :access-broker
action: 'post
oauth: make credentials [
oauth-token: (params/oauth_token)
oauth-token-secret: (secret)
]
] compose [
oauth_verifier: (params/oauth_verifier)
]
][
print reject response 500 "Bad Request, <a href='/request'>Try Again</a> (curl error)"
print form-error :handshake
]
handshake/status = 200 [
print reject response 500 "Bad Request, <a href='/request'>Try Again</a> (status error)"
help handshake
]
block? values: try [load-webform handshake/content][
print reject response 500 "Bad Request, <a href='/request'>Try Again</a> (encoding error)"
help handshake
]
params: validate values [
oauth_token: string! [30 hex]
oauth_token_secret: string! [10 hex]
][
print reject response 500 "Bad Request, <a href='/request'>Try Again</a> (params error)"
help handshake
probe values
]
][
response/kill?: true ; we're done with the server!
display response 200 views/done [credentials params]
]
]
][
response/content: "Not sure what you're trying to do here..."
]
]
server: open/custom join httpd://: port handler
do probe compose [browse (root/request)]
wait 60 ; automatically closes after a minute
close server
]