-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy pathSidecarEndpointsE2ETests.cs
More file actions
202 lines (163 loc) · 8.97 KB
/
SidecarEndpointsE2ETests.cs
File metadata and controls
202 lines (163 loc) · 8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#if !FROM_GITHUB_ACTION
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Identity.Abstractions;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.Sidecar.Models;
using Microsoft.Identity.Web.TokenCacheProviders.InMemory;
using Xunit;
namespace Sidecar.Tests;
public class SidecarEndpointsE2ETests : IClassFixture<SidecarApiFactory>
{
private readonly SidecarApiFactory _factory;
public SidecarEndpointsE2ETests(SidecarApiFactory factory) => _factory = factory;
const string TenantId = "31a58c3b-ae9c-4448-9e8f-e9e143e800df"; // Replace with your tenant ID
const string AgentApplication = "d15884b6-a447-4dd5-a5a5-a668c49f6300"; // Replace with the actual agent application client ID
const string AgentIdentity = "d84da24a-2ea2-42b8-b5ab-8637ec208024"; // Replace with the actual agent identity
const string UserUpn = "aui1@msidlabtoint.onmicrosoft.com"; // Replace with the actual user upn.
string UserOid = "51c1aa1c-f6d0-4a92-936c-cadb27b717f2"; // Replace with the actual user OID.
[Fact]
public async Task Validate_WhenBadTokenAsync()
{
var client = _factory.CreateClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "dummy-token");
var response = await client.GetAsync("/Validate");
Assert.Equal(System.Net.HttpStatusCode.Unauthorized, response.StatusCode);
var content = await response.Content.ReadAsStringAsync();
Assert.Contains("invalid_token", response.Headers.WwwAuthenticate.ToString(), StringComparison.CurrentCultureIgnoreCase);
}
[Fact]
public async Task Validate_WhenGoodTokenAsync()
{
// Getting a token to call the API.
string authorizationHeader = await GetAuthorizationHeaderToCallTheSideCarAsync();
// Calling the API
var client = _factory.CreateClient();
client.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(authorizationHeader);
var response = await client.GetAsync("/Validate");
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
var content = await response.Content.ReadAsStringAsync();
Assert.NotEmpty(content);
}
[Fact]
public async Task GetAuthorizationHeaderForAgentUserIdentityAuthenticatedAsync()
{
// Getting a token to call the API.
string authorizationHeader = await GetAuthorizationHeaderToCallTheSideCarAsync();
// Calling the API
var client = _factory.CreateClient();
client.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(authorizationHeader);
var response = await client.GetAsync($"/AuthorizationHeader/MsGraph?agentidentity={AgentIdentity}&agentUsername={UserUpn}");
var content = await response.Content.ReadAsStringAsync();
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
}
[Fact]
public async Task DownstreamApiForAgentUserIdentityAuthenticated()
{
// Getting a token to call the API.
string authorizationHeader = await GetAuthorizationHeaderToCallTheSideCarAsync();
// Calling the API
var client = _factory.CreateClient();
client.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(authorizationHeader);
var response = await client.GetAsync($"/AuthorizationHeader/MsGraph?agentidentity={AgentIdentity}&agentUsername={UserUpn}");
var content = await response.Content.ReadAsStringAsync();
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
}
[Fact]
public async Task DownstreamApiForAgentUserIdentityAuthenticatedUsingOid()
{
// Getting a token to call the API.
string authorizationHeader = await GetAuthorizationHeaderToCallTheSideCarAsync();
// Calling the API
var client = _factory.CreateClient();
client.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse(authorizationHeader);
var response = await client.GetAsync($"/AuthorizationHeader/MsGraph?agentidentity={AgentIdentity}&agentUserId={UserOid}");
var content = await response.Content.ReadAsStringAsync();
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
}
[Fact]
public async Task GetAuthorizationHeaderForAgentUserIdentityUnauthenticatedAsync()
{
// Calling the API
var client = _factory.CreateClient();
var response = await client.GetAsync($"/AuthorizationHeaderUnauthenticated/MsGraph?agentidentity={AgentIdentity}&agentUsername={UserUpn}");
var content = await response.Content.ReadAsStringAsync();
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
}
[Fact]
public async Task GetAuthorizationHeaderForAgentUserIdentityUnauthenticatedAsyncUseUpn()
{
// Calling the API
var client = _factory.CreateClient();
var response = await client.GetAsync($"/AuthorizationHeaderUnauthenticated/MsGraph?agentidentity={AgentIdentity}&agentUserId={UserOid}");
var content = await response.Content.ReadAsStringAsync();
Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
}
[Fact]
public async Task GetAuthorizationHeaderForAgentUserIdentityUnauthenticated_WithOptionsOverride()
{
var client = _factory.CreateClient();
var result = await client.GetAsync(
$"/AuthorizationHeaderUnauthenticated/AgentUserIdentityCallsGraph?AgentIdentity={AgentIdentity}&AgentUsername={UserUpn}&OptionsOverride.Tenant={TenantId}&OptionsOverride.Scopes=user.read");
Assert.True(result.IsSuccessStatusCode);
var response = await result.Content.ReadFromJsonAsync<Microsoft.Identity.Web.Sidecar.Models.AuthorizationHeaderResult>();
Assert.NotNull(response?.AuthorizationHeader);
Assert.StartsWith("Bearer ey", response.AuthorizationHeader, StringComparison.Ordinal);
}
[Fact]
public async Task GetDownstreamApiForAgentUserIdentityUnauthenticated()
{
// Calling the API
var client = _factory.CreateClient();
var response = await client.PostAsync($"/DownstreamApi/MsGraph?agentidentity={AgentIdentity}&agentUsername={UserUpn}", null);
Assert.Equal(System.Net.HttpStatusCode.Unauthorized, response.StatusCode);
}
[Fact]
public async Task GetDownstreamApiForAgentUserIdentityUnauthenticatedUseOid()
{
// Calling the API
var client = _factory.CreateClient();
var response = await client.PostAsync($"/DownstreamApi/MsGraph?agentidentity={AgentIdentity}&agentUserId={UserOid}", null);
Assert.Equal(System.Net.HttpStatusCode.Unauthorized, response.StatusCode);
}
[Fact]
public async Task TestAgentIdentityConfiguration_InvalidTenant()
{
var client = _factory.CreateClient();
var result = await client.GetAsync(
$"/AuthorizationHeaderUnauthenticated/AgentUserIdentityCallsGraph?AgentIdentity={AgentIdentity}&AgentUsername={UserUpn}&OptionsOverride.AcquireTokenOptions.Tenant=invalid-tenant&OptionsOverride.Scopes=user.read");
Assert.Equal(HttpStatusCode.Unauthorized, result.StatusCode);
}
private static async Task<string> GetAuthorizationHeaderToCallTheSideCarAsync()
{
ServiceCollection services = new();
IConfiguration configuration = new ConfigurationBuilder().AddInMemoryCollection().Build();
services.AddSingleton<IConfiguration>(configuration);
configuration["Instance"] = "https://login.microsoftonline.com/";
configuration["TenantId"] = "31a58c3b-ae9c-4448-9e8f-e9e143e800df";
configuration["ClientId"] = "5cbcd9ff-c994-49ac-87e7-08a93a9c0794";
configuration["SendX5C"] = "true";
configuration["ClientCredentials:0:SourceType"] = "StoreWithDistinguishedName";
configuration["ClientCredentials:0:CertificateStorePath"] = "LocalMachine/My";
configuration["ClientCredentials:0:CertificateDistinguishedName"] = "CN=LabAuth.MSIDLab.com";
services.AddTokenAcquisition().AddHttpClient().AddInMemoryTokenCaches();
services.Configure<MicrosoftIdentityApplicationOptions>(configuration);
IServiceProvider serviceProvider = services.BuildServiceProvider();
IAuthorizationHeaderProvider authorizationHeaderProvider = serviceProvider.GetRequiredService<IAuthorizationHeaderProvider>();
string authorizationHeader = await authorizationHeaderProvider.CreateAuthorizationHeaderForAppAsync("api://d15884b6-a447-4dd5-a5a5-a668c49f6300/.default",
new AuthorizationHeaderProviderOptions()
{
AcquireTokenOptions = new AcquireTokenOptions()
{
AuthenticationOptionsName = ""
}
});
return authorizationHeader;
}
}
#endif