Skip to content

Commit 9bbbc10

Browse files
author
api.jscudder
committed
The valid scopes for a token are now stored within the token object, so the token store will use the token's scope to determine URL matches.
1 parent 06e7856 commit 9bbbc10

2 files changed

Lines changed: 39 additions & 24 deletions

File tree

src/atom/token_store.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/atom_tests/token_store_test.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
import unittest
2222
import atom.token_store
2323
import atom.http_interface
24+
import atom.service
2425

2526
class TokenStoreTest(unittest.TestCase):
2627

2728
def setUp(self):
28-
self.token = 'aaa1'
29+
self.token = atom.service.BasicAuthToken('aaa1', scopes=[
30+
'http://example.com/', 'http://example.org'])
2931
self.tokens = atom.token_store.TokenStore()
30-
self.tokens.add_token(self.token, ['http://example.com/',
31-
'http://example.org'])
32+
self.tokens.add_token(self.token)
3233

3334
def testAddAndFindTokens(self):
3435
self.assert_(self.tokens.find_token('http://example.com/') == self.token)
@@ -40,6 +41,17 @@ def testAddAndFindTokens(self):
4041
self.assert_(isinstance(self.tokens.find_token('example.com/'),
4142
atom.http_interface.GenericToken))
4243

44+
def testFindTokenUsingMultipleUrls(self):
45+
self.assert_(self.tokens.find_token(
46+
'http://example.com/') == self.token)
47+
self.assert_(self.tokens.find_token(
48+
'http://example.org/bar') == self.token)
49+
self.assert_(isinstance(self.tokens.find_token(''),
50+
atom.http_interface.GenericToken))
51+
self.assert_(isinstance(self.tokens.find_token(
52+
'http://example.net/'),
53+
atom.http_interface.GenericToken))
54+
4355
def testRemoveTokens(self):
4456
self.assert_(self.tokens.remove_token('http://example.com/') == True)
4557
self.assert_(self.tokens.find_token('http://example.org/') == self.token)

0 commit comments

Comments
 (0)