Azure Entra ID#

Previously known as Azure Active Directory.

To set up OAuth2.0 authentication for Azure Entra ID follow these instructions. Ensure that you supply the tenant ID using oauth_extra_params, a configuration may look like:

panel serve app.py \
--oauth-provider=azure \
--oauth-key='CLIENT_ID' \
--oauth-secret='CLIENT_SECRET' \
--cookie-secret='COOKIE_SECRET' \
--oauth-encryption-key='ENCRYPTION_KEY' \
--oauth-redirect-uri=REDIRECT_URI \
--oauth-extra-params "{'tenant': 'TENANT_ID'}" \
...

or with environment variables

PANEL_OAUTH_PROVIDER=azure \
PANEL_OAUTH_KEY=CLIENT_ID \
PANEL_OAUTH_SECRET=CLIENT_SECRET \
PANEL_COOKIE_SECRET=COOKIE_SECRET \
PANEL_OAUTH_ENCRYPTION=ENCRYPTION_KEY \
PANEL_OAUTH_REDIRECT_URI=REDIRECT_URI \
PANEL_OAUTH_EXTRA_PARAMS="{'tenant': 'TENANT_ID'}" \
panel serve app.py ...

The CLIENT_ID corresponds to the Application (client) ID and the TENANT_ID to the Directory (tenant) ID below:

CLIENT_ID and TENANT_ID

The CLIENT_SECRET corresponds to the Value below:

CLIENT_SECRET

The REDIRECT_URI should be included in the list of Web Redirect URIs. If you do not set it, Panel uses the public URL of the server, e.g. https://example.org or, when served with a --prefix or behind a reverse proxy on a sub-path, https://example.org/myapp. The value registered here has to match it exactly:

REDIRECT_URI

Tenant configuration and user identity#

When you register the application in Azure, choose a single-tenant account type (“Accounts in this organizational directory only”) unless you really need people from other organizations to sign in. Panel also needs to be told which tenant to use. If neither the AAD_TENANT_ID environment variable nor the tenant entry of oauth_extra_params is set, the login URLs use common, which accepts accounts from any directory. Set the tenant explicitly with either of

panel serve app.py --oauth-provider=azure --oauth-extra-params "{'tenant': 'TENANT_ID'}" ...
AAD_TENANT_ID=TENANT_ID panel serve app.py --oauth-provider=azure ...

where AAD_TENANT_ID takes precedence if both are set. The same setting applies to the azurev2 provider.

pn.state.user holds the unique_name claim for the azure provider and the email claim for azurev2. In Microsoft Entra ID these are mutable, human-readable values, and Microsoft recommends against using them for authorization or to identify a user’s data (see the ID token claims reference and Secure applications and APIs by validating claims). With a single-tenant registration only accounts from your directory can sign in, so this mostly matters for multi-tenant registrations, where an account from another tenant may carry an email address that belongs to someone else. Microsoft also describes the related removeUnverifiedEmailClaim application setting in Manage application authenticationBehaviors.

For multi-tenant registrations, do not rely on pn.state.user alone in an authorization callback, admin lists or per-user data. pn.state.user_info contains the decoded ID token, including the tid (tenant ID) and oid (object ID) claims, which Microsoft documents as the stable identifiers (oid together with tid, or sub):

import panel as pn

TENANT_ID = 'TENANT_ID'
ALLOWED_OIDS = {'OBJECT_ID_1', 'OBJECT_ID_2'}

def authorize(user_info):
    if user_info.get('tid') != TENANT_ID:
        return False
    return user_info.get('oid') in ALLOWED_OIDS

pn.config.authorize_callback = authorize

Drop the oid check if every user in the tenant should have access.

Handling multiple accounts#

Users who are signed into more than one Microsoft account in the same browser (for example a corporate and a personal account) may be logged in with the wrong account, or be confused about which account is used. You can control this by forwarding a prompt parameter to the Azure authorization endpoint via oauth_extra_params:

panel serve app.py \
--oauth-provider=azure \
--oauth-key='CLIENT_ID' \
--oauth-secret='CLIENT_SECRET' \
--cookie-secret='COOKIE_SECRET' \
--oauth-redirect-uri=REDIRECT_URI \
--oauth-extra-params "{'tenant': 'TENANT_ID', 'prompt': 'select_account'}" \
...
  • prompt=select_account always shows the account picker so the user can choose which account to sign in with.

  • prompt=login forces the user to re-authenticate.

Restricting tenant to your directory ID (rather than common) additionally ensures that only accounts from your organization are accepted.

Note

Forwarding arbitrary authorization parameters such as prompt requires the Panel release that includes #8635.

Warning

Multiple accounts are sometimes mistaken for the cause of a Could not open websocket error. That error is almost always a request-header size problem rather than an account problem — see Troubleshooting OAuth for the diagnosis and fixes.