Skip to content

A Python package to deal with OAuth PKCE authentication flow to generate JWT tokens from desktop apps.

License

Notifications You must be signed in to change notification settings

HungryGnu/pkce_client

 
 

Repository files navigation

Python PKCE Client

This python package contains a simple client to request tokens using the OAuth 2/OIDC Authorization Code PKCE flow.

Sample usage

from pkce_client import PkceClient, PkceLoginConfig

config = PkceLoginConfig(
    authorization_uri="https://localhost:44300/connect/authorize",
    token_uri="https://localhost:44300/connect/token",
    scopes=[ "openid", "profile", "api" ],
    client_id="python-nb",
    internal_port=4444,
    add_random_state=True,
    random_state_length=32,
    verify_authorization_server_https=False
)

login_client = PkceClient(config)
pkce_token = login_client.login()
headers = { "Authorization": "Bearer " + str(pkce_token.access_token) }

Using a JSON config file

If you use a single configuration frequently it will be easier to store the config above in JSON format.

The pkce_token_map object allows you to override the default token map options for instance if your enpoint is 'scope' (singular) instead of 'scopes' (plural).

An example of the expected JSON format is demonstrated below. You will need to update the configuration for your client and endpoint.

{
  "pkce_login":{
      "authorization_uri":"<YOUR AUTH URI>",
      "token_uri":"<YOUR TOKEN URI>",
      "scopes":[ "openid", "email", "roles" ],
      "client_id": "<CLIENT ID PROVIDED BY YOUR AUTH ADMIN>",
      "internal_port":4444,
      "add_random_state":true,
      "random_state_length":32,
      "verify_authorization_server_https":true
   },
  "pkce_token_map": {
      "token_type":"token_type",
      "expires_in":"expires_in",
      "access_token":"access_token",
      "id_token":"id_token",
      "scopes":"scope",
      "refresh_token":"refresh_token"
   }
}

If the config is stored in ~/my_config.json:

config = PkceLoginConfig.from_config_file('~/my_config.json')

login_client = PkceClient(config)
pkce_token = login_client.login()

About

A Python package to deal with OAuth PKCE authentication flow to generate JWT tokens from desktop apps.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%