Skip to main content

PKCE Generator Module

Author: 70L0-0J0

Description

PKCE (Proof Key for Code Exchange) Generator for OAuth2 Authentication This repository contains a flexible and customizable Python class for generating PKCE parameters required for OAuth2 authentication flows. It supports both S256 and plain code challenge methods and provides configurable options for generating code verifiers, code challenges, states, and nonces, ensuring compatibility with the PKCE standard. Features: PKCE Compliance: Generate code verifiers between 43 and 128 characters, ensuring compliance with OAuth2 PKCE standards. Customizable Code Challenge Method: Supports both S256 (SHA-256 hashing) and plain challenge methods. Random State & Nonce Generation: Securely generate state and nonce parameters for your OAuth2 requests. URL-Encoded Payload: Easily generate the complete OAuth2 request payload with all required parameters. This implementation is ideal for developers looking to integrate secure PKCE-based OAuth2 flows into their applications, ensuring robust and flexible authentication.

Features

  1. PKCE Compliance: Generates code verifiers with lengths between 43 and 128 characters, ensuring compatibility with the PKCE standard.

  2. Secure Hashing: Supports the S256 hashing method (recommended) for code challenges, along with an optional plain method.

  3. Random State & Nonce Generation: Generates cryptographically secure random values for state and nonce parameters.

  4. Customizable Parameters: Allows users to specify lengths for code verifiers, states, and nonces.

  5. Convenient Payload Generation: Provides a static method to generate URL-encoded OAuth2 payloads, making integration simple and efficient.

Installation

pip install pkcegen

Usage Examples

1: Basic PKCE Payload Generation

This example demonstrates how to use the static method pkce to generate a full OAuth2 payload with PKCE parameters.

# Call the static method 'pkce' from the PKCEGenerator class to generate the PKCE payload on S256 challenge method
# Provide the required parameters: client_id, redirect_uri, and scope
# Optionally, you can also specify the code_challenge_method, code_verifier_length, state_length, and nonce_length

import pkcegen

payload, code_verifier = pkcegen.pkce(
    client_id="your_client_id",         # Your OAuth2 client ID
    redirect_uri="Your Redirect URI",   # Redirect URI after authentication
    scope="Your scope"                  # OAuth2 scope of permissions
)
# Print the generated OAuth2 payload and the code verifier
print("Payload:", payload)
print("Code Verifier:", code_verifier)

2: Customizing PKCE Parameters

This example demonstrates how to use the static method pkce to generate a full OAuth2 payload with customizing PKCE parameters.

# Generate OAuth2 payload with customized PKCE parameters
# Use the static method 'pkce' to create the PKCE parameters with specific settings

import pkcegen

payload, code_verifier = pkcegen.pkce(
    client_id="your_client_id",         # Your OAuth2 client ID
    redirect_uri="Your Redirect URI",   # Redirect URI after authentication
    scope="Your scope",                 # OAuth2 scope of permissions
    code_challenge_method="plain",      # Use plain method instead of S256
    code_verifier_length=50,            # Custom code verifier length
    state_length=20,                    # Custom state length
    nonce_length=24                     # Custom nonce length
)

# Print the generated OAuth2 payload and the code verifier
print("Payload:", payload)
print("Code Verifier:", code_verifier)

3: Using PKCE with Custom Parameters and Different Methods

This example demonstrates how to use the static method pkce to generate a full OAuth2 payload with custom PKCE parameters and different methods.

# Generate OAuth2 payload with customized PKCE parameters using the S256 method
# The static method 'pkce' allows you to specify various parameters for PKCE generation

import pkcegen

payload, code_verifier = pkcegen.pkce(
    client_id="your_client_id",         # Replace with your actual OAuth2 client ID. This is a unique identifier for your application.
    redirect_uri="Your Redirect URI",   # Replace with the URI where users will be redirected after authentication. This should match what is registered with your OAuth2 provider.
    scope="Your scope",                 # Replace with the scope of permissions your application needs.
    code_challenge_method="S256",       # Specify the method for generating the code challenge. Here, "S256" is used, which means the code challenge will be generated using SHA-256 hashing.
    code_verifier_length=64,            # Custom length for the code verifier. A longer verifier adds more security. The length should be between 43 and 128 characters.
    state_length=32,                    # Custom length for the state parameter. A longer state can increase security by adding more entropy.
    nonce_length=32                     # Custom length for the nonce parameter. A longer nonce increases security by adding more randomness.
)

# Print the generated OAuth2 payload and the code verifier
print("Payload:", payload)
print("Code Verifier:", code_verifier)

License

MIT License

Copyright (c) 2024 70L0-0j0

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Release files for pkcegen 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pkcegen 1.0.1
File Size Uploaded
pkcegen-1.0.1.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pkcegen 1.0.1
File Interpreter ABI Platform
pkcegen-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.2 kB

Release files / pkcegen-1.0.1.tar.gz

Download URL pkcegen-1.0.1.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
59a529816bd6228585ad8efe376143c04f723ff83d67ad28ceb42f19531bbc1a
BLAKE2b-256 checksum
How to use checksums
7a87814984ea99bd72b298d879769cab6744d507a8fde2a004c7ccf95eb43c29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release files / pkcegen-1.0.1-py3-none-any.whl

Download URL pkcegen-1.0.1-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ba5f80ed1c6274b4d402e3e2a1103d2c0077ea03aef3a015f72a726bcbde82eb
BLAKE2b-256 checksum
How to use checksums
12b1d8c97ac14b5a9a0cdfb92a597cade9b173c1e8eb303152de05f10104f623
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page