Keycloak Authentication Flows

Published
Length
3 min read

Luis Felipe Dias

All posts

Keycloak authentication flows define the exact steps a user takes to log in. You build these flows by ordering checks like passwords, tokens, and identity links. Most teams start with the default browser flow for their apps. They add a second factor and bind the flow to a client. This setup works well for basic web apps. Problems start when a team maps broad roles to these login tokens. Once a user passes the login checks, the token grants wide permissions across backend databases and servers.

Keycloak + Multi-Factor Authentication (MFA): the complete guide

Architecture patterns, implementation strategies, and security best practices. Delivered to your inbox.

Free white paper. No spam. Unsubscribe anytime.

Keycloak authentication flows define the exact steps a user takes to log in. You build these flows by ordering checks like passwords, tokens, and identity links. Most teams start with the default browser flow for their apps. They add a second factor and bind the flow to a client. This setup works well for basic web apps. Problems start when a team maps broad roles to these login tokens. Once a user passes the login checks, the token grants wide permissions across backend databases and servers.

How keycloak authentication flows work in practice

A flow is a list of login steps that run in order. Each step checks one piece of user data or state. You set each step as required, alternative, disabled, or conditional. A required step must pass before the user moves forward. An alternative step lets the user pick one login method from a group. For example, a user might pick a passkey instead of a password.

Teams often group steps into a sub-flow to test conditions. A conditional sub-flow checks facts like user role or network address. If the condition matches, the system runs the extra steps inside that group. This lets you ask for a second factor only for admin roles. You can also set up separate flows for browser logins, direct API grants, and first-time social logins. After you build a custom flow, you bind it to the realm or to one client app.

Where login flows stop and runtime risk begins

Custom login flows do a great job of checking who a person is. They stop bad actors from guessing passwords or stealing accounts without a second factor. They also centralize login rules across many apps in one place. Your team does not have to write custom login code for every new service.

Still, a login flow only guards the front door of your system. Once the flow finishes, the server issues a signed token to the user. That token does not inspect what the user does next inside a database or server. If an engineer logs in with an admin role, they get full access to run any query. The login server cannot stop a bad update query or a bulk data export. It also cannot rewrite sensitive fields like emails, phone numbers, card numbers, or national IDs in query results.

Keycloak + Multi-Factor Authentication (MFA): architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Why static role mapping creates broad exposure

Many teams try to fix runtime risk by making more roles in their identity tool. They build complex sub-flows and map dozens of fine-grained groups into tokens. Over time, this role list grows hard to read and maintain. Engineers ask for permanent admin roles so they can fix production issues fast. Reviewers approve these broad roles because they lack a way to check individual commands.

When an account with a broad role gets compromised, the attacker inherits all those standing permissions. Even a strong multi-factor flow cannot stop an active session from running destructive commands. Teams need a way to inspect and control actions at the command level after login completes.

Check database and API queries on the wire

Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.

Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.

Frequently asked questions

What is the difference between a required step and an alternative step?

A required step must succeed for the login to pass. An alternative step only needs one option at its level to pass. You use alternative steps when users can pick between login methods like passwords or social accounts.

Should I edit the built-in flows directly?

No, you should duplicate a built-in flow before you make changes. Keeping the default flow untouched gives you a working backup if your new rule locks users out. Once your copy works well, you bind it to your target app.

Can a login flow restrict which SQL queries an engineer runs?

No, a login flow only checks identity and issues a session token at sign-in. It does not read database traffic or block destructive queries during an active session. You need query-level controls to inspect commands and mask fields like card numbers or emails in results.

Save the open-source gateway.

hoop is MIT-licensed infrastructure for controlling how AI agents reach production data. Star hoophq/hoop so you can inspect it, deploy it, or share it when your team starts governing agent access.