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

Uploaded Python 3

File details

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

File metadata

  • Download URL: singpass_client-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 f6a9a4d62b0f9f32f6e0c8976d2ae830d2fb29cc2e013c77f3eb983e09733c99
MD5 cd703c3e479efbcc04a729e9a8633a32
BLAKE2b-256 b401eff5d99d0ff12bc824105084cb00f1d31c8af9c3c23a1662d358aa55ec18

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for singpass_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cfff238ae28fa6caa27a944445933e41bd49f0833cb858d986a6ac65e5e0c7ea
MD5 a588044981f92d7fb1ff91e32b0537a2
BLAKE2b-256 a53bbb60158944b4e82c94d1c2753dd76ab14896fd6dc4c7035e4736fc43b98c

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