GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - embesozzi/keycloak-openfga-multitenancy-workshop: Keycloak integration with OpenFGA and Apache APISIX for multi-tenancy authentication and authorization at Scale

BranchesTags
Open more actions menu

Latest commit

 

History

7 Commits
7 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
apisix
apisix
 
 
docs
docs
 
 
keycloak
keycloak
 
 
openfga
openfga
 
 
tests
tests
 
 
.env
.env
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
docker-compose.yml
docker-compose.yml
 
 

Repository files navigation

Building Scalable Multi-Tenancy Authentication and Authorization using Open Standards and Open-Source Software: Keycloak, OpenFGA and Apache APISIX

A hands-on workshop for multi-tenant SaaS authorization with open standards and open-source software. Keycloak Organizations model the tenants. The keycloak-openfga-event-publisher extension publishes roles and organization membership to OpenFGA. Apache APISIX allows a request to a tenant's API only if the user has the role and belongs to that tenant.

The workshop is based on the article Building Scalable Multi-Tenancy Authentication and Authorization using Open Standards and Open-Source Software, which explains the architecture in detail.

Tip

New: extending fine-grained authorization to AI agents

Fine-grained authorization doesn't stop at users. AI agents need it too.

When an agent calls MCP tools on behalf of a user, the questions of this workshop come back, with more at stake: who is calling (the user, the agent, or both), what can they do (which tool, on which resource), and where is it enforced.

Agentic Security Labs: hands-on Docker labs that secure MCP tools with open standards, no custom protocols:

  • MCP tool authorization: Agent Gateway is the Policy Enforcement Point (PEP) in front of the MCP server: it asks Keycloak to allow or deny each tools/call, per tool and per user.
  • OpenID AuthZEN: the standard API between the PEP and the PDP, with Keycloak as the AuthZEN Policy Decision Point. Change the PDP without changing the gateway.
  • Cedar policies in Keycloak: the Keycloak Cedar provider brings Cedar to Keycloak authorization: readable policy as code, RBAC and ABAC in one language, policies validated against a schema, and a formally verified engine.

Read more

  • Implementing OpenID AuthZEN in Agent Gateway: MCP Tool Authorization
  • Cedar Policy Engine in Keycloak: Fine-Grained Authorization for Users and AI Agents via AuthZEN
  • AI Agents Require Runtime Authorization: Just In Time Passkey Approval for Sensitive Actions
  • More on the blog: embesozzi.github.io · all links in More workshops, labs and articles

Architecture

architecture

  • Keycloak authenticates users with OpenID Connect. Its Organizations feature models the tenants (org-a, org-b), and the organization scope adds the user's organizations to the tokens.
  • keycloak-openfga-event-publisher (3.2.0) listens to Keycloak admin events and publishes them to OpenFGA:
    • role assignments and composite roles: user:<id> assignee role:<name>, role:<parent> parent role:<child>,
    • organization membership: user:<id> member organization:<alias>.
  • OpenFGA answers the checks against the authorization model.
  • Apache APISIX with the authz-openfga plugin protects each tenant route with several checks combined with AND (or OR), see apisix/routes.yaml.
  • PayPlus portal: a Vue app that signs users in with Keycloak and shows the transactions of their organization.
Authorization model
model
  schema 1.1
type client
type group
  relations
    define assignee: [user, client]
type organization
  relations
    define member: [user]
type role
  relations
    define assignee: [user, client] or assignee from parent or assignee from parent_group
    define parent: [role]
    define parent_group: [group]
type user
Tenant route: role AND organization
- uris: ["/api/org-a/transactions"]
  plugins:
    authz-openfga:
      check:
        condition: AND
        tuples:
        - user_id: claim::sub
          relation: assignee
          object_type: role
          object_id: view_transaction_any
        - user_id: claim::sub
          relation: member
          object_type: organization
          object_id: org-a

Quick start

Requires Git, Docker with Docker Compose, and make. The workshop uses the host ports 4000, 8080, 8081 and 9080, the same as keycloak-openfga-workshop: run one workshop at a time.

Component Version
Keycloak 26.8.2
keycloak-openfga-event-publisher 3.2.0
OpenFGA v1.23.0

Versions are set in .env.

1. Clone the repository

git clone https://github.com/embesozzi/keycloak-openfga-multitenancy-workshop
cd keycloak-openfga-multitenancy-workshop

2. Add the workshop hosts to your hosts file (/etc/hosts)

127.0.0.1  payplus.lab keycloak openfga

3. Start the workshop

make up      # or: docker compose up -d --build

Run make to list all targets (up, down, restart, logs, test, clean). This builds Keycloak with the extension (keycloak/Dockerfile downloads release 3.2.0 and checks its checksum), creates the keycloak store and the model in OpenFGA, and creates the PayPlus realm with its organizations, roles and users (keycloak/init.sh). The extension publishes them to OpenFGA.

4. Open the workshop

Component URL Credentials
PayPlus portal http://payplus.lab:4000 paula.smith / demo1234!
Keycloak Admin Console http://keycloak:8081/admin admin / password
OpenFGA API http://localhost:8080
Apache APISIX http://localhost:9080

5. Stop the workshop

make down    # or: docker compose down -v

What the workshop sets up

Keycloak (realm PayPlus)

  • Organizations enabled, with org-a (domain orga.com) and org-b (domain orgb.com).
  • Roles: account-manager, a composite of view_transaction_any.
  • User paula.smith (password demo1234!): has account-manager and is a member of org-a.
  • The openfga-events-publisher event listener is enabled.

OpenFGA

After the import, the keycloak store has these tuples:

User Relation Object
role:account-manager parent role:view_transaction_any
user:<paula's id> assignee role:account-manager
user:<paula's id> member organization:org-a

Users are identified by their Keycloak id (the sub claim) and organizations by their alias (a key of the organization claim).

Read the tuples
STORE_ID=$(curl -s http://localhost:8080/stores | jq -r '.stores[] | select(.name == "keycloak") | .id')
curl -s -X POST http://localhost:8080/stores/$STORE_ID/read -H 'content-type: application/json' -d '{}' \
  | jq -r '.tuples[].key | "\(.user) \(.relation) \(.object)"'

Use cases

User Role Organization GET /api/org-a/transactions GET /api/org-b/transactions
Paula account-manager org-a ✅ ❌
Use case 1: Paula views the transactions of her organization
  1. Open the PayPlus portal and sign in with Keycloak as paula.smith / demo1234!.

  2. The token's organization claim holds org-a, so the portal calls GET /api/org-a/transactions.

  3. APISIX asks OpenFGA both questions: is Paula assignee of role:view_transaction_any (through account-manager), and member of organization:org-a? Both are allowed, so APISIX returns the transactions.

    payplus-1

Use case 2: Paula cannot view the transactions of another organization
  1. Call the API of org-b with Paula's token.

  2. Paula has the role, but she is not a member of org-b, so the AND check fails and APISIX returns 403.

    payplus-2

Try it: add Paula to org-b and watch OpenFGA follow

In the Admin Console, open org-b > Members > Add member > paula.smith. The extension publishes user:<paula's id> member organization:org-b, and GET /api/org-b/transactions is now allowed. Remove her and the extension deletes the tuple.

Automated test

tests/test_workshop.py (pytest) checks the running workshop end to end: the tuples in OpenFGA, the organization claim in the token, and the tenant routes of APISIX. It runs in a container on the workshop network. CI runs it on every push and pull request.

make up
make test    # docker run --rm --network keycloak-openfga-multitenancy-workshop_default <tests image>

Warning

The import creates a smoke-test client with the password grant for the tests. Testing only, not recommended (RFC 9700).

More workshops, labs and articles

Agentic AI security

  • Agentic Security Labs: hands-on labs for securing AI agents and the MCP tools they call with open identity and authorization standards (Keycloak, OpenID AuthZEN, Cedar, Agent Gateway).
  • Implementing OpenID AuthZEN in Agent Gateway: MCP Tool Authorization
  • Cedar Policy Engine in Keycloak: Fine-Grained Authorization for Users and AI Agents via AuthZEN, with the Keycloak Cedar authorization provider
  • Agentic AI Security: Protecting Interactive Keycloak MCP App with OpenID AuthZEN Gateway for FGA
  • AI Agents Require Runtime Authorization: Just In Time Passkey Approval for Sensitive Actions
  • Mastering Secure APIs and AI Agents with Zero Trust and Fine-Grained Authorization Based on OpenID AuthZEN

Keycloak and OpenFGA

  • keycloak-openfga-workshop: the first workshop, ReBAC for a product catalog with protection at the API and at the gateway
  • Building Scalable Multi-Tenancy Authentication and Authorization using Open Standards and Open-Source Software
  • keycloak-openfga-event-publisher: the Keycloak extension used in this workshop (organization support since 3.2.0)
  • apisix-authz-openfga: the APISIX plugin behind the tenant routes
  • Keycloak integration with OpenFGA (based on Zanzibar) for Fine-Grained Authorization at Scale (ReBAC)
  • Mastering Access Control: Implementing Low-Code Authorization Based on ReBAC and Decoupling Pattern

More

  • Blog: embesozzi.github.io: posts on IAM, passkeys and AI agent security
  • Book: Keycloak: Identity and Access Management for Modern Applications, 2nd Edition

About

Keycloak integration with OpenFGA and Apache APISIX for multi-tenancy authentication and authorization at Scale

embesozzi.medium.com/building-scalable-multi-tenancy-authentication-and-authorization-using-open-standards-and-7341fcd87b64

Topics

api-gatewayapisixapisix-pluginauthenticationauthorizationauthzenidentity-provider-idpkeycloakkeycloak-pluginmultitenancyoauth2openfgaopenid-connect

Resources

Readme
Apache-2.0 license
Activity

Stars

17 stars

Watchers

1 watching

Forks

2 forks
Report repository

Releases

Sponsor this project

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.