Skip to content

Commit 3838fca

Browse files
authored
Update import document tool to new auth model (microsoft#239)
### Motivation and Context The recent changes to the authentication model in Chat Copilot effiectively broke the import document tool. This change fixes it, by enabling users to: - Use the tool with an unauthenticated local instance of Chat Copilot without signing in - Use the tool with an Azure-deployed instance of Chat Copilot with Azure AD authentication enabled Fixes microsoft#212, fixes microsoft#231 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> - Adds code paths to authenticate user (or not) depending on configuration - Removes user name/info from document import form - Updates README with instructions for setting up the app registration for import document tool and configuring appsettings.json ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [Contribution Guidelines](https://github.com/microsoft/copilot-chat/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/copilot-chat/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - N/A, but both supported scenarios have been tested - [x] I didn't break anyone 😄
1 parent c0012fa commit 3838fca

4 files changed

Lines changed: 119 additions & 50 deletions

File tree

‎tools/importdocument/Config.cs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,33 @@ public sealed class Config
1212
/// <summary>
1313
/// Client ID for the app as registered in Azure AD.
1414
/// </summary>
15+
public string AuthenticationType { get; set; } = "None";
16+
17+
/// <summary>
18+
/// Client ID for the import document tool as registered in Azure AD.
19+
/// </summary>
1520
public string ClientId { get; set; } = string.Empty;
1621

22+
/// <summary>
23+
/// Client ID for the backend web api as registered in Azure AD.
24+
/// </summary>
25+
public string BackendClientId { get; set; } = string.Empty;
26+
27+
/// <summary>
28+
/// Tenant ID against which to authenticate users in Azure AD.
29+
/// </summary>
30+
public string TenantId { get; set; } = string.Empty;
31+
32+
/// <summary>
33+
/// Azure AD cloud instance for authenticating users.
34+
/// </summary>
35+
public string Instance { get; set; } = string.Empty;
36+
37+
/// <summary>
38+
/// Scopes that the client app requires to access the API.
39+
/// </summary>
40+
public string Scopes { get; set; } = string.Empty;
41+
1742
/// <summary>
1843
/// Redirect URI for the app as registered in Azure AD.
1944
/// </summary>

‎tools/importdocument/Program.cs‎

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,33 +59,23 @@ public static void Main(string[] args)
5959
/// Acquires a user account from Azure AD.
6060
/// </summary>
6161
/// <param name="config">The App configuration.</param>
62-
/// <param name="setAccount">Sets the account to the first account found.</param>
6362
/// <param name="setAccessToken">Sets the access token to the first account found.</param>
6463
/// <returns>True if the user account was acquired.</returns>
65-
private static async Task<bool> AcquireUserAccountAsync(
64+
private static async Task<bool> AcquireTokenAsync(
6665
Config config,
67-
Action<IAccount> setAccount,
6866
Action<string> setAccessToken)
6967
{
70-
Console.WriteLine("Requesting User Account ID...");
68+
Console.WriteLine("Attempting to authenticate user...");
7169

72-
string[] scopes = { "User.Read" };
70+
var webApiScope = $"api://{config.BackendClientId}/{config.Scopes}";
71+
string[] scopes = { webApiScope };
7372
try
7473
{
7574
var app = PublicClientApplicationBuilder.Create(config.ClientId)
7675
.WithRedirectUri(config.RedirectUri)
76+
.WithAuthority(config.Instance, config.TenantId)
7777
.Build();
7878
var result = await app.AcquireTokenInteractive(scopes).ExecuteAsync();
79-
IEnumerable<IAccount>? accounts = await app.GetAccountsAsync();
80-
IAccount? first = accounts.FirstOrDefault();
81-
82-
if (first is null)
83-
{
84-
Console.WriteLine("Error: No accounts found");
85-
return false;
86-
}
87-
88-
setAccount(first);
8979
setAccessToken(result.AccessToken);
9080
return true;
9181
}
@@ -113,15 +103,16 @@ private static async Task ImportFilesAsync(IEnumerable<FileInfo> files, Config c
113103
}
114104
}
115105

116-
IAccount? userAccount = null;
117106
string? accessToken = null;
118-
119-
if (await AcquireUserAccountAsync(config, v => { userAccount = v; }, v => { accessToken = v; }) == false)
107+
if (config.AuthenticationType == "AzureAd")
120108
{
121-
Console.WriteLine("Error: Failed to acquire user account.");
122-
return;
109+
if (await AcquireTokenAsync(config, v => { accessToken = v; }) == false)
110+
{
111+
Console.WriteLine("Error: Failed to acquire access token.");
112+
return;
113+
}
114+
Console.WriteLine($"Successfully acquired access token. Continuing...");
123115
}
124-
Console.WriteLine($"Successfully acquired User ID. Continuing...");
125116

126117
using var formContent = new MultipartFormDataContent();
127118
List<StreamContent> filesContent = files.Select(file => new StreamContent(file.OpenRead())).ToList();
@@ -130,11 +121,6 @@ private static async Task ImportFilesAsync(IEnumerable<FileInfo> files, Config c
130121
formContent.Add(filesContent[i], "formFiles", files.ElementAt(i).Name);
131122
}
132123

133-
var userId = userAccount!.HomeAccountId.Identifier;
134-
var userName = userAccount.Username;
135-
using var userNameContent = new StringContent(userName);
136-
formContent.Add(userNameContent, "userName");
137-
138124
if (chatCollectionId != Guid.Empty)
139125
{
140126
Console.WriteLine($"Uploading and parsing file to chat {chatCollectionId}...");
@@ -144,7 +130,7 @@ private static async Task ImportFilesAsync(IEnumerable<FileInfo> files, Config c
144130
formContent.Add(chatCollectionIdContent, "chatId");
145131

146132
// Calling UploadAsync here to make sure disposable objects are still in scope.
147-
await UploadAsync(formContent, accessToken!, config);
133+
await UploadAsync(formContent, accessToken, config);
148134
}
149135
else
150136
{
@@ -153,7 +139,7 @@ private static async Task ImportFilesAsync(IEnumerable<FileInfo> files, Config c
153139
formContent.Add(globalScopeContent, "documentScope");
154140

155141
// Calling UploadAsync here to make sure disposable objects are still in scope.
156-
await UploadAsync(formContent, accessToken!, config);
142+
await UploadAsync(formContent, accessToken, config);
157143
}
158144

159145
// Dispose of all the file streams.
@@ -170,7 +156,7 @@ private static async Task ImportFilesAsync(IEnumerable<FileInfo> files, Config c
170156
/// <param name="config">Configuration.</param>
171157
private static async Task UploadAsync(
172158
MultipartFormDataContent multipartFormDataContent,
173-
string accessToken,
159+
string? accessToken,
174160
Config config)
175161
{
176162
// Create a HttpClient instance and set the timeout to infinite since
@@ -183,8 +169,12 @@ private static async Task UploadAsync(
183169
{
184170
Timeout = Timeout.InfiniteTimeSpan
185171
};
186-
// Add required properties to the request header.
187-
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken}");
172+
173+
if (config.AuthenticationType == "AzureAd")
174+
{
175+
// Add required properties to the request header.
176+
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken!}");
177+
}
188178

189179
try
190180
{

‎tools/importdocument/README.md‎

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,14 @@ relevant information from memories to provide more meaningful answers to users t
1010
Memories can be generated from conversations as well as imported from external sources, such as documents.
1111
Importing documents enables Copilot Chat to have up-to-date knowledge of specific contexts, such as enterprise and personal data.
1212

13-
## Configure your environment
1413

15-
1. A registered App in Azure Portal (https://learn.microsoft.com/azure/active-directory/develop/quickstart-register-app)
16-
- Select Mobile and desktop applications as platform type, and the Redirect URI will be `http://localhost`
17-
- Select **`Accounts in any organizational directory (Any Azure AD directory - Multitenant)
18-
and personal Microsoft accounts (e.g. Skype, Xbox)`** as the supported account
19-
type for this sample.
20-
- Note the **`Application (client) ID`** from your app registration.
21-
2. Make sure the service is running. To start the service, see [here](../../webapi/README.md).
22-
23-
## Running the app
14+
## Running the app against a local Chat Copilot instance
2415

2516
1. Ensure the web api is running at `https://localhost:40443/`.
26-
2. Configure the appsettings.json file under this folder root with the following variables and fill
27-
in with your information, where
28-
`ClientId` is the GUID copied from the **Application (client) ID** from your app registration in the Azure Portal,
29-
`RedirectUri` is the Redirect URI also from the app registration in the Azure Portal, and
30-
`ServiceUri` is the address the web api is running at.
17+
2. Configure the appsettings.json file under this folder root with the following variables:
18+
- `ServiceUri` is the address the web api is running at
19+
- `AuthenticationType` should be set to "None"
20+
- The remaining variables can be left blank or with their default values
3121

3222
3. Change directory to this folder root.
3323
4. **Run** the following command to import a document to the app under the global document collection where
@@ -40,8 +30,6 @@ and personal Microsoft accounts (e.g. Skype, Xbox)`** as the supported account
4030

4131
`dotnet run --files .\sample-docs\ms10k.txt --chat-id [chatId]`
4232

43-
> Note that this will open a browser window for you to sign in to retrieve your user id to make sure you have access to the chat session.
44-
4533
> Currently only supports txt and pdf files. A sample file is provided under ./sample-docs.
4634
4735
Importing may take some time to generate embeddings for each piece/chunk of a document.
@@ -61,3 +49,64 @@ and personal Microsoft accounts (e.g. Skype, Xbox)`** as the supported account
6149
With [Microsoft Responsible AI Standard v2 General Requirements.pdf](./sample-docs/Microsoft-Responsible-AI-Standard-v2-General-Requirements.pdf):
6250

6351
![Document-Memory-Sample-2](https://github.com/microsoft/chat-copilot/assets/52973358/f0e95104-72ca-4a0a-9555-ee335d8df696)
52+
53+
54+
## Running the app against a deployed Chat Copilot instance
55+
56+
### Configure your environment
57+
58+
1. Create a registered app in Azure Portal (https://learn.microsoft.com/azure/active-directory/develop/quickstart-register-app).
59+
60+
> Note that this needs to be a separate app registration from those you created when deploying Chat Copilot.
61+
62+
- Select Mobile and desktop applications as platform type, and the Redirect URI will be `http://localhost`
63+
- Select **`Accounts in this organizational directory only (Microsoft only - Single tenant)`** as the supported account
64+
type for this sample.
65+
66+
> **IMPORTANT:** The supported account type should match that of the backend's app registration. If you changed this setting to allow allow multitenant and personal Microsoft accounts access to your Chat Copilot application, you should change it here as well.
67+
68+
- Note the **`Application (client) ID`** from your app registration.
69+
70+
2. Update the API permissions in the app registration you just created.
71+
72+
- From the left-hand menu, select **API permissions** and then **Add a permission**.
73+
- Select **My APIs** and then select the application corresponding to your backend web api.
74+
- Check the box next to `access_as_user` and then press **Add permissions**.
75+
76+
3. Update the authorized client applications for your backend web api.
77+
- In the Azure portal, navigate to your backend web api's app registration.
78+
- From the left-hand menu, select **Expose an API** and then **Add a client application**.
79+
- Enter the client ID of the app registration you just created and check the box under **Authorized scopes**. Then press **Add application**.
80+
81+
4. Configure the appsettings.json file under this folder root with the following variables:
82+
- `ServiceUri` is the address the web api is running at
83+
- `AuthenticationType` should be set to "AzureAd"
84+
- `ClientId` is the **Application (client) ID** GUID from the app registration you just created in the Azure Portal
85+
- `RedirectUri` is the Redirect URI also from the app registration you just created in the Azure Portal (e.g. `http://localhost`)
86+
- `BackendClientId` is the **Application (client) ID** GUID from your backend web api's app registration in the Azure Portal,
87+
- `TenantId` is the Azure AD tenant ID that you want to authenticate users against. For single-tenant applications, this is the same as the **Directory (tenant) ID** from your app registration in the Azure Portal.
88+
- `Instance` is the Azure AD cloud instance to authenticate users against. For most users, this is `https://login.microsoftonline.com`.
89+
- `Scopes` should be set to "access_as_user"
90+
91+
### Run the app
92+
93+
1. Change directory to this folder root.
94+
2. **Run** the following command to import a document to the app under the global document collection where
95+
all users will have access to:
96+
97+
`dotnet run --files .\sample-docs\ms10k.txt`
98+
99+
Or **Run** the following command to import a document to the app under a chat isolated document collection where
100+
only the chat session will have access to:
101+
102+
`dotnet run --files .\sample-docs\ms10k.txt --chat-id [chatId]`
103+
104+
> Note that both of these commands will open a browser window for you to sign in to ensure you have access to the Chat Copilot service.
105+
106+
> Currently only supports txt and pdf files. A sample file is provided under ./sample-docs.
107+
108+
Importing may take some time to generate embeddings for each piece/chunk of a document.
109+
110+
To import multiple files, specify multiple files. For example:
111+
112+
`dotnet run --files .\sample-docs\ms10k.txt .\sample-docs\Microsoft-Responsible-AI-Standard-v2-General-Requirements.pdf`
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
{
22
"Config": {
3+
"ServiceUri": "https://localhost:40443",
4+
"AuthenticationType": "None", // Supported authentication types are "None" or "AzureAd"
35
"ClientId": "",
4-
"RedirectUri": "",
5-
"ServiceUri": "https://localhost:40443"
6+
"RedirectUri": "http://localhost",
7+
"BackendClientId": "",
8+
"TenantId": "",
9+
"Instance": "https://login.microsoftonline.com",
10+
"Scopes": "access_as_user" // Scopes that the client app requires to access the API
611
}
712
}

0 commit comments

Comments
 (0)