Skip to main content

Helpers to integrate Single Sign-On in nameko-based applications using Keycloak.

Project description

docs

Documentation Status

tests

Github Actions Build Status
Coverage Status

package

PyPI Package latest release PyPI Wheel Supported versions Supported implementations
Commits since latest release

This package is still work in progress.

Helpers to integrate Single Sign-On in nameko-based applications using Keycloak.

Features

  • nameko service mixin

  • database and model-agnostic user management

  • authentication service

  • fake Keycloak client for use in tests

Installation

pip install nameko-keycloak

You can also install the in-development version with:

pip install https://github.com/emplocity/nameko-keycloak/archive/master.zip

Usage

To set up SSO with Keycloak in your nameko service, follow these steps.

  1. Get Keycloak configuration from realm -> Clients -> Installation, download as Keycloak OIDC JSON.

    Save this configuration in a .json file.

  2. Add the mixin and dependency provider to your service and point to OIDC JSON config:

    from nameko_keycloak.dependencies import KeycloakProvider
    from nameko_keycloak.service import KeycloakSsoServiceMixin
    
    class MyService(KeycloakSsoServiceMixin):
        keycloak = KeycloakProvider("/tmp/keycloak.json")
  3. Set up URLs for HTTP endpoints. The mixin exposes five methods prefixed with keycloak_, which you should use in your HTTP service. Delegate from your entrypoints like this:

    @http("GET", "/login")
    def login_sso(self, request):
        return self.keycloak_login_sso(request)

    This way it is up to you to control the URL routes and any middleware or extra request handling (such as CORS headers).

  4. Implement a fetch_user() method on your service that takes user’s email address as a single argument and returns a user instance for that email (or None if no such user exists in whatever storage you’re using).

    For example:

    def fetch_user(self, email: str) -> Optional[User]:
        user_manager = UserManager(self.db.session)
        return user_manager.get_by_email(email)

    This method is used to ensure that there is a local application user who matches the global identity stored in Keycloak.

  5. (Optionally) Implement success and failure hook methods on your service.

    If you provide keycloak_success() method, the mixin will call it after successful login and redirect from Keycloak back to your application. The method will receive currently logged user as its argument. Similarly the mixin will call keycloak_failure() upon Keycloak errors.

    Example:

    def keycloak_success(self, user: User) -> None:
        logger.info(f"Successful login: {user=}")
    
    def keycloak_failure(self) -> None:
        logger.error("Failed to log in")

Documentation

https://nameko-keycloak.readthedocs.io/

Authors

nameko-keycloak is developed and maintained by Emplocity.

License

This work is released under the Apache 2.0 license.

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

nameko-keycloak-0.6.0.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

nameko_keycloak-0.6.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file nameko-keycloak-0.6.0.tar.gz.

File metadata

  • Download URL: nameko-keycloak-0.6.0.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for nameko-keycloak-0.6.0.tar.gz
Algorithm Hash digest
SHA256 81d4abc7930215da2b3f29c3a6c1aeb01044e94a9e3623df823ef9ba46de48ac
MD5 f3d5922dc8cdb9f0c91384bf82a72352
BLAKE2b-256 392e1ec8036665551d4c420c37a90fe80cb284d96c9ea5c30758f6e518d92845

See more details on using hashes here.

File details

Details for the file nameko_keycloak-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: nameko_keycloak-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for nameko_keycloak-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 972ae8b2ff542b8b4156766665f46878c75604f900ddd5654869e5bd65cb5d71
MD5 f3f63a6dd3abaa30aa30ca904cf38dcc
BLAKE2b-256 05fcdb1381da4edaf4acbbdeff77296e5c93387900a9f9702031bd21abc4b09c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page