|
| 1 | +# reflex-azure-auth |
| 2 | + |
| 3 | +This package requires the `reflex_enterprise` package to be installed. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +pip install reflex-azure-auth |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Set Up an Azure (Microsoft identity platform) Application |
| 14 | + |
| 15 | +Create a new Application (App Registration) in the Azure portal and set up a .env file with the following variables: |
| 16 | + |
| 17 | +```env |
| 18 | +AZURE_CLIENT_ID=your_client_id |
| 19 | +AZURE_CLIENT_SECRET=your_client_secret |
| 20 | +AZURE_ISSUER_URI=your tenant issuer or authority URL |
| 21 | +``` |
| 22 | + |
| 23 | +Reflex will need to access these variables to authenticate users via OpenID Connect on the Microsoft identity platform. |
| 24 | + |
| 25 | +#### Step-by-step: App Registration |
| 26 | + |
| 27 | +1. Sign in to the Azure portal and open "Azure Active Directory" → "App registrations". |
| 28 | +2. Click "New registration". |
| 29 | + - Name: choose a friendly name (example: "Reflex Demo App"). |
| 30 | + - Supported account types: choose the tenant(s) you want (single or multi-tenant). |
| 31 | + - Redirect URI: add the authorization callback path for your app, e.g. `https://your-app.example.com/authorization-code/callback` (use `http://localhost:3000/authorization-code/callback` for local development). |
| 32 | +3. Register the app and copy the "Application (client) ID" → this is `AZURE_CLIENT_ID`. |
| 33 | +4. Under "Certificates & secrets" create a new client secret and copy the value → this is `AZURE_CLIENT_SECRET`. |
| 34 | +5. Under "Expose an API" or "API permissions" add the scopes your app needs. For typical OpenID Connect sign-in, request the `openid`, `profile`, and `email` scopes. If you need access to a custom API, expose an application ID URI (e.g. `api://<client-id>`) and create delegated scopes. |
| 35 | +6. Determine your issuer (authority) URL: |
| 36 | + - For a single tenant: `https://login.microsoftonline.com/<your-tenant-id>` |
| 37 | + - For common/multi-tenant flows: `https://login.microsoftonline.com/common` |
| 38 | + Use the `AZURE_ISSUER_URI` env var to set this (you can include the `/v2.0` suffix or we default to `v2.0` for endpoint assembly). |
| 39 | + |
| 40 | +Example .env (local development): |
| 41 | + |
| 42 | +```env |
| 43 | +AZURE_CLIENT_ID=00000000-0000-0000-0000-000000000000 |
| 44 | +AZURE_CLIENT_SECRET=very-secret-value |
| 45 | +AZURE_ISSUER_URI=https://login.microsoftonline.com/common |
| 46 | +AZURE_AUDIENCE=api://default |
| 47 | +``` |
| 48 | + |
| 49 | +Notes: |
| 50 | +- Redirect URIs must match exactly. For Reflex demo pages running locally, use the full local URL including the `/authorization-code/callback` path. |
| 51 | +- Use `openid email profile` in the authorization request to receive an ID token containing standard claims (sub, name, email). |
| 52 | +- When testing with a real tenant, use the tenant-specific issuer URL (recommended for production). |
| 53 | + |
| 54 | +### Register Auth Callback |
| 55 | + |
| 56 | +```python |
| 57 | +from reflex_enterprise import App |
| 58 | +from reflex_azure_auth import register_auth_endpoints |
| 59 | + |
| 60 | +... |
| 61 | + |
| 62 | +app = App() |
| 63 | +register_auth_endpoints(app) |
| 64 | +``` |
| 65 | + |
| 66 | +### Check `AzureAuthState.userinfo` for user identity/validity |
| 67 | + |
| 68 | +```python |
| 69 | +import reflex as rx |
| 70 | +from reflex_azure_auth import AzureAuthState |
| 71 | + |
| 72 | +@rx.page() |
| 73 | +def index(): |
| 74 | + return rx.container( |
| 75 | + rx.vstack( |
| 76 | + rx.heading("Azure (Microsoft) Auth Demo"), |
| 77 | + rx.cond( |
| 78 | + rx.State.is_hydrated, |
| 79 | + rx.cond( |
| 80 | + AzureAuthState.userinfo, |
| 81 | + rx.vstack( |
| 82 | + rx.text(f"Welcome, {AzureAuthState.userinfo["name"]}!"), |
| 83 | + rx.text(AzureAuthState.userinfo.to_string()), |
| 84 | + rx.button("Logout", on_click=AzureAuthState.redirect_to_logout), |
| 85 | + ), |
| 86 | + rx.button("Log In with Microsoft", on_click=AzureAuthState.redirect_to_login), |
| 87 | + ), |
| 88 | + rx.spinner(), |
| 89 | + ), |
| 90 | + ), |
| 91 | + ) |
| 92 | +``` |
| 93 | + |
| 94 | +### Validate the Tokens |
| 95 | + |
| 96 | +tokens to ensure they have not been tampered with. Use |
| 97 | +Before performing privileged backend operations, it is important to validate the |
| 98 | +tokens to ensure they have not been tampered with. Use |
| 99 | +`AzureAuthState._validate_tokens()` helper method to validate the tokens. |
0 commit comments