Skip to main content

Add your description here

Project description

Singpass OAuth Client

Description

A lightweight Python client for integrating with Singpass' OAuth2 endpoints. This client helps:

  • Generate Singpass authorization URLs
  • Exchange authorization codes for tokens
  • Decrypt and verify ID and access tokens
  • Retrieve and verify user info from Singpass

Note: This library does not handle session management out of the box.

It is built around the official Singpass standards and uses JOSE for handling JWTs and JWKs.

Usage

import asyncio
from singpass_client.client import SingpassOauthClient

# Load Singpass discovery data (usually from the Singpass metadata URL)
discovery = {
    "issuer": "https://stg-id.singpass.gov.sg",
    "authorization_endpoint": "https://stg-id.singpass.gov.sg/auth",
    "jwks_uri": "https://stg-id.singpass.gov.sg/.well-known/keys",
    "userinfo_endpoint": "https://stg-id.singpass.gov.sg/userinfo",
}

# Initialize the client
client = SingpassOauthClient(discovery=discovery, client_id="YOUR_CLIENT_ID", scope="openid")

# Step 1: Generate the authorization URL
auth_url, code_verifier, nonce, state = client.create_authorization_url(
    redirect_uri="https://yourapp.com/callback"
)

print("Visit this URL to authenticate:", auth_url)

# After redirection, handle the callback with the returned `code`
# Step 2: Exchange code for tokens
async def exchange_code():
    token_set = await client.handle_callback(
        nonce=nonce,
        code_verifier=code_verifier,
        authorization_code="AUTHORIZATION_CODE_FROM_CALLBACK",
        key_file_path="path/to/your_private_jwks.json",
        sig_key_id="your-signing-key-id",
        en_key_id="your-encryption-key-id"
    )

    access_token = token_set["access_token"]

    # Step 3: Retrieve and verify user info
    user_info = await client.get_info(
        access_token=access_token,
        en_key_id="your-encryption-key-id",
        key_file_path="path/to/your_private_jwks.json",
        nonce=nonce
    )

    print("User info:", user_info)

# Run the async flow
asyncio.run(exchange_code())

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

singpass_client-0.1.2.tar.gz (14.4 kB view details)

Uploaded Source

Built Distribution

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

singpass_client-0.1.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file singpass_client-0.1.2.tar.gz.

File metadata

  • Download URL: singpass_client-0.1.2.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.3

File hashes

Hashes for singpass_client-0.1.2.tar.gz
Algorithm Hash digest
SHA256 af5d2b6b8bba4b94b890c27c57e30ff438af99dbeb903a28ace4202a132cd654
MD5 0e321d15c062e9d8db27c8ad034d4a05
BLAKE2b-256 d9944986ba2c2e8287c2e32bf69cd3f8186fe22fe1335585049f3f218b27688c

See more details on using hashes here.

File details

Details for the file singpass_client-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for singpass_client-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 77ed16b7f59a5e45514521b8c0a8b3ee0da0a9387f59cad8f86577d15b7c5433
MD5 652b04dda4435fdfe8e24ad4fb838eac
BLAKE2b-256 3b24fe7c9b8fc17328f2f37eef51f22448052a0c7219bc0dd6a2c2bf7b3f2cb9

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