n8henrie / pycookiecheat

Borrow cookies from your browser's authenticated session for use in Python scripts.
https://n8henrie.com/2013/11/use-chromes-cookies-for-easier-downloading-with-python-requests/
MIT License
744 stars 111 forks source link

Enhancement: support the Slack app's cookies #39

Closed hraftery closed 1 year ago

hraftery commented 2 years ago

macOS 11.6.5 and Linux Mint 20

3.9.12 and 3.8.10

v0.4.7 and master head.

My Issue

The Slack desktop app uses the same cookie storage mechanism as Chrome and Chromium, and there appears to be no more advanced tool for Cookie extraction than pycookiecheat. Although the use cases for extracting cookies from a browser (ie. to browse web pages) might be slightly different to the use cases for extracting cookies from Slack (ie. to access the Slack API manually), the modification to allow pycookiecheat to do both turns out to be really small.

After much consideration about whether to diverge or merge, I believe the best option is to generalise pycookiecheat's functionality slightly, and keep it as the best resource for cookie extraction.

WHYT

Simply passing "Slack" as the browser parameter and Slack's cookie file as the cookie_file parameter to chrome_cookies() results in the misleading "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb9 in position 3: invalid start byte" type errors. It turns out this is simply because the wrong password is pulled from the keyring. All that needs to change for this to work is to point to a different keyring. Specifying the correct cookie_file can easily be achieved using the existing interface, but specifying the keyring name cannot.

By simply allowing "Slack" as a browser name, the keyring name is correctly determined on macos. The story is a bit more complicated on Linux (tested with some effort on Linux Mint), but turns out to provide a more robust keyring determination method in general anyway.

Thus, I'd like to offer the improvements in a pull request, in the hope that they find their way to PyPI. The relevant commit is here.


Please make sure you've taken these steps before submitting a new issue:

hraftery commented 2 years ago

Sorry, relevant commit is here

n8henrie commented 2 years ago

Interesting find!

Yes, can you please make this into a PR?

Any chance you can also incorporate a test or two?

n8henrie commented 2 years ago

@hraftery Are you interested in making a pull request? Otherwise I'll likely close this soonish.

hraftery commented 2 years ago

I am yes. Went travelling immediately after your message and haven’t returned to it, but intend to do so.

Sent from my iPad

On 16 Jul 2022, at 3:28 am, Nathan Henrie @.***> wrote:

 @hraftery Are you interested in making a pull request? Otherwise I'll likely close this soonish.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.

n8henrie commented 2 years ago

Sweet, thanks. Enjoy your travels!