-
Notifications
You must be signed in to change notification settings - Fork 45
Task/refactor graph session #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
527e29a
Refactor GraphSession to take scopes and credential through the const…
jobala d322378
Update samples
jobala cc2dd9b
Update README.md
jobala 790b020
Update integration tests to use new GraphSession API
jobala 4f2a1fd
Update unit tests
jobala 612bc15
Fix lint errors
jobala 4bc0cb8
Fix lint errors
jobala 519bb08
Add AuthorizationHandler tests
jobala 8e738e3
Update test
jobala e0cb6e7
Update test
jobala 2df2546
Update README.md
jobala 22f90de
Merge branch 'dev' into task/refactor-graph-session
jobala db67357
Remove unused imports from README.md
jobala a199bda
Merge remote-tracking branch 'origin/task/refactor-graph-session' int…
jobala File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| """msgraph-core""" | ||
|
|
||
| from msgraphcore.graph_session import GraphSession | ||
| from .middleware.authorization import AuthProviderBase, TokenCredentialAuthProvider | ||
| from .constants import SDK_VERSION | ||
|
|
||
| __version__ = SDK_VERSION |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,34 @@ | ||
| from ._base_auth import AuthProviderBase, TokenCredential | ||
| from .abc_token_credential import TokenCredential | ||
| from ..constants import AUTH_MIDDLEWARE_OPTIONS | ||
| from ._middleware import BaseMiddleware | ||
| from .middleware import BaseMiddleware | ||
| from .options.middleware_control import middleware_control | ||
|
|
||
|
|
||
| class AuthorizationHandler(BaseMiddleware): | ||
| def __init__(self, auth_provider: AuthProviderBase): | ||
| def __init__(self, credential: TokenCredential, scopes: [str]): | ||
| super().__init__() | ||
| self.auth_provider = auth_provider | ||
| self.credential = credential | ||
| self.scopes = scopes | ||
| self.retry_count = 0 | ||
|
|
||
| def send(self, request, **kwargs): | ||
| # Checks if there are any options for this middleware | ||
| options = self._get_middleware_options() | ||
| # If there is, get the scopes from the options | ||
| if options: | ||
| self.auth_provider.scopes = options.scopes | ||
|
|
||
| token = self.auth_provider.get_access_token() | ||
| request.headers.update({'Authorization': 'Bearer {}'.format(token)}) | ||
| request.headers.update({'Authorization': 'Bearer {}'.format(self._get_access_token())}) | ||
| response = super().send(request, **kwargs) | ||
|
|
||
| # Token might have expired just before transmission, retry the request | ||
| # Token might have expired just before transmission, retry the request one more time | ||
| if response.status_code == 401 and self.retry_count < 2: | ||
| self.retry_count += 1 | ||
| return self.send(request, **kwargs) | ||
|
|
||
| return response | ||
|
|
||
| def _get_middleware_options(self): | ||
| return middleware_control.get(AUTH_MIDDLEWARE_OPTIONS) | ||
| def _get_access_token(self): | ||
| return self.credential.get_token(*self.get_scopes())[0] | ||
|
|
||
|
|
||
| class TokenCredentialAuthProvider(AuthProviderBase): | ||
| def __init__(self, credential: TokenCredential, scopes: [str] = ['.default']): | ||
| self.credential = credential | ||
| self.scopes = scopes | ||
|
|
||
| def get_access_token(self): | ||
| return self.credential.get_token(*self.scopes)[0] | ||
| def get_scopes(self): | ||
| # Checks if there are any options for this middleware | ||
| auth_options_present = middleware_control.get(AUTH_MIDDLEWARE_OPTIONS) | ||
| # If there is, get the scopes from the options | ||
| if auth_options_present: | ||
| return auth_options_present.scopes | ||
| else: | ||
| return self.scopes |
2 changes: 1 addition & 1 deletion
2
msgraphcore/middleware/_middleware.py → msgraphcore/middleware/middleware.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import unittest | ||
|
|
||
| from msgraphcore.constants import AUTH_MIDDLEWARE_OPTIONS | ||
| from msgraphcore.middleware.authorization import AuthorizationHandler | ||
| from msgraphcore.middleware.options.middleware_control import middleware_control | ||
| from msgraphcore.middleware.options.auth_middleware_options import AuthMiddlewareOptions | ||
|
|
||
|
|
||
| class TestAuthorizationHandler(unittest.TestCase): | ||
| def test_auth_options_override_default_scopes(self): | ||
| auth_option = ['email.read'] | ||
| default_scopes = ['.default'] | ||
|
|
||
| middleware_control.set(AUTH_MIDDLEWARE_OPTIONS, AuthMiddlewareOptions(auth_option)) | ||
| auth_handler = AuthorizationHandler(None, default_scopes) | ||
|
|
||
| auth_handler_scopes = auth_handler.get_scopes() | ||
| self.assertEqual(auth_option, auth_handler_scopes) | ||
|
|
||
| def test_auth_handler_get_scopes_does_not_overwrite_default_scopes(self): | ||
| auth_option = ['email.read'] | ||
| default_scopes = ['.default'] | ||
|
|
||
| middleware_control.set(AUTH_MIDDLEWARE_OPTIONS, AuthMiddlewareOptions(auth_option)) | ||
| auth_handler = AuthorizationHandler(None, default_scopes) | ||
| auth_handler.get_scopes() | ||
|
|
||
| self.assertEqual(auth_handler.scopes, default_scopes) | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a couple of the imports are no longer needed above on line 17.