PKCE Code Verifier and Challenge Generator
The `pkce` library is a lightweight Python module designed to simplify the generation of Proof Key for Code Exchange (PKCE) code verifiers and code challenges. It provides essential cryptographic helper functions for implementing the PKCE extension to the OAuth 2.0 Authorization Code Flow, particularly important for public clients (like mobile or single-page applications) that cannot securely store a client secret. The current version is 1.0.3, with an infrequent release cadence reflecting its stable and focused functionality.
Warnings
- breaking OAuth 2.1 mandates PKCE for all Authorization Code flows, and the 'plain' code challenge method is considered insecure and a downgrade attack vector. Ensure your Authorization Server is configured to require PKCE (S256 method) and that clients always use S256.
- gotcha While the `pkce` library generates the verifier and challenge, proper implementation of the full OAuth 2.0 Authorization Code Flow with PKCE requires careful handling of redirect URIs, state parameters, and secure token storage.
- gotcha The `pkce` library is minimal, focusing solely on code verifier and challenge generation. It does not handle the full OAuth flow (e.g., making HTTP requests to authorization or token endpoints, token storage, or refresh).
Install
-
pip install pkce
Imports
- pkce
import pkce
Quickstart
import pkce
# Generate a code verifier and code challenge pair
code_verifier, code_challenge = pkce.generate_pkce_pair()
print(f"Code Verifier: {code_verifier}")
print(f"Code Challenge: {code_challenge}")
# Alternatively, generate separately
single_code_verifier = pkce.generate_code_verifier(length=128)
single_code_challenge = pkce.get_code_challenge(single_code_verifier)
print(f"\nSingle Code Verifier: {single_code_verifier}")
print(f"Single Code Challenge: {single_code_challenge}")