Skip to main content

Async Python library for Telegram OpenID Connect (OIDC) login

Project description

pyTelegramOIDC

PyPI - Version Python Versions License

pyTelegramOIDC — is a modern, fully asynchronous Python library for integrating the official Telegram Login (OpenID Connect) into your web applications.

It strictly follows standard OIDC specifications, supports PKCE (Proof Key for Code Exchange), and natively extracts all user data (including phone numbers) directly from cryptographically signed ID Tokens.

Features

  • Fully Async: Uses httpx for non-blocking HTTP requests.
  • Secure: Validates JWT signatures automatically using Telegram's official JWKS keys.
  • Pydantic Models: Strict typing for user profiles and tokens out of the box.
  • Phone Number Support: Easily extract requested user phone numbers.

Installation

You can install the library via pip:

pip install pyTelegramOIDC

Note: Although you install it via pyTelegramOIDC, the module is imported as teleoidc (similar to how pyTelegramBotAPI is imported as telebot).

Quickstart (JS Widget Flow)

If you are using the official Telegram JS Popup Widget, your frontend will receive an id_token. Use teleoidc to validate it and get the user's data safely.

from teleoidc import TelegramAsyncOIDCClient

# 1. Initialize the client
client = TelegramAsyncOIDCClient(
    client_id="YOUR_BOT_ID",        # From @BotFather
    client_secret="YOUR_BOT_SECRET", # From @BotFather
    redirect_uri="https://yoursite.com/auth/callback"
)

# 2. Validate the token received from the frontend
id_token = "eyJhbGciOiJSUzI1NiIs..." # Token from Telegram JS

try:
    user = client.validate_id_token(id_token)
    
    print(f"Logged in as: {user.name} (@{user.preferred_username})")
    print(f"Telegram ID: {user.id}")
    
    if user.phone_number:
        print(f"Phone: {user.phone_number}")

except Exception as e:
    print(f"Invalid token: {e}")

Manual OAuth2 Redirect Flow

If you are doing a full backend redirect instead of the JS widget, pyTelegramOIDC provides native methods to generate auth links and exchange codes for tokens (get_auth_request and exchange_code_for_token).

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytelegramoidc-1.0.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytelegramoidc-1.0.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file pytelegramoidc-1.0.0.tar.gz.

File metadata

  • Download URL: pytelegramoidc-1.0.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for pytelegramoidc-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d67a832dafa2ca585e106a1d9887f3477503e7bc83e4a6d9fdfffbd3f2783ccd
MD5 7fa73dc20750891f90587a82da03a682
BLAKE2b-256 b0817d5d95596905689d43e3b9f06b6934f4dd7e5aaaae582ffed2e92dfde85b

See more details on using hashes here.

File details

Details for the file pytelegramoidc-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pytelegramoidc-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for pytelegramoidc-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6e8af5e4510a220bf1adebd633f7823dab9bb2b32f732b2993f6ff768d4dd26b
MD5 8e3442b3ef3906f69bed8b8ebc715052
BLAKE2b-256 5bc869e0154fdbbe1ec33d344f5b348c888725502e24c9ea3821e75aa5ada066

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page