CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-oauthlib

A comprehensive Python library for implementing OAuth 1.0 and OAuth 2.0 authentication protocols

Overview
Eval results
Files

common-utilities.mddocs/

Common Utilities

Shared utilities for parameter handling, token generation, URI validation, and cryptographic operations. Includes request/response handling and OAuth-specific data structures used across all OAuth implementations.

Capabilities

Request Object

Core request object representing HTTP requests with OAuth-specific attributes and parameter extraction capabilities.

class Request:
    def __init__(
        self,
        uri: str,
        http_method: str = "GET",
        body: str | dict[str, str] | list[tuple[str, str]] | None = None,
        headers: dict[str, str] | None = None,
        encoding: str = "utf-8",
    ):
        """
        OAuth request object.
        
        Parameters:
        - uri: Request URI
        - http_method: HTTP method
        - body: Request body (various formats)
        - headers: Request headers
        - encoding: String encoding
        """
    
    @property
    def uri_query(self) -> str:
        """Query portion of URI."""
    
    @property
    def uri_query_params(self) -> list[tuple[str, str]]:
        """Parsed query parameters as list of tuples."""
    
    @property
    def duplicate_params(self) -> list[str]:
        """Parameter names that appear multiple times."""
    
    def __getattr__(self, name: str) -> str | None:
        """Get OAuth parameter values by name."""

Parameter Utilities

Functions for parameter encoding, decoding, and manipulation in OAuth contexts.

def quote(s: str | bytes, safe: bytes = b"/") -> str:
    """URL-encode string with OAuth-safe characters."""

def unquote(s: str | bytes) -> str:
    """URL-decode string."""

def urlencode(params: list[tuple[str | bytes, str | bytes]]) -> str:
    """Encode parameters as URL query string."""

def urldecode(query: str | bytes) -> list[tuple[str, str]]:
    """Decode URL query string to parameter list."""

def encode_params_utf8(params: list[tuple[str | bytes, str | bytes]]) -> list[tuple[bytes, bytes]]:
    """Encode parameters as UTF-8 bytes."""

def decode_params_utf8(params: list[tuple[str | bytes, str | bytes]]) -> list[tuple[str, str]]:
    """Decode UTF-8 parameters to strings."""

def extract_params(raw: str | bytes | dict[str, str] | list[tuple[str, str]]) -> list[tuple[str, str]] | None:
    """Extract parameters from various formats."""

def add_params_to_qs(query: str, params: dict[str, str] | list[tuple[str, str]]) -> str:
    """Add parameters to existing query string."""

def add_params_to_uri(uri: str, params: dict[str, str] | list[tuple[str, str]], fragment: bool = False) -> str:
    """Add parameters to URI query or fragment."""

Token Generation

Cryptographically secure token and key generation functions.

def generate_nonce() -> str:
    """Generate cryptographic nonce for OAuth requests."""

def generate_timestamp() -> str:
    """Generate OAuth timestamp (seconds since epoch)."""

def generate_token(length: int = 30, chars: str = ...) -> str:
    """Generate random token with specified length and character set."""

def generate_client_id(length: int = 30, chars: str = ...) -> str:
    """Generate client identifier."""

def generate_signed_token(private_pem: str, request: Request) -> str:
    """Generate signed JWT token using RSA private key."""

def verify_signed_token(public_pem: str, token: str) -> bool:
    """Verify signed JWT token using RSA public key."""

String Utilities

Secure string handling and Unicode conversion functions.

def safe_string_equals(a: str, b: str) -> bool:
    """Constant-time string comparison to prevent timing attacks."""

def to_unicode(data: str | bytes | dict, encoding: str = "UTF-8") -> str | dict:
    """Convert data to Unicode with proper encoding handling."""

Case Insensitive Dictionary

Dictionary implementation for HTTP headers that ignores case.

class CaseInsensitiveDict(dict[str, str]):
    def __init__(self, data: dict[str, str]) -> None: ...
    def __contains__(self, k: str) -> bool: ...
    def __getitem__(self, k: str) -> str: ...
    def get(self, k: str, default=None) -> str: ...
    def __setitem__(self, k: str, v: str) -> None: ...
    def update(self, *args, **kwargs) -> None: ...

Usage Examples

from oauthlib.common import Request, generate_token, safe_string_equals

# Create OAuth request
request = Request(
    'https://api.example.com/data?param=value',
    http_method='POST',
    body='data=test',
    headers={'Authorization': 'Bearer token123'}
)

# Access request properties
print(request.uri_query)  # 'param=value'
print(request.uri_query_params)  # [('param', 'value')]

# Generate secure tokens
client_id = generate_token(16)
access_token = generate_token(32)
nonce = generate_nonce()

# Secure string comparison
is_valid = safe_string_equals(provided_token, stored_token)

Install with Tessl CLI

npx tessl i tessl/pypi-oauthlib

docs

common-utilities.md

device-flow.md

error-handling.md

index.md

oauth1.md

oauth2-clients.md

oauth2-servers.md

openid-connect.md

request-validation.md

token-management.md

tile.json