def callback(self, req_key): """ Call this when authentication has been established with browser. This will save the credentials for future use. Supply request_token to lookup object credentials from previous authentication. Returns: the request key (aka cookie to set to browser) Raises: rest.ErrorResponse: e.g. 'Token is disabled or invalid' """ req_tuple = tokens.get_request_pair(req_key) self.sess = session.DropboxSession(APP_KEY, APP_SECRET, access_type=ACCESS_TYPE) self.api_client = client.DropboxClient(self.sess) self.sess.set_request_token(req_tuple[0], req_tuple[1]) acc_pair = self.sess.obtain_access_token( ) #only works if request token is verified tokens.save_access_tokens(req_tuple[0], req_tuple[1], acc_pair.key, acc_pair.secret) log.debug("DELETE: saving: " + ` (req_tuple[0], req_tuple[1], acc_pair.key, acc_pair.secret) `) return req_key
def login(self, req_key = None, callback = None): """ Create a URL which the browser can use to verify a token and redirect to webapp. Args: req_key : request key (not secret!), cached by session cookie (optional) callback : where to redirect the browser after login Returns: None: if connection is already established (from cookie) or URL: a URL to authorize the token Raises: rest.ErrorResponse """ self.sess = session.DropboxSession(APP_KEY, APP_SECRET, access_type=ACCESS_TYPE) self.api_client = client.DropboxClient(self.sess) #check if user has request token. if (req_key): # check if a req token has an access pair accesspair = tokens.get_access_pair(req_key) if not accesspair: req_tuple = tokens.get_request_pair(req_key) # if we have a req_tuple, assume it is a callback request to obtain access token if (req_tuple): log.debug("Assumming callback within login") #only works if request token is verified self.sess.set_request_token(req_tuple[0], req_tuple[1]) try: ap = self.sess.obtain_access_token() except rest.ErrorResponse as e: return { "state" : STATE_CODES["connected"], "msg": "Dropbox error: %s" % `e` } accesspair = [ap.key , ap.secret] # and save tokens.save_access_tokens(req_tuple[0], req_tuple[1], accesspair[0], accesspair[1] ) log.debug("TOKEN[%s] -> %s" %(req_key, `accesspair`)) # check if token has credentials associate with it if accesspair : #ipdb.set_trace() self.sess.set_token(*accesspair) #if we don't have a sessions get a URL for authn if (not self.sess.is_linked()): log.debug("Session not linked -- Creating new session") self.request_token = self.sess.obtain_request_token() url = self.sess.build_authorize_url(self.request_token, callback) self.state = { "url" : url , "userid" : self.request_token.key , "state" : STATE_CODES["verify_token"] } tokens.save_unverified_request( self.request_token.key, self.request_token.secret ) else: self.state = { "state" : STATE_CODES["connected"], "name": self.account_info()["display_name"]} return self.state
def callback(self, req_key): """ Call this when authentication has been established with browser. This will save the credentials for future use. Supply request_token to lookup object credentials from previous authentication. Returns: the request key (aka cookie to set to browser) Raises: rest.ErrorResponse: e.g. 'Token is disabled or invalid' """ req_tuple = tokens.get_request_pair(req_key) self.sess = session.DropboxSession(APP_KEY, APP_SECRET, access_type=ACCESS_TYPE) self.api_client = client.DropboxClient(self.sess) self.sess.set_request_token(req_tuple[0], req_tuple[1]) acc_pair = self.sess.obtain_access_token() #only works if request token is verified tokens.save_access_tokens(req_tuple[0], req_tuple[1], acc_pair.key, acc_pair.secret ) log.debug( "DELETE: saving: " + `(req_tuple[0], req_tuple[1], acc_pair.key, acc_pair.secret )`) return req_key
def test_all_cookie_functions(self): ## Create temp token tokens.save_unverified_request(userid, req_secret) ## Read request list -> success rpair = tokens.get_request_pair(userid) self.assertEqual(rpair[0], userid) self.assertEqual(rpair[1], req_secret) ## Read access list -> None apair = tokens.get_access_pair(userid) #set_trace() self.assertFalse(apair) ## Save access list tokens.save_access_tokens(userid, req_secret, acc_key, acc_secret) ## Read access list (again) -> success apair = tokens.get_access_pair(userid) self.assertEqual(apair[0], acc_key) self.assertEqual(apair[1], acc_secret)
def test_all_cookie_functions(self): ## Create temp token tokens.save_unverified_request(userid,req_secret) ## Read request list -> success rpair = tokens.get_request_pair(userid) self.assertEqual (rpair[0],userid) self.assertEqual (rpair[1],req_secret) ## Read access list -> None apair = tokens.get_access_pair(userid) #set_trace() self.assertFalse(apair) ## Save access list tokens.save_access_tokens(userid, req_secret, acc_key, acc_secret) ## Read access list (again) -> success apair = tokens.get_access_pair(userid) self.assertEqual (apair[0],acc_key) self.assertEqual (apair[1],acc_secret)
def login(self, req_key=None, callback=None): """ Create a URL which the browser can use to verify a token and redirect to webapp. Args: req_key : request key (not secret!), cached by session cookie (optional) callback : where to redirect the browser after login Returns: None: if connection is already established (from cookie) or URL: a URL to authorize the token Raises: rest.ErrorResponse """ self.sess = session.DropboxSession(APP_KEY, APP_SECRET, access_type=ACCESS_TYPE) self.api_client = client.DropboxClient(self.sess) #check if user has request token. if (req_key): # check if a req token has an access pair accesspair = tokens.get_access_pair(req_key) if not accesspair: req_tuple = tokens.get_request_pair(req_key) # if we have a req_tuple, assume it is a callback request to obtain access token if (req_tuple): log.debug("Assumming callback within login") #only works if request token is verified self.sess.set_request_token(req_tuple[0], req_tuple[1]) try: ap = self.sess.obtain_access_token() except rest.ErrorResponse as e: return { "state": STATE_CODES["connected"], "msg": "Dropbox error: %s" % ` e ` } accesspair = [ap.key, ap.secret] # and save tokens.save_access_tokens(req_tuple[0], req_tuple[1], accesspair[0], accesspair[1]) log.debug("TOKEN[%s] -> %s" % (req_key, ` accesspair `)) # check if token has credentials associate with it if accesspair: #ipdb.set_trace() self.sess.set_token(*accesspair) #if we don't have a sessions get a URL for authn if (not self.sess.is_linked()): log.debug("Session not linked -- Creating new session") self.request_token = self.sess.obtain_request_token() url = self.sess.build_authorize_url(self.request_token, callback) self.state = { "url": url, "userid": self.request_token.key, "state": STATE_CODES["verify_token"] } tokens.save_unverified_request(self.request_token.key, self.request_token.secret) else: self.state = { "state": STATE_CODES["connected"], "name": self.account_info()["display_name"] } return self.state