Setting up on-behalf-of sign-in¶
A person signs in to your application with Entra ID. The application calls Snowflake for them, and Snowflake sees that person: their user, their default role, their grants. The application holds no Snowflake credential.
sequenceDiagram
participant P as Person
participant A as Your app (backend)
participant E as Entra ID
participant S as Snowflake
P->>A: sign in
A->>E: authorization code flow
E-->>A: token (aud = your app)
A->>E: on-behalf-of: exchange it for scope session:role-any
E-->>A: token (aud = Snowflake OAuth Resource, upn = person)
A->>S: connect(authenticator="oauth", token=...)
S->>S: check issuer, signature, audience; map upn to a user
S-->>A: session as that person
The token the application receives at sign-in is addressed to the application. Snowflake rejects it, because its audience is wrong. On-behalf-of (OBO) is the Entra flow in which an API trades a token it received for a token to call another API as the same user. Here the "other API" is an Entra app registration that stands in for Snowflake.
The Entra side¶
Two app registrations. The reference repository creates them with the
Microsoft Graph Bicep extension (snowflake-oauth/infra/main.bicep).
| App | Configuration | Why |
|---|---|---|
| Snowflake OAuth Resource | Identifier URI api://<tenant-id>/snowflake-oauth-resource. One delegated scope, session:role-any. requestedAccessTokenVersion: 2. Optional access-token claim upn. |
Snowflake validates tokens issued for this app. The scope name is meaningful to Snowflake (see below). |
| Your application | Confidential client with a redirect URI. Delegated permission to the resource's session:role-any. Admin consent granted. |
OBO needs the client to be allowed to call the resource for the user. Without pre-granted consent the exchange fails with AADSTS65001. |
The scope name is the role request¶
Snowflake reads the token's scopes as role requests.
session:role:ANALYST asks for the ANALYST role. session:role-any means
"use the user's default role". We use session:role-any because users should
keep all their normal access in the application, not a role picked per app.
If you want an application limited to one role, define a session:role:<ROLE>
scope per role instead and set EXTERNAL_OAUTH_ANY_ROLE_MODE = 'DISABLE'.
Use v2 access tokens¶
Snowflake's Entra documentation shows v1 tokens. v2 tokens also work: Snowflake validates whatever issuer and audience the integration names. We use v2 because it's Entra's current format. Three values differ, and a mismatch on any of them makes Snowflake reject every token:
| v2 | v1 | |
|---|---|---|
iss, the integration's EXTERNAL_OAUTH_ISSUER |
https://login.microsoftonline.com/<tenant-id>/v2.0 |
https://sts.windows.net/<tenant-id>/, with the trailing slash |
aud, the integration's EXTERNAL_OAUTH_AUDIENCE_LIST |
the resource app's client ID (a GUID) | the identifier URI |
upn |
absent unless you add it as an optional claim | present |
The version is a property of the resource app, not the client. Setting
requestedAccessTokenVersion: 2 on the Snowflake OAuth Resource changes
every token issued for it.
One integration accepts one issuer, so switching versions means updating the
integration. Clients that already hold a token keep using it until it expires,
up to about 90 minutes. Expect a window of Invalid OAuth access token errors
while you switch.
Read iss and aud from a real token
Don't type the issuer and audience from documentation. Get a token for the
resource (az account get-access-token --scope api://<tenant-id>/snowflake-oauth-resource/session:role-any
works if you pre-authorize the Azure CLI on the resource app for testing),
decode its payload, and copy iss and aud. deploy.py snowflake-up does
this before it creates the integration.
The Snowflake side¶
One security integration, run as ACCOUNTADMIN:
CREATE OR REPLACE SECURITY INTEGRATION ENTRA_EXTERNAL_OAUTH
TYPE = EXTERNAL_OAUTH
ENABLED = TRUE
EXTERNAL_OAUTH_TYPE = AZURE
EXTERNAL_OAUTH_ISSUER = 'https://login.microsoftonline.com/<tenant-id>/v2.0'
EXTERNAL_OAUTH_JWS_KEYS_URL = 'https://login.microsoftonline.com/<tenant-id>/discovery/v2.0/keys'
EXTERNAL_OAUTH_AUDIENCE_LIST = ('<client id of Snowflake OAuth Resource>')
EXTERNAL_OAUTH_TOKEN_USER_MAPPING_CLAIM = 'upn'
EXTERNAL_OAUTH_SNOWFLAKE_USER_MAPPING_ATTRIBUTE = 'LOGIN_NAME'
EXTERNAL_OAUTH_ANY_ROLE_MODE = 'ENABLE';
None of these values are secrets, so an Entra administrator can hand them to a Snowflake administrator in a ticket. The integration has no network policy of its own. The account's policy applies, and connections arrive from your application's servers, so allow their egress addresses.
Map tokens to users¶
External OAuth never creates users. Each person needs an existing Snowflake
user whose mapping attribute equals the token's upn:
LOGIN_NAME(recommended). SetLOGIN_NAME = '<upn>'. If you provision users from Entra with SCIM, this is already true.EMAIL_ADDRESS. SetEMAIL = '<upn>'. This leaves how people sign in today unchanged, but every user's email must be unique, which rules out the second-user pattern below.
Match the UPN exactly. Guest (B2B) users' UPNs look like
jane_partner.com#EXT#@contoso.onmicrosoft.com, not like their email address.
According to Microsoft's optional-claims reference, guests only get that UPN
in the token if the upn optional claim has the additional property
include_externally_authenticated_upn. The reference Bicep sets it, but we
haven't tested with a guest user.
Secondary roles let administrator roles in¶
This is the part to read before production.
EXTERNAL_OAUTH_BLOCKED_ROLES_LIST defaults to ACCOUNTADMIN, ORGADMIN,
GLOBALORGADMIN and SECURITYADMIN, and it is easy to read that as
"administrator roles can't be used through OAuth". The list only stops them
from being the primary role. With ANY_ROLE_MODE = 'ENABLE', the user's
DEFAULT_SECONDARY_ROLES activate too, and Snowflake's default for that
property is ('ALL'). For an administrator, an application session then
looks like this:
current_role() SYSADMIN
is_role_in_session('ACCOUNTADMIN') TRUE
The application, and anything that can make it run SQL, now acts with ACCOUNTADMIN's privileges.
Options considered:
| Option | Effect | Verdict |
|---|---|---|
DEFAULT_SECONDARY_ROLES = () on administrators |
Fixes it, but changes every session those people open, including Snowsight | Surprising for the people affected |
Role-specific scopes, ANY_ROLE_MODE = 'DISABLE' |
The app can only request the roles you define scopes for | Users lose access they normally have |
| A separate non-admin user per administrator | JANE_DOE keeps the admin roles and her normal sign-in. JANE_DOE_APP has LOGIN_NAME = <her UPN>, no password, and only non-admin roles |
Chosen. Applications get everything except admin |
USE ROLE USERADMIN;
CREATE USER IF NOT EXISTS JANE_DOE_APP
TYPE = PERSON
LOGIN_NAME = '[email protected]';
ALTER USER JANE_DOE_APP SET
DEFAULT_ROLE = ANALYST
DEFAULT_SECONDARY_ROLES = ('ALL');
USE ROLE SECURITYADMIN;
GRANT ROLE ANALYST TO USER JANE_DOE_APP; -- and her other working roles
LOGIN_NAME is unique across users, so first change the administrator
user's LOGIN_NAME to something other than the UPN, or the CREATE USER
fails. Grant the _APP user the roles the person works with day to day.
SYSADMIN isn't on the blocked list, but it owns most objects, so think twice
before giving it to an application session. After the change:
current_user() JANE_DOE_APP
current_role() ANALYST
current_available_roles() ANALYST, PUBLIC, ...
is_role_in_session('ACCOUNTADMIN') FALSE
Put those four expressions in whatever smoke test you run after onboarding someone. They cost nothing and catch the mapping landing on the wrong user.
The application code¶
With MSAL for Python, the exchange is a single call on the confidential client:
result = msal_app.acquire_token_on_behalf_of(
user_assertion=token_your_app_received,
scopes=["api://<tenant-id>/snowflake-oauth-resource/session:role-any"],
)
snowflake_token = result["access_token"]
Then connect with it:
import snowflake.connector
with snowflake.connector.connect(
account="<org>-<account>",
authenticator="oauth",
token=snowflake_token,
) as conn:
conn.cursor().execute("select current_user(), current_role()").fetchone()
Two connector settings worth knowing for a web backend:
SNOWFLAKE_DISABLE_PLATFORM_DETECTION=truestops the connector from probing cloud metadata endpoints at connect time. Off Azure those probes time out and log warnings about169.254.169.254.enable_server_session_keep_alive_auto_detection=Falsesilences aFutureWarningin 5.x about a changing default. Set it explicitly to whatever you want.
In the demo app the token cache is shared by every user of the confidential
client. Pick the signed-in user's account by home_account_id before calling
acquire_token_silent. Using the first cached account is the classic bug that
hands one user's token to another.
Where the client's own credential comes from¶
OBO needs the application to authenticate as itself. On Azure compute, use a managed identity as the app's credential (Entra's "app trusts a managed identity" federated credential), so no client secret exists. The demo runs on a laptop, so it uses a client secret from Key Vault. That secret is rotated by the job described in Rotating Snowflake secrets with a managed identity.
Hardening¶
The setup above is enough to learn the flow. Before real users rely on it:
- Turn off the Azure CLI pre-authorization on the resource app
(
ALLOW_AZURE_CLI=falsein the reference config). It exists so you can test withaz account get-access-token. Left on, anyone in the tenant can get a Snowflake token from a terminal without a consent prompt. - Give the application a managed identity as its Entra credential on Azure, instead of a client secret.
- Allow only the application's egress addresses in the Snowflake network policy that applies to these users.
- Add the four-expression smoke test from the previous section to user onboarding, so a mapping that lands on an administrator is caught on day one.
Check a token without the application¶
If Snowflake rejects a token, start without sending the token anywhere.
Decode its payload locally and compare iss, aud and upn with
DESC SECURITY INTEGRATION (EXTERNAL_OAUTH_ISSUER,
EXTERNAL_OAUTH_AUDIENCE_LIST). Almost every rejection is one of those three.
deploy.py verify does that comparison for you.
If they all match, Snowflake can tell you which check failed:
SELECT SYSTEM$VERIFY_EXTERNAL_OAUTH_TOKEN('<token>');
That puts the token in the statement text, so it lands in query history, where anyone who can read the history can use it until it expires, up to about 90 minutes. Run it deliberately, as an administrator, and never in a shared worksheet.
Next: Troubleshooting.