Skip to content
This repository was archived by the owner on Jun 29, 2019. It is now read-only.

Commit 6cddb2c

Browse files
committed
Setting up docs
1 parent 8578d84 commit 6cddb2c

File tree

10 files changed

+118
-63
lines changed

10 files changed

+118
-63
lines changed

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Contents:
66

77
.. toctree::
8-
:maxdepth: 2
8+
:maxdepth: 3
99

1010
grant.rst
1111
store.rst

docs/oauth2.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
``oauth2`` --- Controller classes
22
========================================
33

4-
.. autoclass:: oauth2.AuthorizationController
4+
.. autoclass:: oauth2.Provider
55

6-
.. automethod:: oauth2.AuthorizationController.add_grant
7-
.. automethod:: oauth2.AuthorizationController.dispatch
6+
.. automethod:: oauth2.Provider.add_grant
7+
.. automethod:: oauth2.Provider.dispatch

docs/store.rst

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
Data types
77
----------
88

9-
.. autoclass:: oauth2.AccessToken
9+
.. autoclass:: oauth2.datatype.AccessToken
1010

11-
.. autoclass:: oauth2.AuthorizationCode
11+
.. autoclass:: oauth2.datatype.AuthorizationCode
1212

13-
.. autoclass:: oauth2.Client
13+
.. autoclass:: oauth2.datatype.Client
1414

1515
Base classes
1616
------------
@@ -24,17 +24,12 @@ Base classes
2424
.. autoclass:: ClientStore
2525
:members:
2626

27-
Concrete classes
28-
----------------
27+
Implementations
28+
---------------
2929

30-
.. autoclass:: LocalClientStore
31-
:show-inheritance:
32-
:members:
33-
34-
.. autoclass:: LocalTokenStore
35-
:show-inheritance:
36-
:members:
30+
.. toctree::
31+
:maxdepth: 2
3732

38-
.. autoclass:: MemcacheTokenStore
39-
:show-inheritance:
40-
:members:
33+
store/memcache.rst
34+
store/memory.rst
35+
store/mongodb.rst

docs/store/memcache.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
``oauth2.store.memcache`` --- Memcache store adapters
2+
=====================================================
3+
4+
.. automodule:: oauth2.store.memcache
5+
6+
.. autoclass:: MemcacheTokenStore

docs/store/memory.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
``oauth2.store.memory`` --- In-memory store adapters
2+
=====================================================
3+
4+
.. automodule:: oauth2.store.memory
5+
6+
.. autoclass:: MemoryClientStore
7+
:members:
8+
9+
.. autoclass:: MemoryTokenStore
10+
:members:

docs/store/mongodb.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
``oauth2.store.mongodb`` --- Mongodb store adapters
2+
=====================================================
3+
4+
.. automodule:: oauth2.store.mongodb
5+
6+
.. autoclass:: MongodbStore
7+
8+
.. autoclass:: MongodbAccessTokenStore
9+
10+
.. autoclass:: MongodbAuthCodeStore
11+
12+
.. autoclass:: MongodbClientStore

oauth2/store/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def save_token(self, access_token):
1616
"""
1717
Stores an access token and additional data.
1818
19-
:param access_token: An instance of :class:`oauth2.AccessToken`.
19+
:param access_token: An instance of :class:`oauth2.datatype.AccessToken`.
2020
2121
"""
2222
raise NotImplementedError
@@ -40,7 +40,7 @@ def fetch_by_code(self, code):
4040
Returns an AuthorizationCode fetched from a storage.
4141
4242
:param code: The authorization code.
43-
:return: An instance of :class:`oauth2.AuthorizationCode`.
43+
:return: An instance of :class:`oauth2.datatype.AuthorizationCode`.
4444
:raises: :class:`AuthCodeNotFound` if no data could be retrieved for
4545
given code.
4646

oauth2/store/memory.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
"""
2+
Read or write data from or to local memory.
3+
4+
Though not very valuable in a production setup, these store adapters are great
5+
for testing purposes.
6+
"""
7+
18
from oauth2.store import AccessTokenStore, AuthCodeStore, ClientStore
29
from oauth2.error import AccessTokenNotFound, AuthCodeNotFound,\
310
ClientNotFoundError
@@ -57,7 +64,7 @@ def fetch_by_code(self, code):
5764
Returns an AuthorizationCode.
5865
5966
:param code: The authorization code.
60-
:return: An instance of :class:`oauth2.AuthorizationCode`.
67+
:return: An instance of :class:`oauth2.datatype.AuthorizationCode`.
6168
:raises: :class:`AuthCodeNotFound` if no data could be retrieved for
6269
given code.
6370
@@ -72,7 +79,7 @@ def save_code(self, authorization_code):
7279
Stores the data belonging to an authorization code token.
7380
7481
:param authorization_code: An instance of
75-
:class:`oauth2.AuthorizationCode`.
82+
:class:`oauth2.datatype.AuthorizationCode`.
7683
7784
"""
7885
self.auth_codes[authorization_code.code] = authorization_code
@@ -83,7 +90,7 @@ def save_token(self, access_token):
8390
"""
8491
Stores an access token and additional data in memory.
8592
86-
:param client_id: An instance of :class:`oauth2.AccessToken`.
93+
:param access_token: An instance of :class:`oauth2.datatype.AccessToken`.
8794
"""
8895
self.access_tokens[access_token.token] = access_token
8996

@@ -98,7 +105,7 @@ def fetch_by_refresh_token(self, refresh_token):
98105
99106
:param refresh_token: The refresh token that was assigned to an
100107
``AccessToken``.
101-
:return: The :class:`oauth2.AccessToken`.
108+
:return: The :class:`oauth2.datatype.AccessToken`.
102109
:raises: :class:`oauth2.error.AccessTokenNotFound`
103110
"""
104111
if refresh_token not in self.refresh_tokens:
@@ -115,7 +122,7 @@ def fetch_by_token(self, token):
115122
read again.
116123
117124
:param token: A access token code.
118-
:return: An instance of :class:`oauth2.AccessToken`.
125+
:return: An instance of :class:`oauth2.datatype.AccessToken`.
119126
"""
120127
if token not in self.access_tokens:
121128
raise AccessTokenNotFound

oauth2/store/mongodb.py

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
1+
"""
2+
Store adapters to read/write data to from/to mongodb using pymongo.
3+
"""
4+
15
from oauth2.store import AccessTokenStore, AuthCodeStore, ClientStore
26
from oauth2.datatype import AccessToken, AuthorizationCode, Client
37
from oauth2.error import AccessTokenNotFound, AuthCodeNotFound,\
48
ClientNotFoundError
59

610
class MongodbStore(object):
7-
def __init__(self, collection, db):
11+
"""
12+
Base class extended by all concrete store adapters.
13+
"""
14+
15+
def __init__(self, collection):
816
self.collection = collection
9-
self.db = db
1017

1118
class MongodbAccessTokenStore(AccessTokenStore, MongodbStore):
19+
"""
20+
Create a new instance like this::
21+
22+
from pymongo import MongoClient
23+
24+
client = MongoClient('localhost', 27017)
25+
26+
db = client.test_database
27+
28+
access_token_store = MongodbAccessTokenStore(collection=db["access_tokens"])
29+
30+
"""
31+
1232
def fetch_by_refresh_token(self, refresh_token):
13-
data = self.db[self.collection].\
14-
find_one({"refresh_token": refresh_token})
33+
34+
data = self.collection.find_one({"refresh_token": refresh_token})
1535

1636
if data is None:
1737
raise AccessTokenNotFound
@@ -24,7 +44,7 @@ def fetch_by_refresh_token(self, refresh_token):
2444
scopes=data["scopes"])
2545

2646
def save_token(self, access_token):
27-
self.db[self.collection].insert({
47+
self.collection.insert({
2848
"client_id": access_token.client_id,
2949
"grant_type": access_token.grant_type,
3050
"token": access_token.token,
@@ -36,8 +56,21 @@ def save_token(self, access_token):
3656
return True
3757

3858
class MongodbAuthCodeStore(AuthCodeStore, MongodbStore):
59+
"""
60+
Create a new instance like this::
61+
62+
from pymongo import MongoClient
63+
64+
client = MongoClient('localhost', 27017)
65+
66+
db = client.test_database
67+
68+
access_token_store = MongodbAuthCodeStore(collection=db["auth_codes"])
69+
70+
"""
71+
3972
def fetch_by_code(self, code):
40-
code_data = self.db[self.collection].find_one({"code": code})
73+
code_data = self.collection.find_one({"code": code})
4174

4275
if code_data is None:
4376
raise AuthCodeNotFound
@@ -50,7 +83,7 @@ def fetch_by_code(self, code):
5083
data=code_data["data"])
5184

5285
def save_code(self, authorization_code):
53-
self.db[self.collection].insert({
86+
self.collection.insert({
5487
"client_id": authorization_code.client_id,
5588
"code": authorization_code.code,
5689
"expires_at": authorization_code.expires_at,
@@ -61,8 +94,21 @@ def save_code(self, authorization_code):
6194
return True
6295

6396
class MongodbClientStore(ClientStore, MongodbStore):
97+
"""
98+
Create a new instance like this::
99+
100+
from pymongo import MongoClient
101+
102+
client = MongoClient('localhost', 27017)
103+
104+
db = client.test_database
105+
106+
access_token_store = MongodbClientStore(collection=db["clients"])
107+
108+
"""
109+
64110
def fetch_by_client_id(self, client_id):
65-
client_data = self.db[self.collection].find_one(
111+
client_data = self.collection.find_one(
66112
{"identifier": client_id})
67113

68114
if client_data is None:

oauth2/test/store/test_mongodb.py

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from oauth2.datatype import AccessToken, AuthorizationCode, Client
66
from oauth2.error import AccessTokenNotFound, AuthCodeNotFound,\
77
ClientNotFoundError
8-
from oauth2.store import ClientStore
98

109
class MongodbAccessTokenStoreTestCase(unittest.TestCase):
1110
def setUp(self):
@@ -25,10 +24,7 @@ def test_fetch_by_refresh_token(self):
2524
collection_mock = Mock(spec=["find_one"])
2625
collection_mock.find_one.return_value = self.access_token_data
2726

28-
db_mock = {"access_tokens": collection_mock}
29-
30-
store = MongodbAccessTokenStore(collection="access_tokens",
31-
db=db_mock)
27+
store = MongodbAccessTokenStore(collection=collection_mock)
3228
token = store.fetch_by_refresh_token(refresh_token=refresh_token)
3329

3430
collection_mock.find_one.assert_called_with(
@@ -40,10 +36,7 @@ def test_fetch_by_refresh_token_no_data(self):
4036
collection_mock = Mock(spec=["find_one"])
4137
collection_mock.find_one.return_value = None
4238

43-
db_mock = {"access_tokens": collection_mock}
44-
45-
store = MongodbAccessTokenStore(collection="access_tokens",
46-
db=db_mock)
39+
store = MongodbAccessTokenStore(collection=collection_mock)
4740

4841
with self.assertRaises(AccessTokenNotFound):
4942
store.fetch_by_refresh_token(refresh_token="abcd")
@@ -53,10 +46,7 @@ def test_save_token(self):
5346

5447
collection_mock = Mock(spec=["insert"])
5548

56-
db_mock = {"access_tokens": collection_mock}
57-
58-
store = MongodbAccessTokenStore(collection="access_tokens",
59-
db=db_mock)
49+
store = MongodbAccessTokenStore(collection=collection_mock)
6050
store.save_token(access_token)
6151

6252
collection_mock.insert.assert_called_with(self.access_token_data)
@@ -69,19 +59,14 @@ def setUp(self):
6959

7060
self.collection_mock = Mock(spec=["find_one", "insert", "remove"])
7161

72-
self.collection_name = "auth_codes"
73-
74-
self.db_mock = {self.collection_name: self.collection_mock}
75-
7662
def test_fetch_by_code(self):
7763
code = "abcd"
7864

7965
self.collection_mock.find_one.return_value = self.auth_code_data
8066

8167
self.auth_code_data["code"] = "abcd"
8268

83-
store = MongodbAuthCodeStore(collection=self.collection_name,
84-
db=self.db_mock)
69+
store = MongodbAuthCodeStore(collection=self.collection_mock)
8570
auth_code = store.fetch_by_code(code=code)
8671

8772
self.collection_mock.find_one.assert_called_with({"code": "abcd"})
@@ -91,8 +76,7 @@ def test_fetch_by_code(self):
9176
def test_fetch_by_code_no_data(self):
9277
self.collection_mock.find_one.return_value = None
9378

94-
store = MongodbAuthCodeStore(collection=self.collection_name,
95-
db=self.db_mock)
79+
store = MongodbAuthCodeStore(collection=self.collection_mock)
9680

9781
with self.assertRaises(AuthCodeNotFound):
9882
store.fetch_by_code(code="abcd")
@@ -102,8 +86,7 @@ def test_save_code(self):
10286

10387
auth_code = AuthorizationCode(**self.auth_code_data)
10488

105-
store = MongodbAuthCodeStore(collection=self.collection_name,
106-
db=self.db_mock)
89+
store = MongodbAuthCodeStore(collection=self.collection_mock)
10790
store.save_code(auth_code)
10891

10992
self.collection_mock.insert.assert_called_with(self.auth_code_data)
@@ -116,9 +99,7 @@ def test_fetch_by_client_id(self):
11699
collection_mock = Mock(spec=["find_one"])
117100
collection_mock.find_one.return_value = client_data
118101

119-
db_mock = {"clients": collection_mock}
120-
121-
store = MongodbClientStore(collection="clients", db=db_mock)
102+
store = MongodbClientStore(collection=collection_mock)
122103
client = store.fetch_by_client_id(client_id=client_data["identifier"])
123104

124105
collection_mock.find_one.assert_called_with({
@@ -130,9 +111,7 @@ def test_fetch_by_client_id_no_data(self):
130111
collection_mock = Mock(spec=["find_one"])
131112
collection_mock.find_one.return_value = None
132113

133-
db_mock = {"clients": collection_mock}
134-
135-
store = MongodbClientStore(collection="clients", db=db_mock)
114+
store = MongodbClientStore(collection=collection_mock)
136115

137116
with self.assertRaises(ClientNotFoundError):
138117
store.fetch_by_client_id(client_id="testclient")

0 commit comments

Comments
 (0)