Why teams store external identity provider tokens in Keycloak
Teams store external identity provider tokens in Keycloak so that applications can call an upstream API, such as Google or Microsoft Graph, as the signed-in user without each application running its own OAuth flow. When a user signs in through an identity provider that Keycloak brokers, Keycloak can keep the tokens that provider issued and make them available to the client application. The application then gets a Keycloak-issued token for its own use and can retrieve the stored upstream token when it needs one. The central decision is whether one identity broker should hold those upstream credentials, or whether each application should hold its own.
Storing tokens centrally has clear advantages when several applications need the same upstream access. Consent, sign-in policy, and token handling stay in one place, and the applications do not each need a client registration with the upstream provider or their own refresh logic. The trade-off is that Keycloak becomes a holder of third-party credentials, so the token store, its access rules and its logging become security-relevant. Each application that retrieves a stored token must be scoped so it sees only its own user's tokens. Confirm in the Keycloak administration documentation for your version which token fields are stored, whether the stored token is the upstream access token, refresh token or both, and how it is exposed to clients.
Where only one application calls one upstream API, a direct OAuth flow in that application may be simpler to audit and to retire. Check the exact behavior for your Keycloak version before you design around it, because storage and retrieval options have changed across releases. The Keycloak server administration guide describes identity brokering and the provider settings that control what is kept, and it is the right starting point for confirming the design with your platform and security teams.
How an upstream token moves from login to an API call
The flow starts when a user signs in through Keycloak and chooses an external provider. Keycloak completes the brokered login, stores the provider token on the user's linked identity, and then serves it only to a client that authenticates with its own credentials. The application then calls the upstream API with that token. Check which browser, Keycloak, application and provider boundaries the token crosses, and confirm that only the intended client can request it.
Provider token flow from brokered login to an upstream API call.
Which token storage and retrieval option fits your design
Use this option when your application needs the upstream token only after a sign-in that starts with the external provider, and Keycloak can hold that token for later use. When the identity provider is set to store tokens, Keycloak keeps the access and refresh tokens it received from the provider and ties them to the user's federated identity link. The application does not receive the provider's client secret or handle the provider's login flow itself. The application must hold a Keycloak client that is allowed to read the stored value, a valid Keycloak session for the user, and a plan for what happens when the stored token expires.
- Storage setting: The identity provider's store tokens option controls whether Keycloak keeps the provider tokens at all.
- Readability: A separate setting decides whether clients can read the stored tokens, so confirm it before you design the call path.
- Requested scopes: The scopes requested at sign-in decide what the stored token can call later, so request them during login.
- Token lifetime: The provider sets how long the token lasts, so plan for refresh or a new sign-in when it lapses.
- Operational exposure: Stored tokens are sensitive credentials, so restrict who can read them and log their use.
Confirm the exact identity provider settings and their defaults in the Keycloak server administration guide for your release before you set this up.
Use this option when an application needs a current upstream token on demand, while the user is signed in to Keycloak. The application calls the brokering API with a Keycloak access token for that user and names the identity provider alias whose stored token it needs. Keycloak returns the token it holds for that link, so the design depends on whether the stored token is still valid or can be refreshed. Each call is also a permission decision, so the calling client must be entitled to read brokered tokens for that provider. The application must hold a valid Keycloak access token for the user, the provider alias, and a client identity configured for this call.
- Purpose: The endpoint returns the stored upstream token for one named provider, which suits calls to APIs such as Microsoft Graph or Google APIs.
- Client permissions: The calling client needs explicit permission to read brokered tokens, so confirm how that permission is granted.
- Refresh behavior: Confirm what Keycloak does when the stored token has expired and whether the application must trigger a new login.
- Audience and scopes: The upstream API accepts only the audience and scopes granted at login, so check those before you write the call.
- Failure handling: Plan responses for users who never linked the provider or whose link carries no stored token.
Before you build on V2, confirm the endpoint path, parameters, client settings and the release in which it applies in the current Keycloak server administration guide.
Keep the deprecated V1 endpoints only for existing code that already depends on them and cannot move within the current project window. V1 still appears in older applications and scripts, but it is deprecated, so new integrations should start on V2. Treat any V1 dependency as technical debt with an owner, because a deprecated interface can be removed in a later release. The application holds the same kind of inputs as with V2 (a Keycloak user token, a provider alias and a permitted client), and the existing call pattern must be documented before anyone changes it.
- Inventory: List every service, job and script that calls V1 before you upgrade Keycloak, since hidden callers are the usual source of breakage.
- Parallel testing: Call V2 with the same provider alias and user, then compare the token returned and the errors raised.
- Permission scope: Keep the legacy client's permissions as narrow as the existing code needs, and review them during the migration.
- Logging: Record V1 calls while they remain active so you can see when traffic stops.
- Exit criteria: Assign a named owner and a removal target for each legacy caller, and confirm that no traffic remains before you retire it.
Read the deprecation notes in the Keycloak upgrading guide for your target release, and confirm the removal status of V1 with the current Keycloak documentation before you schedule the change.
How to set up token storage and retrieval for one provider
This sequence stores an upstream identity provider's token when a user signs in, makes that token readable by one approved client, and proves that a test application can retrieve it and call the upstream API. Run it once on a non-production realm before you enable it for real users.
Enable token storage on the identity provider
A Keycloak administrator opens the identity provider in the target realm and turns on the setting that stores tokens returned at login. Confirm the exact label in the admin console for your Keycloak version, since the setting lives on the provider record and applies to every user who signs in through it. Because stored tokens are sensitive credentials, record who approved the change and which realm it applies to. Done when the saved provider configuration shows the setting enabled and a second administrator can see the same value.
Request the scopes the upstream API needs
The administrator adds the scopes the upstream API requires to the provider configuration, so the authorization request asks the provider for them. Take the exact scope strings from the upstream provider's documentation and from the app registration on that side, because a missing scope produces a token that cannot call the API. Check that the upstream app registration has consented to those permissions for the tenant or project in use. Done when a test sign-in shows the consent screen listing the expected permissions and the upstream registration lists no pending admin consent.
Confirm the first login stores the token
A test user signs in through the provider while the stored-token setting is enabled. An administrator then checks the user's linked identity in the admin console or admin API to confirm that a stored token is present. Keep in mind that a session which is already active may not trigger a new login, so sign out first and use a fresh browser session. Done when the linked identity reports a stored token and the scope and expiry values match what the upstream provider issued during the test sign-in.
Grant the calling client permission to read tokens
The administrator gives the client that will retrieve tokens the broker role that allows reading stored tokens, using the role your Keycloak version documents for this purpose. Grant it to one client only, and keep the client's other roles minimal. Confirm the role name in the server administration guide for your release before changing production settings. Done when the approved client's service or user context can read the stored token and a second client without the role receives an access denied response, which shows the permission works as intended.
Call the V2 endpoint from a test application
A developer builds a small test application that signs the test user in to Keycloak, obtains an access token for itself, and calls the identity brokering API V2 endpoint for the provider's stored token. Use the V2 endpoint in all new code and confirm its path and request format in the current server administration guide, because V1 is deprecated. Record the request and response shape for the team. Done when the endpoint returns the upstream access token along with its expiry and the test application handles the response without errors.
Verify the upstream API call succeeds
The test application passes the retrieved provider token to a read-only call on the upstream API, such as reading the signed-in user's own profile. A successful response confirms that the token, scopes and upstream permissions line up end to end. If the upstream API rejects the call, compare the scopes in the token with the scopes the API requires before changing anything else. Done when the upstream call returns the expected data for the test user, and the team notes the token lifetime so it can plan refresh handling as a separate task.
How client policies control who can read stored tokens
Retrieval of stored external identity provider tokens should be limited to a short list of confidential clients, because anyone who can call the broker endpoint with a valid session can read the upstream token. The control point is the combination of client policies and client scopes: client policies decide which clients may use the features that expose tokens, and client scopes decide what each permitted client receives. Start by listing every client that genuinely needs an upstream token, such as a backend service that calls Microsoft Graph or Google APIs on the user's behalf, and confirm that each one authenticates with a client secret, signed JWT or other confidential method. Public clients such as single-page apps and mobile apps should not be on that list, because they cannot keep a secret and any token they obtain can be copied out of the browser or device.
Keep the policy design narrow and written down. Map each permitted client to the upstream provider it needs, and make sure the policy and scope assignments do not grant access to the broker endpoint by default. Then test the result from the outside: sign in as a test user through an application that should be blocked, call the retrieval endpoint, and confirm the request is refused. Repeat for an allowed client. The exact policy names, condition types and the settings that restrict token retrieval change between Keycloak versions, so confirm them in the Keycloak server administration guide for the version you deploy before writing any configuration.
When a new application needs an upstream token, treat it as an access change, not a configuration detail. The owning team should state the upstream API and scopes it needs, the reason it cannot use its own sign-in token, and whether it is a confidential client. Security or the identity team then reviews the request, adds the client to the policy and scope set, and records the approval. Retest the new client and one existing blocked client after each change. Decide in advance who may approve these changes, how removed clients lose access, and how you will detect a client that was granted access but no longer uses it.
Security risks of holding user provider tokens
Once Keycloak stores a user's provider tokens, it becomes a holder of credentials that can call upstream APIs on that user's behalf. The cards below cover the risks that follow from that: what sits in the database, how wide the scopes are, how client secrets are handled, what gets logged, how long tokens live, and how the server itself is exposed.
Stored tokens in the database
Keycloak keeps stored external tokens in its database, so a copy of that database is a copy of every linked user's upstream access. Confirm who can read the database, whether backups are encrypted and where restored copies go. Check that a database snapshot taken for troubleshooting is treated with the same care as the live store, and that tokens are removed when a link is deleted.
Keycloak database docsOverly broad provider scopes
A token is only as dangerous as the scopes it carries. Requesting broad scopes at login so that later calls never fail gives every stored token more reach than the application needs. Define the minimum scopes each application requires, request them in the identity provider configuration, and review the grants Keycloak holds against that list at each release.
Confidential client secret handling
The client secret that Keycloak uses to talk to the upstream provider is a credential in its own right. Store it in a secret manager or a Kubernetes secret rather than in realm exports or source control, restrict who can view it in the admin console, and plan rotation at the provider and in Keycloak together so that token exchange does not break during the change.
Audit logging of token retrievals
Each time an application reads a stored token, that read is a sensitive event. Turn on event logging for admin and user actions, forward it to a central log store, and confirm that retrieval of provider tokens is recorded in a form your security team can search. Without that trail, a misused token may not be visible until the upstream provider reports it.
Token lifetime and revocation on disable
Stored tokens can outlive the user's access to your system. Decide how long Keycloak keeps refresh material, and make disabling or deleting a user remove their stored tokens and sessions. Test this path directly: disable a test user, then confirm that the stored token can no longer be retrieved and that the upstream grant can be revoked from the provider side.
TLS and reverse proxy exposure
Keycloak's production guide calls for TLS on all communication and a reverse proxy with path restrictions. Expose the admin console and Administration REST API on a different hostname or context path from public frontend URLs, so that a request for a stored token cannot reach the admin surface from the internet. Verify the separation with a scan from outside your network.
Production guideMoving off the deprecated V1 token endpoints
Code that retrieves stored external identity provider tokens through the V1 endpoints should move to V2 now, while the V1 calls still work. Deprecated endpoints tend to stay in place until a later release removes them, and a removal can break an application at upgrade time rather than at the moment the team chose to change it. Start with an inventory of every caller. Search application repositories, API gateway routes, scripts, scheduled jobs and integration middleware for calls that retrieve upstream tokens. For each call, record the realm, the client, the identity provider alias, the calling service, the response fields the code reads and the owner who can change it. Many teams discover calls in places nobody maintains, such as a reporting job or a legacy service account, so include those in the list. The inventory gives you the scope of the cutover before any code changes.
Next, test the V2 behavior in a non-production realm that matches the production configuration. Export the production realm settings, import them into the test realm and point the identity provider at a test tenant or test account at the upstream provider, so you do not expose production user data during testing. Run the same sign-in and token retrieval through both the V1 and V2 calls, then compare the results. Check that the returned upstream token matches the one stored at login, that expiry and scope values are what your application expects, and what each endpoint returns when the stored token has expired or the user has revoked consent. Confirm that the setting which stores upstream tokens is enabled on the provider, because retrieval depends on it. Also confirm that any client policy restricting which clients may read tokens applies to the V2 calls the same way.
For the cutover, change one client or one caller at a time, starting with the lowest-impact integration. Keep the V1 call available behind a configuration switch until the V2 path has run through a full business cycle without retrieval errors, and watch failed retrievals and unexpected re-authentication prompts during that period. Before you set the target release, read the upgrading guide and the backwards compatibility notes for the exact release you plan to run, since the deprecation status, removal timing and any opt-in behavior are stated there and can change between releases. Once no callers remain in the inventory, remove the V1 code paths and record the change in your configuration management so that a future upgrade does not reintroduce them. Ask your upstream provider integration owner to sign off on the final state.
Questions teams ask before storing external tokens in Keycloak
How much does it cost to store external identity provider tokens in Keycloak?
The cost is driven by the work, not by a license fee, because Keycloak is open source under Apache 2.0 and the platform itself carries no license cost. You pay for the design and configuration of each upstream provider, the client changes in each application that calls an upstream API, testing of token refresh and expiry, and the runbook your team will follow later. Your internal engineering time and any external services are the main cost lines. Ask any partner for a quote built from a written scope that lists the number of providers, the number of applications and the upstream APIs each application calls. DeployOpen does not publish a price list, and a quote follows from that written scope.
How long does an external token storage project take?
Duration depends on how many identity providers store tokens, how many applications need them, and how many upstream APIs each application calls. A single provider with one application that calls one API is a much smaller job than several providers feeding several applications, because each combination needs its own scope request, token handling, and refresh test. No duration figure is supplied for this topic, so ask any implementation partner to break the timeline into provider setup, application changes, refresh testing, and cutover, and to tie each phase to a written deliverable.
What does our team need to provide for this work?
Your team needs to supply the identity provider tenants or OAuth clients, the list of upstream APIs and the scopes each application requires, and a named owner for each provider's administration. You also need access to the Keycloak realm for configuration, a test environment that mirrors production redirect URLs and hostnames, and the people who own each calling application. Security and compliance staff should review where tokens are stored and for how long, because the stored provider tokens grant access to upstream data. Agreeing these inputs before work starts keeps the scope fixed.
Can our own team store external tokens in Keycloak without a partner?
Yes, a team with Keycloak administration experience can do this in-house, and the official server administration guide documents identity brokering and the token options for external providers. The harder parts are usually not the console settings but the application side: requesting the right scopes, calling upstream APIs with the stored token, handling expiry, and recovering when a refresh fails. Teams that have not run an OAuth client against Google or Microsoft Graph before tend to underestimate this. A partner is most useful for a fixed-scope build with a written test plan, or for a retest after changes.
Which Keycloak version should we plan for external token storage?
Plan for the current release line you can keep patched, and confirm the exact version against the release notes before you start. The current Keycloak release is 26.8.1, published October 13, 2026. Since Keycloak 26.0, minor releases are planned four times a year and breaking changes in a minor release are opt-in, so upgrades are more predictable than before. Also note that the Identity Brokering API V1 is deprecated, so new work should target the V2 approach and existing V1 use should be scheduled for migration. Read the upgrading guide before choosing a target version.
What happens when a stored external token fails to refresh?
A failed refresh means the stored upstream token can no longer be renewed, usually because the user revoked consent, the provider changed its policy, the refresh token expired, or the provider client secret was rotated. Your application should detect the failure, stop calling the upstream API for that user, and prompt the user to sign in again so Keycloak can obtain a new token. Design this path before launch, log refresh failures with enough context to find the cause, and alert the operations team when failures rise. Test revoked consent and expired refresh tokens explicitly, since they do not appear in normal sign-in tests.
How do the number of providers and applications change the effort?
Each identity provider adds its own configuration, its own token lifetime and refresh behavior, and its own failure modes. Each application adds its own scopes, its own upstream API calls, and its own error handling. Effort therefore grows with the number of provider and application pairs more than with either count alone. Scope count matters too: more scopes mean more consent prompts and more places where a missing permission can break a call. Ask for a scope matrix showing which application uses which provider, API, and scope before accepting any quote.
Do we need to review token storage for security and compliance?
Yes. Stored provider tokens can call upstream APIs on a user's behalf, so they belong in your access control and audit review. Confirm where tokens are kept, who can read them, how long they live, and whether logs expose them. Keycloak's production guidance on TLS, admin endpoint exposure, and secrets handling applies here as well. DeployOpen prepares systems, controls and evidence for compliance assessments, but it does not issue audit reports or certifications, and independent auditors make that determination.
How DeployOpen scopes and delivers external token integrations
DeployOpen plans and builds external identity provider token handling in Keycloak inside your own infrastructure, from deciding which upstream tokens to store to documenting how each application retrieves them. Engagements are fixed-scope, with scope, deliverables and timelines agreed in writing before work starts.
How we scope
- Inventory which upstream APIs each application needs, such as Google or Microsoft Graph, and the scopes each call requires
- Confirm which Keycloak version and identity brokering API your team will target before design starts
- Agree in writing how stored provider tokens, refresh behavior and client policies are covered in the fixed scope
How we build
- Configure identity providers to store tokens on login and set client policies that limit which clients can read them
- Build and test token retrieval and refresh paths against each upstream provider in a staging realm first
- Test token exposure, permission boundaries and logout behavior, then fix findings and retest the configuration
How we hand over
- Deliver written configuration, client policy settings and runbooks for token retrieval and refresh in your environment
- Document the migration steps from the deprecated V1 brokering endpoints to V2 for your application code
- Walk your platform, security and application teams through the configuration and the retest results
How we support
- Plan upgrades to new Keycloak releases and review how each one affects brokering and token storage settings
- Offer ongoing support, upgrades and continuous testing as a retainer after handover
- Agree support boundaries, response paths and change control in the written scope before go-live

