-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.py
81 lines (65 loc) · 2.68 KB
/
authentication.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
import os
if os.environ.has_key('DUBNOTES_DEBUG'):
from fake_dropbox import client, rest, auth
else:
from dropbox import client, rest, auth
from oauth import oauth
from database import *
class Session(object):
def __init__ (self, request):
dubnotes_path = os.path.join(os.path.dirname(__file__), 'dubnotes.ini')
self.config = auth.Authenticator.load_config (dubnotes_path)
self.dropbox_auth = auth.Authenticator(self.config)
self.request = request
def needs_redirect(self):
return False
class RedirectedSession(Session):
def __init__(self, request):
super(RedirectedSession, self).__init__(request)
self.request_token = None
def get_authorization_url(self):
self.request_token = self.obtain_request_token()
db_store_token(self.request_token)
authorize_url = self.dropbox_auth.build_authorize_url(self.request_token, self.request.uri)
return authorize_url
def obtain_request_token(self):
return self.dropbox_auth.obtain_request_token()
def needs_redirect(self):
return True
class AuthenticatedSession(Session):
def __init__(self, request):
super(AuthenticatedSession, self).__init__(request)
self.uid = self.request.get('uid')
self.request_token = db_get_token(self.request.get('oauth_token'))
self.access_token = None
self.user = None
def authenticate_user (self):
self.get_user_and_access_token()
def get_user_and_access_token(self):
self.user = db_get_user(self.uid)
if self.user:
self.get_access_token()
else:
self.obtain_access_token()
self.user = self.create_user()
def get_access_token(self):
self.access_token = oauth.OAuthToken(str(self.user.access_key), str(self.user.access_secret))
def obtain_access_token(self):
oauth_token = oauth.OAuthToken(str(self.request_token.req_key), str(self.request_token.req_secret))
self.access_token = self.dropbox_auth.obtain_access_token (oauth_token, self.config['verifier'])
def create_user(self):
return db_store_user(self.uid, self.access_token.key, self.access_token.secret)
class SessionFactory:
@staticmethod
def create(request):
if request.get('uid',None) == None or request.get('oauth_token',None) == None:
return RedirectedSession(request)
else:
return AuthenticatedSession(request)
class AuthenticationException(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)
class RedirectionException(AuthenticationException):
pass