Identity Broker
An Identity Broker is an intermediary service that connects multiple service providers with different identity providers.
As an intermediary service, the identity broker is responsible to create a trust relationship with an external
identity provider in order to use its identities to access internal services exposed by service providers.
From an user perspective, an identity broker provides an user-centric and centralized way to manage identities across
different security domains or realms, where an existing account can be linked with one or more identities from different
identity providers or even created based on the identity information obtained from them.
An identity provider is usually based on a specific protocol in order to authenticate and communicate authentication and
authorization information to their users. It can be a social provider such as Facebook, Google or Twitter, a business partner
which you want to allow its users to access your services or a cloud-based identity service that you want to integrate with.
Usually, identity providers are based on the following protocols:
SAML v2.0
OpenID Connect v1.0
oAuth v2.0
In the next sections we'll see how to configure and use Keycloak as an identity broker, covering some important aspects such as:
Social Authentication
OpenID Connect v1.0 Brokering
SAML v2.0 Brokering
Identity Federation
Overview
When using Keycloak as an identity broker, users are not forced to provide their credentials in order to
authenticate in a specific realm. Instead of that, they are presented with a list of identity providers from
where they can pick one and authenticate. The following diagram demonstrates the steps involved when using
Keycloak to broker an external identity provider:
User is not authenticated and requests a protected resource in a service provider.
The service provider redirects the user to Keycloak to authenticate.
At this point the user is presented to the login page where there is a list of identity providers supported by a realm.
User selects one of the identity providers by clicking on its respective button or link.
Keycloak issues an authentication request to the target identity provider asking for authentication and the
user is redirect to the login page or just to a consent page in the identity provider.
The connection properties and other configuration options for the identity provider were previously
set by the administrator in the admin console.
User provides his credentials or consent in order to authenticate in the identity provider.
Upon a successful authentication by the identity provider, the user is redirected back to Keycloak
with an authentication response. Usually this response contains a security token that will be used
by Keycloak to trust the authentication performed by the identity provider and retrieve information
about the user.
Now Keycloak is going to check if the response from the identity provider is valid. If valid, it will create an user
or just skip that if the user already exists. If it is a new user, Keycloak will ask informations about the user to the identity provider
(or just read that from a security token) and create the user locally. This is what we call identity federation.
If the user already exists Keycloak will ask him to link the identity returned from the identity provider
with his existing account. A process that we call account linking.
At the end of this step, Keycloak authenticates the user and issues its own token in order to access
the requested resource in the service provider.
Once the user is locally authenticated, Keycloak redirects the user to the service provider by sending
the token previously issued during the local authentication.
The service provider receives the token from Keycloak and allows access to the protected resource.
There are some variations of this flow that we will talk about later. For instance, instead of present a
list of identity providers, the service provider can automatically select a specific one to authenticate an user.
Or you can tell Keycloak to force the user to provide additional information before federate his identity.
Different protocols may require different authentication flows. At this moment, all the identity providers
supported by KeyCloak use a flow just like described above. However, despite the protocol in use, user experience should
be pretty much the same.
As you may notice, at the end of the authentication process Keycloak will always issue its own token
to service providers, what means that service providers are completely decoupled from external identity providers.
They don't need to know which protocol (eg.: SAML, OpenID Connect, oAuth, etc) was used or how the user's
identity was validated. They only need to know about Keycloak !
Configuration
The identity broker configuration is all based on identity providers. Identity providers are created for each
realm and by default they are enabled for every single application. That means that users from a realm can use any
of the registered identity providers when signing in to an application.
In order to create an identity provider, follow these steps:
In the admin console, select a realm.
On the left side menu, click on Settings.
Select the Identity Provider tab on the settings page.
You should now be able to see a table that lists all registered identity providers.
To add a new identity provider, click the select box on the top right of this table and select
which type of identity provider you want to create.
Identity providers are organized in two main categories:
Social
Social providers allows you to enable social authentication to your realm.
Keycloak makes it easy to let users log in to your application using an existing account with a social network.
Currently Facebook, Google and Twitter are supported with more planned for the future.
Protocol-based
Protocol-based providers are those that rely on a specific protocol in order to authenticate and authorize
users. They allow you to connect to any identity provider compliant with a specific protocol.
Keycloak provides support for SAML v2.0 and OpenID Connect v1.0 protocols.
It makes it easy to configure and broker any identity provider based on these open standards.
Although each type of identity provider has its own configuration options, all of them share some very common configuration.
Regardless the identity provider you are creating, you'll see the following configuration options avaiable:
Common Configuration
Configuration
Description
Alias
The alias is an unique identifier for an identity provider. It is used to reference internally an identity provider.
Some protocols require a redirect uri or callback url in order to communicate with an identity provider. For instance, OpenID Connect.
In this case, the alias is used to build the redirect uri.
Every single identity provider must have an alias. For example, facebook, google, idp.acme.com, etc.
Name
You can give a friendly name to an identity provider.
The name will be used, for example, to display a link or a button in the login page.
Enabled
Allows you to enable or disable an identity provider.
When disabled, the identity provider will not be available from the login page and can not
be used by any other means.
Update Profile on First Login
Allows you to force users to update their profile right after the authentication finishes and
before the account is actually created in Keycloak. When enabled, users will be presented with the
update profile page asking for additional information in order to federate their identities.
If disabled, the account will be created with the minimal information obtained from the identity provider
during the authentication process.
Store Tokens
Allows you to store tokens issued by an identity provider during the authentication of a specific user.
Tokens are stored and can be retrieved later.
This options is useful if you are using social authentication and need to access the token in order to invoke the
API of a social provider on behalf of the user.
On the next sections, we'll see how to configure each type of identity provider individually.
Social Identity Providers
Forcing users to register to your realm when they want to access applications is hard.
So is trying to remember yet another username and password combination.
Social identity providers makes it easy for users to register on your realm and quickly sign in using a social network.
Keycloak provides built-in support for the most common social networks out there, such as Google, Facebook, Twitter and
even Github.
Google
To enable login with Google you first have to create a project and a client in the
Google Developer Console. Then you need to copy
the client id and secret into the Keycloak Admin Console.
Let's see first how to create a project with Google.
Log in to the Google Developer Console. Click the
Create Project button. Use any value for Project name and
Project ID you want, then click the Create button. Wait for the project to
be created (this may take a while).
Once the project has been created click on APIs & auth in sidebar on the left. To retrieve
user profiles the Google+ API has to be enabled. Scroll down to find it in the list. If its
status is OFF, click on OFF to enable it (it should move to the top of
the list and the status should be ON).
Now click on the Consent screen link on the sidebar menu on the left. You must specify
a project name and choose an email for the consent screen. Otherwise users will get a login error. There's
other things you can configure here like what the consent screen looks like. Feel free to play around with this.
Now click Credentials in the sidebar on the left. Then click
Create New Client ID. Select Web application as
Application type. Empty the Authorized Javascript origins textarea.
Click the Create Client ID button.
Copy Client ID and Client secret.
Now that you have the client id and secret, you can proceed with the creation of a Google Identity Provider in Keycloak. As follows:
Select the Google identity provider from the drop-down box on the top right corner of the identity providers table in Keycloak's Admin Console. You should be presented with a specific page to configure the selected provided.
Copy the client id and secret to their corresponding fields in the Keycloak Admin Console. Click Save.
Once you create the identity provider in Keycloak, you must update your Google project with the redirect url that was
generated to your identity provider.
Open the Google Developer Console and select your project. Click Credentials in the
sidebar on the left. In Authorized redirect URI insert the redirect uri created by Keycloak. The redirect uri
usually have the following format: http://{host}:{port}/auth/realms/{realm}/broker/{provider_alias}.
You can always get the redirect url for a specific identity provider from the table presented when you
click on the 'Identity Provider' tab in Realm > Settings.
That is it! This is pretty much what you need to do in order to setup this identity provider.
The table below lists some additional configuration options you may use when configuring this provider.
Configuration Options
Configuration
Description
Default Scopes
Allows you to manually specify the scopes that users must authorize when authenticating with this provider. For a complete list of scopes, please take a look at https://developers.google.com/oauthplayground/. By default, Keycloak uses the following scopes: openid profile email
Facebook
To enable login with Facebook you first have to create an application in the
Facebook Developer Console. Then you need to copy
the client id and secret into the Keycloak Admin Console.
Let's see first how to create an application with Facebook.
Log in to the Facebook Developer Console. Click
Apps in the menu and select Create a New App. Use any value for
Display Name and Category you want, then click the
Create App button. Wait for the project to be created (this may take a while). If after
creating the app you are not redirected to the app settings, click on Apps in the
menu and select the app you created.
Once the app has been created click on Settings in sidebar on the left. You must specify
a contact email. Save your changes. Then click
on Advanced. Under Security make sure
Client OAuth Login is enabled. Scroll down and click on the Save Changes button.
Click Status & Review and select YES for Do you want
to make this app and all its live features available to the general public?. You will
not be able to set this until you have provided a contact email in the general settings of this application.
Click Basic. Copy App ID and App Secret
(click show) from the Facebook Developer Console.
Now that you have the client id and secret, you can proceed with the creation of a Facebook Identity Provider in Keycloak. As follows:
Select the Facebook identity provider from the drop-down box on the top right corner of the identity providers table in Keycloak's Admin Console. You should be presented with a specific page to configure the selected provided.
Copy the client id and secret to their corresponding fields in the Keycloak Admin Console. Click Save.
Once you create the identity provider in Keycloak, you must update your Facebook application with the redirect url that was
generated to your identity provider.
Open the Facebook Developer Console and select your application. Click
on Advanced. Under Security make sure
Client OAuth Login is enabled. In Valid OAuth redirect URIs insert
the redirect uri created by Keycloak. The redirect uri
usually have the following format: http://{host}:{port}/auth/realms/{realm}/broker/{provider_alias}.
You can always get the redirect url for a specific identity provider from the table presented when you
click on the 'Identity Provider' tab in Realm > Settings.
That is it! This pretty much what you need to do in order to setup this identity provider.
The table below lists some additional configuration options you may use when configuring this provider.
Configuration Options
Configuration
Description
Default Scopes
Allows you to manually specify the scopes that users must authorize when authenticating with this provider. For a complete list of scopes, please take a look at https://developers.facebook.com/docs/graph-api. By default, Keycloak uses the following scopes: email
Twitter
To enable login with Twtter you first have to create an application in the
Twitter Developer Console. Then you need to copy
the consumer key and secret into the Keycloak Admin Console.
Let's see first how to create an application with Twitter.
Log in to the Twitter Developer Console. Click the
Create a new application button. Use any value for Name,
Description and Website you want. Insert the social callback url
in Callback URL. Then click Create your Twitter application.
Now click on Settings and tick the box Allow this application to be used to Sign in with Twitter,
then click on Update this Twitter application's settings.
Now click API Keys tab. Copy API key and API secret from the
Twitter Developer Console.
Twitter doesn't allow localhost in the redirect URI. To test on a local server
replace localhost with 127.0.0.1.
Now that you have the client id and secret, you can proceed with the creation of a Twitter Identity Provider in Keycloak. As follows:
Select the Twitter identity provider from the drop-down box on the top right corner of the identity providers table in Keycloak's Admin Console. You should be presented with a specific page to configure the selected provided.
Copy the client id and secret to their corresponding fields in the Keycloak Admin Console. Click Save.
That is it! This pretty much what you need to do in order to setup this identity provider.
The table below lists some additional configuration options you may use when configuring this provider.
Configuration Options
Configuration
Description
Default Scopes
Not supported by Twitter.
Github
To enable login with GitHub you first have to create an application in
GitHub Settings. Then you need to copy
the client id and secret into the Keycloak Admin Console.
Let's see first how to create an application with GitHub.
Log in to GitHub Settings. Click the
Register new application button. Use any value for Application name,
Homepage URL and Application Description you want. Click the
Register application button.
Copy Client ID and Client Secret from the GitHub Settings.
Now that you have the client id and secret, you can proceed with the creation of a Github Identity Provider in Keycloak. As follows:
Select the Github identity provider from the drop-down box on the top right corner of the identity providers table in Keycloak's Admin Console. You should be presented with a specific page to configure the selected provided.
Copy the client id and secret to their corresponding fields in the Keycloak Admin Console. Click Save.
Once you create the identity provider in Keycloak, you must update your GitHub application with the redirect url that was
generated to your identity provider.
Open the GitHub Settings and select your application. In Authorization callback URL
insert the redirect uri created by Keycloak. The redirect uri
usually have the following format: http://{host}:{port}/auth/realms/{realm}/broker/{provider_alias}.
You can always get the redirect url for a specific identity provider from the table presented when you
click on the 'Identity Provider' tab in Realm > Settings.
That is it! This pretty much what you need to do in order to setup this identity provider.
The table below lists some additional configuration options you may use when configuring this provider.
Configuration Options
Configuration
Description
Default Scopes
Allows you to manually specify the scopes that users must authorize when authenticating with this provider. For a complete list of scopes, please take a look at https://developer.github.com/v3/oauth/#scopes. By default, Keycloak uses the following scopes: user:email
LinkedIn
To enable login with LinkedIn you first have to create an application in
LinkedIn Developer Network. Then you need to copy
the client id and secret into the Keycloak Admin Console.
Let's see first how to create an application with LinkedIn.
Log in to LinkedIn Developer Network. Click the
Add New Application link. Use any value for Application Name,
Website URL, Description, Developer Contact Email and Phone you want.
Select r_basicprofile and r_emailaddress in the Default Scope section.
Click the Add Application button.
Copy Consumer Key / API Key and Consumer Secret / Secret Key from the shown page.
Now that you have the client id and secret, you can proceed with the creation of a LinkedIn Identity Provider in Keycloak. As follows:
Select the LinkedIn identity provider from the drop-down box on the top right corner of the identity providers table in Keycloak's Admin Console. You should be presented with a specific page to configure the selected provided.
Copy the client id and secret to their corresponding fields in the Keycloak Admin Console. Click Save.
Once you create the identity provider in Keycloak, you must update your LinkedIn application with the redirect url that was
generated to your identity provider.
Open the LinkedIn Developer Network and select your application. In OAuth 2.0 Redirect URLs
insert the redirect uri created by Keycloak. The redirect uri
usually have the following format: http://{host}:{port}/auth/realms/{realm}/broker/{provider_alias}/endpoint.
You can always get the redirect url for a specific identity provider from the table presented when you
click on the 'Identity Provider' tab in Realm > Settings.
That is it! This pretty much what you need to do in order to setup this identity provider.
The table below lists some additional configuration options you may use when configuring this provider.
Configuration Options
Configuration
Description
Default Scopes
Allows you to manually specify the scopes that users must authorize when authenticating with this provider.
For a complete list of scopes, please take a look at application configuration in LinkedIn Developer Network. By default, Keycloak uses the following scopes: r_basicprofile r_emailaddress
StackOverflow
To enable login with StackOverflow you first have to register an OAuth application on
StackApps. Then you need to copy the client id, secret and key into the Keycloak Admin Console.
Let's see first how to create an application with StackOverflow.
Go to registering your application on Stack Apps url and login here.
Use any value for Application Name, Application Website and Description you want.
Set OAuth Domain to the domain where your Keycloak instance runs.
Click the Register Your Application button.
Copy Client Id, Client Secret and Key from the shown page.
Now that you have the client id, secret and key, you can proceed with the creation of a StackOverflow Identity Provider in Keycloak. As follows:
Select the StackOverflow identity provider from the drop-down box on the top right corner of the identity providers table in Keycloak's Admin Console. You should be presented with a specific page to configure the selected provided.
Copy the client id, client secret and key to their corresponding fields in the Keycloak Admin Console. Click Save.
That is it! This pretty much what you need to do in order to setup this identity provider.
The table below lists some additional configuration options you may use when configuring this provider.
Configuration Options
Configuration
Description
Default Scopes
Allows you to manually specify the scopes that users must authorize when authenticating with this provider.
For a complete list of scopes, please take a look at application configuration in StackExchange API Authentication documentation. Keycloak uses the empty scope by default.
SAML v2.0 Identity Providers
Keycloak can broker identity providers based on the SAML v2.0 protocol.
In order to configure a SAML identity provider, follow these steps:
Select the SAML v2.0 identity provider from the drop-down box on the top right
corner of the identity providers table in Keycloak's Admin Console.
You should be presented with a specific page to configure the selected provider.
When configuring a SAML identity provider you are presented with different configuration options in order to
properly communicate and integrate with the external identity provider.
In this case, you must keep in mind that Keycloak will act as an Service Provider that issues authentication requests(AuthnRequest)
to the external identity provider.
Configuration Options
Configuration
Description
Import IdP SAML Metadata
When creating a new identity provider, you may just upload the SAML Metadata for the brokered IdP (IDPSSODescriptor). In this case, Keycloak will read all the necessary configuration from the metadata and automatically configure the identity provider for you.
Single Sign-On Service Url
Allows you to specify the URL that will be used to send SAML authentication requests.
NameID Policy Format
Allows you to specify a NameID Policy that will be sent in the SAML authentication request.
Validating X509 Certificate
Allows you to specify the certificate in PEM format that will be used to validate signatures for all messages received from the brokered identity provider.
Want AuthnRequests Signed
Allows you to specify whether the brokered identity provider is expecting signed SAML authentication requests or not.
Force Authentication
Allows you to tell the brokered identity provider that user must be authenticated even if he was previously authenticated (re-authentication) in the same session.
Validate Signature
Enable or disable signature validation of any message returned by the brokered identity provider.
HTTP-POST Binding Response
Allows you to specify if responses from the brokered identity providers are returned using the HTTP-POST or HTTP-Redirect protocol bindings. If enabled, only responses using HTTP-POST binding are accepted.
HTTP-POST Binding for AuthnReques
Allows you to specify wheter SAML authentication requests must be sent using the HTTP-POST or HTTP-Redirect protocol bindings. If enabled, it will send requests using HTTP-POST binding.
OpenID Connect v1.0 Identity Providers
Keycloak can broker identity providers based on the OpenID Connect v1.0 protocol.
In order to configure a OIDC identity provider, follow these steps:
Select the OpenID Connect v1.0 identity provider from the drop-down box on the top right corner of the identity providers table in Keycloak's Admin Console. You should be presented with a specific page to configure the selected provider.
When configuring an OIDC identity provider you are presented with different configuration options in order to send authentication requests to the external identity provider. In this case, the brokered identity provider must support the Authorization Code Flow (as defined by the specification) in order to authenticate the user and authorize access for the scopes specified in the configuration.
Configuration Options
Configuration
Description
Authorization Url
The authorization url.
Token Url
The token url.
User Info Url
The user info url. This is usually an url from where Keycloak will obtain user information in order to create a local account.
Client ID
The client id is usually generated when configuring an application or a project on the brokered identity provider.
Client Secret
The client secret is usually generated when configuring an application or a project on the brokered identity provider.
Issuer
Allows you to specify the expected value for the "issuer" claim when validating the ID token.
Default Scopes
Allows you to specify additional scopes when asking for user authentication and consent. By default, scope openid is always appended to the list of the scopes.
Prompt
Allows you to specify how the brokered identity provider must prompt user for authentication. You must check which values are supported by the brokered identity provider before choosing a value.
Retrieving Tokens from Identity Providers
Keycloak allows you to store tokens and responses from identity providers during the authentication process.
For that, you can use the Store Token configuration option, as mentioned before.
It also allows you to retrieve these tokens and responses once the user is authenticated in order to use their
information or use them to invoke external resources protected by these tokens.
The latter case is usually related with social providers,
where you usually need to use their tokens to invoke methods on their APIs.
To retrieve a token for a particular identity provider you need to send a request as follows:
In this case, given that you are accessing an protected service in Keycloak, you need to send the access token
issued by Keycloak during the user authentication.
By default, the Keycloak access token issued for the application can't be automatically used for retrieve thirdparty token. You will
need to enable this in admin console first:
Click 'Applications' on the left side menu.
Select an application from the list.
Click the 'Identity Provider' tab.
From this page you can configure if an application is allowed to retrieve tokens from an specific identity provider. For that,
just click on the Can Retrieve Token button.
If your application is not at the same origin as the authentication server, make sure you have properly configured CORS.
Automatically Select and Identity Provider
Applications can automatically select an identity provider in order to authenticate an user. In this case, the user will not be presented to the login page but automatically redirected to the identity provider.
Keycloak supports a specific HTTP query parameter that you can use as a hint to tell the server which identity provider should be used to authenticate the user.
For that, you can append the kc_idp_hint as a query parameter to your application url, as follows:
In this case, is expected that your realm has an identity provider with an alias facebook.
If you are using keycloak.js adapter, you can also achieve the same behavior:
Examples
Keycloak provides some useful examples about how to use it as an identity broker.
Take a look at {KEYCLOAK_HOME}/examples/broker for more details.
Each example application has its own README file where you can find additional information about how to configure Keycloak and
run it.