Azure Ad Authorization Code Flow

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up the azure ad authorization code flow by trading a temporary login code for a token. Your app redirects the user to sign in, receives a code, and exchanges it on the backend. Teams use this setup to link cloud identities with internal tools and databases. Still, broad directory groups often grant users far too much power once they log in. How the azure ad authorization code flow works This sign-in flow splits the login process into two clear steps for better safety. First, the user

Dynamic Authorization + Azure RBAC: the complete guide

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

Free white paper. No spam. Unsubscribe anytime.

You set up the azure ad authorization code flow by trading a temporary login code for a token. Your app redirects the user to sign in, receives a code, and exchanges it on the backend. Teams use this setup to link cloud identities with internal tools and databases. Still, broad directory groups often grant users far too much power once they log in.

How the azure ad authorization code flow works

This sign-in flow splits the login process into two clear steps for better safety. First, the user opens your app and clicks the sign-in button. The app sends their browser to the Microsoft identity platform login page. There, the user enters their password and completes any required multi-factor checks.

Once the directory confirms the user identity, it redirects the browser back to your app. This redirect carries a short-lived code in the address bar. The browser sees this temporary code, but the code alone cannot read any data.

Next, your backend server takes over to finish the exchange. The server sends the temporary code directly to the directory token endpoint. It also includes its own secret key or a proof key to verify its identity. The directory checks these items and returns an ID token and an access token. Your server stores these tokens and lets the user into the app.

Adding proof keys for public apps

Not every app can keep a client secret safe on a backend server. Single-page apps and mobile tools run right on the user device. Anyone with local device access can inspect the app and steal a static secret. To fix this risk, teams add a proof key step to the login flow.

Before the login starts, the app creates a random secret phrase in memory. It hashes this phrase and sends the hash along with the first login request. When the app trades the temporary code for a token, it sends the raw phrase. The directory hashes the raw phrase and compares it to the first hash. If both match, the directory issues the token without needing a static secret.

Dynamic Authorization + Azure RBAC: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

What identity tokens solve and what they miss

The authorization code flow does a great job of proving who a user is. It keeps user passwords away from your app and centralizes multi-factor checks. When someone leaves the company, you disable one directory account to block their login everywhere.

Yet this identity check stops the moment the user gets a valid session token. The token decides who can open a session, not what happens inside that session. If a developer maps to a broad database role, their token unlocks every table.

Identity providers cannot see the actual SQL queries or shell commands a user runs. They cannot pause a destructive drop statement and ask a manager for quick approval. They also cannot rewrite query results to mask emails, phone numbers, or card numbers. A valid token grants full entry until it expires, leaving live data exposed to mistakes.

Govern statements and responses across databases and APIs

Good config and scoped roles are needed setup. They do not check single queries in flight or mask sensitive fields in returned rows. Across Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, and internal HTTP endpoints, hoop.dev runs beside each supported service as an open-source proxy, a sidecar for your database or API.

First, hoop.dev guardrails block banned queries before they run. Then hoop.dev data masking rewrites matched sensitive values in responses in memory. The audit trail records every statement and verdict per resource. Furthermore, hoop.dev holds SOC 2 Type II and is open source on GitHub.

Frequently asked questions

Why use an authorization code instead of returning tokens directly?

Returning tokens in the browser address bar exposes them to browser history and local scripts. Trading a short-lived code on the backend keeps the real tokens off the front end.

How do refresh tokens fit into this login flow?

Access tokens expire fast to limit damage if someone steals them. Your server uses a refresh token to get a new access token without making the user log in again.

Does Azure AD filter what queries a user runs after sign-in?

No, the directory only checks identity and issues the initial token. It does not inspect or filter live database queries, terminal commands, or API calls during the session.

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.