scg-auth (Python)
A lightweight, zero-dependency OAuth 2.0 client library for Python.
Supports all major OAuth 2.0 flows with built-in PKCE and CSRF protection.
Uses the Python standard library only — no requests, no third-party packages.
Features
- Authorization Code Flow — with PKCE (S256) support
- Client Credentials Flow — machine-to-machine / service accounts
- Refresh Token — seamless token renewal
- Device Code Flow — CLI tools, smart TVs, IoT devices
- Implicit Flow — parse-only (deprecated in OAuth 2.1)
- State / CSRF protection — automatic state generation and validation
- Token management — in-memory storage with expiry checking
- Zero dependencies — standard library only (
urllib,hashlib,secrets, etc.)
Installation
pip install scg-auth
Quick Start
Authorization Code Flow (with PKCE)
from scg_auth import SCGAuth
client = SCGAuth(
client_id="your-client-id",
client_secret="your-client-secret",
authorization_url="https://provider.example.com/oauth/authorize",
token_url="https://provider.example.com/oauth/token",
redirect_uri="https://yourapp.com/callback",
scopes=["openid", "profile", "email"],
)
# 1. Generate the authorization URL
result = client.generate_auth_url(pkce=True)
# Redirect user to result["url"], store result["state"] and result["code_verifier"]
# 2. Handle the callback
tokens = client.exchange_code(
code,
state=returned_state, # validates CSRF automatically
code_verifier=code_verifier,
)
print(tokens["access_token"])
# 3. Refresh when near expiry
if client.is_token_expired(buffer_seconds=120):
tokens = client.refresh_token(tokens["refresh_token"])
Client Credentials Flow
tokens = client.client_credentials()
print(tokens["access_token"])
Device Code Flow
device_auth = client.device_code()
print(f"Visit {device_auth['verification_uri']} and enter: {device_auth['user_code']}")
tokens = client.poll_device_token(device_auth)
print(tokens["access_token"])
API
SCGAuth(client_id, authorization_url, token_url, ...)
| Parameter | Type | Required | Description |
|---|---|---|---|
client_id |
str | ✓ | OAuth client ID |
authorization_url |
str | ✓ | Provider authorization endpoint |
token_url |
str | ✓ | Provider token endpoint |
client_secret |
str | Client secret (required for confidential clients) | |
redirect_uri |
str | Redirect URI | |
scopes |
list[str] | Default scopes | |
device_authorization_url |
str | Device authorization endpoint |
Methods
| Method | Description |
|---|---|
generate_auth_url(pkce, state, scopes, response_type) |
Build auth URL + register CSRF state |
validate_state(state) |
Validate CSRF state from callback |
exchange_code(code, state, code_verifier) |
Exchange code for tokens |
client_credentials(scopes) |
Client Credentials flow |
refresh_token(refresh_token) |
Refresh an access token |
device_code(scopes) |
Initiate Device Code flow |
poll_device_token(response, timeout, interval) |
Poll until user authorizes |
generate_implicit_url(state, scopes) |
Build Implicit flow auth URL |
parse_implicit_response(url_or_fragment, validate_state) |
Parse Implicit flow response |
get_stored_tokens() |
Get cached tokens |
is_token_expired(buffer_seconds) |
Check token expiry |
clear_tokens() |
Clear cached tokens |
Running Tests
cd python
python -m pytest test_scg_auth.py -v
# or
python test_scg_auth.py
License
MIT — Analytics With Harry / Squid Consultancy Group Limited
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
scg_auth-1.1.1.tar.gz
(10.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file scg_auth-1.1.1.tar.gz.
File metadata
- Download URL: scg_auth-1.1.1.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd73d724be9f2e08ded9f1a23442d449c98384433c55d97b0e34133c0f4d6a8f
|
|
| MD5 |
9b370e22ea332d835b83b799ece90efb
|
|
| BLAKE2b-256 |
9b123aa2b227f6608892acb20a962bae8118405c99e635b1be609df0156f1fb8
|
File details
Details for the file scg_auth-1.1.1-py3-none-any.whl.
File metadata
- Download URL: scg_auth-1.1.1-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
942c78051e5196073911156fe2d98a487c590950e06e648334128fe47b62f4af
|
|
| MD5 |
ab4d218972841d850194a8999db57759
|
|
| BLAKE2b-256 |
dcf83672818982d7a4750a7a901f11463e1d2a658bce22633f8277e676235759
|