Beispiel #1
0
 def probe(self, req_key):
     """ Check if req_key has associated access key.
         Only use this when polling for the first time otherwise you may get false positives for
         stored credentials that have expired.
     """
     if not tokens.get_access_pair(req_key):
         self.state = {"state": STATE_CODES["verify_token"]}
     else:
         self.state = {"state": STATE_CODES["connected"]}
     return self.state
Beispiel #2
0
 def probe(self, req_key):
     """ Check if req_key has associated access key.
         Only use this when polling for the first time otherwise you may get false positives for
         stored credentials that have expired.
     """
     if not tokens.get_access_pair(req_key):
         self.state = { "state" : STATE_CODES["verify_token"] }
     else:
         self.state = { "state" : STATE_CODES["connected"] }
     return self.state
Beispiel #3
0
    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
Beispiel #4
0
    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)
Beispiel #5
0
    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)        
Beispiel #6
0
    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