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.1.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.1-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: singpass_client-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 d938e3a98950fdd58956a42248ff4fdb508c891f3b5d1d06d6b539189de4082d
MD5 0b038e3f7d0e0f93abfc7a0b8cc6605f
BLAKE2b-256 9bf802948cbbb9460a1a936b325340b1b86fc790450652f53a247e33d8baedcc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for singpass_client-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b0fd356ef0208c38d415227412642324d76c77fb20d82fa4bc0dd20eafcfecb6
MD5 4de58504a137302b704f94a11924e204
BLAKE2b-256 4b1bad99abede2b7f8fe50d6bdeb95aaecdaf30c3a256a1624e93e4754620a51

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