|
1 | | -//// Copyright (c) Microsoft Corporation. All rights reserved. |
2 | | -//// Licensed under the MIT License. |
3 | | -//#if NET6_0_OR_GREATER |
4 | | -//using System; |
5 | | -//using System.Collections.Generic; |
6 | | -//using System.Linq; |
7 | | -//using System.Runtime.ConstrainedExecution; |
8 | | -//using System.Text; |
9 | | -//using System.Threading.Tasks; |
10 | | -//using Microsoft.Identity.Client; |
11 | | -//using Microsoft.Identity.Client.Extensibility; |
12 | | -//using Microsoft.Identity.Client.Internal; |
13 | | -//using Microsoft.Identity.Client.Utils; |
14 | | -//using Microsoft.Identity.Test.Common; |
15 | | -//using Microsoft.Identity.Test.Common.Core.Helpers; |
16 | | -//using Microsoft.Identity.Test.LabInfrastructure; |
17 | | -//using Microsoft.Identity.Test.Unit; |
18 | | -//using Microsoft.VisualStudio.TestTools.UnitTesting; |
19 | | - |
20 | | -//namespace Microsoft.Identity.Test.Integration.NetCore.HeadlessTests |
21 | | -//{ |
22 | | -// [TestClass] |
23 | | -// public class CdtTests |
24 | | -// { |
25 | | -// private static readonly string[] s_scopes = new[] { "88f91eac-c606-4c67-a0e2-a5e8a186854f/.default" }; |
26 | | - |
27 | | -// [TestInitialize] |
28 | | -// public void TestInitialize() |
29 | | -// { |
30 | | -// TestCommon.ResetInternalStaticCaches(); |
31 | | -// } |
32 | | - |
33 | | -// [TestMethod] |
34 | | -// //[Ignore("Need to wait for ESTS to release feature from test slice.")] |
35 | | -// public async Task CDT_WithCertIntegrationTest_Async() |
36 | | -// { |
37 | | -// //Client.Constraint constraint = new Client.Constraint(); |
38 | | -// //constraint.Type = "wk:user"; |
39 | | -// //constraint.Action = "U"; |
40 | | -// //constraint.Version = "1.0"; |
41 | | -// //constraint.Targets = new List<ConstraintTarget>(); |
42 | | - |
43 | | -// //constraint.Targets.Add(new ConstraintTarget("constraint1", "pol1")); |
44 | | -// //constraint.Targets.Add(new ConstraintTarget("constraint2", "pol2")); |
45 | | - |
46 | | -// //var constraintAsString = JsonHelper.SerializeToJson(new[] { constraint }); |
47 | | - |
48 | | -// //TODO: Resolve serialization failure in test. Seems to be related to some internal .net serialization issue |
49 | | -// //Using a hardcoded string for now |
50 | | -// var constraintAsString = "[{\"Version\":\"1.0\",\"Type\":\"wk:user\",\"Action\":\"U\",\"Targets\":[{\"Value\":\"constraint1\",\"Policy\":\"pol1\",\"AdditionalProperties\":null},{\"Value\":\"constraint2\",\"Policy\":\"pol2\",\"AdditionalProperties\":null}],\"AdditionalProperties\":null}]"; |
51 | | - |
52 | | -// var secret = GetSecretLazy(KeyVaultInstance.MSIDLab, TestConstants.MsalCCAKeyVaultSecretName).Value; |
53 | | -// var certificate = CertificateHelper.FindCertificateByName(TestConstants.AutomationTestCertName); |
54 | | - |
55 | | -// var confidentialApp = ConfidentialClientApplicationBuilder |
56 | | -// .Create("88f91eac-c606-4c67-a0e2-a5e8a186854f") |
57 | | -// .WithAuthority("https://login.microsoftonline.com/msidlab4.onmicrosoft.com") |
58 | | -// .WithClientSecret(secret) |
59 | | -// .WithExperimentalFeatures(true) |
60 | | -// .BuildConcrete(); |
61 | | - |
62 | | -// var provider = new CdtCryptoProvider(); |
63 | | - |
64 | | -// MsalAuthenticationExtension cdtExtension = new MsalAuthenticationExtension() |
65 | | -// { |
66 | | -// AuthenticationOperation = new CdtAuthenticationScheme(constraintAsString), |
67 | | -// AdditionalCacheParameters = new[] { CdtAuthenticationScheme.CdtNonce, CdtAuthenticationScheme.CdtEncKey } |
68 | | -// }; |
69 | | - |
70 | | -// var result = await confidentialApp.AcquireTokenForClient(s_scopes) |
71 | | -// .WithAuthenticationExtension(cdtExtension) |
72 | | -// .WithExtraQueryParameters("dc=ESTS-PUB-JPELR1-AZ1-FD000-TEST1") |
73 | | -// .ExecuteAsync() |
74 | | -// .ConfigureAwait(false); |
75 | | - |
76 | | -// // access token parsing can be done with MSAL's id token parsing logic |
77 | | -// var claims = IdToken.Parse(result.AccessToken).ClaimsPrincipal; |
78 | | - |
79 | | -// Assert.AreEqual(TokenSource.IdentityProvider, result.AuthenticationResultMetadata.TokenSource); |
80 | | -// AssertConstrainedDelegationClaims(provider, claims, constraintAsString); |
81 | | - |
82 | | -// //Verify that the original AT token is cached and the CDT can be recreated |
83 | | -// result = await confidentialApp.AcquireTokenForClient(s_scopes) |
84 | | -// .WithAuthenticationExtension(cdtExtension) |
85 | | -// .ExecuteAsync() |
86 | | -// .ConfigureAwait(false); |
87 | | - |
88 | | -// // access token parsing can be done with MSAL's id token parsing logic |
89 | | -// claims = IdToken.Parse(result.AccessToken).ClaimsPrincipal; |
90 | | - |
91 | | -// Assert.AreEqual(TokenSource.Cache, result.AuthenticationResultMetadata.TokenSource); |
92 | | -// AssertConstrainedDelegationClaims(provider, claims, constraintAsString); |
93 | | -// } |
94 | | - |
95 | | -// private static Lazy<string> GetSecretLazy(string keyVaultInstance, string secretName) => new Lazy<string>(() => |
96 | | -// { |
97 | | -// var keyVault = new KeyVaultSecretsProvider(keyVaultInstance); |
98 | | -// var secret = keyVault.GetSecretByName(secretName).Value; |
99 | | -// return secret; |
100 | | -// }); |
101 | | - |
102 | | -// private static void AssertConstrainedDelegationClaims(CdtCryptoProvider cdtCryptoProvider, System.Security.Claims.ClaimsPrincipal claims, string constraint) |
103 | | -// { |
104 | | -// var ticket = claims.FindAll("t").Single().Value; |
105 | | -// var constraints = claims.FindAll("c").Single().Value; |
106 | | - |
107 | | -// Assert.IsTrue(!string.IsNullOrEmpty(ticket)); |
108 | | -// Assert.IsTrue(!string.IsNullOrEmpty(constraints)); |
109 | | - |
110 | | -// Assert.IsTrue(!string.IsNullOrEmpty(ticket)); |
111 | | - |
112 | | -// var constraintsClaims = IdToken.Parse(constraints).ClaimsPrincipal; |
113 | | -// var constraintsClaim = constraintsClaims.FindAll("constraints").Single().Value; |
114 | | - |
115 | | -// Assert.AreEqual(constraint, constraintsClaim); |
116 | | -// } |
117 | | -// } |
118 | | -//} |
119 | | -//#endif |
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +#if NET6_0_OR_GREATER |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Runtime.ConstrainedExecution; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Microsoft.Identity.Client; |
| 11 | +using Microsoft.Identity.Client.Extensibility; |
| 12 | +using Microsoft.Identity.Client.Internal; |
| 13 | +using Microsoft.Identity.Client.Utils; |
| 14 | +using Microsoft.Identity.Test.Common; |
| 15 | +using Microsoft.Identity.Test.Common.Core.Helpers; |
| 16 | +using Microsoft.Identity.Test.LabInfrastructure; |
| 17 | +using Microsoft.Identity.Test.Unit; |
| 18 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 19 | + |
| 20 | +namespace Microsoft.Identity.Test.Integration.NetCore.HeadlessTests |
| 21 | +{ |
| 22 | + [TestClass] |
| 23 | + public class CdtTests |
| 24 | + { |
| 25 | + private static readonly string[] s_scopes = new[] { "88f91eac-c606-4c67-a0e2-a5e8a186854f/.default" }; |
| 26 | + |
| 27 | + [TestInitialize] |
| 28 | + public void TestInitialize() |
| 29 | + { |
| 30 | + TestCommon.ResetInternalStaticCaches(); |
| 31 | + } |
| 32 | + |
| 33 | + [TestMethod] |
| 34 | + //[Ignore("Need to wait for ESTS to release feature from test slice.")] |
| 35 | + public async Task CDT_WithCertIntegrationTest_Async() |
| 36 | + { |
| 37 | + //Client.Constraint constraint = new Client.Constraint(); |
| 38 | + //constraint.Type = "wk:user"; |
| 39 | + //constraint.Action = "U"; |
| 40 | + //constraint.Version = "1.0"; |
| 41 | + //constraint.Targets = new List<ConstraintTarget>(); |
| 42 | + |
| 43 | + //constraint.Targets.Add(new ConstraintTarget("constraint1", "pol1")); |
| 44 | + //constraint.Targets.Add(new ConstraintTarget("constraint2", "pol2")); |
| 45 | + |
| 46 | + //var constraintAsString = JsonHelper.SerializeToJson(new[] { constraint }); |
| 47 | + |
| 48 | + //TODO: Resolve serialization failure in test. Seems to be related to some internal .net serialization issue |
| 49 | + //Using a hardcoded string for now |
| 50 | + var constraintAsString = "[{\"Version\":\"1.0\",\"Type\":\"wk:user\",\"Action\":\"U\",\"Targets\":[{\"Value\":\"constraint1\",\"Policy\":\"pol1\",\"AdditionalProperties\":null},{\"Value\":\"constraint2\",\"Policy\":\"pol2\",\"AdditionalProperties\":null}],\"AdditionalProperties\":null}]"; |
| 51 | + |
| 52 | + var secret = GetSecretLazy(KeyVaultInstance.MSIDLab, TestConstants.MsalCCAKeyVaultSecretName).Value; |
| 53 | + var certificate = CertificateHelper.FindCertificateByName(TestConstants.AutomationTestCertName); |
| 54 | + |
| 55 | + var confidentialApp = ConfidentialClientApplicationBuilder |
| 56 | + .Create("88f91eac-c606-4c67-a0e2-a5e8a186854f") |
| 57 | + .WithAuthority("https://login.microsoftonline.com/msidlab4.onmicrosoft.com") |
| 58 | + .WithClientSecret(secret) |
| 59 | + .WithExperimentalFeatures(true) |
| 60 | + .BuildConcrete(); |
| 61 | + |
| 62 | + var provider = new CdtCryptoProvider(); |
| 63 | + |
| 64 | + MsalAuthenticationExtension cdtExtension = new MsalAuthenticationExtension() |
| 65 | + { |
| 66 | + AuthenticationOperation = new CdtAuthenticationScheme(constraintAsString), |
| 67 | + AdditionalCacheParameters = new[] { CdtAuthenticationScheme.CdtNonce, CdtAuthenticationScheme.CdtEncKey } |
| 68 | + }; |
| 69 | + |
| 70 | + var result = await confidentialApp.AcquireTokenForClient(s_scopes) |
| 71 | + .WithAuthenticationExtension(cdtExtension) |
| 72 | + .WithExtraQueryParameters("dc=ESTS-PUB-JPELR1-AZ1-FD000-TEST1") |
| 73 | + .ExecuteAsync() |
| 74 | + .ConfigureAwait(false); |
| 75 | + |
| 76 | + // access token parsing can be done with MSAL's id token parsing logic |
| 77 | + var claims = IdToken.Parse(result.AccessToken).ClaimsPrincipal; |
| 78 | + |
| 79 | + Assert.AreEqual(TokenSource.IdentityProvider, result.AuthenticationResultMetadata.TokenSource); |
| 80 | + AssertConstrainedDelegationClaims(provider, claims, constraintAsString); |
| 81 | + |
| 82 | + //Verify that the original AT token is cached and the CDT can be recreated |
| 83 | + result = await confidentialApp.AcquireTokenForClient(s_scopes) |
| 84 | + .WithAuthenticationExtension(cdtExtension) |
| 85 | + .ExecuteAsync() |
| 86 | + .ConfigureAwait(false); |
| 87 | + |
| 88 | + // access token parsing can be done with MSAL's id token parsing logic |
| 89 | + claims = IdToken.Parse(result.AccessToken).ClaimsPrincipal; |
| 90 | + |
| 91 | + Assert.AreEqual(TokenSource.Cache, result.AuthenticationResultMetadata.TokenSource); |
| 92 | + AssertConstrainedDelegationClaims(provider, claims, constraintAsString); |
| 93 | + } |
| 94 | + |
| 95 | + private static Lazy<string> GetSecretLazy(string keyVaultInstance, string secretName) => new Lazy<string>(() => |
| 96 | + { |
| 97 | + var keyVault = new KeyVaultSecretsProvider(keyVaultInstance); |
| 98 | + var secret = keyVault.GetSecretByName(secretName).Value; |
| 99 | + return secret; |
| 100 | + }); |
| 101 | + |
| 102 | + private static void AssertConstrainedDelegationClaims(CdtCryptoProvider cdtCryptoProvider, System.Security.Claims.ClaimsPrincipal claims, string constraint) |
| 103 | + { |
| 104 | + var ticket = claims.FindAll("t").Single().Value; |
| 105 | + var constraints = claims.FindAll("c").Single().Value; |
| 106 | + |
| 107 | + Assert.IsTrue(!string.IsNullOrEmpty(ticket)); |
| 108 | + Assert.IsTrue(!string.IsNullOrEmpty(constraints)); |
| 109 | + |
| 110 | + Assert.IsTrue(!string.IsNullOrEmpty(ticket)); |
| 111 | + |
| 112 | + var constraintsClaims = IdToken.Parse(constraints).ClaimsPrincipal; |
| 113 | + var constraintsClaim = constraintsClaims.FindAll("constraints").Single().Value; |
| 114 | + |
| 115 | + Assert.AreEqual(constraint, constraintsClaim); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | +#endif |
0 commit comments