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

device-flow.mddocs/

Device Flow

OAuth 2.0 device authorization grant (RFC 8628) for input-constrained devices like smart TVs, game consoles, and IoT devices. Provides device and user codes for out-of-band authorization.

Capabilities

Device Client

OAuth 2.0 client implementation for device authorization flow.

class DeviceClient:
    def prepare_device_authorization_request(
        self,
        device_authorization_endpoint: str,
        scope: str | list[str] | None = None,
        **kwargs,
    ) -> tuple[str, dict[str, str], str]:
        """Prepare device authorization request."""
    
    def prepare_token_request(
        self,
        token_url: str,
        device_code: str,
        **kwargs,
    ) -> tuple[str, dict[str, str], str]:
        """Prepare device token request."""

Device Authorization Endpoint

Server endpoint for handling device authorization requests.

class DeviceAuthorizationEndpoint:
    def create_device_authorization_response(
        self,
        uri: str,
        http_method: str = "POST",
        body: str | None = None,
        headers: dict[str, str] | None = None,
        **kwargs,
    ) -> tuple[dict[str, str], str, int]:
        """Create device authorization response with device and user codes."""

Device Application Server

Pre-configured server for device flow combining device authorization and token endpoints.

class DeviceApplicationServer:
    def __init__(self, request_validator, **kwargs): ...

Usage Example

from oauthlib.oauth2.rfc8628 import DeviceClient
import requests
import time

# Device flow example
client = DeviceClient('your-client-id')

# Step 1: Get device and user codes
device_auth_url, headers, body = client.prepare_device_authorization_request(
    'https://auth.example.com/device/authorize',
    scope=['read', 'write']
)

response = requests.post(device_auth_url, headers=headers, data=body)
device_data = response.json()

print(f"Visit: {device_data['verification_uri']}")
print(f"Enter code: {device_data['user_code']}")
print("Or visit complete URI:", device_data.get('verification_uri_complete'))

# Step 2: Poll for token
device_code = device_data['device_code']
interval = device_data.get('interval', 5)

while True:
    token_url, headers, body = client.prepare_token_request(
        'https://auth.example.com/token',
        device_code=device_code
    )
    
    response = requests.post(token_url, headers=headers, data=body)
    
    if response.status_code == 200:
        token = response.json()
        print("Access token received:", token['access_token'])
        break
    elif response.status_code == 400:
        error = response.json()
        if error['error'] == 'authorization_pending':
            time.sleep(interval)
            continue
        elif error['error'] == 'slow_down':
            interval += 5
            time.sleep(interval)
            continue
        else:
            print("Authorization failed:", error)
            break

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