Skip to content

Commit a91a347

Browse files
authored
Merge pull request #40 from microsoftgraph/feat/graph-session-scopes
Feat/graph session scopes
2 parents 2a716b3 + 7491c15 commit a91a347

File tree

9 files changed

+44
-73
lines changed

9 files changed

+44
-73
lines changed

README.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,14 @@ device_credential = DeviceCodeCredential(
3131

3232
```
3333

34-
Create AuthorizationProvider, AuthorizationHandler and list of middleware
34+
Create an authorization provider object and a list of scopes
3535
```python
36-
auth_provider = TokenCredentialAuthProvider(device_credential)
37-
options = AuthMiddlewareOptions(['mail.send', 'user.read'])
38-
auth_handler = AuthorizationHandler(auth_provider, auth_provider_options=options)
39-
40-
middleware = [
41-
auth_handler
42-
]
36+
scopes = ['mail.send', 'user.read']
37+
auth_provider = TokenCredentialAuthProvider(scopes, device_credential)
4338
```
4439

4540
```python
46-
graph_session = GraphSession(middleware=middleware)
41+
graph_session = GraphSession(auth_provider)
4742
result = graph_session.get('/me')
4843
print(result.json())
4944
```

msgraphcore/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
"""msgraph-core"""
22

33
from msgraphcore.graph_session import GraphSession
4-
from .middleware.authorization_handler import AuthorizationHandler
5-
from .middleware.authorization_provider import AuthProviderBase, TokenCredentialAuthProvider
6-
from .middleware.options.auth_middleware_options import AuthMiddlewareOptions
4+
from .middleware.authorization import AuthProviderBase, TokenCredentialAuthProvider
75
from .constants import SDK_VERSION
86

97
__version__ = SDK_VERSION

msgraphcore/graph_session.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
"""
2-
Creates a session object
2+
Graph Session
33
"""
44
from requests import Session, Request, Response
55

66
from msgraphcore.constants import BASE_URL, SDK_VERSION
77
from msgraphcore.middleware._middleware import MiddlewarePipeline, BaseMiddleware
8+
from msgraphcore.middleware._base_auth import AuthProviderBase
9+
from msgraphcore.middleware.authorization import AuthorizationHandler
810

911

1012
class GraphSession(Session):
1113
"""
1214
Extends session object with graph functionality
1315
"""
14-
def __init__(self, **kwargs):
16+
def __init__(self, auth_provider: AuthProviderBase, middleware: list = []):
1517
super().__init__()
1618
self.headers.update({'sdkVersion': 'graph-python-' + SDK_VERSION})
1719
self._base_url = BASE_URL
18-
middleware = kwargs.get('middleware')
20+
21+
auth_handler = AuthorizationHandler(auth_provider)
22+
23+
middleware.insert(0, auth_handler)
1924
self._register(middleware)
2025

2126
def get(self, url: str, **kwargs) -> Response:
@@ -55,12 +60,7 @@ def _prepare_and_send_request(self, method: str = '', url: str = '', **kwargs) -
5560
prepared_request = self.prepare_request(request)
5661

5762
if list_of_scopes is not None:
58-
# prepare scopes middleware option
59-
graph_scopes = BASE_URL + '?scopes='
60-
for scope in list_of_scopes:
61-
graph_scopes += scope + '%20'
62-
6363
# Append middleware options to the request object, will be used by MiddlewareController
64-
prepared_request.scopes = graph_scopes
64+
prepared_request.scopes = list_of_scopes
6565

6666
return self.send(prepared_request, **kwargs)

msgraphcore/middleware/authorization_handler.py renamed to msgraphcore/middleware/authorization.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
from ._base_auth import AuthProviderBase
1+
from ._base_auth import AuthProviderBase, TokenCredential
22
from ..constants import AUTH_MIDDLEWARE_OPTIONS
33
from ._middleware import BaseMiddleware
44

55

66
class AuthorizationHandler(BaseMiddleware):
7-
def __init__(self, auth_provider: AuthProviderBase, auth_provider_options=None):
7+
def __init__(self, auth_provider: AuthProviderBase):
88
super().__init__()
99
self.auth_provider = auth_provider
10-
self.auth_provider_options = auth_provider_options
1110
self.retry_count = 0
1211

1312
def send(self, request, **kwargs):
@@ -27,3 +26,12 @@ def send(self, request, **kwargs):
2726

2827
def _get_middleware_options(self, request):
2928
return request.middleware_control.get(AUTH_MIDDLEWARE_OPTIONS)
29+
30+
31+
class TokenCredentialAuthProvider(AuthProviderBase):
32+
def __init__(self, credential: TokenCredential, scopes: [str] = ['.default']):
33+
self.credential = credential
34+
self.scopes = scopes
35+
36+
def get_access_token(self):
37+
return self.credential.get_token(*self.scopes)[0]

msgraphcore/middleware/authorization_provider.py

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,5 @@
1-
from msgraphcore.constants import BASE_URL
2-
31

42
class AuthMiddlewareOptions:
5-
def __init__(self, scopes=[]):
3+
def __init__(self, scopes: str):
64
self.scopes = scopes
75

8-
@property
9-
def scopes(self):
10-
return self._scopes
11-
12-
@scopes.setter
13-
def scopes(self, list_of_scopes):
14-
if type(list_of_scopes) == list:
15-
graph_scopes = BASE_URL + '?scopes='
16-
17-
for scope in list_of_scopes:
18-
graph_scopes += scope + '%20'
19-
20-
self._scopes = graph_scopes
21-
elif type(list_of_scopes) == str:
22-
self._scopes = list_of_scopes

tests/integration/test_middleware_pipeline.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
from unittest import TestCase
33

44
from msgraphcore.graph_session import GraphSession
5-
6-
from msgraphcore.middleware.authorization_provider import AuthProviderBase
7-
from msgraphcore.middleware.authorization_handler import AuthorizationHandler
8-
from msgraphcore.middleware.options.auth_middleware_options import AuthMiddlewareOptions
5+
from msgraphcore.middleware.authorization import AuthProviderBase
96

107

118
class MiddlewarePipelineTest(TestCase):
@@ -14,22 +11,17 @@ def setUp(self):
1411

1512
def test_middleware_pipeline(self):
1613
url = 'https://proxy.apisandbox.msdn.microsoft.com/svc?url=https://graph.microsoft.com/v1.0/me'
17-
18-
auth_provider = _CustomAuthProvider()
19-
options = AuthMiddlewareOptions(['user.read'])
20-
auth_handler = AuthorizationHandler(auth_provider, auth_provider_options=options)
21-
22-
middleware = [
23-
auth_handler
24-
]
25-
26-
graph_session = GraphSession(middleware=middleware)
14+
scopes = ['user.read']
15+
auth_provider = _CustomAuthProvider(scopes)
16+
graph_session = GraphSession(auth_provider)
2717
result = graph_session.get(url)
2818

2919
self.assertEqual(result.status_code, 200)
3020

3121

3222
class _CustomAuthProvider(AuthProviderBase):
23+
def __init__(self, scopes):
24+
pass
3325

3426
def get_access_token(self):
3527
return '{token:https://graph.microsoft.com/}'

tests/unit/test_graph_session.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
from msgraphcore.graph_session import GraphSession
88
from msgraphcore.constants import BASE_URL, SDK_VERSION
9+
from msgraphcore.middleware._base_auth import AuthProviderBase
910

1011

1112
class GraphSessionTest(TestCase):
1213
def setUp(self) -> None:
13-
self.requests = GraphSession()
14+
self.auth_provider = _CustomAuthProvider(['user.read'])
15+
self.requests = GraphSession(self.auth_provider)
1416

1517
def tearDown(self) -> None:
1618
self.requests = None
@@ -25,11 +27,7 @@ def test_has_sdk_version_header(self):
2527
self.assertEqual(self.requests.headers.get('sdkVersion'), 'graph-python-'+SDK_VERSION)
2628

2729
def test_initialized_with_middlewares(self):
28-
middlewares = [
29-
HTTPAdapter() # Middlewares inherit from the HTTPAdapter class
30-
]
31-
32-
graph_session = GraphSession(middlewares=middlewares)
30+
graph_session = GraphSession(self.auth_provider)
3331
mocked_middleware = graph_session.get_adapter('https://')
3432

3533
self.assertIsInstance(mocked_middleware, HTTPAdapter)
@@ -54,3 +52,10 @@ def test_does_not_build_graph_urls_for_full_urls(self):
5452

5553
self.assertEqual(other_url, request_url)
5654

55+
56+
class _CustomAuthProvider(AuthProviderBase):
57+
def __init__(self, scopes):
58+
pass
59+
60+
def get_access_token(self):
61+
return '{token:https://graph.microsoft.com/}'

tests/unit/test_middleware_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
class TestMiddlewareOptions(TestCase):
77
def test_multiple_scopes(self):
88
graph_scopes = 'https://graph.microsoft.com/v1.0?scopes=mail.read%20user.read%20'
9-
auth_options = AuthMiddlewareOptions(scopes=['mail.read', 'user.read'])
9+
auth_options = AuthMiddlewareOptions(graph_scopes)
1010
self.assertEqual(auth_options.scopes, graph_scopes)

0 commit comments

Comments
 (0)