:rocket: Try it for free in the new Phase Two keycloak managed service. Go to Phase Two for more information.
Single realm, multi-tenancy for SaaS apps
This project intends to provide a range of Keycloak extensions focused on solving several of the common use cases of multi-tenant SaaS applications that Keycloak does not solve out of the box.
The extensions herein are used in the Phase Two cloud offering, and are released here as part of its commitment to making its core extensions open source. Please consult the license for information regarding use.
:exclamation: See our note regarding Keycloak's upcoming organizations feature.
If you search for "multi-tenant Keycloak", you'll find several opinionated approaches, each promising, and each with their own trade-offs. This project represents one such approach. It was built initially for a multi-tenant, public cloud, SaaS application. It has now been, in the form of the Phase Two cloud offering, adopted by several other companies for the same purpose.
Other approaches that we tried and decided against were:
But each of these approaches had trade-offs of scale or frailty we found undesirable or unacceptable to meet our requirements. Instead, we opted to make Organizations, and their Invitations, Roles and Memberships first-class entities in Keycloak.
We recently did a presentation at Keycloak DevDay 2024 on the features of the keycloak-orgs extension. Watch the full video for an introduction and more information about what is possible
The easiest way to get started is our Docker image. Documentation and examples for using it are in the phasetwo-containers repo. The most recent version of this extension is included.
Checkout this project and run mvn clean install
, which will build the source, run all unit/integration tests, and produce a jar in the target/
directory.
For more information you can refer to cypress-tests.
The maven build only produces a jar of the code here, and some additional libraries are necessary when using this library. If you are intending to use this library outside of the Phase Two Docker image, please consult the pom.xml
file in the phasetwo-containers repo for more information on what is required. Furthermore, there are some uses of the keycloak-admin-client
by this library. Because of the way Quarkus does augmentation of this dependency, it is necessary to include this when building Keycloak itself. Our image has these changes, but they will never be included in the default Keycloak image. We encourage you to either use our base Docker image that includes this, or see the pom.xml
diffs for an example of how to do it yourself.
During the first run, some initial migrations steps will occur:
realm-management
client roles (view-organizations
and manage-organizations
) will be be added to each realm.If you are using the extension as bundled in the Docker image or by building our Admin UI theme, you must take an additional step in order to show that theme. In the Admin Console UI, go to the Realm Settings -> Themes page and select phasetwo.v2
. Then, the "Organizations" section will be available in the left navigation. Because of a quirk in Keycloak, if you are logging in to the master
realm, the theme must be set in that realm, rather than the realm you wish to administer.
Although it has been developed and working since Keycloak 9.0.0, the extensions are currently known to work with Keycloak > 17.0.0. Other versions may work also. Additionally, because of the fast pace of breaking changes since Keycloak "X" (Quarkus version), we don't make any guaranteed that this will work with any version other than it is packaged with in the Docker image.
We've adopted a similar model that Keycloak uses for making the Organization data available to the application. There is a custom SPI that makes the OrganizationProvider available. The methods provided are:
OrganizationModel createOrganization(
RealmModel realm, String name, UserModel createdBy, boolean admin);
OrganizationModel getOrganizationById(RealmModel realm, String id);
Stream<OrganizationModel> searchForOrganizationStream(
RealmModel realm,
Map<String, String> attributes,
Integer firstResult,
Integer maxResults,
Optional<UserModel> member);
Long getOrganizationsCount(RealmModel realm, String search);
boolean removeOrganization(RealmModel realm, String id);
void removeOrganizations(RealmModel realm);
Stream<OrganizationModel> getOrganizationsStreamForDomain(
RealmModel realm, String domain, boolean verified);
Stream<OrganizationModel> getUserOrganizationsStream(RealmModel realm, UserModel user);
Stream<InvitationModel> getUserInvitationsStream(RealmModel realm, UserModel user);
The OrganizationProvider returns model delegates that wrap the underlying entities and provide conveniences for working with the data. They are available in the io.phasetwo.service.model
package.
There are JPA entities that represent the underlying tables that are available in the io.phasetwo.service.model.jpa.entity
package. The providers and models are implemented using these entities in the io.phasetwo.service.model.jpa
package.
A group of custom REST resources are made available for administrator and customer use and UI. Current documentation on the available resource methods is in this openapi.yaml specification file, and you can find browsable documentation on the Phase Two API site.
For more information you can refer to: Events
For more information you can refer to: Import/Export
There are currently two OIDC mapper that adds either Organization attributes or Organization membership and roles to the token. An example of the format of the membership addition to the token is:
"organizations": {
"5aeb9aeb-97a3-4deb-af9f-516615b59a2d" : {
"name": "foo",
"roles": [ "admin", "viewer" ]
}
}
You can configure the mapper, by going to Clients > your-client-name > Client scopes > your-client-name-dedicated and choosing to add a new mapper By configuration. Once selected, choose the Organization Role mapper from the list and specify the details like the following:
For most use cases, set the Invitation
required action to Enabled
in Authentication->Required Actions. It does not need to be set as a default. It will automatically check on each login if the user has outstanding Invitations to Organizations, and enable itself.
There are some non-standard flows where the required action does not do this detection. For these cases, there is a custom Authenticator you can add to a copy of the standard browser flow. Add the Invitation
authenticator as a "REQUIRED" execution following the "Username Password Form" as a child of the forms group. Both the Required Action and the Authenticator check to see if the authenticated user has outstanding Invitations to Organizations, and then adds the Required Action that they must complete to accept or reject their Invitations following a successful authentication.
Note that it is a default to require that an email address be verified, as it would present a security issue to allow anyone who uses an email address to register to join an organization without verifying that the user is the owner of that email address. Because of that, it is assumed that you are using invitations in conjunction with setting Verify Email as a default Required Action.
Organizations may optionally be given permission to manage their own IdP. The custom resources that allow this write a configuration in the IdP entities that is compatible with a 3rd party extension that allows for IdP discovery based on email domain configured for the Organization. It works by writing the home.idp.discovery.orgs
value into the config
map for the IdP. Information on further configuration is available at sventorben/keycloak-home-idp-discovery. However, please note that the internal discovery portion has been forked from his version, and does not look up IdPs in the same way.
These are the configuration options for the "Home IdP Discovery" Authenticator. It will need to be placed in your flow as a replacement for a "Username form", or after another Authenticator/Form that sets the ATTEMPTED_USERNAME
note.
It is possible to define an active organization and switch it. It's currently based on user's attribute and the active organization id, name, role or attribute can be mapped into tokens with a configurable mapper.
For more information you can refer to active-organization.
In the Organizations
tab it is possible to switch between two master configuration settings: "Create Admin User" and "Shared IDPs"
The Create Admin User
setting controls the creation of the initial administrator when a new organization is created.
The Shared IDPs
will give a keycloak admin user the possibility to control the assignment of a Keycloak identity provider in the context of multiple organization. If turned on
the same IDP can be shared between multiple organizations. If turned off
a IDP can be assigned to one organization. Switching this setting from on
to off
will erase all the IDP settings the current organizations have.
These configs are persisted in the realm config under the flags _providerConfig.orgs.config.createAdminUser
and _providerConfig.orgs.config.sharedIdps
It is possible to share the same IDP between multiple organizations by switching on
the Shared IDPs
config.
This offers the possibility to login using the same IDP to different organizations by using the IdP Discovery method.
For a shared IdP if the Post login flow
authentication flow is set to post org broker login
the Add User to Org
authenticator will add the new member to all organizations which contain the user email domain in their domains configuration list.
We’ve changed the license of our core extensions from the AGPL v3 to the Elastic License v2.
Portions of the Home IdP Discovery code are Copyright (c) 2021-2024 Sven-Torben Janus, and are licensed under the MIT License.
All other documentation, source code and other files in this repository are Copyright 2024 Phase Two, Inc.