This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
/
chooser.rb
555 lines (473 loc) · 14.8 KB
/
chooser.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# encoding: utf-8
# Copyright 2012-14 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'sinatra'
require './body'
require './database'
require './color'
require './rp'
require './gat'
require './persona'
module Chooser
TEXT_HTML = { 'Content-type' => 'text/html; charset=utf-8' }
APPLICATION_JSON = { 'Content-type' => 'application/json' }
class Chooser < Sinatra::Base
# enable :sessions
use Rack::Session::Cookie, :key => 'rack.session',
:domain => 'favcolor.net',
:path => '/',
:expire_after => 2592000,
:secret => 'orthoepy ftw',
:old_secret => 'orthoepy ftl'
### App code
# Home page
get '/' do
# active session?
email = session[:logged_in]
if email
# Session is active, branch to favorite-color app
account = database.find_account email
erb :home, :locals => Color.favorite(account)
else
# No session, they have to log in
redirect '/account-login'
end
end
# Only for our native Android app; a POST so they can send along
# the secret ID Token
#
post '/get-color' do
params = Body::parse_json request
id_token = params['id-token']
gat_token = params['git_token']
if id_token
params = RP::from_id_token id_token
email = params['email']
else
email = GAT::get_session(request, database, gat_token)
end
if email
account = database.find_account email
if account
fields = ['email', 'displayName', 'photoUrl', 'color']
fields = fields.select { |field| is_useful?(account[field]) }
fields = fields.map { |field| "\"#{field}\":\"#{account[field]}\"" }
json = "{" + fields.join(',') + "}"
[200, APPLICATION_JSON, json]
else
[404, nil, nil]
end
else
[404, nil, nil]
end
end
# Save favorite color, after they've picked it
post '/set-color' do
id_token = params['id_token']
gat_token = params['git_token']
destination = params['dest'] || '/'
if id_token
# coming in from Android client
jwt = RP::from_id_token id_token
email = jwt['email']
elsif gat_token
# coming in from Android gat flavor
email = GAT.get_session(request, database, gat_token)
else
# active session?
email = session[:logged_in]
if !email
from_gat = GAT.get_session(request, database)
end
end
if !email
if destination == 'gat'
# launch GAT login process
erb :gatlogin
else
redirect '/gat'
end
else
# we have a valid email
session[:logged_in] = email
if database.find_account(email)
account = database.find_account email
account['color'] = params['color']
database.save_account account
if id_token
[200, nil, nil]
else
redirect destination
end
else
[404, nil, nil]
end
end
end
### GAT experiment
# called back by GAT, just dispatch to the GAT JavaScript
get '/gat-callback' do
locals = { :developer_key => GAT.key, :post_body => '' }
erb :gatcallback, :locals => locals
end
post '/gat-callback' do
request.body.rewind
locals = { :developer_key => GAT.key, :post_body => request.body.read }
erb :gatcallback, :locals => locals
end
get '/gat-signout' do
session[:logged_in] = nil
redirect '/gat'
end
post '/gat-forgot' do
prob = GAT.forgot(params, request.ip)
if !prob
json = "{\"success\":true}"
else
json = "{\"error\":\"#{prob}\"}"
end
[200, APPLICATION_JSON, json]
end
# Home page
get '/gat' do
# active session?
email = session[:logged_in]
if !email
email = GAT.get_session(request, database)
end
if !email
# launch GAT login process
erb :gatlogin
else
session[:logged_in] = email
# Session is active, branch to favorite-color app
account = database.find_account(email)
if account.idp_is_google?
share_uri = "/share?k=#{database.set_share(email)}"
else
share_uri = nil
end
locals = Color.favorite(account)
locals[:share_uri] = share_uri
erb :gat, :locals => locals
end
end
get '/share' do
email = database.get_share params['k']
account = database.find_account email
erb :share, :locals => share_page(account)
end
### Identity/Authentication/Authorization code
# Come here to log in
get '/account-login' do
erb :accountlogin
end
# Come here to register a new account
get '/account-create' do
erb :signup
end
# launch Persona sign-in process
get '/persona-sign-in' do
# should be able to pass along email
erb :persona
end
# Check persona sign-in assertion
post '/persona-assertion' do
params = Body::parse_form(request)
assertion = params['assert']
if assertion == nil || assertion == ""
auth_failed "Empty Persona assertion"
else
params = Persona.verify_assertion(assertion)
if params == nil
auth_failed "Unable to verify Persona assertion"
else
account = Account.new(params)
account = update_account account
session[:logged_in] = account['email']
[200]
end
end
end
get '/persona-succeeded' do
email = session[:logged_in]
account = database.find_account email
update_ac_js account
end
# Google redirect with ¶ms
get '/gauth-fetch-redirect' do
if params['error']
auth_failed params['error_description']
else
# params.each {|k,v| puts " #{k} => #{v}"}
auth_succeeded(:google, params)
end
end
# Facebook redirects here
get '/fbauth-redirect' do
if params['error']
auth_failed params['error_description']
else
auth_succeeded(:facebook, params)
end
end
# Microsoft Live redirects here
get '/liveauth-redirect' do
if params['error']
auth_failed params['error_description']
else
auth_succeeded(:live, params)
end
end
# ac.js comes here to see if we know this person
post '/account-status' do
params = Body::parse_form(request)
# speculatively save ac.js fields into a new account IF there's an IDP
email = params['email']
registered = database.find_account email
if params['providerId']
update_account params
end
state = make_state
auth_uri = RP::auth_uri(params, request, state)
if auth_uri
database.set_state(state, email)
[ 200, APPLICATION_JSON, "{ \"authUri\" : \"#{auth_uri}\" }" ]
else
account = database.find_account email
[ 200, APPLICATION_JSON,
'{"registered":' + ((registered) ? 'true' : 'false') + '}']
end
end
# Kill session on logout
get '/logout' do
session[:logged_in] = nil
redirect '/'
end
post '/logout' do
session[:logged_in] = nil
redirect '/'
end
# Creation form submission
post '/new-login' do
params = Body::parse_form request
email = params['email']
provider = params['providerId']
state = is_useful?(email) ? make_state : nil
if provider
# provider selected
if is_useful? email
# got an email too; if we have an account with the same
# provider, use that to help redirect
database.set_state(state, email)
account = database.find_account email
if account && account['providerId'].eql?(provider)
params = account
end
end
# redirect to provider
redirect RP::auth_uri(params, request, state)
else
# no provider
# They have to give us at least an email and password
if !(is_useful?(email) && is_useful?(params['password']))
redirect back
else
# got an email and password
# is there already an account?
if database.find_account(email)
redirect '/dupe'
else
# We really have a new account, persist it & start session
account = Account.new(params)
database.save_account account
session[:logged_in] = email
update_ac_js account
end
end
end
end
# tried to register an account with an existing email address
get '/dupe' do
locals = {
:error_title => 'Duplicate account!',
:error_message => 'Sorry, that email is taken.'
}
erb :error, :locals => locals
end
# login form submission
post '/done-login' do
params = Body::parse_form request
email = params['email']
provider = params['providerId']
state = is_useful?(email) ? make_state : nil
if provider
# provider selected
if is_useful? email
# got an email too; if we have an account with the same
# provider, use that to help redirect
session[state] = email
account = database.find_account email
if account && account['providerId'].eql?(provider)
params = account
end
end
# redirect to provider
session[make_state] = email if is_useful? email
s = RP::auth_uri(params, request, state)
redirect s
else
# no provider selected
if !is_useful?(email)
# no email either, can't help them
redirect back
else
# got an email
account = database.find_account email
if account == nil
# no existing account, try to sign them up
redirect '/account-create'
else
# we have an account for that email
if account['providerId']
# the account has a provider, use it
session[state] = email
redirect RP::auth_uri(params, request, state)
else
# no provider, have to use password
if !is_useful?(params['password'])
# no password either, can't help them
redirect back
else
# email & password supplied
if account.check_password(params['password'])
# success! Establish a session
session[:logged_in] = email
update_ac_js account
else
# wrong password or email
auth_failed 'Incorrect email or password.'
end
end
end
end
end
end
end
# will redirect back to '/'
def update_ac_js account
fvals = []
['email', 'displayName', 'photoUrl', 'providerId'].each do |name|
field = account[name]
fvals << "#{name}: \"#{field}\"" if is_useful?(field)
end
fields = fvals.join(', ')
erb :updateac, :locals => { :ac_fields => fields }
end
MARKETING_HEADERS = {
'Content-type' => 'text/html; charset=utf-8',
"Access-Control-Allow-Origin" => "*",
"Access-Control-Allow-Methods" => "GET, POST, OPTIONS",
"Access-Control-Max-Age" => "86400"
}
MARKETING_TEXT = "<img src=\"https://favcolor.net/g60.png\" " +
"style=\"float:left;width:150px;box-shadow: 3px 3px 8px #222; margin-bottom:10px;\"/>\n" +
"<p style='margin-left: 160px;'>" +
"FavColor — We know your favorite!</p>"
get '/login-marketing' do
[ 200, MARKETING_HEADERS, MARKETING_TEXT ]
end
# testbed for the HTTP status code 451 proposal
T451 = <<EOFEOF
<html><head><title>451 Unavailable For Legal Reasons</title></head>
<body><h2>451 Unavailable For Legal Reasons</h2></body></html>
EOFEOF
get '/451' do
[451, TEXT_HTML, T451]
end
### Utility code
private
def share_page account
fave = account['color']
name = account['displayName'] || account['email']
locals = {}
locals[:fave_color] = fave
locals[:display_name] = name
if fave
r = fave[0..1]; g = fave[2..3]; b = fave[4..5]
locals[:rgb] = "0x#{r}, 0x#{g}, 0x#{b}"
end
locals
end
def auth_failed problem
locals = {
:problem_title => 'Authorization failed',
:problem_header => 'Authorization failed',
:problem_detail => problem
}
html = erb(:problem, :locals => locals)
[403, TEXT_HTML, html]
end
def auth_succeeded(provider, params)
code = params['code']
idp_account = Account.new(RP::fetch_account(provider, code, request))
# we want to update accountchooser with whoever they logged in with,
# not who we might think their primary IDP is.
account = idp_account.dup
account = update_account account
session[:logged_in] = account['email']
account = account.clone
account['providerId'] = RP::provider_name provider
update_ac_js idp_account
end
def update_account incoming
email = incoming['email']
existing_account = database.find_account email
updated = false
if existing_account
['displayName', 'photoUrl', 'providerId'].each do |field|
# only update if old one not there
if incoming[field] && !existing_account[field]
updated = true
existing_account[field] = incoming[field]
end
end
incoming = existing_account
else
updated = true
end
if updated
if !incoming.kind_of? Account
incoming = Account.new(incoming)
end
database.save_account incoming
end
incoming
end
def logger
@logger = Logger.new(STDOUT) unless @logger
@logger
end
def database
@database = Database.new unless @database
@database
end
def is_useful? object
(object != nil) && !object.empty?
end
def make_state
rand(10 ** 9).to_s + rand(10 ** 9).to_s
end
end
end