sag_py_auth_brand
This provides a way to secure your fastapi with keycloak jwt bearer authentication. This library bases on sag_py_auth and adds support for instances/brands.
What it does
- Secure your api endpoints
- Verifies auth tokens: signature, expiration, issuer, audience
- Verifies the brand/customer over a token role
- Verifies the instance over a token role
- Verifies the stage over a realm role
- Allows to set additional permissions by specifying further token roles
- Supplies brand information from context
How to use
Installation
pip install sag-py-auth-brand
Secure your apis
First create the fast api dependency with the auth config:
from sag_py_auth import TokenRole
from sag_py_auth_brand.models import AuthConfig
from sag_py_auth_brand.brand_jwt_auth import BrandJwtAuth
from fastapi import Depends
auth_config = BrandAuthConfig("https://authserver.com/auth/realms/projectName", "myaudience", "myinstance", "mystage")
required_roles = [TokenRole("clientname", "adminrole")]
requires_admin = Depends(BrandJwtAuth(auth_config, required_endpoint_roles))
Afterwards you can use it in your route like that:
@app.post("/posts", dependencies=[requires_admin], tags=["posts"])
async def add_post(post: PostSchema) -> dict:
Or if you use sub routes, auth can also be enforced for the entire route like that:
router = APIRouter()
router.include_router(sub_router, tags=["my_api_tag"], prefix="/subroute",dependencies=[requires_admin])
Get brand information
See sag_py_auth to find out how to access the token and user info.
Furthermore you can get the brand by accessing it over the context:
from sag_py_auth_brand.request_brand_context import get_request_brand as get_brand_from_context
brand = get_brand_from_context()
This works in async calls but not in sub threads (without additional changes).
See:
- https://docs.python.org/3/library/contextvars.html
- https://kobybass.medium.com/python-contextvars-and-multithreading-faa33dbe953d
Log the brand
It is possible to log the brand by adding a filter.
import logging
from sag_py_auth_brand.request_brand_logging_filter import RequestBrandLoggingFilter
console_handler = logging.StreamHandler(sys.stdout)
console_handler.addFilter(RequestBrandLoggingFilter())
The filter provides the field request_brand with the brand.
How a token has to look like
{
"iss": "https://authserver.com/auth/realms/projectName",
"aud": ["audienceOne", "audienceTwo"],
"typ": "Bearer",
"azp": "public-project-swagger",
"preferred_username": "preferredUsernameValue",
.....
"realm_access": {
"roles": ["myStage"]
},
"resource_access": {
"role-instance": {
"roles": ["myInstance"]
},
"role-brand": {
"roles": ["myBrand"]
},
"role-endpoint": {
"roles": ["permissionOne", "permissionTwo"]
}
}
}
- role-endpoint is just required for permission checks of the api endpoint
How to start developing
With vscode
Just install vscode with dev containers extension. All required extensions and configurations are prepared automatically.
With pycharm
- Install latest pycharm
- Install pycharm plugin BlackConnect
- Install pycharm plugin Mypy
- Configure the python interpreter/venv
- pip install requirements-dev.txt
- pip install black[d]
- Ctl+Alt+S => Check Tools => BlackConnect => Trigger when saving changed files
- Ctl+Alt+S => Check Tools => BlackConnect => Trigger on code reformat
- Ctl+Alt+S => Click Tools => BlackConnect => "Load from pyproject.yaml" (ensure line length is 120)
- Ctl+Alt+S => Click Tools => BlackConnect => Configure path to the blackd.exe at the "local instance" config (e.g. C:\Python310\Scripts\blackd.exe)
- Ctl+Alt+S => Click Tools => Actions on save => Reformat code
- Restart pycharm
How to publish
- Update the version in setup.py and commit your change
- Create a tag with the same version number
- Let github do the rest
How to test
To avoid publishing to pypi unnecessarily you can do as follows
- Tag your branch however you like
- Use the chosen tag in the requirements.txt-file of the project you want to test this library in, eg.
sag_py_auth_brand==<your tag> - Rebuild/redeploy your project
Metadata
Release files for sag-py-auth-brand 1.2.5
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 |
|---|---|---|---|---|
| sag_py_auth_brand-1.2.5.tar.gz | 8.3 kB | Without provenance | Details for sag_py_auth_brand-1.2.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sag_py_auth_brand-1.2.5-py3-none-any.whl | Python 3 | none | any | Details for sag_py_auth_brand-1.2.5-py3-none-any.whl |
Total release size: 15.3 kB
Release files / sag_py_auth_brand-1.2.5.tar.gz
| Download URL | sag_py_auth_brand-1.2.5.tar.gz |
|---|---|
| Size | 8.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bc57125645facae24e783f2da6157aafd0d44316ee80a5b246e3bed90468d869
|
|
BLAKE2b-256 checksum How to use checksums |
017df47a719c7dcf0c6f7a8657e7636c6298bc1d9706ef3214b7eaab48b4a313
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / sag_py_auth_brand-1.2.5-py3-none-any.whl
| Download URL | sag_py_auth_brand-1.2.5-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fcd948146f80d0787d38900b43ce8df68ab98727263e83476b5a941ab0c240e6
|
|
BLAKE2b-256 checksum How to use checksums |
a9342f5ed5304a3dfe9ff478f943a01684d0394a42447cb871062d4f98e46b7b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Security for sag-py-auth-brand 1.2.5
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A 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.