|
4 | 4 | #if !FROM_GITHUB_ACTION |
5 | 5 |
|
6 | 6 | using System; |
| 7 | +using System.IdentityModel.Tokens.Jwt; |
| 8 | +using System.Linq; |
7 | 9 | using System.Security.Claims; |
8 | 10 | using System.Security.Cryptography.X509Certificates; |
9 | 11 | using Microsoft.Extensions.DependencyInjection; |
10 | 12 | using Microsoft.Graph; |
11 | 13 | using Microsoft.Identity.Abstractions; |
12 | 14 | using Microsoft.Identity.Web; |
| 15 | +using Microsoft.IdentityModel.Tokens; |
13 | 16 |
|
14 | 17 | namespace AgentApplicationsTests |
15 | 18 | { |
@@ -52,6 +55,19 @@ public async Task AgentUserIdentityGetsTokenForGraphAsync() |
52 | 55 | options); |
53 | 56 | Assert.NotNull(authorizationHeaderWithUserToken); |
54 | 57 |
|
| 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 | + |
55 | 71 | // If you want to call Microsoft Graph, just inject and use the Microsoft Graph SDK with the agent identity. |
56 | 72 | GraphServiceClient graphServiceClient = serviceProvider.GetRequiredService<GraphServiceClient>(); |
57 | 73 | var me = await graphServiceClient.Me.GetAsync(r => r.Options.WithAuthenticationOptions(options => options.WithAgentUserIdentity(agentIdentity, userUpn))); |
@@ -103,6 +119,19 @@ public async Task AgentUserIdentityGetsTokenForGraphWithTenantOverrideAsync() |
103 | 119 | options); |
104 | 120 | Assert.NotNull(authorizationHeaderWithUserToken); |
105 | 121 |
|
| 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 | + |
106 | 135 | // If you want to call Microsoft Graph, just inject and use the Microsoft Graph SDK with the agent identity. |
107 | 136 | GraphServiceClient graphServiceClient = serviceProvider.GetRequiredService<GraphServiceClient>(); |
108 | 137 | var me = await graphServiceClient.Me.GetAsync(r => r.Options.WithAuthenticationOptions(options => |
@@ -162,6 +191,19 @@ public async Task AgentUserIdentityGetsTokenForGraphWithCacheAsync() |
162 | 191 | Assert.True(user.HasClaim(c => c.Type == "uid")); |
163 | 192 | Assert.True(user.HasClaim(c => c.Type == "utid")); |
164 | 193 |
|
| 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 | + |
165 | 207 | // Use the cached user |
166 | 208 | authorizationHeaderWithUserToken = await authorizationHeaderProvider.CreateAuthorizationHeaderForUserAsync( |
167 | 209 | scopes: ["https://graph.microsoft.com/.default"], |
@@ -214,6 +256,19 @@ public async Task AgentUserIdentityGetsTokenForGraphByUserIdAsync() |
214 | 256 | options); |
215 | 257 | Assert.NotNull(authorizationHeaderWithUserToken); |
216 | 258 |
|
| 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 | + |
217 | 272 | // If you want to call Microsoft Graph, just inject and use the Microsoft Graph SDK with the agent identity. |
218 | 273 | GraphServiceClient graphServiceClient = serviceProvider.GetRequiredService<GraphServiceClient>(); |
219 | 274 | var me = await graphServiceClient.Me.GetAsync(r => r.Options.WithAuthenticationOptions(options => options.WithAgentUserIdentity(agentIdentity, Guid.Parse(userOid)))); |
|
0 commit comments