Skip to content

Commit

Permalink
1.0.2 (2023-02-07)
Browse files Browse the repository at this point in the history
------------------
* [fix] ccsrftoken for fortios v7
  • Loading branch information
vprusakovs committed Feb 7, 2023
1 parent 6e668fa commit ec28cbb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
CHANGELOG
=========

1.0.2 (2023-02-07)
------------------
* [fix] ccsrftoken for fortios v7


1.0.1 (2022-11-01)
------------------
* [fix] py.typed
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ or install the package from github.com release

.. code:: bash
pip install https://github.com/vladimirs-git/fortigate-api/archive/refs/tags/1.0.1.tar.gz
pip install https://github.com/vladimirs-git/fortigate-api/archive/refs/tags/1.0.2.tar.gz
or install the package from github.com repository

Expand Down
20 changes: 13 additions & 7 deletions fortigate_api/fortigate.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,19 @@ def login(self) -> None:
verify=self.verify)
except Exception as ex:
raise self._hide_secret_ex(ex)
csrf_token_name = f"ccsrftoken_{self.port}"
for cookie in session.cookies:
if cookie.name == csrf_token_name and isinstance(cookie.value, str):
session.headers.update({"X-CSRFTOKEN": cookie.value[1:-1]})
break
else:
raise ValueError(f"invalid login credentials, absent cookie {csrf_token_name}")

cookie_name = "ccsrftoken"
cookies = [o for o in session.cookies if o.name == cookie_name]
if not cookies:
regex = cookie_name + r"_\d+$"
cookies = [o for o in session.cookies if re.match(regex, o.name)]
cookies = [o for o in cookies if isinstance(o.value, str)]
if not cookies:
raise ValueError("invalid login credentials, absent cookie ccsrftoken")
cookie = cookies[0]
token = cookie.value.strip("\"")
session.headers.update({"X-CSRFTOKEN": token})

response: Response = session.get(url=f"{self.url}/api/v2/cmdb/system/vdom")
response.raise_for_status()
self._session = session
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "fortigate_api"
version = "1.0.1"
version = "1.0.2"
authors = [
{ name="Vladimir Prusakov", email="[email protected]" },
]
Expand Down Expand Up @@ -30,7 +30,7 @@ classifiers = [
"Homepage" = "https://github.com/vladimirs-git/fortigate-api"
"Repository" = "https://github.com/vladimirs-git/fortigate-api"
"Bug Tracker" = "https://github.com/vladimirs-git/fortigate-api/issues"
"Download URL" = "https://github.com/vladimirs-git/fortigate-api/archive/refs/tags/1.0.1.tar.gz"
"Download URL" = "https://github.com/vladimirs-git/fortigate-api/archive/refs/tags/1.0.2.tar.gz"
[tool.setuptools.packages.find]
include = ["fortigate_api"]
[tool.setuptools.package-data]
Expand Down

0 comments on commit ec28cbb

Please sign in to comment.