/
authenticate.py
668 lines (490 loc) · 21.5 KB
/
authenticate.py
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
"""
Carries out the responsiblity of authenticating
the client application/program and resource owner/user
"""
from cherrypy import expose, HTTPRedirect, config, tools, request
from Cheetah.Template import Template
from cStringIO import StringIO
from urllib import urlencode
import logging
import database.client
import database.user
import database.models
import database.scope
import database.authcode
import database.accesstoken
import database.refreshtoken
import database.tokens
import database.associations
from database.errors import *
from user_resource_grant import user_resource_grant
##Am not sure where to put the associate clients with users code calls.
##
##
class Provider(object):
"""
Authorises clients to access user resources and provides authorisation codes as well as access and refresh tokens.
"""
def __init__(self, scope=tuple()):
self._available_scope = scope
@expose
def index(self, code=None, error=None):
return "Hello World!"
@expose
def manage(self):
pass
@expose
def auth(self,
response_type=None,
client_id=None,
redirect_uri=None,
scope = None,
state = None):
if response_type not in ('code','token'):
#It can only be the value 'code'
#redirect to redirect_uri
#with error message as a parameter
error_str = StringIO()
error_str.write(redirect_uri)
error_list = [('error', 'invalid_request',)]
if state != None:
error_list.append(('state', state,))
error_str.write('?')
error_str.write(urlencode(error_list))
raise HTTPRedirect(error_str.getvalue())
if not database.client.client_exists(client_id):
#client is not found in db
error_str = StringIO()
error_str.write(redirect_uri)
error_list = [('error', 'invalid_client')]
if state != None:
error_list.append(('state', state))
error_str.write('?')
error_str.write(urlencode(error_list))
raise HTTPRedirect(error_str.getvalue())
if scope != None:
available = self.available_scope
scope_list = scope.split()
available_scope = [x for x in scope_list if x in available]
if available_scope != scope_list:
#all the values in the scope
#must match one in the available list
#since it doesn't return a invalid_scope error
error_str = StringIO()
error_str.write(redirect_uri)
error_list = [('error', 'invalid_scope')]
if state != None:
error_list.append(('state', state))
error_str.write('?')
error_str.write(urlencode(error_list))
raise HTTPRedirect(error_str.getvalue())
client = database.client.get_client(client_id)
if client.redirect_uri != redirect_uri:
#does not match one stored in DB for client
error_str = StringIO()
error_str.write(redirect_uri)
error_list = [('error', 'redirect_uri_mismatch')]
if state != None:
error_list.append(('state', state))
error_str.write('?')
error_str.write(urlencode(error_list))
raise HTTPRedirect(error_str.getvalue())
uid = request.login
user = database.user.get_user(uid)
if user == None:
#for some reason the user logged in is not in DB
error_str = StringIO()
error_str.write(redirect_uri)
error_list = [('error', 'access_denied')]
if state != None:
error_list.append(('state', state))
error_str.write('?')
error_str.write(urlencode(error_list))
raise HTTPRedirect(error_str.getvalue())
#will have to redesign user_resource_grant template to
#add the response type value so it knows how to format
#the return URL
grant = user_resource_grant()
grant.client_id = client_id
grant.user_id = user.id
grant.redirect_uri = redirect_uri
if scope:
grant.scope = scope
grant.message = "Allow %s to access to %s?" % (client.name, scope)
else:
grant.scope = None
grant.message = "Allow %s to access all your resources?" % (client.name)
grant.state = state
grant.title = "Access grant"
grant.action = "get_auth_code"
if response_type == 'code':
grant.token = None
return str(grant)
elif response_type == 'token':
grant.token = True
return str(grant)
@expose
def get_auth_code(self,
allow=None,
deny=None,
user_id=None,
client_id=None,
redirect_uri=None,
scope=None,
state=None,
token=None):
if deny:
error_str = StringIO()
error_str.write(redirect_uri)
error_list = [('error', 'access_denied')]
if state != None:
error_list.append(('state', state))
if token:
error_str.write('#')
else:
error_str.write('?')
error_str.write(urlencode(error_list))
raise HTTPRedirect(error_str.getvalue())
elif allow:
response_str = StringIO()
response_str.write(redirect_uri)
if token:
response_str.write('#')
else:
response_str.write('?')
response_list = list()
if token:
uid = request.login
auth_token_str = \
database.accesstoken.create_implicit_grant_access_token(
uid,
client_id,
redirect_uri,
scope)
if not auth_token_str:
logging.warn('token unauthorised: ' + str(auth_token_str));
##Error not authorised
error_str = StringIO()
error_str.write(redirect_uri)
error_list = [('error', 'unauthorized_client')]
if state:
error_list.append(('state', state))
if token:
error_str.write('#')
else:
error_str.write('?')
error_str.write(urlencode(error_list))
raise HTTPRedirect(error_str.getvalue())
## the client is probably a javascript client which
## can't be trusted for more than one session. Thus
## the client does not get a refresh token in this flow
auth_token = database.accesstoken.get_access_token(auth_token_str)
response_list.append(('access_token', auth_token.code))
response_list.append(('expires_in', auth_token.expire))
response_list.append(('token_type', 'bearer'))
if scope:
response_list.append(('scope', scope))
else:
## This is the normal 2 headed snake authentication.
## The client is sent back an authorisation code,
## which it uses to gain an access token later.
uid = request.login
auth_code_str = database.authcode.create_auth_code(client_id, uid, scope)
response_list.append(('code', auth_code_str))
if state:
## State is optional, but if present must be returned
## back to the client.
response_list.append(['state', state])
response_str.write(urlencode(response_list))
raise HTTPRedirect(response_str.getvalue())
#if both allow and deny are
#None or False then will fall through to here
return "<html><head><title>A problem occured" + \
"</title></head><body><div>A problem occured</div></body></html>"
@expose
@tools.json_out()
def token(self,
grant_type=None,
client_id=None,
client_secret=None,
username=None,
password=None,
assertion_type=None,
assertion=None,
scope=None,
code=None,
refresh_token=None,
redirect_uri=None,
state=None):
'''
returns a json string containing the
access token and/or refresh token.
'''
if 'authorization_code' == grant_type:
return self.process_auth_code_grant(client_id,
client_secret,
scope,
code,
redirect_uri)
elif 'password' == grant_type:
return self.process_password_grant(client_id,
client_secret,
username,
password,
scope)
elif 'client_credentials' == grant_type:
return {'error' : 'invalid_grant' }
elif 'assertion' == grant_type:
return self.process_assertion_grant(client_id,
client_secret,
assertion_type,
assertion,
scope)
elif 'refresh_token' == grant_type:
return self.process_refresh_token_grant(client_id,
client_secret,
refresh_token)
elif 'none' == grant_type:
return {'error' : 'invalid_grant' }
else:
#the grant_type specified is not a valid one
return {'error' : 'invalid_grant' }
def process_auth_code_grant(self,
client_id,
client_secret,
scope,
code,
redirect_uri):
client = database.client.get_client(client_id)
auth_code = database.authcode.get_auth_code(client_id,
client_secret,
code)
print client, auth_code,
if client:
print redirect_uri, client.redirect_uri
if auth_code and \
client and \
redirect_uri == client.redirect_uri:
access_token_str = database.accesstoken.create_access_token_from_code(auth_code)
refresh_token_str = database.refreshtoken.create_refresh_token_from_code(
auth_code,
access_token_str)
if not access_token_str:
return { 'error' : 'access_denied' }
tokens = {
'access_token' : access_token_str,
'token_type' : 'bearer',
'expires_in' : database.tokens.get_token(client_id,
client_secret,
access_token_str).expire,
'refresh_token' : refresh_token_str,
}
if scope:
tokens.update({'scope' : scope})
try:
if not database.associations.isassociated(auth_code.user,
auth_code.client,
refresh_token_str):
database.associations.associate_client_with_user(auth_code.user,
auth_code.client,
refresh_token_str)
else:
#import pdb; pdb.set_trace()
database.associations.update_association(auth_code.user,
auth_code.client,
refresh_token_str)
except AssociationExistsWarning, e:
logging.warn('process_auth_code_grant: ' + str(e))
finally:
database.authcode.delete_auth_code(code)
return tokens
#something went wrong
return {'error' : 'invalid_client' }
def process_password_grant(self,
client_id,
client_secret,
username,
password,
scope):
access_token = database.accesstoken.create_access_token_from_user_pass(
client_id,
client_secret,
username,
password,
scope)
refresh_token = database.refreshtoken.create_refresh_token_from_user_pass(
client_id,
client_secret,
username,
password,
scope,
access_token)
if access_token is not None and \
refresh_token is not None:
#turn it into a AccessToken instance & RefreshToken instance
if client_id != None:
access_token = database.tokens.get_token(client_id,
client_secret,
access_token)
refresh_token = database.tokens.get_token(client_id,
client_secret,
refresh_token)
else:
access_token = database.tokens.get_token(username,
password,
access_token)
refresh_token = database.tokens.get_token(username,
password,
refresh_token)
tokens = {
'access_token' : access_token.code,
'token_type' : 'bearer',
'expires_in' : access_token.expire,
'refresh_token' : refresh_token.code,
}
if scope:
tokens.update({'scope' : scope})
try:
if not database.associations.isassociated(refresh_token.user,
refresh_token.client,
refresh_token):
database.associations.associate_client_with_user(refresh_token.user,
refresh_token.client,
refresh_token)
else:
database.associations.update_association(refresh_token.user,
refresh_token.client,
refresh_token)
except AssociationExistsWarning, e:
logging.warn('process_auth_code_grant: ' + str(e))
return tokens
#something went wrong
return {'error' : 'invalid_client' }
def process_assertion_grant(self,
client_id,
client_secret,
assertion_type,
assertion,
scope):
#I'm not going to support this yet
return { 'error' : 'invalid_grant' }
def process_refresh_token_grant(self,
client_id,
client_secret,
refresh_token):
token = database.tokens.get_token(client_id,
client_secret,
refresh_token)
if not isinstance(token, database.models.RefreshToken):
return { 'error' : 'invalid_grant' }
access_token = token.access_token
#database.tokens.delete_token(access_token)
del access_token
new_access_token = database.tokens.get_token(
client_id,
client_secret,
database.accesstoken.create_access_token_from_refresh_token(token))
tokens = {
'access_token' : new_access_token.code,
'token_type' : 'bearer',
'expires_in' : new_access_token.expire,
}
if new_access_token.scope:
tokens.update({'scope' : new_access_token.scope})
return tokens
@property
def available_scope(self):
return self._available_scope
@available_scope.setter
def available_scope(self, scope):
self._available_scope = scope
def access_resource_authorised(token_str):
'''
@attr token_str - the access token string representation
@returns the AccessToken object for the token string on success or the error message in the form of a python dictionary
'''
token = database.accesstoken.get_access_token(token_str)
expired = available_scope = scope_list = True
#if token is not false or none checks to see if it is a AccessToken or not
if token and isinstance(token, database.models.AccessToken):
if token.scope == None or \
token.scope != None and token.scope.lower() == 'all':
return token
elif token.scope != None and request.path_info in token.scope:
return token
else:
return {'error':'insufficient_scope'}
#assume to be a invalid token if it got this far
return {'error':'invalid_token'}
def check(username, password):
if database.user.get_password(username) != password:
return u'Authentication failed'
def anonymous():
urls = database.scope.get_anonymous_urls()
if urls:
urls.add('/oauth/token')
if request.path_info in urls:
return 'anonymous'
config.update({
'tools.sessions.on': True,
'tools.session_auth.on': True,
'tools.session_auth.check_username_and_password':check,
'tools.session_auth.anonymous':anonymous,
})
@expose
@tools.response_headers(headers = [('Content-Type', 'image/svg')])
def avatar(access_token=None):
token = access_resource_authorised(access_token)
if isinstance(token, database.models.AccessToken):
user = token.user
try:
with open('users/%s/avatar.svg' % (user.id)) as avatar:
return avatar.read()
except IOError, io:
logging.error(str(io))
return None
return str(token)
@expose
@tools.json_out()
def who_am_i(access_token=None):
token = access_resource_authorised(access_token)
if isinstance(token, database.models.AccessToken):
user = database.user.get_user(token.user)
logging.error(str(user))
return {'id' : user.id,
'firstname' : user.firstname,
'lastname' : user.lastname,
}
#if it got this far then
#something went wrong and
#the token should be a
#dict which represents the error
return token
if __name__ == '__main__':
from cherrypy import quickstart
from urllib2 import urlopen, Request
class Root(object):
@expose
def index(self, error=None, code=None, state=None):
if error != None:
return "<html><head><title>Index</title></head><body><div>This is the index where a error %s occured</div></body></html>" % (error)
elif code != None:
data = {'grant_type': 'authorization_code',
'client_id': 'o9999o',
'client_secret': 'secret',
'code': code,
'redirect_uri': 'http://127.0.0.1:8080',}
request = Request('http://localhost:8080/oauth/token',
urlencode(data))
logging.info(str(request))
response = urlopen(request)
message = response.read()
return "<html><head><title>Index</title></head><body><div>This is the index where the code is %s</div><div>%s</div><with a returned state of %s</div></body></html>" % (code, message, state)
return "<html><head><title>Index</title></head><body><div>This is the index</div></body></html>"
index = Root()
index.oauth = Provider()
index.who_am_i = who_am_i
index.avatar = avatar
database.scope.add_anonymous_url('/who_am_i')
database.scope.add_anonymous_url('/avatar')
quickstart(index)