Skip to content

Commit 6d847c5

Browse files
authored
Session injection supported (#378)
1 parent e5f445b commit 6d847c5

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

libraries/botframework-connector/botframework/connector/auth/microsoft_app_credentials.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
from datetime import datetime, timedelta
55
from urllib.parse import urlparse
6-
from msrest.authentication import BasicTokenAuthentication, Authentication
76
import requests
7+
8+
from msrest.authentication import Authentication
89
from .constants import Constants
910

1011
# TODO: Decide to move this to Constants or viceversa (when porting OAuth)
@@ -82,20 +83,25 @@ def __init__(self, app_id: str, password: str, channel_auth_tenant: str = None):
8283
self.oauth_scope = AUTH_SETTINGS["refreshScope"]
8384
self.token_cache_key = app_id + "-cache"
8485

85-
def signed_session(self) -> requests.Session: # pylint: disable=arguments-differ
86+
# pylint: disable=arguments-differ
87+
def signed_session(self, session: requests.Session = None) -> requests.Session:
8688
"""
8789
Gets the signed session.
8890
:returns: Signed requests.Session object
8991
"""
90-
auth_token = self.get_access_token()
91-
92-
basic_authentication = BasicTokenAuthentication({"access_token": auth_token})
93-
session = basic_authentication.signed_session()
92+
if not session:
93+
session = requests.Session()
9494

9595
# If there is no microsoft_app_id and no self.microsoft_app_password, then there shouldn't
9696
# be an "Authorization" header on the outgoing activity.
9797
if not self.microsoft_app_id and not self.microsoft_app_password:
98-
del session.headers["Authorization"]
98+
session.headers.pop("Authorization", None)
99+
100+
elif not session.headers.get("Authorization"):
101+
auth_token = self.get_access_token()
102+
header = "{} {}".format("Bearer", auth_token)
103+
session.headers["Authorization"] = header
104+
99105
return session
100106

101107
def get_access_token(self, force_refresh: bool = False) -> str:

0 commit comments

Comments
 (0)