more-sso-auth — Lightweight SSO for Python
A small, easy-to-use Single Sign-On (SSO) helper for Python apps and AWS Lambda.
What it gives you
- RS256 JWT validation using a public key fetched from a URL.
- In-memory caching for public keys (no constant network calls).
- A header-based decorator
@auth_requiredfor quick route protection. - A
root_auth_requireddecorator suitable for Lambda handlers. - Programmatic and environment-variable configuration.
- An extensible permission system via
BasePermissionso you can write custom rules.
Quick start (3 minutes)
- Install
pip install more-sso-auth
pip install PyJWT
pip install cryptography
- Configure (either method)
Programmatic
from more_sso import init_sso_config
init_sso_config(
public_key_uri="<your-kms-id>",
audience="<your-app>"
)
Environment variables
export PUBLIC_KEY_URI="<your-kms-id>"
export AUDIENCE="<your-app>"
- Protect a function
from more_sso import auth_required
@auth_required(permission="pma.role", value="admin")
def my_func(event, *args, **kwargs):
user = event["requestContext"]["user"]
return {"ok": True}
The decorator will validate the bearer JWT from the
Authorizationheader and inject the decoded payload consistently intoevent["requestContext"]["user"].
Core Concepts
Token validation
Use validate_token(token) to validate a JWT programmatically. It raises JWTValidationError on invalid tokens.
from more_sso import validate_token, JWTValidationError
try:
user = validate_token(token)
except JWTValidationError as e:
# handle unauthenticated
print("Invalid token:", str(e))
validate_token performs usual checks: signature (RS256), audience (the AUDIENCE you configured), and token expiry.
Decorators
@auth_required(...)— decorator for fine-grained route enforcement. It supports either a simple permission check or a custom permission class. After validation it injects the decoded token intoevent["requestContext"]["user"].@root_auth_required— a simple decorator for Lambda handlers that enforces authentication at the top level and injects the decoded user intoevent["requestContext"]["user"]. If authentication fails, the decorator returns a 401 response (for AWS Lambda-style handlers).
Example: root-level Lambda
from more_sso import root_auth_required
@root_auth_required
def lambda_handler(event, context):
user = event["requestContext"]["user"]
return {"statusCode": 200, "body": f"Hello {user['sub']}"}
Permissions
There are two ways to enforce authorization with auth_required:
-
Simple claim check — pass
permissionandvalue.permissionis a dotted path into the decoded JWT payload (for example:pma.rolewill look upuser['permissions']["pma"]["role"]).valueis the expected value.- the
userclaims will also contain thepermissionsas a json with app specific roles and permisson attributes
@auth_required(permission="my_app.role", value="admin")
def handler(event, *args, **kwargs):
user = event["requestContext"].get("user", {})
# ...
- Custom permission class — extend
BasePermissionand implementhas_access().
from more_sso import BasePermission, auth_required, AccessDeniedError
class CustomPermission(BasePermission):
def __init__(self,*args,**kwargs):
super.__init__(*args,**kwargs)
self.extras = kwargs
def check_something(self )
if self.extras['id'] == self.user['permissions']['id']
return "something"
...
# to be implemented
def has_access(self) -> bool:
# example: allow only admins
if self.check_something() =='something'
return self.user['permissions'].get("role") == "admin"
@auth_required(permission_class=CustomPermission,**kwargs)
# kwargs are stored in self.extras of permission class which can be accessed in has_access
def admin_only(event, *args, **kwargs):
return {"ok": "admin access granted"}
# caller example
try:
admin_only()
except AccessDeniedError:
# return 403
pass
Behavior: when permission check fails the decorator raises AccessDeniedError (or returns an appropriate 403 when used as a top-level Lambda decorator if configured that way).
AWS Lambda patterns
Top-level enforcement (recommended for simple APIs):
from more_sso import root_auth_required
@root_auth_required
def lambda_handler(event, context):
user = event["requestContext"]["user"]
# authorized
return {"statusCode": 200, "body": "ok"}
Function-level enforcement (fine-grained):
from more_sso import auth_required, AccessDeniedError
@auth_required(permission="my_app.role", value="admin")
def admin_action(event, *args, **kwargs):
return {"ok": True}
def lambda_handler(event, context):
try:
return admin_action(event)
except AccessDeniedError:
return {"statusCode": 403, "body": "Access denied"}
Note: Both auth_required and root_auth_required inject the decoded token consistently into event["requestContext"]["user"].
API reference (quick)
init_sso_config(public_key_uri: str, audience: str)— initialize the library programmatically.validate_token(token: str) -> dict— validate token and return decoded payload. RaisesJWTValidationErroron failure.auth_required(permission: str = None, value: Any = None, permission_class: Type[BasePermission] = None)— decorator to protect functions. Injects user intoevent["requestContext"]["user"].root_auth_required— decorator for Lambda handlers.BasePermission— extend this and implementhas_access(self) -> boolfor custom checks. The class has access toself.user.JWTValidationError— raised for invalid JWTs.AccessDeniedError— raised when permission checks fail.
Security notes
- Always validate
aud(audience) andexp(expiry).more-sso-authchecks these by default when configured. - Rotate keys at the Identity Provider (IdP) and ensure the IdP exposes the new public key at the configured
PUBLIC_KEY_URI.
Troubleshooting & FAQ
Q: Where does the library read the token from?
A: From the Authorization: Bearer <token> header in incoming requests.
Q: How do I check complex permissions (lists, nested claims)?
A: Use a BasePermission subclass and implement has_access() — you have full access to the decoded payload and can evaluate arbitrarily complex logic.
Contributing & License
- Source:
https://github.com/more-retail/moresso - License: MIT
Contributions are welcome via PRs. Please follow the repository's contribution guidelines for tests and code style.
Metadata
Release files for more-sso-auth 1.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| more_sso_auth-1.5.0.tar.gz | 9.6 kB | With provenance | Details for more_sso_auth-1.5.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| more_sso_auth-1.5.0-py3-none-any.whl | Python 3 | none | any | Details for more_sso_auth-1.5.0-py3-none-any.whl |
Total release size: 18.6 kB
Release files / more_sso_auth-1.5.0.tar.gz
| Download URL | more_sso_auth-1.5.0.tar.gz |
|---|---|
| Size | 9.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5e4671a88da1a33b01acd842cc029855a29733cd824589b9693c0a3e612f5465
|
|
BLAKE2b-256 checksum How to use checksums |
01bd183764f0212d1b7ca914e39bf900c96640496e3f9e271b7915e72da1ff77
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 6, 2026.
Transparency logRelease files / more_sso_auth-1.5.0-py3-none-any.whl
| Download URL | more_sso_auth-1.5.0-py3-none-any.whl |
|---|---|
| Size | 9.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9ddf09ca61a6b6e21ea02b3abe4b32b51ec8d009615443263aeb5271cf687343
|
|
BLAKE2b-256 checksum How to use checksums |
f2780bba1390aecfd71b7b78b37239d1983c0c11c7020cad8a36eb18bf4621dc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 6, 2026.
Transparency logSecurity for more-sso-auth 1.5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for more_sso_auth-1.5.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 6, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.