Skip to content

Commit 722ac3b

Browse files
Copilotjmprieur
andcommitted
Add token validation using ClaimsIdentity extension methods in E2E tests
Co-authored-by: jmprieur <[email protected]>
1 parent 071555b commit 722ac3b

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

tests/E2E Tests/AgentApplications/AgentUserIdentityTestscs.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
#if !FROM_GITHUB_ACTION
55

66
using System;
7+
using System.IdentityModel.Tokens.Jwt;
8+
using System.Linq;
79
using System.Security.Claims;
810
using System.Security.Cryptography.X509Certificates;
911
using Microsoft.Extensions.DependencyInjection;
1012
using Microsoft.Graph;
1113
using Microsoft.Identity.Abstractions;
1214
using Microsoft.Identity.Web;
15+
using Microsoft.IdentityModel.Tokens;
1316

1417
namespace AgentApplicationsTests
1518
{
@@ -52,6 +55,19 @@ public async Task AgentUserIdentityGetsTokenForGraphAsync()
5255
options);
5356
Assert.NotNull(authorizationHeaderWithUserToken);
5457

58+
// Extract token from authorization header and validate claims using extension methods
59+
string token = authorizationHeaderWithUserToken.Substring("Bearer ".Length);
60+
var handler = new JwtSecurityTokenHandler();
61+
var jwtToken = handler.ReadJwtToken(token);
62+
var claimsIdentity = new CaseSensitiveClaimsIdentity(jwtToken.Claims);
63+
64+
// Verify the token represents an agent user identity using the extension method
65+
Assert.True(claimsIdentity.IsAgentUserIdentity());
66+
67+
// Verify we can retrieve the parent agent blueprint if present
68+
string? parentBlueprint = claimsIdentity.GetParentAgentBlueprint();
69+
// Note: parentBlueprint may be null if the claim is not present in this token
70+
5571
// If you want to call Microsoft Graph, just inject and use the Microsoft Graph SDK with the agent identity.
5672
GraphServiceClient graphServiceClient = serviceProvider.GetRequiredService<GraphServiceClient>();
5773
var me = await graphServiceClient.Me.GetAsync(r => r.Options.WithAuthenticationOptions(options => options.WithAgentUserIdentity(agentIdentity, userUpn)));
@@ -103,6 +119,19 @@ public async Task AgentUserIdentityGetsTokenForGraphWithTenantOverrideAsync()
103119
options);
104120
Assert.NotNull(authorizationHeaderWithUserToken);
105121

122+
// Extract token from authorization header and validate claims using extension methods
123+
string token = authorizationHeaderWithUserToken.Substring("Bearer ".Length);
124+
var handler = new JwtSecurityTokenHandler();
125+
var jwtToken = handler.ReadJwtToken(token);
126+
var claimsIdentity = new CaseSensitiveClaimsIdentity(jwtToken.Claims);
127+
128+
// Verify the token represents an agent user identity using the extension method
129+
Assert.True(claimsIdentity.IsAgentUserIdentity());
130+
131+
// Verify we can retrieve the parent agent blueprint if present
132+
string? parentBlueprint = claimsIdentity.GetParentAgentBlueprint();
133+
// Note: parentBlueprint may be null if the claim is not present in this token
134+
106135
// If you want to call Microsoft Graph, just inject and use the Microsoft Graph SDK with the agent identity.
107136
GraphServiceClient graphServiceClient = serviceProvider.GetRequiredService<GraphServiceClient>();
108137
var me = await graphServiceClient.Me.GetAsync(r => r.Options.WithAuthenticationOptions(options =>
@@ -162,6 +191,19 @@ public async Task AgentUserIdentityGetsTokenForGraphWithCacheAsync()
162191
Assert.True(user.HasClaim(c => c.Type == "uid"));
163192
Assert.True(user.HasClaim(c => c.Type == "utid"));
164193

194+
// Extract token from authorization header and validate claims using extension methods
195+
string token = authorizationHeaderWithUserToken.Substring("Bearer ".Length);
196+
var handler = new JwtSecurityTokenHandler();
197+
var jwtToken = handler.ReadJwtToken(token);
198+
var claimsIdentity = new CaseSensitiveClaimsIdentity(jwtToken.Claims);
199+
200+
// Verify the token represents an agent user identity using the extension method
201+
Assert.True(claimsIdentity.IsAgentUserIdentity());
202+
203+
// Verify we can retrieve the parent agent blueprint if present
204+
string? parentBlueprint = claimsIdentity.GetParentAgentBlueprint();
205+
// Note: parentBlueprint may be null if the claim is not present in this token
206+
165207
// Use the cached user
166208
authorizationHeaderWithUserToken = await authorizationHeaderProvider.CreateAuthorizationHeaderForUserAsync(
167209
scopes: ["https://graph.microsoft.com/.default"],
@@ -214,6 +256,19 @@ public async Task AgentUserIdentityGetsTokenForGraphByUserIdAsync()
214256
options);
215257
Assert.NotNull(authorizationHeaderWithUserToken);
216258

259+
// Extract token from authorization header and validate claims using extension methods
260+
string token = authorizationHeaderWithUserToken.Substring("Bearer ".Length);
261+
var handler = new JwtSecurityTokenHandler();
262+
var jwtToken = handler.ReadJwtToken(token);
263+
var claimsIdentity = new CaseSensitiveClaimsIdentity(jwtToken.Claims);
264+
265+
// Verify the token represents an agent user identity using the extension method
266+
Assert.True(claimsIdentity.IsAgentUserIdentity());
267+
268+
// Verify we can retrieve the parent agent blueprint if present
269+
string? parentBlueprint = claimsIdentity.GetParentAgentBlueprint();
270+
// Note: parentBlueprint may be null if the claim is not present in this token
271+
217272
// If you want to call Microsoft Graph, just inject and use the Microsoft Graph SDK with the agent identity.
218273
GraphServiceClient graphServiceClient = serviceProvider.GetRequiredService<GraphServiceClient>();
219274
var me = await graphServiceClient.Me.GetAsync(r => r.Options.WithAuthenticationOptions(options => options.WithAgentUserIdentity(agentIdentity, Guid.Parse(userOid))));

0 commit comments

Comments
 (0)