Skip to content

Commit 6fbce8a

Browse files
committed
Refactor to have scopes passed through an auth provider instead of GraphSession. Moves AuthorizationHandler and TokenCredentialAuthProvider to the same module
1 parent f823577 commit 6fbce8a

File tree

6 files changed

+14
-35
lines changed

6 files changed

+14
-35
lines changed

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from msgraphcore.middleware._middleware import MiddlewarePipeline, BaseMiddleware
88
from msgraphcore.middleware.options.auth_middleware_options import AuthMiddlewareOptions
99
from msgraphcore.middleware._base_auth import AuthProviderBase
10-
from msgraphcore.middleware.authorization_handler import AuthorizationHandler
10+
from msgraphcore.middleware.authorization import AuthorizationHandler
1111

1212

1313
class GraphSession(Session):

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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

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

2727
def _get_middleware_options(self, request):
2828
return request.middleware_control.get(AUTH_MIDDLEWARE_OPTIONS)
29+
30+
31+
class TokenCredentialAuthProvider(AuthProviderBase):
32+
def __init__(self, scopes: str, credential: TokenCredential):
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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
from unittest import TestCase
33

44
from msgraphcore.graph_session import GraphSession
5-
6-
from msgraphcore.middleware.authorization_provider import AuthProviderBase
5+
from msgraphcore.middleware.authorization import AuthProviderBase
76

87

98
class MiddlewarePipelineTest(TestCase):

0 commit comments

Comments
 (0)