Skip to content

Commit e854195

Browse files
Allow disabling app services auth (#1932)
* guidance on disabling app services auth for programmatic access * Update docs/login_and_acl.md * Update docs/login_and_acl.md * Update docs/login_and_acl.md * Update infra/main.bicep --------- Co-authored-by: Pamela Fox <[email protected]>
1 parent f7969c0 commit e854195

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

docs/login_and_acl.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [Client App](#client-app)
1111
- [Configure Server App Known Client Applications](#configure-server-app-known-client-applications)
1212
- [Testing](#testing)
13+
- [Programmatic Access With Authentication](#programmatic-access-with-authentication)
1314
- [Troubleshooting](#troubleshooting)
1415
- [Adding data with document level access control](#adding-data-with-document-level-access-control)
1516
- [Using the Add Documents API](#using-the-add-documents-api)
@@ -35,7 +36,7 @@ Two Microsoft Entra applications must be registered in order to make the optiona
3536

3637
The easiest way to setup the two apps is to use the `azd` CLI. We've written scripts that will automatically create the two apps and configure them for use with the sample. To trigger the automatic setup, run the following commands:
3738

38-
1. Run `azd env set AZURE_USE_AUTHENTICATION true` to enable the login UI and App Service authentication.
39+
1. Run `azd env set AZURE_USE_AUTHENTICATION true` to enable the login UI and use App Service authentication by default.
3940
1. Ensure access control is enabled on your search index. If your index doesn't exist yet, run prepdocs with `AZURE_USE_AUTHENTICATION` set to `true`. If your index already exists, run `pwsh ./scripts/manageacl.ps1 --acl-action enable_acls`.
4041
1. (Optional) To require access control when using the app, run `azd env set AZURE_ENFORCE_ACCESS_CONTROL true`. Authentication is always required to search on documents with access control assigned, regardless of if unauthenticated access is enabled or not.
4142
1. (Optional) To allow authenticated users to search on documents that have no access controls assigned, even when access control is required, run `azd env set AZURE_ENABLE_GLOBAL_DOCUMENT_ACCESS true`.
@@ -134,6 +135,21 @@ Ensure you run `azd env set AZURE_USE_AUTHENTICATION` to enable the login UI onc
134135

135136
In both the chat and ask a question modes, under **Developer settings** optional **Use oid security filter** and **Use groups security filter** checkboxes will appear. The oid (User ID) filter maps to the `oids` field in the search index and the groups (Group ID) filter maps to the `groups` field in the search index. If `AZURE_ENFORCE_ACCESS_CONTROL` has been set, then both the **Use oid security filter** and **Use groups security filter** options are always enabled and cannot be disabled.
136137

138+
#### Programmatic Access with Authentication
139+
140+
If you want to use the chat endpoint without the UI and still use authentication, you must disable [App Service built-in authentication](https://learn.microsoft.com/azure/app-service/overview-authentication-authorization) and use only the app's MSAL-based authentication flow. Ensure the `AZURE_DISABLE_APP_SERVICES_AUTHENTICATION` environment variable is set before deploying.
141+
142+
Get an access token that can be used for calling the chat API using the following code:
143+
144+
```python
145+
from azure.identity import DefaultAzureCredential
146+
import os
147+
148+
token = DefaultAzureCredential().get_token(f"api://{os.environ['AZURE_SERVER_APP_ID']}/access_as_user", tenant_id=os.getenv('AZURE_AUTH_TENANT_ID', os.getenv('AZURE_TENANT_ID')))
149+
150+
print(token.token)
151+
```
152+
137153
### Troubleshooting
138154

139155
- If your primary tenant restricts the ability to create Entra applications, you'll need to use a separate tenant to create the Entra applications. You can create a new tenant by following [these instructions](https://learn.microsoft.com/entra/identity-platform/quickstart-create-new-tenant). Then run `azd env set AZURE_AUTH_TENANT_ID <YOUR-AUTH-TENANT-ID>` before running `azd up`.
@@ -235,6 +251,7 @@ The following environment variables are used to setup the optional login and doc
235251
- `AZURE_ENFORCE_ACCESS_CONTROL`: Enforces Entra ID based login and document level access control on documents with access control assigned. Set to true before running `azd up`. If `AZURE_ENFORCE_ACCESS_CONTROL` is enabled and `AZURE_ENABLE_UNAUTHENTICATED_ACCESS` is not enabled, then authentication is required to use the app.
236252
- `AZURE_ENABLE_GLOBAL_DOCUMENT_ACCESS`: Allows users to search on documents that have no access controls assigned
237253
- `AZURE_ENABLE_UNAUTHENTICATED_ACCESS`: Allows unauthenticated users to access the chat app, even when `AZURE_ENFORCE_ACCESS_CONTROL` is enabled. `AZURE_ENABLE_GLOBAL_DOCUMENT_ACCESS` should be set to true to allow unauthenticated users to search on documents that have no access control assigned. Unauthenticated users cannot search on documents with access control assigned.
254+
- `AZURE_DISABLE_APP_SERVICES_AUTHENTICATION`: Disables [use of built-in authentication for App Services](https://learn.microsoft.com/azure/app-service/overview-authentication-authorization). An authentication flow based on the MSAL SDKs is used instead. Useful when you want to provide programmatic access to the chat endpoints with authentication.
238255
- `AZURE_SERVER_APP_ID`: (Required) Application ID of the Microsoft Entra app for the API server.
239256
- `AZURE_SERVER_APP_SECRET`: [Client secret](https://learn.microsoft.com/entra/identity-platform/v2-oauth2-client-creds-grant-flow) used by the API server to authenticate using the Microsoft Entra server app.
240257
- `AZURE_CLIENT_APP_ID`: Application ID of the Microsoft Entra app for the client UI.

infra/core/host/appservice.bicep

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ param authenticationIssuerUri string = ''
4848
@allowed([ 'Enabled', 'Disabled' ])
4949
param publicNetworkAccess string = 'Enabled'
5050
param enableUnauthenticatedAccess bool = false
51+
param disableAppServicesAuthentication bool = false
5152

5253
var msftAllowedOrigins = [ 'https://portal.azure.com', 'https://ms.portal.azure.com' ]
5354
var loginEndpoint = environment().authentication.loginEndpoint
@@ -134,7 +135,7 @@ resource appService 'Microsoft.Web/sites@2022-03-01' = {
134135
}
135136
}
136137

137-
resource configAuth 'config' = if (!(empty(clientAppId))) {
138+
resource configAuth 'config' = if (!(empty(clientAppId)) && !disableAppServicesAuthentication) {
138139
name: 'authsettingsV2'
139140
properties: {
140141
globalValidation: {

infra/main.bicep

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ param authTenantId string = ''
128128
// Used for the optional login and document level access control system
129129
param useAuthentication bool = false
130130
param enforceAccessControl bool = false
131+
// Force using MSAL app authentication instead of built-in App Service authentication
132+
// https://learn.microsoft.com/azure/app-service/overview-authentication-authorization
133+
param disableAppServicesAuthentication bool = false
131134
param enableGlobalDocuments bool = false
132135
param enableUnauthenticatedAccess bool = false
133136
param serverAppId string = ''
@@ -276,6 +279,7 @@ module backend 'core/host/appservice.bicep' = {
276279
clientAppId: clientAppId
277280
serverAppId: serverAppId
278281
enableUnauthenticatedAccess: enableUnauthenticatedAccess
282+
disableAppServicesAuthentication: disableAppServicesAuthentication
279283
clientSecretSettingName: !empty(clientAppSecret) ? 'AZURE_CLIENT_APP_SECRET' : ''
280284
authenticationIssuerUri: authenticationIssuerUri
281285
use32BitWorkerProcess: appServiceSkuName == 'F1'

infra/main.parameters.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@
179179
"enforceAccessControl": {
180180
"value": "${AZURE_ENFORCE_ACCESS_CONTROL=false}"
181181
},
182+
"disableAppServicesAuthentication": {
183+
"value": "${AZURE_DISABLE_APP_SERVICES_AUTHENTICATION=false}"
184+
},
182185
"enableGlobalDocuments": {
183186
"value": "${AZURE_ENABLE_GLOBAL_DOCUMENT_ACCESS=false}"
184187
},

0 commit comments

Comments
 (0)