Skip to content

Commit

Permalink
Added support for manual saving of cookiejar, or disabling of cookie …
Browse files Browse the repository at this point in the history
…autosave on close
  • Loading branch information
meson800 committed Jan 18, 2023
1 parent e224d0e commit 8f7aba4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.0] - 2023-01-17
### Added
- Added the ability to control cookiejar saving. Autosave can be disabled
and the session cookies can now be saved at any time, not just on close.

## [0.4.0] - 2022-09-12
### Added
- Added the ability to do initial authentication with username/password
Expand Down
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,10 @@ having to re-install after each update.
## Changelog
See the [CHANGELOG](CHANGELOG.md) for detailed changes.
```
## [0.4.0] - 2022-09-12
## [0.5.0] - 2023-01-17
### Added
- Added the ability to do initial authentication with username/password
or with Kerberos tickets.
### Deprecated
- Passing the certfile/cert password as their own arguments is now deprecated.
Use the new `auth_type` argument to pass authentication information.
### New contributors
- Nickolai Zeldovich (zeldovich)
- Added the ability to control cookiejar saving. Autosave can be disabled
and the session cookies can now be saved at any time, not just on close.
```

## License
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name='touchstone-auth',
version='0.4.0',
version='0.5.0',
author='Christopher Johnstone',
author_email='[email protected]',
description='Access Touchstone SSO sites without a web browser.',
Expand Down
21 changes: 16 additions & 5 deletions src/touchstone_auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ def __init__(self,
twofactor_type:TwofactorType=TwofactorType.DUO_PUSH,
verbose:bool=False,
*,
auth_type:Optional[Union[CertificateAuth,UsernamePassAuth,KerberosAuth]]=None) -> None:
auth_type:Optional[Union[CertificateAuth,UsernamePassAuth,KerberosAuth]]=None,
autosave_cookies:bool=True) -> None:
"""
Creates a new Touchstone session.
Expand All @@ -108,7 +109,9 @@ def __init__(self,
Only Duo Push (TwofactorType.DUO_PUSH) and phone call (TwofactorType.PHONE_CALL)
are currently supported.
verbose: If True, extra information during log-in is printed to stdout
auth_type:
auth_type: Determines the type of authentication to use. Pass an enum type.
autosave_cookies: If cookies should be automatically re-saved back to the same cookiejar file
when the session is closed.
"""

self._session: requests.Session = requests.Session()
Expand All @@ -129,6 +132,7 @@ def __init__(self,
self._session.headers.update({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
})
self._autosave_cookies = autosave_cookies

# Load cookiejar from path (if it exists)
try:
Expand Down Expand Up @@ -402,12 +406,19 @@ def vlog(self, string: str) -> None:
def __enter__(self) -> requests.Session:
"""Returns the internal session when called as a context manager"""
return self._session

def save_cookies(self, cookiejar: Union[str,pathlib.Path]) -> None:
"""Saves session cookies into a cookiejar file, overwriting if present"""
# Save cookiejar
with open(cookiejar, 'wb') as cookies:
pickle.dump(self._session.cookies, cookies)


def close(self) -> None:
"""Closes the session while saving the session cookies."""
# Save cookiejar
with open(self._cookiejar_filename, 'wb') as cookies:
pickle.dump(self._session.cookies, cookies)
# Save cookies if we are autosaving...
if self._autosave_cookies:
self.save_cookies(self._cookiejar_filename)
# and close the internal session
self._session.close()

Expand Down

0 comments on commit 8f7aba4

Please sign in to comment.