Commits

Sybren Stüvel  committed b75489b

Proper checking of cached tokens before we use them

  • Participants
  • Parent commits b7e6bd3

Comments (0)

Files changed (3)

File flickrapi/auth.py

         
         return local_addr
     
-    def wait_for_oauth_verifier(self):
+    def wait_for_oauth_verifier(self, timeout=None):
         '''Starts the HTTP server, waits for the OAuth verifier.'''
             
-        while self.oauth_verifier is None:
+        if self.oauth_verifier is None:
+            self.timeout = timeout
             self.handle_request()
     
-        self.log.info('OAuth verifier: %s' % self.oauth_verifier)
+        if self.oauth_verifier:
+            self.log.info('OAuth verifier: %s' % self.oauth_verifier)
+
         return self.oauth_verifier
 
     @property
     
     @token.setter
     def token(self, new_token):
-        assert isinstance(new_token, FlickrAccessToken)
+        
+        if new_token is None:
+            self.oauth_token = None
+            self.oauth.client.resource_owner_key = None
+            self.oauth.client.resource_owner_secret = None
+            self.oauth.client.verifier = None
+            self.requested_permissions = None
+            return
+
+        assert isinstance(new_token, FlickrAccessToken), new_token
         
         self.oauth_token = new_token
         

File flickrapi/core.py

             perms = 'read'
         
         token = self.token_cache.token
-        if token and token.has_level(perms):
+        if token:
+            # Check token for validity
             self.flickr_oauth.token = token
-            return
-        
+            try:
+                resp = self.auth.oauth.checkToken(format='etree')
+                token_perms = resp.findtext('oauth/perms')
+                if token_perms == token.access_level and token.has_level(perms):
+                    # Token is valid, and for the expected permissions, so no
+                    # need to continue authentication.
+                    return
+                else:
+                    # Token was for other permissions, so erase it as it is
+                    # not usable for this request.
+                    self.flickr_oauth.token = None
+                    del self.token_cache.token
+            except FlickrError:
+                # There was an error talking to Flickr, we assume this is due
+                # to an invalid token.
+                self.flickr_oauth.token = None
+                del self.token_cache.token
+                    
         method(self, *args, **kwargs)
     
     return decorated

File oauth_test_3.py

 #!/usr/bin/env python
 
+from xml.etree import ElementTree as ET
 import logging
 logging.basicConfig(level=logging.INFO)
 
 print('Creating FlickrAPI object')
 
 flickr = FlickrAPI(keys.apikey, keys.apisecret)
+#
+#token = flickr.token_cache.token
+#flickr.flickr_oauth.token = token
+#print('Step 0: check token %r' % token.token)
+#
+#resp = flickr.auth.oauth.checkToken(format='etree')
+#ET.dump(resp)
+#raise SystemExit()
 
 # ------------------------------------------------------------------------------
 print('Step 1: authenticate')
 print('Step 2: user Flickr')
 resp = flickr.photos.getInfo(photo_id='7658567128')
 
-from xml.etree import ElementTree as ET
 ET.dump(resp)