Skip to main content

Simple OAuth2 client.

Project description

feedr-oauth2

This is a Python library that implements the client side of following OAuth 2.0 flows:

  • Authorization Code
  • Client Credentials
  • Device Code
  • Refresh Token

The implementation follows the OAuth2 standard described in https://oauth.net/2/.

Quickstart

Authorization Code

from feedr.oauth2.authorization_code import Client, PKCE

client = ac.Client(
  auth_uri='https://authorization-server.com/oauth2/authorize',
  token_uri='https://authorization-server.com/oauth2/token',
  client_id='...',
  client_secret='...',
  redirect_uri='...',
  use_pkce=True,
)

# ------------------
# /api/login
# ------------------

request = client.authorization_request(
  scope='read+write',
  redirect_uri=EXTERNAL_URL + '/api/login/collect',
)

persist_state(request.state, request.pkce)

return redirect(request.auth_uri)

# ------------------
# /api/login/collect?code=...&state=...
# ------------------

pkce: PKCE = retrieve_state(state)

response = client.authorization_code(code, pkce)

print(response)  # AccessTokenResponse(access_token='...', token_type='bearer', ...)

Client Credentials

from feedr.oauth2.client_credentials as Client

client = Client(
  token_uri='https://authorization-server.com/oauth2/token',
  client_id='...',
  client_secret='...',
)

token_info = client.get_token()
print(token_info)  # AccessTokenResponse(access_token='...', token_type='bearer', ...)

Device Code

from feedr.oauth2.device_code import Client

client = Client(
  device_code_uri='https://authorization-server.com/device/code',
  token_uri='https://authorization-server.com/oauth2/token',
  client_id='...',
  client_secret='...',
)

request = client.get_device_code()
print(f'Visit {request.verification_url} and enter the code {request.user_code}.')

token_info = client.poll(request)
print(token_info)  # AccessTokenResponse(access_token='...', token_type='bearer', ...)

Refresh Token

The authorization_code.Client class has a refresh_token() method that makes refreshing the access token easy by simply supplying the refresh token.

new_token = client.refresh_token(token_info.refresh_token)

Alternatively, the refresh_token.Client can be used to create a refresh_token request.

client = Client(
  token_uri='https://authorization-server.com/oauth2/token',
  refresh_token='...',
  client_id='...',
  client_secret='...',
)

token_info = client.get_token()
print(token_info)  # AccessTokenResponse(access_token='...', token_type='bearer', ...)

Copyright © 2020 Niklas Rosenstein

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

feedr.oauth2-0.1.0.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

feedr.oauth2-0.1.0-py3-none-any.whl (1.9 kB view details)

Uploaded Python 3

File details

Details for the file feedr.oauth2-0.1.0.tar.gz.

File metadata

  • Download URL: feedr.oauth2-0.1.0.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.3

File hashes

Hashes for feedr.oauth2-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fd9c8deb41de8aedfa9ed129874e596f6bc725289960883945f8fd09bedf40f5
MD5 09c363e0e20cf379deb43a1f118f0d30
BLAKE2b-256 aafcaea88c15fe0b25dd733777fcae244dcbdf2b7042c51f8f01ac5cb24768b3

See more details on using hashes here.

File details

Details for the file feedr.oauth2-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: feedr.oauth2-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 1.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.3

File hashes

Hashes for feedr.oauth2-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 259b6441a1e8087e5ad4848fb71548b428ca10d35efb034ea855e365ada73e11
MD5 77f9a1a54aa1dace43cb8e00c23f0b95
BLAKE2b-256 1b0975c2a3fdf73cb2e286e69502328d840e4e1c06661392cf51aa164957bfbe

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