Skip to content

Commit 2aaa301

Browse files
pwoosamPatrick Woo-SamembettenPatrick Woo-Sam
authored
releases/2.0.0 Mac/Linux MSAL Broker support, entra token opt-in, and testing (#604)
Catches releases/2.0.0 up to master - Add support of Mac and Linux broker - Add entra token opt-in - Add updated testing files and cross platform testing script --------- Co-authored-by: Patrick Woo-Sam <[email protected]> Co-authored-by: Emily Bettencourt <[email protected]> Co-authored-by: Patrick Woo-Sam <[email protected]> Co-authored-by: embetten <[email protected]>
1 parent 9ed2abf commit 2aaa301

File tree

15 files changed

+858
-637
lines changed

15 files changed

+858
-637
lines changed

.vscode/launch.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
"version": "0.2.0",
33
"configurations": [
44
{
5-
"name": ".NET Core Launch (console)",
5+
"name": "CredentialProvider.Microsoft (stand-alone)",
66
"type": "coreclr",
77
"request": "launch",
88
"preLaunchTask": "build",
99
"program": "${workspaceFolder}/CredentialProvider.Microsoft/bin/Debug/net6.0/CredentialProvider.Microsoft.dll",
1010
"args": [
11-
"-Uri", "https://pkgs.dev.azure.com/mseng/PipelineTools/_packaging/artifacts-credprovider/nuget/v3/index.json",
12-
"-Verbosity", "Debug"
11+
"-Uri",
12+
"${input:packageFeedUri}",
13+
"-Verbosity",
14+
"Debug"
1315
],
16+
"env": {},
1417
"cwd": "${workspaceFolder}/CredentialProvider.Microsoft",
1518
"console": "integratedTerminal",
1619
"stopAtEntry": false
@@ -20,5 +23,13 @@
2023
"type": "coreclr",
2124
"request": "attach"
2225
}
26+
],
27+
"inputs": [
28+
{
29+
"id": "packageFeedUri",
30+
"description": "Package feed URI",
31+
"default": "https://pkgs.dev.azure.com/mseng/PipelineTools/_packaging/artifacts-credprovider/nuget/v3/index.json",
32+
"type": "promptString"
33+
}
2334
]
2435
}

CredentialProvider.Microsoft/CredentialProviders/Vsts/MsalTokenProvidersFactory.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading.Tasks;
88
using Microsoft.Artifacts.Authentication;
99
using Microsoft.Extensions.Logging;
10+
using Microsoft.Identity.Client;
1011
using Microsoft.Identity.Client.Extensions.Msal;
1112
using NuGetCredentialProvider.Util;
1213

@@ -30,7 +31,6 @@ public async Task<IEnumerable<ITokenProvider>> GetAsync(Uri authority)
3031
}
3132

3233
var app = AzureArtifacts.CreateDefaultBuilder(authority)
33-
.WithBroker(EnvUtil.MsalAllowBrokerEnabled(), EnvUtil.GetMsalBrokerWindowHandle(), logger)
3434
.WithHttpClientFactory(HttpClientFactory.Default)
3535
.WithLogging(
3636
(Microsoft.Identity.Client.LogLevel level, string message, bool containsPii) =>
@@ -42,9 +42,33 @@ public async Task<IEnumerable<ITokenProvider>> GetAsync(Uri authority)
4242
)
4343
.Build();
4444

45+
var brokerEnabled = EnvUtil.MsalAllowBrokerEnabled();
46+
#nullable enable
47+
IPublicClientApplication? appInteractiveBroker = null;
48+
#nullable disable
49+
if (brokerEnabled)
50+
{
51+
appInteractiveBroker = AzureArtifacts.CreateDefaultBuilder(authority)
52+
.WithHttpClientFactory(HttpClientFactory.Default)
53+
.WithLogging(
54+
(Microsoft.Identity.Client.LogLevel level, string message, bool containsPii) =>
55+
{
56+
// We ignore containsPii param because we are passing in enablePiiLogging below.
57+
logger.LogTrace("MSAL Log ({level}): {message}", level, message);
58+
},
59+
enablePiiLogging: EnvUtil.GetLogPIIEnabled()
60+
)
61+
.WithBroker(brokerEnabled, EnvUtil.GetMsalBrokerWindowHandle(), logger)
62+
.Build();
63+
}
64+
4565
cache?.RegisterCache(app.UserTokenCache);
66+
if (appInteractiveBroker != null)
67+
{
68+
cache?.RegisterCache(appInteractiveBroker.UserTokenCache);
69+
}
4670

47-
return MsalTokenProviders.Get(app, logger);
71+
return MsalTokenProviders.Get(app, logger, appInteractiveBroker: appInteractiveBroker);
4872
}
4973
}
5074
}

CredentialProvider.Microsoft/CredentialProviders/Vsts/VstsCredentialProvider.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ public override async Task<GetAuthenticationCredentialsResponse> HandleRequestAs
155155
}
156156

157157
Info(string.Format(Resources.AcquireBearerTokenSuccess, tokenProvider.Name));
158+
if (EnvUtil.EntraTokenOptInEnabled())
159+
{
160+
return new GetAuthenticationCredentialsResponse(
161+
"EntraToken",
162+
bearerToken,
163+
message: null,
164+
authenticationTypes: ["Basic"],
165+
responseCode: MessageResponseCode.Success);
166+
}
167+
158168
Info(Resources.ExchangingBearerTokenForSessionToken);
159169
try
160170
{
@@ -167,7 +177,7 @@ public override async Task<GetAuthenticationCredentialsResponse> HandleRequestAs
167177
Username,
168178
sessionToken,
169179
message: null,
170-
authenticationTypes: new List<string>() { "Basic" },
180+
authenticationTypes: ["Basic"],
171181
responseCode: MessageResponseCode.Success);
172182
}
173183
}

CredentialProvider.Microsoft/Program.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading;
1010
using System.Threading.Tasks;
1111
using Microsoft.Artifacts.Authentication;
12+
using Microsoft.Identity.Client.Utils;
1213
using NuGet.Common;
1314
using NuGet.Protocol.Plugins;
1415
using NuGetCredentialProvider.CredentialProviders;
@@ -28,7 +29,27 @@ public static class Program
2829
private static bool shuttingDown = false;
2930
public static bool IsShuttingDown => Volatile.Read(ref shuttingDown);
3031

31-
public static async Task<int> Main(string[] args)
32+
public static int Main(string[] args)
33+
{
34+
var scheduler = MacMainThreadScheduler.Instance();
35+
36+
int returnCode = -1;
37+
_ = Task.Run(async () => {
38+
try
39+
{
40+
returnCode = await BackgroundWork(args);
41+
}
42+
finally
43+
{
44+
scheduler.Stop();
45+
}
46+
});
47+
48+
scheduler.StartMessageLoop();
49+
return returnCode;
50+
}
51+
52+
public static async Task<int> BackgroundWork(string[] args)
3253
{
3354
CancellationTokenSource tokenSource = new CancellationTokenSource();
3455
var parsedArgs = await Args.ParseAsync<CredentialProviderArgs>(args);

CredentialProvider.Microsoft/RequestHandlers/GetAuthenticationCredentialsRequestHandler.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public override async Task<GetAuthenticationCredentialsResponse> HandleRequestAs
7373
username: "VssSessionToken",
7474
password: cachedToken,
7575
message: null,
76-
authenticationTypes: new List<string> { "Basic" },
76+
authenticationTypes: ["Basic"],
7777
responseCode: MessageResponseCode.Success);
7878
}
7979

@@ -125,14 +125,21 @@ protected override AutomaticProgressReporter GetProgressReporter(IConnection con
125125

126126
private static ICache<Uri, string> GetSessionTokenCache(ILogger logger, CancellationToken cancellationToken)
127127
{
128-
if (EnvUtil.SessionTokenCacheEnabled())
128+
if (!EnvUtil.SessionTokenCacheEnabled())
129129
{
130-
logger.Verbose(string.Format(Resources.SessionTokenCacheLocation, EnvUtil.SessionTokenCacheLocation));
131-
return new SessionTokenCache(EnvUtil.SessionTokenCacheLocation, logger, cancellationToken);
130+
logger.Verbose(Resources.SessionTokenCacheDisabled);
131+
return new NoOpCache<Uri, string>();
132132
}
133133

134-
logger.Verbose(Resources.SessionTokenCacheDisabled);
135-
return new NoOpCache<Uri, string>();
134+
// Disable session token cache when Entra token opt-in is enabled. Entra tokens are cached by the MSAL cache instead.
135+
if (EnvUtil.EntraTokenOptInEnabled())
136+
{
137+
logger.Verbose(Resources.SessionTokenCacheDisabledByEntraTokenOptIn);
138+
return new NoOpCache<Uri, string>();
139+
}
140+
141+
logger.Verbose(string.Format(Resources.SessionTokenCacheLocation, EnvUtil.SessionTokenCacheLocation));
142+
return new SessionTokenCache(EnvUtil.SessionTokenCacheLocation, logger, cancellationToken);
136143
}
137144

138145
private bool TryCache(GetAuthenticationCredentialsRequest request, out string cachedToken)

0 commit comments

Comments
 (0)