@@ -39,33 +39,32 @@ class TokenStore(object):
3939 def __init__ (self , scoped_tokens = None ):
4040 self ._tokens = scoped_tokens or {}
4141
42- def add_token (self , token , scopes ):
42+ def add_token (self , token ):
4343 """Adds a new token to the store (replaces tokens with the same scope).
4444
4545 Args:
4646 token: A subclass of http_interface.GenericToken. The token object is
4747 responsible for adding the Authorization header to the HTTP request.
48- scopes: list of atom.url.Url objects, or strings which specify the
49- URLs for which this token can be used. These do not need to be
50- full URLs, any URL that begins with the scope will be considered
51- a match.
48+ The scopes defined in the token are used to determine if the token
49+ is valid for a requested scope when find_token is called.
5250
5351 Returns:
5452 True if the token was added, False if the token was not added becase
5553 no scopes were provided.
5654 """
57- if scopes :
58- for scope in scopes :
59- self ._tokens [str (scope )] = token
60- return True
61- else :
55+ if not hasattr (token , 'scopes' ) or not token .scopes :
6256 return False
6357
58+ for scope in token .scopes :
59+ self ._tokens [str (scope )] = token
60+ return True
61+
6462 def find_token (self , url ):
6563 """Selects an Authorization header token which can be used for the URL.
6664
6765 Args:
68- url: str or atom.url.Url The URL which is going to be requested. All
66+ url: str or atom.url.Url or a list containing the same.
67+ The URL which is going to be requested. All
6968 tokens are examined to see if any scopes begin match the beginning
7069 of the URL. The first match found is returned.
7170
@@ -76,13 +75,18 @@ def find_token(self, url):
7675 returned because the GenericToken calls through to the http client
7776 without adding an Authorization header.
7877 """
78+ if url is None :
79+ return None
7980 url = str (url )
8081 if url in self ._tokens :
81- return self ._tokens [url ]
82- else :
83- for scope , token in self ._tokens .iteritems ():
84- if url .startswith (scope ):
85- return token
82+ token = self ._tokens [url ]
83+ if token .valid_for_scope (url ):
84+ return token
85+ else :
86+ self .remove_token (url )
87+ for scope , token in self ._tokens .iteritems ():
88+ if token .valid_for_scope (url ):
89+ return token
8690 return http_interface .GenericToken ()
8791
8892 def remove_token (self , url ):
@@ -99,9 +103,8 @@ def remove_token(self, url):
99103 if url in self ._tokens :
100104 del self ._tokens [url ]
101105 return True
102- else :
103- for scope in self ._tokens :
104- if url .startswith (scope ):
105- del self ._tokens [scope ]
106- return True
106+ for scope , token in self ._tokens .iteritems ():
107+ if token .valid_for_scope (url ):
108+ del self ._tokens [scope ]
109+ return True
107110 return False
0 commit comments