forked from microsoftgraph/msgraph-sdk-python-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthorization.py
More file actions
37 lines (30 loc) · 1.38 KB
/
authorization.py
File metadata and controls
37 lines (30 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
from .._enums import FeatureUsageFlag
from .abc_token_credential import TokenCredential
from .middleware import BaseMiddleware
class AuthorizationHandler(BaseMiddleware):
def __init__(self, credential: TokenCredential, **kwargs):
super().__init__()
self.credential = credential
self.scopes = kwargs.get("scopes", ['.default'])
self.retry_count = 0
def send(self, request, **kwargs):
context = request.context
request.headers.update(
{'Authorization': 'Bearer {}'.format(self._get_access_token(context))}
)
context.set_feature_usage = FeatureUsageFlag.AUTH_HANDLER_ENABLED
response = super().send(request, **kwargs)
# 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_access_token(self, context):
return self.credential.get_token(*self.get_scopes(context))[0]
def get_scopes(self, context):
# Checks if there are any options for this middleware
return context.middleware_control.get('scopes', self.scopes)