Skip to main content

FastAPI-Keycloak Integration

This package is used when you want to manage users in your services in a keycloak environment.

Install

Installation command from pypi:

 pip install fastapi-keycloak-sso

Setting up the authenticate function and UserPayload to use authentication:

from fastapi import FastAPI, Depends
from fastapi_keycloak_sso.auth import UserPayload, authenticate

app = FastAPI()

@app.get('/')
def test_api(user: UserPayload = Depends(authenticate)):
    pass

For proper identification, you should use the 'user' parameter in your route.

Environment Configurations

To use the authentication features of this package, you need to set the following in your '.env' file:

(Preferably the '.env' file is next to your project's main file)

# Keycloak SSO
KEYCLOAK_SERVER_URL=https://sso.domain # if using in dokcer : https://<keycloak_container>:8443
KEYCLOAK_ISSUER_PREFIX=https://sso.domain 
KEYCLOAK_REALM=your_realm
KEYCLOAK_CLIENT_ID=your_client_id 
KEYCLOAK_CLIENT_PK=your_client_primary_key # example: 'test-client'
KEYCLOAK_CLIENT_SECRET=your_client_secret_key
KEYCLOAK_OAUTH_REDIRECT_URI=http://127.0.0.1:8000/auth/callback/ # for login in ssr sites
KEYCLOAK_CLIENT_NAME=your_client_name
KEYCLOAK_CLIENT_TITLE=your_client_title
KEYCLOAK_ALGORITHMS=RS256
KEYCLOAK_SECRET_KEY_ALGORITHM=your_algorithm_secret_key # if using RS256: Go to Realm Settings > Keys > your algorithm public key 

# redis config (to user's data caching)
KEYCLOAK_REDIS_HOST=<your_redis_host>

Predefined fields

Model fields

  • SSOUserField

The default for this field is string type.

Usage Example

from fastapi_keycloak_sso.sso import fields as sso_fields
from sqlalchemy import Column

class UserTest(Base):
    ...
    user_id = Column(sso_fields.SSOUserField)
    

Pydantic fields

  • SSOUserPydanticField
  • SSOUserPydanticWithValidation

If you want to make sure the user exists in your system before saving or reading information, use the 'SSOUserPydanticWithValidation' field.

Usage Example

from fastapi_keycloak_sso.sso import schemas as sso_schemas
from pydantic import BaseModel

class UserSchema(BaseModel):
    ...
    user_id: sso_schemas.SSOUserPydanticField

To get the complete information of the desired user and display it, you can do the following to get a dictionary of information:

from fastapi_keycloak_sso.sso import schemas as sso_schemas
from pydantic import (
    BaseModel,
    field_serializer
)

class UserSchema(BaseModel):
    ...
    user_id = sso_schemas.SSOUserPydanticField
    
    @field_serializer("user_id")
    def serialize_user_id(self, value: sso_schemas.LazySSOUser, _info):
        return value.get_full_data()

Permissions

To use access levels for users in your Keycloak system, you can use the decorators available in the package:

from fastapi_keycloak_sso.decorators import (
    require_roles,
    require_groups,
    require_group_roles,
    require_any_group,
    require_any_role,
    require_all_permissions
)

@app.get('/')
@requiire_groups('test-admin','test-editor')
def test_api(user: UserPayload = Depends(authenticate)):
    pass

All decorators should be close to the rout and the associated permissions should be entered as str inside the decorator.

require_roles:

In this decorator, the user must have all the permissions entered to access rout.

require_groups:

To check that the user must exist in all groups entered in the decorator.

require_group_roles:

@require_group_roles(*group_roles, match_group_roles=False)

Checks if the user has at least one of the specified roles within any group. Use match_group_roles=True to only allow matches where the group name is also explicitly listed via @require_groups.

@app.get('/')
@require_group_roles('manager')  # Any group
@require_group_roles('admin', match_group_roles=True)  # Must match both group and role
def test_api():
    pass

require_any_group:

The user must belong to at least one of the groups listed.

require_any_role:

The user must belong to at least one of the roles listed.

require_all_permissions:

@require_all_permissions(role_titles=[], group_titles=[], group_roles=[], match_group_roles=False)

Combined decorator that allows you to check all types of permissions in one call.

@app.get('/')
@require_all_permissions(
    role_titles=['superuser'],
    group_titles=['group_1'],
    group_roles=['manager'],
    match_group_roles=True
)
def test_api():
    pass

Default APIs

To use default APIs, you must include them in your system app.

from fastapi_keycloak_sso.api.routers.base_routers import router as keycloak_router


app = FastAPI()

app.include_router(keycloak_router)

The package provides a number of default APIs to make it easy to work with your keycloak system:

  • Set token cookie
  • Logout token from cookie
  • Create token
  • Refresh token
  • User profile
  • Group read
  • Find group detail exact
  • Group create
  • Group delete
  • Role read
  • Assign role to group
  • User join to group

Setting permissions on default APIs:

By default, the existing APIs do not have any specific permissions, but you can enforce them by adding the permissions you need to your '.env' file.

It is worth noting that your input permissions are checked against user groups, and the user must have at least one of the entries.

# Keycloak access groups
KEYCLOAK_GROUP_READ_ACCESS=admin,test
KEYCLOAK_GROUP_FIND_ACCESS=admin,test
KEYCLOAK_GROUP_CREATE_ACCESS=admin,test
KEYCLOAK_GROUP_DELETE_ACCESS=admin,test
KEYCLOAK_USER_READ_ACCESS=admin,test
KEYCLOAK_ROLE_READ_ACCESS=admin,test
KEYCLOAK_ASSIGN_ROLE_GROUP_ACCESS=admin,test
KEYCLOAK_JOIN_USER_GROUP_ACCESS=admin,test

Release files for fastapi-keycloak-sso 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-keycloak-sso 0.1.1
File Size Uploaded
fastapi_keycloak_sso-0.1.1.tar.gz 33.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-keycloak-sso 0.1.1
File Interpreter ABI Platform
fastapi_keycloak_sso-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 70.1 kB

Release files / fastapi_keycloak_sso-0.1.1.tar.gz

Download URL fastapi_keycloak_sso-0.1.1.tar.gz
Size 33.4 kB
Tags Source
SHA-256 checksum
How to use checksums
db5f69cd9a495d36f50d488b0411c953df3c47c99b035569d9a65428c65e1335
BLAKE2b-256 checksum
How to use checksums
d6f1bd14cae59bd1fd6fe89fc2f6412fa9bcda47d8eba4625ab09b9d3a997c34
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release files / fastapi_keycloak_sso-0.1.1-py3-none-any.whl

Download URL fastapi_keycloak_sso-0.1.1-py3-none-any.whl
Size 36.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d2b86dbeb16332d2ba8514f1e88efbdd299299b714f19fe3e7be141e76f45db2
BLAKE2b-256 checksum
How to use checksums
2967097c3336e58094b467ec3b220e23377af4597490a1928b7830b4153c99b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.9

Release history Release notifications | RSS feed

0.1.3

2 release files

0.1.2

2 release files

This release

0.1.1 This release

2 release files

0.1.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page