Skip to content

Session injection supported #378

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 1 commit into from
Oct 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

from datetime import datetime, timedelta
from urllib.parse import urlparse
from msrest.authentication import BasicTokenAuthentication, Authentication
import requests

from msrest.authentication import Authentication
from .constants import Constants

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

def signed_session(self) -> requests.Session: # pylint: disable=arguments-differ
# pylint: disable=arguments-differ
def signed_session(self, session: requests.Session = None) -> requests.Session:
"""
Gets the signed session.
:returns: Signed requests.Session object
"""
auth_token = self.get_access_token()

basic_authentication = BasicTokenAuthentication({"access_token": auth_token})
session = basic_authentication.signed_session()
if not session:
session = requests.Session()

# If there is no microsoft_app_id and no self.microsoft_app_password, then there shouldn't
# be an "Authorization" header on the outgoing activity.
if not self.microsoft_app_id and not self.microsoft_app_password:
del session.headers["Authorization"]
session.headers.pop("Authorization", None)

elif not session.headers.get("Authorization"):
auth_token = self.get_access_token()
header = "{} {}".format("Bearer", auth_token)
session.headers["Authorization"] = header

return session

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