Skip to main content

Helper library for OAuth2 in command-line tools

Project description

CLI OAuth2

This Python library help command-line tool authors to use OAuth2 services. Built using requests-oauthlib with parts of google_auth_oauthlib.

Installation

pip install cli-oauth2

Usage

Do something like this:

from oauthcli import OpenStreetMapAuth

auth = OpenStreetMapAuth(
    client_id, secret_id, ['read_prefs']
).auth_server(token_test=lambda r: r.get('user/details'))

data = auth.get('user/details.json')
if data.status_code != 200:
    print(f'Error {data.status_code}: {data.text})')
else:
    print(f'Hello, {data.json()["user"]["display_name"]}')

Tokens are saved to disk, so subsequent runs won't require authorization.

Auth objects have these methods and properties:

  • auth_server() opens a web browser and catches the response by starting a local server.
  • auth_code() opens a web browser and expected a user to copy the code presented by the provider. It uses urn:ietf:wg:oauth:2.0:oob redirect uri.
  • authorized returns whether there is an active access token.
  • get, post etc call the relevant requests methods, but often shadow the server name. See the example above.
  • session is the underlying OAuth2Session object.

There are some predefined providers:

  • OpenStreetMapAuth
  • OpenStreetMapDevAuth
  • GoogleAuth
  • GitHubAuth
  • MastodonAuth (requires a server parameter)
  • RedditAuth
  • FacebookAuth
  • LinkedInAuth

Note that only OSM and GitHub providers were tested. I welcome pull requests with fixes.

If you need to use another provider, just subclass AuthFlow and pass it provider_id (the key for the stored token map), OAuth2Session(client_id, scope=scopes), auth_url, token_url, and client_secret.

Cleanup

The tool stores tokens in a json in the configuration directory. To clean some or all tokens, use the oauthclean command-line tool.

Author and License

Written by Ilya Zverev, published under Apache License 2.0.

Contains portions of google_auth_oauthlib as of commit 1a9dca889357b93bdad17d75a28ac81e3ba6067f, published under Apache License 2.0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cli-oauth2-0.3.1.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

cli_oauth2-0.3.1-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file cli-oauth2-0.3.1.tar.gz.

File metadata

  • Download URL: cli-oauth2-0.3.1.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.64.1 CPython/3.10.8

File hashes

Hashes for cli-oauth2-0.3.1.tar.gz
Algorithm Hash digest
SHA256 6f989848989f6778c296e6c91ff6efada429f9cf647b8432a1b40e067670fc5e
MD5 a79103f3026dc8c1f0948ad359c82e2d
BLAKE2b-256 cb472b9fd0bb9285249914ca87c231853212e43d550654a0d1b4aa519f836261

See more details on using hashes here.

File details

Details for the file cli_oauth2-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: cli_oauth2-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.64.1 CPython/3.10.8

File hashes

Hashes for cli_oauth2-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b18f69770673892d2202b408d04246ea23a5e5d05cfca1577d8c1fb162dcb1c5
MD5 7725743ee3a92969ba16d513461a1e76
BLAKE2b-256 a66cee8728c95e28ea4103b625975b34e961b639fe95a985d5da1efe2d08c342

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page