Skip to content
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

feat: add proxy support to OAuth sessions #89

Merged
merged 3 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pysnc"
version = "1.1.6a0"
version = "1.1.7a0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

version is auto managed - don't need to manually bump

description = "Python SNC (REST) API"
authors = ["Matthew Gill <[email protected]>"]
license = "MIT"
Expand Down
4 changes: 2 additions & 2 deletions pysnc/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, username, password, client_id, client_secret):
def authorization_url(self, authorization_base_url):
return f"{authorization_base_url}/oauth_token.do"

def authenticate(self, instance: str) -> requests.Session:
def authenticate(self, instance: str, proxies: dict) -> requests.Session:
"""
Designed to be called by ServiceNowClient - internal method.
"""
Expand All @@ -49,7 +49,7 @@ def authenticate(self, instance: str) -> requests.Session:
auto_refresh_kwargs=dict(client_id=self.client_id, client_secret=self.__secret))
oauth.fetch_token(token_url=self.authorization_url(instance),
username=self.__username, password=self.__password, client_id=self.client_id,
client_secret=self.__secret)
client_secret=self.__secret, proxies=proxies)
self.__password = None # no longer need this.
return oauth
except ImportError:
Expand Down
20 changes: 12 additions & 8 deletions pysnc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ def __init__(self, instance, auth, proxy=None, verify=None, cert=None, auto_retr
self._log = logging.getLogger(__name__)
self.__instance = get_instance(instance)

if proxy:
if type(proxy) != dict:
proxies = dict(http=proxy, https=proxy)
else:
proxies = proxy
self.__proxies = proxies
if verify is None:
verify = True # default to verify with proxy

if auth is not None and cert is not None:
raise AuthenticationException('Cannot specify both auth and cert')
elif isinstance(auth, (list, tuple)) and len(auth) == 2:
Expand All @@ -48,20 +57,15 @@ def __init__(self, instance, auth, proxy=None, verify=None, cert=None, auto_retr
# maybe we've got an oauth token? Let this be permissive
self.__session = auth
elif isinstance(auth, ServiceNowFlow):
self.__session = auth.authenticate(self.__instance)
self.__session = auth.authenticate(self.__instance, self.__proxies)
elif cert is not None:
self.__session.cert = cert
else:
raise AuthenticationException('No valid authentication method provided')

if proxy:
if type(proxy) != dict:
proxies = dict(http=proxy, https=proxy)
else:
proxies = proxy
self.__session.proxies = proxies
if verify is None:
verify = True # default to verify with proxy
self.__session.proxies = self.__proxies

if verify is not None:
self.__session.verify = verify

Expand Down