Skip to content

Commit

Permalink
fix: fixes #174, remove hardcoded URL for creating ZPA sessions
Browse files Browse the repository at this point in the history
refactor: move url_base instance attribute from private to public
  • Loading branch information
mitchos committed Feb 20, 2023
1 parent 20671b9 commit e74bf07
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 6 additions & 6 deletions pyzscaler/zpa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ def _build_session(self, **kwargs) -> None:

# Configure URL base for this API session
if self._override_url:
self._url_base = self._override_url
self.url_base = self._override_url
elif not self._cloud or self._cloud == "production":
self._url_base = "https://config.private.zscaler.com"
self.url_base = "https://config.private.zscaler.com"
elif self._cloud == "beta":
self._url_base = "https://config.zpabeta.net"
self.url_base = "https://config.zpabeta.net"
else:
raise ValueError("Missing Attribute: You must specify either cloud or override_url")

# Configure URLs for this API session
self._url = f"{self._url_base}/mgmtconfig/v1/admin/customers/{self._customer_id}"
self.user_config_url = f"{self._url_base}/userconfig/v1/customers/{self._customer_id}"
self._url = f"{self.url_base}/mgmtconfig/v1/admin/customers/{self._customer_id}"
self.user_config_url = f"{self.url_base}/userconfig/v1/customers/{self._customer_id}"
# The v2 URL supports additional API endpoints
self.v2_url = f"{self._url_base}/mgmtconfig/v2/admin/customers/{self._customer_id}"
self.v2_url = f"{self.url_base}/mgmtconfig/v2/admin/customers/{self._customer_id}"

self._auth_token = self.session.create_token(client_id=self._client_id, client_secret=self._client_secret)
return self._session.headers.update({"Authorization": f"Bearer {self._auth_token}"})
Expand Down
8 changes: 7 additions & 1 deletion pyzscaler/zpa/session.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from restfly import APISession
from restfly.endpoint import APIEndpoint


class AuthenticatedSessionAPI(APIEndpoint):
def __init__(self, api: APISession):
super().__init__(api)

self.url_base = api.url_base

def create_token(self, client_id: str, client_secret: str):
"""
Creates a ZPA authentication token.
Expand All @@ -24,4 +30,4 @@ def create_token(self, client_id: str, client_secret: str):
headers = {
"Content-Type": "application/x-www-form-urlencoded",
}
return self._post("https://config.private.zscaler.com/signin", headers=headers, data=payload).access_token
return self._post(f"{self.url_base}/signin", headers=headers, data=payload).access_token

0 comments on commit e74bf07

Please sign in to comment.